Skip to content

Commit be82869

Browse files
committed
Auto merge of #15317 - HKalbasi:mir, r=HKalbasi
Lookup super traits in `is_dyn_method`
2 parents cbf3713 + ed8e1fd commit be82869

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

crates/hir-ty/src/consteval/tests.rs

+27
Original file line numberDiff line numberDiff line change
@@ -1941,6 +1941,33 @@ fn dyn_trait() {
19411941
"#,
19421942
900,
19431943
);
1944+
check_number(
1945+
r#"
1946+
//- minicore: coerce_unsized, index, slice
1947+
trait A {
1948+
fn x(&self) -> i32;
1949+
}
1950+
1951+
trait B: A {}
1952+
1953+
impl A for i32 {
1954+
fn x(&self) -> i32 {
1955+
5
1956+
}
1957+
}
1958+
1959+
impl B for i32 {
1960+
1961+
}
1962+
1963+
const fn f(x: &dyn B) -> i32 {
1964+
x.x()
1965+
}
1966+
1967+
const GOAL: i32 = f(&2i32);
1968+
"#,
1969+
5,
1970+
);
19441971
}
19451972

19461973
#[test]

crates/hir-ty/src/method_resolution.rs

+15-7
Original file line numberDiff line numberDiff line change
@@ -665,13 +665,21 @@ pub fn is_dyn_method(
665665
};
666666
let self_ty = trait_ref.self_type_parameter(Interner);
667667
if let TyKind::Dyn(d) = self_ty.kind(Interner) {
668-
let is_my_trait_in_bounds =
669-
d.bounds.skip_binders().as_slice(Interner).iter().any(|it| match it.skip_binders() {
670-
// rustc doesn't accept `impl Foo<2> for dyn Foo<5>`, so if the trait id is equal, no matter
671-
// what the generics are, we are sure that the method is come from the vtable.
672-
WhereClause::Implemented(tr) => tr.trait_id == trait_ref.trait_id,
673-
_ => false,
674-
});
668+
let is_my_trait_in_bounds = d
669+
.bounds
670+
.skip_binders()
671+
.as_slice(Interner)
672+
.iter()
673+
.map(|it| it.skip_binders())
674+
.flat_map(|it| match it {
675+
WhereClause::Implemented(tr) => {
676+
all_super_traits(db.upcast(), from_chalk_trait_id(tr.trait_id))
677+
}
678+
_ => smallvec![],
679+
})
680+
// rustc doesn't accept `impl Foo<2> for dyn Foo<5>`, so if the trait id is equal, no matter
681+
// what the generics are, we are sure that the method is come from the vtable.
682+
.any(|x| x == trait_id);
675683
if is_my_trait_in_bounds {
676684
return Some(fn_params);
677685
}

0 commit comments

Comments
 (0)