Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider block impls in lookup_impl_assoc_item_for_trait_ref #14855

Merged
merged 1 commit into from May 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions crates/hir-ty/src/consteval/tests.rs
Expand Up @@ -503,6 +503,35 @@ fn trait_method() {
);
}

#[test]
fn trait_method_inside_block() {
check_number(
r#"
trait Twait {
fn a(&self) -> i32;
}

fn outer() -> impl Twait {
struct Stwuct;

impl Twait for Stwuct {
fn a(&self) -> i32 {
5
}
}
fn f() -> impl Twait {
let s = Stwuct;
s
}
f()
}

const GOAL: i32 = outer().a();
"#,
5,
);
}

#[test]
fn generic_fn() {
check_number(
Expand Down
12 changes: 10 additions & 2 deletions crates/hir-ty/src/method_resolution.rs
Expand Up @@ -729,8 +729,16 @@ fn lookup_impl_assoc_item_for_trait_ref(
let self_ty = trait_ref.self_type_parameter(Interner);
let self_ty_fp = TyFingerprint::for_trait_impl(&self_ty)?;
let impls = db.trait_impls_in_deps(env.krate);
let impls =
impls.iter().flat_map(|impls| impls.for_trait_and_self_ty(hir_trait_id, self_ty_fp));
let self_impls = match self_ty.kind(Interner) {
TyKind::Adt(id, _) => {
id.0.module(db.upcast()).containing_block().map(|x| db.trait_impls_in_block(x))
}
_ => None,
};
let impls = impls
.iter()
.chain(self_impls.as_ref())
.flat_map(|impls| impls.for_trait_and_self_ty(hir_trait_id, self_ty_fp));

let table = InferenceTable::new(db, env);

Expand Down
23 changes: 23 additions & 0 deletions crates/ide/src/goto_definition.rs
Expand Up @@ -1492,6 +1492,29 @@ impl Twait for Stwuct {
fn f() {
let s = Stwuct;
s.a$0();
}
"#,
);
}
#[test]
fn method_call_inside_block() {
check(
r#"
trait Twait {
fn a(&self);
}

fn outer() {
struct Stwuct;

impl Twait for Stwuct {
fn a(&self){}
//^
}
fn f() {
let s = Stwuct;
s.a$0();
}
}
"#,
);
Expand Down