diff --git a/compiler/rustc_hir_typeck/messages.ftl b/compiler/rustc_hir_typeck/messages.ftl index 1ed0756fdd6a7..bb705dce6a4ba 100644 --- a/compiler/rustc_hir_typeck/messages.ftl +++ b/compiler/rustc_hir_typeck/messages.ftl @@ -262,6 +262,8 @@ hir_typeck_self_ctor_from_outer_item = can't reference `Self` constructor from o .label = the inner item doesn't inherit generics from this impl, so `Self` is invalid to reference .suggestion = replace `Self` with the actual type +hir_typeck_self_ctor_from_outer_item_inner_item = `Self` used in this inner item + hir_typeck_slicing_suggestion = consider slicing here hir_typeck_struct_expr_non_exhaustive = diff --git a/compiler/rustc_hir_typeck/src/errors.rs b/compiler/rustc_hir_typeck/src/errors.rs index d15d092b7d3da..2ed3a542bae8e 100644 --- a/compiler/rustc_hir_typeck/src/errors.rs +++ b/compiler/rustc_hir_typeck/src/errors.rs @@ -978,6 +978,15 @@ pub(crate) struct SelfCtorFromOuterItem { pub impl_span: Span, #[subdiagnostic] pub sugg: Option, + #[subdiagnostic] + pub item: Option, +} + +#[derive(Subdiagnostic)] +#[label(hir_typeck_self_ctor_from_outer_item_inner_item)] +pub(crate) struct InnerItem { + #[primary_span] + pub span: Span, } #[derive(LintDiagnostic)] @@ -987,6 +996,8 @@ pub(crate) struct SelfCtorFromOuterItemLint { pub impl_span: Span, #[subdiagnostic] pub sugg: Option, + #[subdiagnostic] + pub item: Option, } #[derive(Subdiagnostic)] diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs index f4e65b42cd423..38586c844445b 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs @@ -1094,11 +1094,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { span: path_span, name: self.tcx.item_name(def.did()).to_ident_string(), }); + let item = match self + .tcx + .hir_node_by_def_id(self.tcx.hir_get_parent_item(hir_id).def_id) + { + hir::Node::Item(item) => Some(errors::InnerItem { + span: item.kind.ident().map(|i| i.span).unwrap_or(item.span), + }), + _ => None, + }; if ty.raw.has_param() { let guar = self.dcx().emit_err(errors::SelfCtorFromOuterItem { span: path_span, impl_span: tcx.def_span(impl_def_id), sugg, + item, }); return (Ty::new_error(self.tcx, guar), res); } else { @@ -1109,6 +1119,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { errors::SelfCtorFromOuterItemLint { impl_span: tcx.def_span(impl_def_id), sugg, + item, }, ); } diff --git a/compiler/rustc_resolve/messages.ftl b/compiler/rustc_resolve/messages.ftl index 5bf90d2637df5..d462ff589f0da 100644 --- a/compiler/rustc_resolve/messages.ftl +++ b/compiler/rustc_resolve/messages.ftl @@ -180,6 +180,11 @@ resolve_generic_params_from_outer_item_const = a `const` is a separate item from resolve_generic_params_from_outer_item_const_param = const parameter from outer item +resolve_generic_params_from_outer_item_inner_item = {$is_self -> + [true] `Self` + *[false] generic parameter + } used in this inner {$descr} + resolve_generic_params_from_outer_item_self_ty_alias = `Self` type implicitly declared here, by this `impl` resolve_generic_params_from_outer_item_self_ty_param = can't use `Self` here diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index 236ab1f09d35f..3020ecb6e7113 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -40,7 +40,7 @@ use crate::errors::{ MaybeMissingMacroRulesName, }; use crate::imports::{Import, ImportKind}; -use crate::late::{PatternSource, Rib}; +use crate::late::{DiagMetadata, PatternSource, Rib}; use crate::{ AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, BindingError, BindingKey, Finalize, ForwardGenericParamBanReason, HasGenericParams, LexicalScopeBinding, MacroRulesScope, Module, @@ -553,11 +553,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { resolution_error: ResolutionError<'ra>, ) -> Diag<'_> { match resolution_error { - ResolutionError::GenericParamsFromOuterItem( + ResolutionError::GenericParamsFromOuterItem { outer_res, has_generic_params, def_kind, - ) => { + inner_item, + } => { use errs::GenericParamsFromOuterItemLabel as Label; let static_or_const = match def_kind { DefKind::Static { .. } => { @@ -575,6 +576,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { sugg: None, static_or_const, is_self, + item: inner_item.as_ref().map(|(span, kind)| { + errs::GenericParamsFromOuterItemInnerItem { + span: *span, + descr: kind.descr().to_string(), + } + }), }; let sm = self.tcx.sess.source_map(); @@ -608,7 +615,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { } }; - if let HasGenericParams::Yes(span) = has_generic_params { + if let HasGenericParams::Yes(span) = has_generic_params + && !matches!(inner_item, Some((_, ItemKind::Delegation(..)))) + { let name = self.tcx.item_name(def_id); let (span, snippet) = if span.is_empty() { let snippet = format!("<{name}>"); @@ -2401,6 +2410,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { module: Option>, failed_segment_idx: usize, ident: Ident, + diag_metadata: Option<&DiagMetadata<'_>>, ) -> (String, Option) { let is_last = failed_segment_idx == path.len() - 1; let ns = if is_last { opt_ns.unwrap_or(TypeNS) } else { TypeNS }; @@ -2506,6 +2516,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { None, &ribs[ns_to_try], ignore_binding, + diag_metadata, ) { // we found a locally-imported or available item/module Some(LexicalScopeBinding::Item(binding)) => Some(binding), @@ -2556,6 +2567,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { None, &ribs[ValueNS], ignore_binding, + diag_metadata, ) } else { None diff --git a/compiler/rustc_resolve/src/errors.rs b/compiler/rustc_resolve/src/errors.rs index f0ea97ba8a0c0..5c5938a3260ed 100644 --- a/compiler/rustc_resolve/src/errors.rs +++ b/compiler/rustc_resolve/src/errors.rs @@ -24,6 +24,16 @@ pub(crate) struct GenericParamsFromOuterItem { #[subdiagnostic] pub(crate) static_or_const: Option, pub(crate) is_self: bool, + #[subdiagnostic] + pub(crate) item: Option, +} + +#[derive(Subdiagnostic)] +#[label(resolve_generic_params_from_outer_item_inner_item)] +pub(crate) struct GenericParamsFromOuterItemInnerItem { + #[primary_span] + pub(crate) span: Span, + pub(crate) descr: String, } #[derive(Subdiagnostic)] @@ -47,7 +57,12 @@ pub(crate) enum GenericParamsFromOuterItemLabel { } #[derive(Subdiagnostic)] -#[suggestion(resolve_suggestion, code = "{snippet}", applicability = "maybe-incorrect")] +#[suggestion( + resolve_suggestion, + code = "{snippet}", + applicability = "maybe-incorrect", + style = "verbose" +)] pub(crate) struct GenericParamsFromOuterItemSugg { #[primary_span] pub(crate) span: Span, diff --git a/compiler/rustc_resolve/src/ident.rs b/compiler/rustc_resolve/src/ident.rs index 2a195c8068dac..278c0fe35b7d8 100644 --- a/compiler/rustc_resolve/src/ident.rs +++ b/compiler/rustc_resolve/src/ident.rs @@ -12,7 +12,9 @@ use tracing::{debug, instrument}; use crate::errors::{ParamKindInEnumDiscriminant, ParamKindInNonTrivialAnonConst}; use crate::imports::{Import, NameResolution}; -use crate::late::{ConstantHasGenerics, NoConstantGenericsReason, PathSource, Rib, RibKind}; +use crate::late::{ + ConstantHasGenerics, DiagMetadata, NoConstantGenericsReason, PathSource, Rib, RibKind, +}; use crate::macros::{MacroRulesScope, sub_namespace_match}; use crate::{ AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, BindingKey, CmResolver, Determinacy, @@ -295,6 +297,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { finalize: Option, ribs: &[Rib<'ra>], ignore_binding: Option>, + diag_metadata: Option<&DiagMetadata<'_>>, ) -> Option> { assert!(ns == TypeNS || ns == ValueNS); let orig_ident = ident; @@ -326,6 +329,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { finalize.map(|finalize| finalize.path_span), *original_rib_ident_def, ribs, + diag_metadata, ))); } else if let RibKind::Block(Some(module)) = rib.kind && let Ok(binding) = self.cm().resolve_ident_in_module_unadjusted( @@ -1193,6 +1197,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { finalize: Option, original_rib_ident_def: Ident, all_ribs: &[Rib<'ra>], + diag_metadata: Option<&DiagMetadata<'_>>, ) -> Res { debug!("validate_res_from_ribs({:?})", res); let ribs = &all_ribs[rib_index + 1..]; @@ -1391,13 +1396,26 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { }; if let Some(span) = finalize { + let item = if let Some(diag_metadata) = diag_metadata + && let Some(current_item) = diag_metadata.current_item + { + let span = current_item + .kind + .ident() + .map(|i| i.span) + .unwrap_or(current_item.span); + Some((span, current_item.kind.clone())) + } else { + None + }; self.report_error( span, - ResolutionError::GenericParamsFromOuterItem( - res, + ResolutionError::GenericParamsFromOuterItem { + outer_res: res, has_generic_params, def_kind, - ), + inner_item: item, + }, ); } return Res::Err; @@ -1466,13 +1484,26 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { // This was an attempt to use a const parameter outside its scope. if let Some(span) = finalize { + let item = if let Some(diag_metadata) = diag_metadata + && let Some(current_item) = diag_metadata.current_item + { + let span = current_item + .kind + .ident() + .map(|i| i.span) + .unwrap_or(current_item.span); + Some((span, current_item.kind.clone())) + } else { + None + }; self.report_error( span, - ResolutionError::GenericParamsFromOuterItem( - res, + ResolutionError::GenericParamsFromOuterItem { + outer_res: res, has_generic_params, def_kind, - ), + inner_item: item, + }, ); } return Res::Err; @@ -1501,6 +1532,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { None, None, ignore_import, + None, ) } #[instrument(level = "debug", skip(self))] @@ -1522,6 +1554,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { None, ignore_binding, ignore_import, + None, ) } @@ -1535,6 +1568,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { ribs: Option<&PerNS>>>, ignore_binding: Option>, ignore_import: Option>, + diag_metadata: Option<&DiagMetadata<'_>>, ) -> PathResult<'ra> { let mut module = None; let mut module_had_parse_errors = false; @@ -1675,6 +1709,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { finalize, &ribs[ns], ignore_binding, + diag_metadata, ) { // we found a locally-imported or available item/module Some(LexicalScopeBinding::Item(binding)) => Ok(binding), @@ -1800,6 +1835,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { module, segment_idx, ident, + diag_metadata, ) }, ); diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index e3051dc38eca2..7bc5943123c1b 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -667,7 +667,7 @@ pub(crate) struct UnnecessaryQualification<'ra> { } #[derive(Default, Debug)] -struct DiagMetadata<'ast> { +pub(crate) struct DiagMetadata<'ast> { /// The current trait's associated items' ident, used for diagnostic suggestions. current_trait_assoc_items: Option<&'ast [Box]>, @@ -677,8 +677,8 @@ struct DiagMetadata<'ast> { /// The current self item if inside an ADT (used for better errors). current_self_item: Option, - /// The current trait (used to suggest). - current_item: Option<&'ast Item>, + /// The current item being evaluated (used for suggestions and more detail in errors). + pub(crate) current_item: Option<&'ast Item>, /// When processing generic arguments and encountering an unresolved ident not found, /// suggest introducing a type or const param depending on the context. @@ -1457,6 +1457,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { None, &self.ribs[ns], None, + Some(&self.diag_metadata), ) } @@ -1474,6 +1475,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { finalize, &self.ribs[ns], ignore_binding, + Some(&self.diag_metadata), ) } @@ -1493,6 +1495,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { Some(&self.ribs), None, None, + Some(&self.diag_metadata), ) } diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index f6a4f59cb339a..6b671df433b3d 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -241,7 +241,12 @@ struct BindingError { #[derive(Debug)] enum ResolutionError<'ra> { /// Error E0401: can't use type or const parameters from outer item. - GenericParamsFromOuterItem(Res, HasGenericParams, DefKind), + GenericParamsFromOuterItem { + outer_res: Res, + has_generic_params: HasGenericParams, + def_kind: DefKind, + inner_item: Option<(Span, ast::ItemKind)>, + }, /// Error E0403: the name is already used for a type or const parameter in this generic /// parameter list. NameAlreadyUsedInParameterList(Ident, Span), diff --git a/tests/ui/const-generics/early/const-param-from-outer-fn.stderr b/tests/ui/const-generics/early/const-param-from-outer-fn.stderr index faba4ce10a1b4..3c25dff41aa22 100644 --- a/tests/ui/const-generics/early/const-param-from-outer-fn.stderr +++ b/tests/ui/const-generics/early/const-param-from-outer-fn.stderr @@ -4,9 +4,14 @@ error[E0401]: can't use generic parameters from outer item LL | fn foo() { | - const parameter from outer item LL | fn bar() -> u32 { - | - help: try introducing a local generic parameter here: `` + | --- generic parameter used in this inner function LL | X | ^ use of generic parameter from outer item + | +help: try introducing a local generic parameter here + | +LL | fn bar() -> u32 { + | +++ error: aborting due to 1 previous error diff --git a/tests/ui/delegation/target-expr.stderr b/tests/ui/delegation/target-expr.stderr index f5556bf9f4514..edd1a584eab26 100644 --- a/tests/ui/delegation/target-expr.stderr +++ b/tests/ui/delegation/target-expr.stderr @@ -4,7 +4,7 @@ error[E0401]: can't use generic parameters from outer item LL | fn bar(_: T) { | - type parameter from outer item LL | reuse Trait::static_method { - | - help: try introducing a local generic parameter here: `T,` + | ------------- generic parameter used in this inner delegated function LL | LL | let _ = T::Default(); | ^^^^^^^^^^ use of generic parameter from outer item diff --git a/tests/ui/error-codes/E0401.stderr b/tests/ui/error-codes/E0401.stderr index 5d6878620c89a..8daaf09df1592 100644 --- a/tests/ui/error-codes/E0401.stderr +++ b/tests/ui/error-codes/E0401.stderr @@ -4,9 +4,14 @@ error[E0401]: can't use generic parameters from outer item LL | fn foo(x: T) { | - type parameter from outer item LL | fn bfnr, W: Fn()>(y: T) { - | - ^ use of generic parameter from outer item - | | - | help: try introducing a local generic parameter here: `T,` + | ---- ^ use of generic parameter from outer item + | | + | generic parameter used in this inner function + | +help: try introducing a local generic parameter here + | +LL | fn bfnr, W: Fn()>(y: T) { + | ++ error[E0401]: can't use generic parameters from outer item --> $DIR/E0401.rs:9:16 @@ -15,10 +20,15 @@ LL | fn foo(x: T) { | - type parameter from outer item ... LL | fn baz $DIR/E0401.rs:22:25 @@ -27,10 +37,11 @@ LL | impl Iterator for A { | ---- `Self` type implicitly declared here, by this `impl` ... LL | fn helper(sel: &Self) -> u8 { - | ^^^^ - | | - | use of `Self` from outer item - | refer to the type directly here instead + | ------ ^^^^ + | | | + | | use of `Self` from outer item + | | refer to the type directly here instead + | `Self` used in this inner function error: aborting due to 3 previous errors diff --git a/tests/ui/generics/enum-definition-with-outer-generic-parameter-5997.stderr b/tests/ui/generics/enum-definition-with-outer-generic-parameter-5997.stderr index aea0f049b07d3..fb2d2f247b65b 100644 --- a/tests/ui/generics/enum-definition-with-outer-generic-parameter-5997.stderr +++ b/tests/ui/generics/enum-definition-with-outer-generic-parameter-5997.stderr @@ -4,9 +4,14 @@ error[E0401]: can't use generic parameters from outer item LL | fn f() -> bool { | - type parameter from outer item LL | enum E { V(Z) } - | - ^ use of generic parameter from outer item - | | - | help: try introducing a local generic parameter here: `` + | - ^ use of generic parameter from outer item + | | + | generic parameter used in this inner enum + | +help: try introducing a local generic parameter here + | +LL | enum E { V(Z) } + | +++ error: aborting due to 1 previous error diff --git a/tests/ui/generics/generic-params-nested-fn-scope-error.stderr b/tests/ui/generics/generic-params-nested-fn-scope-error.stderr index 7fd1069c651fa..f809740ed381f 100644 --- a/tests/ui/generics/generic-params-nested-fn-scope-error.stderr +++ b/tests/ui/generics/generic-params-nested-fn-scope-error.stderr @@ -4,9 +4,14 @@ error[E0401]: can't use generic parameters from outer item LL | fn foo(v: Vec) -> U { | - type parameter from outer item LL | fn bar(w: [U]) -> U { - | - ^ use of generic parameter from outer item - | | - | help: try introducing a local generic parameter here: `` + | --- ^ use of generic parameter from outer item + | | + | generic parameter used in this inner function + | +help: try introducing a local generic parameter here + | +LL | fn bar(w: [U]) -> U { + | +++ error[E0401]: can't use generic parameters from outer item --> $DIR/generic-params-nested-fn-scope-error.rs:5:23 @@ -14,9 +19,14 @@ error[E0401]: can't use generic parameters from outer item LL | fn foo(v: Vec) -> U { | - type parameter from outer item LL | fn bar(w: [U]) -> U { - | - ^ use of generic parameter from outer item - | | - | help: try introducing a local generic parameter here: `` + | --- ^ use of generic parameter from outer item + | | + | generic parameter used in this inner function + | +help: try introducing a local generic parameter here + | +LL | fn bar(w: [U]) -> U { + | +++ error: aborting due to 2 previous errors diff --git a/tests/ui/generics/issue-98432.stderr b/tests/ui/generics/issue-98432.stderr index 2b09d43960f62..a1efee78cb7df 100644 --- a/tests/ui/generics/issue-98432.stderr +++ b/tests/ui/generics/issue-98432.stderr @@ -5,9 +5,14 @@ LL | impl Struct { | - type parameter from outer item LL | const CONST: fn() = || { LL | struct _Obligation where T:; - | - ^ use of generic parameter from outer item - | | - | help: try introducing a local generic parameter here: `` + | ----------- ^ use of generic parameter from outer item + | | + | generic parameter used in this inner struct + | +help: try introducing a local generic parameter here + | +LL | struct _Obligation where T:; + | +++ error: aborting due to 1 previous error diff --git a/tests/ui/malformed/do-not-ice-on-note_and_explain.stderr b/tests/ui/malformed/do-not-ice-on-note_and_explain.stderr index 11a8c01e49094..63de7099797ca 100644 --- a/tests/ui/malformed/do-not-ice-on-note_and_explain.stderr +++ b/tests/ui/malformed/do-not-ice-on-note_and_explain.stderr @@ -3,7 +3,9 @@ error[E0401]: can't reference `Self` constructor from outer item | LL | impl A { | ------------ the inner item doesn't inherit generics from this impl, so `Self` is invalid to reference -... +LL | fn d() { +LL | fn d() { + | - `Self` used in this inner item LL | Self(1) | ^^^^ help: replace `Self` with the actual type: `A` diff --git a/tests/ui/resolve/bad-type-env-capture.stderr b/tests/ui/resolve/bad-type-env-capture.stderr index 3f9bc9149c24a..c565997ca2a26 100644 --- a/tests/ui/resolve/bad-type-env-capture.stderr +++ b/tests/ui/resolve/bad-type-env-capture.stderr @@ -4,9 +4,14 @@ error[E0401]: can't use generic parameters from outer item LL | fn foo() { | - type parameter from outer item LL | fn bar(b: T) { } - | - ^ use of generic parameter from outer item - | | - | help: try introducing a local generic parameter here: `` + | --- ^ use of generic parameter from outer item + | | + | generic parameter used in this inner function + | +help: try introducing a local generic parameter here + | +LL | fn bar(b: T) { } + | +++ error: aborting due to 1 previous error diff --git a/tests/ui/resolve/generic-params-from-outer-item-in-const-item.default.stderr b/tests/ui/resolve/generic-params-from-outer-item-in-const-item.default.stderr index fbb9ede8aa175..bc67e9dce4e3b 100644 --- a/tests/ui/resolve/generic-params-from-outer-item-in-const-item.default.stderr +++ b/tests/ui/resolve/generic-params-from-outer-item-in-const-item.default.stderr @@ -4,7 +4,9 @@ error[E0401]: can't use generic parameters from outer item LL | fn outer() { // outer function | - type parameter from outer item LL | const K: u32 = T::C; - | ^^^^ use of generic parameter from outer item + | - ^^^^ use of generic parameter from outer item + | | + | generic parameter used in this inner constant item | = note: a `const` is a separate item from the item that contains it @@ -15,7 +17,9 @@ LL | impl Tr for T { // outer impl block | - type parameter from outer item LL | const C: u32 = { LL | const I: u32 = T::C; - | ^^^^ use of generic parameter from outer item + | - ^^^^ use of generic parameter from outer item + | | + | generic parameter used in this inner constant item | = note: a `const` is a separate item from the item that contains it @@ -25,7 +29,9 @@ error[E0401]: can't use generic parameters from outer item LL | struct S(U32<{ // outer struct | - type parameter from outer item LL | const _: u32 = T::C; - | ^^^^ use of generic parameter from outer item + | - ^^^^ use of generic parameter from outer item + | | + | generic parameter used in this inner constant item | = note: a `const` is a separate item from the item that contains it diff --git a/tests/ui/resolve/generic-params-from-outer-item-in-const-item.generic_const_items.stderr b/tests/ui/resolve/generic-params-from-outer-item-in-const-item.generic_const_items.stderr index 60aa94038c3ad..3959d117c7c9a 100644 --- a/tests/ui/resolve/generic-params-from-outer-item-in-const-item.generic_const_items.stderr +++ b/tests/ui/resolve/generic-params-from-outer-item-in-const-item.generic_const_items.stderr @@ -4,11 +4,15 @@ error[E0401]: can't use generic parameters from outer item LL | fn outer() { // outer function | - type parameter from outer item LL | const K: u32 = T::C; - | - ^^^^ use of generic parameter from outer item - | | - | help: try introducing a local generic parameter here: `` + | - ^^^^ use of generic parameter from outer item + | | + | generic parameter used in this inner constant item | = note: a `const` is a separate item from the item that contains it +help: try introducing a local generic parameter here + | +LL | const K: u32 = T::C; + | +++ error[E0401]: can't use generic parameters from outer item --> $DIR/generic-params-from-outer-item-in-const-item.rs:19:24 @@ -17,11 +21,15 @@ LL | impl Tr for T { // outer impl block | - type parameter from outer item LL | const C: u32 = { LL | const I: u32 = T::C; - | - ^^^^ use of generic parameter from outer item - | | - | help: try introducing a local generic parameter here: `` + | - ^^^^ use of generic parameter from outer item + | | + | generic parameter used in this inner constant item | = note: a `const` is a separate item from the item that contains it +help: try introducing a local generic parameter here + | +LL | const I: u32 = T::C; + | +++ error[E0401]: can't use generic parameters from outer item --> $DIR/generic-params-from-outer-item-in-const-item.rs:27:20 @@ -29,11 +37,15 @@ error[E0401]: can't use generic parameters from outer item LL | struct S(U32<{ // outer struct | - type parameter from outer item LL | const _: u32 = T::C; - | - ^^^^ use of generic parameter from outer item - | | - | help: try introducing a local generic parameter here: `` + | - ^^^^ use of generic parameter from outer item + | | + | generic parameter used in this inner constant item | = note: a `const` is a separate item from the item that contains it +help: try introducing a local generic parameter here + | +LL | const _: u32 = T::C; + | +++ error: aborting due to 3 previous errors diff --git a/tests/ui/resolve/issue-12796.stderr b/tests/ui/resolve/issue-12796.stderr index 2305971303a71..b5828c6f5fc86 100644 --- a/tests/ui/resolve/issue-12796.stderr +++ b/tests/ui/resolve/issue-12796.stderr @@ -2,10 +2,11 @@ error[E0401]: can't use `Self` from outer item --> $DIR/issue-12796.rs:3:22 | LL | fn inner(_: &Self) { - | ^^^^ - | | - | use of `Self` from outer item - | can't use `Self` here + | ----- ^^^^ + | | | + | | use of `Self` from outer item + | | can't use `Self` here + | `Self` used in this inner function error: aborting due to 1 previous error diff --git a/tests/ui/resolve/issue-3021-c.stderr b/tests/ui/resolve/issue-3021-c.stderr index 537bbaf7b6a56..8c554fd1b97dc 100644 --- a/tests/ui/resolve/issue-3021-c.stderr +++ b/tests/ui/resolve/issue-3021-c.stderr @@ -5,9 +5,14 @@ LL | fn siphash() { | - type parameter from outer item LL | LL | trait U { - | - help: try introducing a local generic parameter here: `` + | - generic parameter used in this inner trait LL | fn g(&self, x: T) -> T; | ^ use of generic parameter from outer item + | +help: try introducing a local generic parameter here + | +LL | trait U { + | +++ error[E0401]: can't use generic parameters from outer item --> $DIR/issue-3021-c.rs:4:30 @@ -16,9 +21,14 @@ LL | fn siphash() { | - type parameter from outer item LL | LL | trait U { - | - help: try introducing a local generic parameter here: `` + | - generic parameter used in this inner trait LL | fn g(&self, x: T) -> T; | ^ use of generic parameter from outer item + | +help: try introducing a local generic parameter here + | +LL | trait U { + | +++ error: aborting due to 2 previous errors diff --git a/tests/ui/resolve/issue-3214.stderr b/tests/ui/resolve/issue-3214.stderr index 1c64fdc1711fd..ab12676bdd805 100644 --- a/tests/ui/resolve/issue-3214.stderr +++ b/tests/ui/resolve/issue-3214.stderr @@ -4,9 +4,14 @@ error[E0401]: can't use generic parameters from outer item LL | fn foo() { | - type parameter from outer item LL | struct Foo { - | - help: try introducing a local generic parameter here: `` + | --- generic parameter used in this inner struct LL | x: T, | ^ use of generic parameter from outer item + | +help: try introducing a local generic parameter here + | +LL | struct Foo { + | +++ error[E0107]: struct takes 0 generic arguments but 1 generic argument was supplied --> $DIR/issue-3214.rs:6:22 diff --git a/tests/ui/resolve/issue-65025-extern-static-parent-generics.stderr b/tests/ui/resolve/issue-65025-extern-static-parent-generics.stderr index ca32147d19765..2d21ed0155a70 100644 --- a/tests/ui/resolve/issue-65025-extern-static-parent-generics.stderr +++ b/tests/ui/resolve/issue-65025-extern-static-parent-generics.stderr @@ -1,11 +1,14 @@ error[E0401]: can't use generic parameters from outer item --> $DIR/issue-65025-extern-static-parent-generics.rs:3:28 | -LL | unsafe fn foo() { - | - type parameter from outer item -LL | extern "C" { -LL | static baz: *const A; - | ^ use of generic parameter from outer item +LL | unsafe fn foo() { + | - type parameter from outer item +LL | / extern "C" { +LL | | static baz: *const A; + | | ^ use of generic parameter from outer item +LL | | +LL | | } + | |_____- generic parameter used in this inner extern block | = note: a `static` is a separate item from the item that contains it diff --git a/tests/ui/resolve/issue-65035-static-with-parent-generics.stderr b/tests/ui/resolve/issue-65035-static-with-parent-generics.stderr index 98ffb4567f164..b22bfb719bd50 100644 --- a/tests/ui/resolve/issue-65035-static-with-parent-generics.stderr +++ b/tests/ui/resolve/issue-65035-static-with-parent-generics.stderr @@ -1,11 +1,14 @@ error[E0401]: can't use generic parameters from outer item --> $DIR/issue-65035-static-with-parent-generics.rs:3:26 | -LL | fn f() { - | - type parameter from outer item -LL | extern "C" { -LL | static a: *const T; - | ^ use of generic parameter from outer item +LL | fn f() { + | - type parameter from outer item +LL | / extern "C" { +LL | | static a: *const T; + | | ^ use of generic parameter from outer item +LL | | +LL | | } + | |_____- generic parameter used in this inner extern block | = note: a `static` is a separate item from the item that contains it @@ -15,18 +18,23 @@ error[E0401]: can't use generic parameters from outer item LL | fn g() { | - type parameter from outer item LL | static a: *const T = Default::default(); - | ^ use of generic parameter from outer item + | - ^ use of generic parameter from outer item + | | + | generic parameter used in this inner static item | = note: a `static` is a separate item from the item that contains it error[E0401]: can't use generic parameters from outer item --> $DIR/issue-65035-static-with-parent-generics.rs:15:24 | -LL | fn h() { - | - const parameter from outer item -LL | extern "C" { -LL | static a: [u8; N]; - | ^ use of generic parameter from outer item +LL | fn h() { + | - const parameter from outer item +LL | / extern "C" { +LL | | static a: [u8; N]; + | | ^ use of generic parameter from outer item +LL | | +LL | | } + | |_____- generic parameter used in this inner extern block | = note: a `static` is a separate item from the item that contains it @@ -36,7 +44,9 @@ error[E0401]: can't use generic parameters from outer item LL | fn i() { | - const parameter from outer item LL | static a: [u8; N] = [0; N]; - | ^ use of generic parameter from outer item + | - ^ use of generic parameter from outer item + | | + | generic parameter used in this inner static item | = note: a `static` is a separate item from the item that contains it @@ -46,7 +56,9 @@ error[E0401]: can't use generic parameters from outer item LL | fn i() { | - const parameter from outer item LL | static a: [u8; N] = [0; N]; - | ^ use of generic parameter from outer item + | - ^ use of generic parameter from outer item + | | + | generic parameter used in this inner static item | = note: a `static` is a separate item from the item that contains it diff --git a/tests/ui/resolve/resolve-type-param-in-item-in-trait.stderr b/tests/ui/resolve/resolve-type-param-in-item-in-trait.stderr index 1ab56fdc50441..00aa645688e7f 100644 --- a/tests/ui/resolve/resolve-type-param-in-item-in-trait.stderr +++ b/tests/ui/resolve/resolve-type-param-in-item-in-trait.stderr @@ -5,9 +5,14 @@ LL | trait TraitA { | - type parameter from outer item LL | fn outer(&self) { LL | enum Foo { - | - help: try introducing a local generic parameter here: `A,` + | --- generic parameter used in this inner enum LL | Variance(A) | ^ use of generic parameter from outer item + | +help: try introducing a local generic parameter here + | +LL | enum Foo { + | ++ error[E0401]: can't use generic parameters from outer item --> $DIR/resolve-type-param-in-item-in-trait.rs:16:23 @@ -16,9 +21,14 @@ LL | trait TraitB { | - type parameter from outer item LL | fn outer(&self) { LL | struct Foo(A); - | - ^ use of generic parameter from outer item - | | - | help: try introducing a local generic parameter here: `A,` + | --- ^ use of generic parameter from outer item + | | + | generic parameter used in this inner struct + | +help: try introducing a local generic parameter here + | +LL | struct Foo(A); + | ++ error[E0401]: can't use generic parameters from outer item --> $DIR/resolve-type-param-in-item-in-trait.rs:23:28 @@ -27,9 +37,14 @@ LL | trait TraitC { | - type parameter from outer item LL | fn outer(&self) { LL | struct Foo { a: A } - | - ^ use of generic parameter from outer item - | | - | help: try introducing a local generic parameter here: `A,` + | --- ^ use of generic parameter from outer item + | | + | generic parameter used in this inner struct + | +help: try introducing a local generic parameter here + | +LL | struct Foo { a: A } + | ++ error[E0401]: can't use generic parameters from outer item --> $DIR/resolve-type-param-in-item-in-trait.rs:30:22 @@ -38,9 +53,14 @@ LL | trait TraitD { | - type parameter from outer item LL | fn outer(&self) { LL | fn foo(a: A) { } - | - ^ use of generic parameter from outer item - | | - | help: try introducing a local generic parameter here: `A,` + | --- ^ use of generic parameter from outer item + | | + | generic parameter used in this inner function + | +help: try introducing a local generic parameter here + | +LL | fn foo(a: A) { } + | ++ error: aborting due to 4 previous errors diff --git a/tests/ui/resolve/use-self-in-inner-fn.rs b/tests/ui/resolve/use-self-in-inner-fn.rs index 62f9dc5664ffe..ed64ee885271c 100644 --- a/tests/ui/resolve/use-self-in-inner-fn.rs +++ b/tests/ui/resolve/use-self-in-inner-fn.rs @@ -6,6 +6,7 @@ impl A { fn peach(this: &Self) { //~^ ERROR can't use `Self` from outer item //~| NOTE use of `Self` from outer item + //~| NOTE `Self` used in this inner function //~| NOTE refer to the type directly here instead } } diff --git a/tests/ui/resolve/use-self-in-inner-fn.stderr b/tests/ui/resolve/use-self-in-inner-fn.stderr index 9c388df8bc20c..645875f6e726d 100644 --- a/tests/ui/resolve/use-self-in-inner-fn.stderr +++ b/tests/ui/resolve/use-self-in-inner-fn.stderr @@ -5,10 +5,11 @@ LL | impl A { | ---- `Self` type implicitly declared here, by this `impl` ... LL | fn peach(this: &Self) { - | ^^^^ - | | - | use of `Self` from outer item - | refer to the type directly here instead + | ----- ^^^^ + | | | + | | use of `Self` from outer item + | | refer to the type directly here instead + | `Self` used in this inner function error: aborting due to 1 previous error diff --git a/tests/ui/self/self-ctor-nongeneric.stderr b/tests/ui/self/self-ctor-nongeneric.stderr index b53ecbe55b594..c3ae7df2ad25e 100644 --- a/tests/ui/self/self-ctor-nongeneric.stderr +++ b/tests/ui/self/self-ctor-nongeneric.stderr @@ -5,7 +5,9 @@ LL | impl S0 { | ------- the inner item doesn't inherit generics from this impl, so `Self` is invalid to reference LL | fn foo() { LL | const C: S0 = Self(0); - | ^^^^ help: replace `Self` with the actual type: `S0` + | - ^^^^ help: replace `Self` with the actual type: `S0` + | | + | `Self` used in this inner item | = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #124186 @@ -17,6 +19,8 @@ warning: can't reference `Self` constructor from outer item LL | impl S0 { | ------- the inner item doesn't inherit generics from this impl, so `Self` is invalid to reference ... +LL | fn bar() -> S0 { + | --- `Self` used in this inner item LL | Self(0) | ^^^^ help: replace `Self` with the actual type: `S0` | diff --git a/tests/ui/self/self-ctor.stderr b/tests/ui/self/self-ctor.stderr index 0cb22baaa1a05..b8dfd324c3f36 100644 --- a/tests/ui/self/self-ctor.stderr +++ b/tests/ui/self/self-ctor.stderr @@ -5,7 +5,9 @@ LL | impl S0 { | ------------- the inner item doesn't inherit generics from this impl, so `Self` is invalid to reference LL | fn foo() { LL | const C: S0 = Self(0); - | ^^^^ help: replace `Self` with the actual type: `S0` + | - ^^^^ help: replace `Self` with the actual type: `S0` + | | + | `Self` used in this inner item error[E0401]: can't reference `Self` constructor from outer item --> $DIR/self-ctor.rs:8:13 @@ -13,6 +15,8 @@ error[E0401]: can't reference `Self` constructor from outer item LL | impl S0 { | ------------- the inner item doesn't inherit generics from this impl, so `Self` is invalid to reference ... +LL | fn bar() -> S0 { + | --- `Self` used in this inner item LL | Self(0) | ^^^^ help: replace `Self` with the actual type: `S0` diff --git a/tests/ui/statics/static-generic-param-soundness.stderr b/tests/ui/statics/static-generic-param-soundness.stderr index 47554c7fcb0f0..72f65e2bac7cb 100644 --- a/tests/ui/statics/static-generic-param-soundness.stderr +++ b/tests/ui/statics/static-generic-param-soundness.stderr @@ -4,7 +4,9 @@ error[E0401]: can't use generic parameters from outer item LL | fn foo() { | - type parameter from outer item LL | static a: Bar = Bar::What; - | ^ use of generic parameter from outer item + | - ^ use of generic parameter from outer item + | | + | generic parameter used in this inner static item | = note: a `static` is a separate item from the item that contains it diff --git a/tests/ui/traits/ice-index-out-of-bounds-issue-117446.stderr b/tests/ui/traits/ice-index-out-of-bounds-issue-117446.stderr index ad33a70ed3bb6..2e75003c49b8a 100644 --- a/tests/ui/traits/ice-index-out-of-bounds-issue-117446.stderr +++ b/tests/ui/traits/ice-index-out-of-bounds-issue-117446.stderr @@ -24,6 +24,9 @@ error[E0401]: can't reference `Self` constructor from outer item LL | impl<'a, T> Foo<'a> for Repeated { | ----------------------------------- the inner item doesn't inherit generics from this impl, so `Self` is invalid to reference ... +LL | fn inner(value: Option<()>) -> Repeated { + | ----- `Self` used in this inner item +LL | match value { LL | _ => Self(unimplemented!()), | ^^^^ help: replace `Self` with the actual type: `Repeated` diff --git a/tests/ui/type/type-arg-out-of-scope.stderr b/tests/ui/type/type-arg-out-of-scope.stderr index fcaaca1770f67..3d8850ebccea5 100644 --- a/tests/ui/type/type-arg-out-of-scope.stderr +++ b/tests/ui/type/type-arg-out-of-scope.stderr @@ -4,9 +4,14 @@ error[E0401]: can't use generic parameters from outer item LL | fn foo(x: T) { | - type parameter from outer item LL | fn bar(f: Box T>) { } - | - ^ use of generic parameter from outer item - | | - | help: try introducing a local generic parameter here: `` + | --- ^ use of generic parameter from outer item + | | + | generic parameter used in this inner function + | +help: try introducing a local generic parameter here + | +LL | fn bar(f: Box T>) { } + | +++ error[E0401]: can't use generic parameters from outer item --> $DIR/type-arg-out-of-scope.rs:2:35 @@ -14,9 +19,14 @@ error[E0401]: can't use generic parameters from outer item LL | fn foo(x: T) { | - type parameter from outer item LL | fn bar(f: Box T>) { } - | - ^ use of generic parameter from outer item - | | - | help: try introducing a local generic parameter here: `` + | --- ^ use of generic parameter from outer item + | | + | generic parameter used in this inner function + | +help: try introducing a local generic parameter here + | +LL | fn bar(f: Box T>) { } + | +++ error: aborting due to 2 previous errors