Skip to content

Don't call fn_arg_names query for non-fn foreign items in resolver #130033

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

Merged
merged 1 commit into from
Sep 16, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 25 additions & 24 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2068,33 +2068,34 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
) {
let res = binding.res();
if filter_fn(res) {
let def_id = res.def_id();
let has_self = match def_id.as_local() {
Some(def_id) => {
self.r.delegation_fn_sigs.get(&def_id).map_or(false, |sig| sig.has_self)
}
None => self
.r
.tcx
.fn_arg_names(def_id)
.first()
.is_some_and(|ident| ident.name == kw::SelfLower),
};
if has_self {
return Some(AssocSuggestion::MethodWithSelf { called });
} else {
match res {
Res::Def(DefKind::AssocFn, _) => {
match res {
Res::Def(DefKind::Fn | DefKind::AssocFn, def_id) => {
let has_self = match def_id.as_local() {
Some(def_id) => self
.r
.delegation_fn_sigs
.get(&def_id)
.map_or(false, |sig| sig.has_self),
None => self
.r
.tcx
.fn_arg_names(def_id)
.first()
.is_some_and(|ident| ident.name == kw::SelfLower),
};
if has_self {
return Some(AssocSuggestion::MethodWithSelf { called });
} else {
return Some(AssocSuggestion::AssocFn { called });
}
Res::Def(DefKind::AssocConst, _) => {
return Some(AssocSuggestion::AssocConst);
}
Res::Def(DefKind::AssocTy, _) => {
return Some(AssocSuggestion::AssocType);
}
_ => {}
}
Res::Def(DefKind::AssocConst, _) => {
return Some(AssocSuggestion::AssocConst);
}
Res::Def(DefKind::AssocTy, _) => {
return Some(AssocSuggestion::AssocType);
}
_ => {}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/resolve/auxiliary/foreign-trait-with-assoc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub trait Foo {
type Bar;
}
11 changes: 11 additions & 0 deletions tests/ui/resolve/dont-compute-arg-names-for-non-fn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//@ aux-build:foreign-trait-with-assoc.rs

extern crate foreign_trait_with_assoc;
use foreign_trait_with_assoc::Foo;

// Make sure we don't try to call `fn_arg_names` on a non-fn item.

impl Foo for Bar {}
//~^ ERROR cannot find type `Bar` in this scope

fn main() {}
14 changes: 14 additions & 0 deletions tests/ui/resolve/dont-compute-arg-names-for-non-fn.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0412]: cannot find type `Bar` in this scope
--> $DIR/dont-compute-arg-names-for-non-fn.rs:8:14
|
LL | impl Foo for Bar {}
| ^^^
|
help: you might have meant to use the associated type
|
LL | impl Foo for Self::Bar {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lol that is incorrect

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but in the resolver we don't really have the right context to avoid this suggestion anyways.

| ++++++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0412`.
Loading