From 29f31c58e9428ee6ad6c2faf2c391eea782f020d Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Fri, 6 Sep 2024 11:56:43 -0400 Subject: [PATCH] Don't call fn_arg_names for non-fn in resolver --- .../rustc_resolve/src/late/diagnostics.rs | 49 ++++++++++--------- .../auxiliary/foreign-trait-with-assoc.rs | 3 ++ .../dont-compute-arg-names-for-non-fn.rs | 11 +++++ .../dont-compute-arg-names-for-non-fn.stderr | 14 ++++++ 4 files changed, 53 insertions(+), 24 deletions(-) create mode 100644 tests/ui/resolve/auxiliary/foreign-trait-with-assoc.rs create mode 100644 tests/ui/resolve/dont-compute-arg-names-for-non-fn.rs create mode 100644 tests/ui/resolve/dont-compute-arg-names-for-non-fn.stderr diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index 8f516c2db09..2f5e1b1168d 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -2068,33 +2068,34 @@ fn extract_node_id(t: &Ty) -> Option { ) { 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); + } + _ => {} } } } diff --git a/tests/ui/resolve/auxiliary/foreign-trait-with-assoc.rs b/tests/ui/resolve/auxiliary/foreign-trait-with-assoc.rs new file mode 100644 index 00000000000..952957ec480 --- /dev/null +++ b/tests/ui/resolve/auxiliary/foreign-trait-with-assoc.rs @@ -0,0 +1,3 @@ +pub trait Foo { + type Bar; +} diff --git a/tests/ui/resolve/dont-compute-arg-names-for-non-fn.rs b/tests/ui/resolve/dont-compute-arg-names-for-non-fn.rs new file mode 100644 index 00000000000..20bbbff8fd2 --- /dev/null +++ b/tests/ui/resolve/dont-compute-arg-names-for-non-fn.rs @@ -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() {} diff --git a/tests/ui/resolve/dont-compute-arg-names-for-non-fn.stderr b/tests/ui/resolve/dont-compute-arg-names-for-non-fn.stderr new file mode 100644 index 00000000000..a1a8bb575e1 --- /dev/null +++ b/tests/ui/resolve/dont-compute-arg-names-for-non-fn.stderr @@ -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 {} + | ++++++ + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0412`.