diff --git a/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs b/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs index c193aad2afd00..42fe59d3e5b59 100644 --- a/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs +++ b/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs @@ -1125,13 +1125,13 @@ fn check_region_bounds_on_impl_item<'tcx>( .expect("expected impl item to have generics or else we can't compare them") .span; - let mut generics_span = None; + let mut generics_span = tcx.def_span(trait_m.def_id); let mut bounds_span = vec![]; let mut where_span = None; if let Some(trait_node) = tcx.hir_get_if_local(trait_m.def_id) && let Some(trait_generics) = trait_node.generics() { - generics_span = Some(trait_generics.span); + generics_span = trait_generics.span; // FIXME: we could potentially look at the impl's bounds to not point at bounds that // *are* present in the impl. for p in trait_generics.predicates { diff --git a/compiler/rustc_hir_analysis/src/errors.rs b/compiler/rustc_hir_analysis/src/errors.rs index 4c6c2504126a8..448d819b4eb93 100644 --- a/compiler/rustc_hir_analysis/src/errors.rs +++ b/compiler/rustc_hir_analysis/src/errors.rs @@ -198,7 +198,7 @@ pub(crate) struct LifetimesOrBoundsMismatchOnTrait { #[label] pub span: Span, #[label(hir_analysis_generics_label)] - pub generics_span: Option, + pub generics_span: Span, #[label(hir_analysis_where_label)] pub where_span: Option, #[label(hir_analysis_bounds_label)] diff --git a/compiler/rustc_hir_analysis/src/impl_wf_check.rs b/compiler/rustc_hir_analysis/src/impl_wf_check.rs index c30b39dfe7656..65a65460f37b2 100644 --- a/compiler/rustc_hir_analysis/src/impl_wf_check.rs +++ b/compiler/rustc_hir_analysis/src/impl_wf_check.rs @@ -12,11 +12,12 @@ use std::assert_matches::debug_assert_matches; use min_specialization::check_min_specialization; use rustc_data_structures::fx::FxHashSet; +use rustc_errors::Applicability; use rustc_errors::codes::*; use rustc_hir::def::DefKind; use rustc_hir::def_id::LocalDefId; use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt}; -use rustc_span::ErrorGuaranteed; +use rustc_span::{ErrorGuaranteed, kw}; use crate::constrained_generic_params as cgp; use crate::errors::UnconstrainedGenericParameter; @@ -158,6 +159,27 @@ pub(crate) fn enforce_impl_lifetime_params_are_constrained( const_param_note2: false, }); diag.code(E0207); + for p in &impl_generics.own_params { + if p.name == kw::UnderscoreLifetime { + let span = tcx.def_span(p.def_id); + let Ok(snippet) = tcx.sess.source_map().span_to_snippet(span) else { + continue; + }; + + let (span, sugg) = if &snippet == "'_" { + (span, param.name.to_string()) + } else { + (span.shrink_to_hi(), format!("{} ", param.name)) + }; + diag.span_suggestion_verbose( + span, + "consider using the named lifetime here instead of an implict \ + lifetime", + sugg, + Applicability::MaybeIncorrect, + ); + } + } res = Err(diag.emit()); } } diff --git a/compiler/rustc_resolve/messages.ftl b/compiler/rustc_resolve/messages.ftl index 9f062b3935fd9..ddcf3e519b0b2 100644 --- a/compiler/rustc_resolve/messages.ftl +++ b/compiler/rustc_resolve/messages.ftl @@ -11,9 +11,9 @@ resolve_added_macro_use = resolve_ancestor_only = visibilities can only be restricted to ancestor modules -resolve_anonymous_lifetime_non_gat_report_error = - in the trait associated type is declared without lifetime parameters, so using a borrowed type for them requires that lifetime to come from the implemented type +resolve_anonymous_lifetime_non_gat_report_error = missing lifetime in associated type .label = this lifetime must come from the implemented type + .note = in the trait the associated type is declared without lifetime parameters, so using a borrowed type for them requires that lifetime to come from the implemented type resolve_arguments_macro_use_not_allowed = arguments to `macro_use` are not allowed here diff --git a/compiler/rustc_resolve/src/errors.rs b/compiler/rustc_resolve/src/errors.rs index b5d3e5ea77672..7c71b446e693c 100644 --- a/compiler/rustc_resolve/src/errors.rs +++ b/compiler/rustc_resolve/src/errors.rs @@ -901,6 +901,8 @@ pub(crate) struct AnonymousLivetimeNonGatReportError { #[primary_span] #[label] pub(crate) lifetime: Span, + #[note] + pub(crate) decl: MultiSpan, } #[derive(Subdiagnostic)] diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index 92f5fba1f9b9a..c44b5eaa2bb48 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -11,6 +11,7 @@ use std::borrow::Cow; use std::collections::BTreeSet; use std::collections::hash_map::Entry; use std::mem::{replace, swap, take}; +use std::ops::ControlFlow; use rustc_ast::ptr::P; use rustc_ast::visit::{ @@ -20,20 +21,21 @@ use rustc_ast::*; use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap}; use rustc_errors::codes::*; use rustc_errors::{ - Applicability, DiagArgValue, ErrorGuaranteed, IntoDiagArg, StashKey, Suggestions, + Applicability, Diag, DiagArgValue, ErrorGuaranteed, IntoDiagArg, MultiSpan, StashKey, + Suggestions, pluralize, }; use rustc_hir::def::Namespace::{self, *}; use rustc_hir::def::{self, CtorKind, DefKind, LifetimeRes, NonMacroAttrKind, PartialRes, PerNS}; use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LOCAL_CRATE, LocalDefId}; use rustc_hir::{MissingLifetimeKind, PrimTy, TraitCandidate}; use rustc_middle::middle::resolve_bound_vars::Set1; -use rustc_middle::ty::DelegationFnSig; +use rustc_middle::ty::{AssocKind, DelegationFnSig}; use rustc_middle::{bug, span_bug}; use rustc_session::config::{CrateType, ResolveDocLinks}; use rustc_session::lint::{self, BuiltinLintDiag}; use rustc_session::parse::feature_err; use rustc_span::source_map::{Spanned, respan}; -use rustc_span::{BytePos, Ident, Span, Symbol, SyntaxContext, kw, sym}; +use rustc_span::{BytePos, DUMMY_SP, Ident, Span, Symbol, SyntaxContext, kw, sym}; use smallvec::{SmallVec, smallvec}; use tracing::{debug, instrument, trace}; @@ -359,6 +361,7 @@ enum LifetimeBinderKind { Function, Closure, ImplBlock, + ImplAssocType, } impl LifetimeBinderKind { @@ -369,6 +372,7 @@ impl LifetimeBinderKind { PolyTrait => "bound", WhereBound => "bound", Item | ConstItem => "item", + ImplAssocType => "associated type", ImplBlock => "impl block", Function => "function", Closure => "closure", @@ -684,6 +688,9 @@ struct DiagMetadata<'ast> { /// The current impl items (used to suggest). current_impl_items: Option<&'ast [P]>, + /// The current impl items (used to suggest). + current_impl_item: Option<&'ast AssocItem>, + /// When processing impl trait currently_processing_impl_trait: Option<(TraitRef, Ty)>, @@ -1870,9 +1877,34 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { ty: ty.span, }); } else { - self.r.dcx().emit_err(errors::AnonymousLivetimeNonGatReportError { - lifetime: lifetime.ident.span, - }); + let decl = if !trait_id.is_local() + && let Some(assoc) = self.diag_metadata.current_impl_item + && let AssocItemKind::Type(_) = assoc.kind + && let assocs = self.r.tcx.associated_items(trait_id) + && let Some(assoc) = assocs.find_by_name_and_kind( + self.r.tcx, + assoc.ident, + AssocKind::Type, + trait_id, + ) { + let mut decl: MultiSpan = + self.r.tcx.def_span(assoc.def_id).into(); + decl.push_span_label( + self.r.tcx.def_span(trait_id), + String::new(), + ); + decl + } else { + DUMMY_SP.into() + }; + let mut err = self.r.dcx().create_err( + errors::AnonymousLivetimeNonGatReportError { + lifetime: lifetime.ident.span, + decl, + }, + ); + self.point_at_impl_lifetimes(&mut err, i, lifetime.ident.span); + err.emit(); } } else { self.r.dcx().emit_err(errors::ElidedAnonymousLivetimeReportError { @@ -1909,6 +1941,99 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { self.report_missing_lifetime_specifiers(vec![missing_lifetime], None); } + fn point_at_impl_lifetimes(&mut self, err: &mut Diag<'_>, i: usize, lifetime: Span) { + let Some((rib, span)) = self.lifetime_ribs[..i] + .iter() + .rev() + .skip(1) + .filter_map(|rib| match rib.kind { + LifetimeRibKind::Generics { span, kind: LifetimeBinderKind::ImplBlock, .. } => { + Some((rib, span)) + } + _ => None, + }) + .next() + else { + return; + }; + if !rib.bindings.is_empty() { + err.span_label( + span, + format!( + "there {} named lifetime{} specified on the impl block you could use", + if rib.bindings.len() == 1 { "is a" } else { "are" }, + pluralize!(rib.bindings.len()), + ), + ); + if rib.bindings.len() == 1 { + err.span_suggestion_verbose( + lifetime.shrink_to_hi(), + "consider using the lifetime from the impl block", + format!("{} ", rib.bindings.keys().next().unwrap()), + Applicability::MaybeIncorrect, + ); + } + } else { + struct AnonRefFinder; + impl<'ast> Visitor<'ast> for AnonRefFinder { + type Result = ControlFlow; + + fn visit_ty(&mut self, ty: &'ast ast::Ty) -> Self::Result { + if let ast::TyKind::Ref(None, mut_ty) = &ty.kind { + return ControlFlow::Break(mut_ty.ty.span.shrink_to_lo()); + } + visit::walk_ty(self, ty) + } + + fn visit_lifetime( + &mut self, + lt: &'ast ast::Lifetime, + _cx: visit::LifetimeCtxt, + ) -> Self::Result { + if lt.ident.name == kw::UnderscoreLifetime { + return ControlFlow::Break(lt.ident.span); + } + visit::walk_lifetime(self, lt) + } + } + + if let Some(ty) = &self.diag_metadata.current_self_type + && let ControlFlow::Break(sp) = AnonRefFinder.visit_ty(ty) + { + err.multipart_suggestion_verbose( + "add a lifetime to the impl block and use it in the self type and associated \ + type", + vec![ + (span, "<'a>".to_string()), + (sp, "'a ".to_string()), + (lifetime.shrink_to_hi(), "'a ".to_string()), + ], + Applicability::MaybeIncorrect, + ); + } else if let Some(item) = &self.diag_metadata.current_item + && let ItemKind::Impl(impl_) = &item.kind + && let Some(of_trait) = &impl_.of_trait + && let ControlFlow::Break(sp) = AnonRefFinder.visit_trait_ref(of_trait) + { + err.multipart_suggestion_verbose( + "add a lifetime to the impl block and use it in the trait and associated type", + vec![ + (span, "<'a>".to_string()), + (sp, "'a".to_string()), + (lifetime.shrink_to_hi(), "'a ".to_string()), + ], + Applicability::MaybeIncorrect, + ); + } else { + err.span_label( + span, + "you could add a lifetime on the impl block, if the trait or the self type \ + could have one", + ); + } + } + } + #[instrument(level = "debug", skip(self))] fn resolve_elided_lifetime(&mut self, anchor_id: NodeId, span: Span) { let id = self.r.next_node_id(); @@ -3265,6 +3390,8 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { ) { use crate::ResolutionError::*; self.resolve_doc_links(&item.attrs, MaybeExported::ImplItem(trait_id.ok_or(&item.vis))); + let prev = self.diag_metadata.current_impl_item.take(); + self.diag_metadata.current_impl_item = Some(&item); match &item.kind { AssocItemKind::Const(box ast::ConstItem { generics, ty, expr, .. }) => { debug!("resolve_implementation AssocItemKind::Const"); @@ -3349,7 +3476,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { LifetimeRibKind::Generics { binder: item.id, span: generics.span, - kind: LifetimeBinderKind::Item, + kind: LifetimeBinderKind::ImplAssocType, }, |this| { this.with_lifetime_rib(LifetimeRibKind::AnonymousReportError, |this| { @@ -3400,6 +3527,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { panic!("unexpanded macro in resolve!") } } + self.diag_metadata.current_impl_item = prev; } fn check_trait_item( diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index 84858cfc1b1e4..2f2b3b08e4fc1 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -2933,15 +2933,10 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> { &mut err, Some(lifetime_ref.ident.name.as_str()), |err, _, span, message, suggestion, span_suggs| { - err.multipart_suggestion_with_style( + err.multipart_suggestion_verbose( message, std::iter::once((span, suggestion)).chain(span_suggs.clone()).collect(), Applicability::MaybeIncorrect, - if span_suggs.is_empty() { - SuggestionStyle::ShowCode - } else { - SuggestionStyle::ShowAlways - }, ); true }, @@ -2976,6 +2971,9 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> { { continue; } + if let LifetimeBinderKind::ImplAssocType = kind { + continue; + } if !span.can_be_used_for_suggestions() && suggest_note diff --git a/tests/ui/associated-inherent-types/issue-109299.stderr b/tests/ui/associated-inherent-types/issue-109299.stderr index 1e11c0e8c2af1..f29d3cc7834e7 100644 --- a/tests/ui/associated-inherent-types/issue-109299.stderr +++ b/tests/ui/associated-inherent-types/issue-109299.stderr @@ -2,9 +2,12 @@ error[E0261]: use of undeclared lifetime name `'d` --> $DIR/issue-109299.rs:6:12 | LL | impl Lexer<'d> { - | - ^^ undeclared lifetime - | | - | help: consider introducing lifetime `'d` here: `<'d>` + | ^^ undeclared lifetime + | +help: consider introducing lifetime `'d` here + | +LL | impl<'d> Lexer<'d> { + | ++++ error: aborting due to 1 previous error diff --git a/tests/ui/borrowck/generic_const_early_param.stderr b/tests/ui/borrowck/generic_const_early_param.stderr index 3f56d6a332515..6447f92aba853 100644 --- a/tests/ui/borrowck/generic_const_early_param.stderr +++ b/tests/ui/borrowck/generic_const_early_param.stderr @@ -7,19 +7,24 @@ LL | struct DataWrapper<'static> { error[E0261]: use of undeclared lifetime name `'a` --> $DIR/generic_const_early_param.rs:6:12 | -LL | struct DataWrapper<'static> { - | - help: consider introducing lifetime `'a` here: `'a,` -LL | LL | data: &'a [u8; Self::SIZE], | ^^ undeclared lifetime + | +help: consider introducing lifetime `'a` here + | +LL | struct DataWrapper<'a, 'static> { + | +++ error[E0261]: use of undeclared lifetime name `'a` --> $DIR/generic_const_early_param.rs:10:18 | LL | impl DataWrapper<'a> { - | - ^^ undeclared lifetime - | | - | help: consider introducing lifetime `'a` here: `<'a>` + | ^^ undeclared lifetime + | +help: consider introducing lifetime `'a` here + | +LL | impl<'a> DataWrapper<'a> { + | ++++ warning: the feature `generic_const_exprs` is incomplete and may not be safe to use and/or cause compiler crashes --> $DIR/generic_const_early_param.rs:1:12 diff --git a/tests/ui/cast/ice-cast-type-with-error-124848.stderr b/tests/ui/cast/ice-cast-type-with-error-124848.stderr index 0b2ab1dfc4c17..316a484d97154 100644 --- a/tests/ui/cast/ice-cast-type-with-error-124848.stderr +++ b/tests/ui/cast/ice-cast-type-with-error-124848.stderr @@ -2,27 +2,34 @@ error[E0261]: use of undeclared lifetime name `'unpinned` --> $DIR/ice-cast-type-with-error-124848.rs:7:32 | LL | struct MyType<'a>(Cell>>, Pin); - | - ^^^^^^^^^ undeclared lifetime - | | - | help: consider introducing lifetime `'unpinned` here: `'unpinned,` + | ^^^^^^^^^ undeclared lifetime + | +help: consider introducing lifetime `'unpinned` here + | +LL | struct MyType<'unpinned, 'a>(Cell>>, Pin); + | ++++++++++ error[E0261]: use of undeclared lifetime name `'a` --> $DIR/ice-cast-type-with-error-124848.rs:14:53 | -LL | fn main() { - | - help: consider introducing lifetime `'a` here: `<'a>` -... LL | let bad_addr = &unpinned as *const Cell>> as usize; | ^^ undeclared lifetime + | +help: consider introducing lifetime `'a` here + | +LL | fn main<'a>() { + | ++++ error[E0261]: use of undeclared lifetime name `'a` --> $DIR/ice-cast-type-with-error-124848.rs:14:67 | -LL | fn main() { - | - help: consider introducing lifetime `'a` here: `<'a>` -... LL | let bad_addr = &unpinned as *const Cell>> as usize; | ^^ undeclared lifetime + | +help: consider introducing lifetime `'a` here + | +LL | fn main<'a>() { + | ++++ error[E0412]: cannot find type `Pin` in this scope --> $DIR/ice-cast-type-with-error-124848.rs:7:60 diff --git a/tests/ui/const-generics/generic_const_exprs/unresolved_lifetimes_error.stderr b/tests/ui/const-generics/generic_const_exprs/unresolved_lifetimes_error.stderr index 67eed46eaddc3..ae074373da275 100644 --- a/tests/ui/const-generics/generic_const_exprs/unresolved_lifetimes_error.stderr +++ b/tests/ui/const-generics/generic_const_exprs/unresolved_lifetimes_error.stderr @@ -1,10 +1,13 @@ error[E0261]: use of undeclared lifetime name `'a` --> $DIR/unresolved_lifetimes_error.rs:5:13 | -LL | fn foo() -> [(); { - | - help: consider introducing lifetime `'a` here: `<'a>` LL | let a: &'a (); | ^^ undeclared lifetime + | +help: consider introducing lifetime `'a` here + | +LL | fn foo<'a>() -> [(); { + | ++++ error: aborting due to 1 previous error diff --git a/tests/ui/const-generics/ice-unexpected-inference-var-122549.stderr b/tests/ui/const-generics/ice-unexpected-inference-var-122549.stderr index afad3388145c7..3b24808cd1622 100644 --- a/tests/ui/const-generics/ice-unexpected-inference-var-122549.stderr +++ b/tests/ui/const-generics/ice-unexpected-inference-var-122549.stderr @@ -17,9 +17,12 @@ error[E0261]: use of undeclared lifetime name `'a` --> $DIR/ice-unexpected-inference-var-122549.rs:11:34 | LL | struct ConstChunksExact<'rem, T: 'a, const N: usize> {} - | - ^^ undeclared lifetime - | | - | help: consider introducing lifetime `'a` here: `'a,` + | ^^ undeclared lifetime + | +help: consider introducing lifetime `'a` here + | +LL | struct ConstChunksExact<'a, 'rem, T: 'a, const N: usize> {} + | +++ error[E0046]: not all trait items implemented, missing: `const_chunks_exact` --> $DIR/ice-unexpected-inference-var-122549.rs:9:1 diff --git a/tests/ui/const_prop/ice-type-mismatch-when-copying-112824.stderr b/tests/ui/const_prop/ice-type-mismatch-when-copying-112824.stderr index d95a8861230e0..586c96011e438 100644 --- a/tests/ui/const_prop/ice-type-mismatch-when-copying-112824.stderr +++ b/tests/ui/const_prop/ice-type-mismatch-when-copying-112824.stderr @@ -2,9 +2,12 @@ error[E0261]: use of undeclared lifetime name `'a` --> $DIR/ice-type-mismatch-when-copying-112824.rs:5:21 | LL | pub struct Opcode2(&'a S); - | - ^^ undeclared lifetime - | | - | help: consider introducing lifetime `'a` here: `<'a>` + | ^^ undeclared lifetime + | +help: consider introducing lifetime `'a` here + | +LL | pub struct Opcode2<'a>(&'a S); + | ++++ error[E0412]: cannot find type `S` in this scope --> $DIR/ice-type-mismatch-when-copying-112824.rs:5:24 diff --git a/tests/ui/dyn-compatibility/reference-to-bare-trait-in-fn-inputs-and-outputs-issue-125139.stderr b/tests/ui/dyn-compatibility/reference-to-bare-trait-in-fn-inputs-and-outputs-issue-125139.stderr index 832e7ef4dc38c..c3208b4db3863 100644 --- a/tests/ui/dyn-compatibility/reference-to-bare-trait-in-fn-inputs-and-outputs-issue-125139.stderr +++ b/tests/ui/dyn-compatibility/reference-to-bare-trait-in-fn-inputs-and-outputs-issue-125139.stderr @@ -139,9 +139,12 @@ error[E0261]: use of undeclared lifetime name `'a` --> $DIR/reference-to-bare-trait-in-fn-inputs-and-outputs-issue-125139.rs:96:12 | LL | fn bar(_: &'a Trait) {} - | - ^^ undeclared lifetime - | | - | help: consider introducing lifetime `'a` here: `<'a>` + | ^^ undeclared lifetime + | +help: consider introducing lifetime `'a` here + | +LL | fn bar<'a>(_: &'a Trait) {} + | ++++ error[E0106]: missing lifetime specifier --> $DIR/reference-to-bare-trait-in-fn-inputs-and-outputs-issue-125139.rs:110:13 @@ -171,9 +174,12 @@ error[E0261]: use of undeclared lifetime name `'a` --> $DIR/reference-to-bare-trait-in-fn-inputs-and-outputs-issue-125139.rs:122:17 | LL | fn kitten() -> &'a Trait { - | - ^^ undeclared lifetime - | | - | help: consider introducing lifetime `'a` here: `<'a>` + | ^^ undeclared lifetime + | +help: consider introducing lifetime `'a` here + | +LL | fn kitten<'a>() -> &'a Trait { + | ++++ error[E0106]: missing lifetime specifier --> $DIR/reference-to-bare-trait-in-fn-inputs-and-outputs-issue-125139.rs:133:16 diff --git a/tests/ui/error-codes/E0261.stderr b/tests/ui/error-codes/E0261.stderr index 0eab2dc0ee05f..9ca26dc8459d7 100644 --- a/tests/ui/error-codes/E0261.stderr +++ b/tests/ui/error-codes/E0261.stderr @@ -2,17 +2,23 @@ error[E0261]: use of undeclared lifetime name `'a` --> $DIR/E0261.rs:1:12 | LL | fn foo(x: &'a str) { } - | - ^^ undeclared lifetime - | | - | help: consider introducing lifetime `'a` here: `<'a>` + | ^^ undeclared lifetime + | +help: consider introducing lifetime `'a` here + | +LL | fn foo<'a>(x: &'a str) { } + | ++++ error[E0261]: use of undeclared lifetime name `'a` --> $DIR/E0261.rs:5:9 | -LL | struct Foo { - | - help: consider introducing lifetime `'a` here: `<'a>` LL | x: &'a str, | ^^ undeclared lifetime + | +help: consider introducing lifetime `'a` here + | +LL | struct Foo<'a> { + | ++++ error: aborting due to 2 previous errors diff --git a/tests/ui/generics/generic-extern-lifetime.stderr b/tests/ui/generics/generic-extern-lifetime.stderr index 33332e760f582..6f9b496f1cd7b 100644 --- a/tests/ui/generics/generic-extern-lifetime.stderr +++ b/tests/ui/generics/generic-extern-lifetime.stderr @@ -2,9 +2,12 @@ error[E0261]: use of undeclared lifetime name `'a` --> $DIR/generic-extern-lifetime.rs:6:26 | LL | pub fn life2<'b>(x: &'a i32, y: &'b i32); - | - ^^ undeclared lifetime - | | - | help: consider introducing lifetime `'a` here: `'a,` + | ^^ undeclared lifetime + | +help: consider introducing lifetime `'a` here + | +LL | pub fn life2<'a, 'b>(x: &'a i32, y: &'b i32); + | +++ error[E0261]: use of undeclared lifetime name `'a` --> $DIR/generic-extern-lifetime.rs:8:37 diff --git a/tests/ui/generics/impl-block-params-declared-in-wrong-spot-issue-113073.stderr b/tests/ui/generics/impl-block-params-declared-in-wrong-spot-issue-113073.stderr index c60c4c72a2133..33d0c9c970795 100644 --- a/tests/ui/generics/impl-block-params-declared-in-wrong-spot-issue-113073.stderr +++ b/tests/ui/generics/impl-block-params-declared-in-wrong-spot-issue-113073.stderr @@ -2,9 +2,12 @@ error[E0261]: use of undeclared lifetime name `'a` --> $DIR/impl-block-params-declared-in-wrong-spot-issue-113073.rs:7:13 | LL | impl Foo for u8 {} - | - ^^ undeclared lifetime - | | - | help: consider introducing lifetime `'a` here: `<'a>` + | ^^ undeclared lifetime + | +help: consider introducing lifetime `'a` here + | +LL | impl<'a> Foo for u8 {} + | ++++ error[E0229]: associated item constraints are not allowed here --> $DIR/impl-block-params-declared-in-wrong-spot-issue-113073.rs:3:10 diff --git a/tests/ui/impl-header-lifetime-elision/assoc-type.rs b/tests/ui/impl-header-lifetime-elision/assoc-type.rs index db3c416540fcd..f03a110d7dc1d 100644 --- a/tests/ui/impl-header-lifetime-elision/assoc-type.rs +++ b/tests/ui/impl-header-lifetime-elision/assoc-type.rs @@ -9,7 +9,7 @@ trait MyTrait { impl MyTrait for &i32 { type Output = &i32; - //~^ ERROR 11:19: 11:20: in the trait associated type is declared without lifetime parameters, so using a borrowed type for them requires that lifetime to come from the implemented type + //~^ ERROR missing lifetime in associated type } impl MyTrait for &u32 { @@ -17,6 +17,19 @@ impl MyTrait for &u32 { //~^ ERROR `'_` cannot be used here } +impl<'a> MyTrait for &f64 { + type Output = &f64; + //~^ ERROR missing lifetime in associated type +} + +trait OtherTrait<'a> { + type Output; +} +impl OtherTrait<'_> for f64 { + type Output = &f64; + //~^ ERROR missing lifetime in associated type +} + // This is what you have to do: impl<'a> MyTrait for &'a f32 { type Output = &'a f32; diff --git a/tests/ui/impl-header-lifetime-elision/assoc-type.stderr b/tests/ui/impl-header-lifetime-elision/assoc-type.stderr index e650eeca48ad9..201ea2d894eb7 100644 --- a/tests/ui/impl-header-lifetime-elision/assoc-type.stderr +++ b/tests/ui/impl-header-lifetime-elision/assoc-type.stderr @@ -1,8 +1,15 @@ -error: in the trait associated type is declared without lifetime parameters, so using a borrowed type for them requires that lifetime to come from the implemented type +error: missing lifetime in associated type --> $DIR/assoc-type.rs:11:19 | LL | type Output = &i32; | ^ this lifetime must come from the implemented type + | + = note: in the trait the associated type is declared without lifetime parameters, so using a borrowed type for them requires that lifetime to come from the implemented type +help: add a lifetime to the impl block and use it in the self type and associated type + | +LL ~ impl<'a> MyTrait for &'a i32 { +LL ~ type Output = &'a i32; + | error[E0637]: `'_` cannot be used here --> $DIR/assoc-type.rs:16:20 @@ -10,6 +17,33 @@ error[E0637]: `'_` cannot be used here LL | type Output = &'_ i32; | ^^ `'_` is a reserved lifetime name -error: aborting due to 2 previous errors +error: missing lifetime in associated type + --> $DIR/assoc-type.rs:21:19 + | +LL | impl<'a> MyTrait for &f64 { + | ---- there is a named lifetime specified on the impl block you could use +LL | type Output = &f64; + | ^ this lifetime must come from the implemented type + | + = note: in the trait the associated type is declared without lifetime parameters, so using a borrowed type for them requires that lifetime to come from the implemented type +help: consider using the lifetime from the impl block + | +LL | type Output = &'a f64; + | ++ + +error: missing lifetime in associated type + --> $DIR/assoc-type.rs:29:19 + | +LL | type Output = &f64; + | ^ this lifetime must come from the implemented type + | + = note: in the trait the associated type is declared without lifetime parameters, so using a borrowed type for them requires that lifetime to come from the implemented type +help: add a lifetime to the impl block and use it in the trait and associated type + | +LL ~ impl<'a> OtherTrait<'a> for f64 { +LL ~ type Output = &'a f64; + | + +error: aborting due to 4 previous errors For more information about this error, try `rustc --explain E0637`. diff --git a/tests/ui/impl-trait/precise-capturing/bad-lifetimes.stderr b/tests/ui/impl-trait/precise-capturing/bad-lifetimes.stderr index 98f629f52cf34..ddb09690faf42 100644 --- a/tests/ui/impl-trait/precise-capturing/bad-lifetimes.stderr +++ b/tests/ui/impl-trait/precise-capturing/bad-lifetimes.stderr @@ -15,9 +15,12 @@ error[E0261]: use of undeclared lifetime name `'missing` --> $DIR/bad-lifetimes.rs:7:37 | LL | fn missing_lt() -> impl Sized + use<'missing> {} - | - ^^^^^^^^ undeclared lifetime - | | - | help: consider introducing lifetime `'missing` here: `<'missing>` + | ^^^^^^^^ undeclared lifetime + | +help: consider introducing lifetime `'missing` here + | +LL | fn missing_lt<'missing>() -> impl Sized + use<'missing> {} + | ++++++++++ error: expected lifetime parameter in `use<...>` precise captures list, found `'static` --> $DIR/bad-lifetimes.rs:4:36 diff --git a/tests/ui/inference/ice-ifer-var-leaked-out-of-rollback-122098.stderr b/tests/ui/inference/ice-ifer-var-leaked-out-of-rollback-122098.stderr index ce01e24770d9c..c2ebaee244151 100644 --- a/tests/ui/inference/ice-ifer-var-leaked-out-of-rollback-122098.stderr +++ b/tests/ui/inference/ice-ifer-var-leaked-out-of-rollback-122098.stderr @@ -23,9 +23,12 @@ error[E0261]: use of undeclared lifetime name `'q` --> $DIR/ice-ifer-var-leaked-out-of-rollback-122098.rs:14:21 | LL | impl<'static> Query<'q> { - | - ^^ undeclared lifetime - | | - | help: consider introducing lifetime `'q` here: `'q,` + | ^^ undeclared lifetime + | +help: consider introducing lifetime `'q` here + | +LL | impl<'q, 'static> Query<'q> { + | +++ error[E0392]: lifetime parameter `'q` is never used --> $DIR/ice-ifer-var-leaked-out-of-rollback-122098.rs:11:14 diff --git a/tests/ui/inference/issue-107090.stderr b/tests/ui/inference/issue-107090.stderr index e509e262fb1bd..0deafdfb931be 100644 --- a/tests/ui/inference/issue-107090.stderr +++ b/tests/ui/inference/issue-107090.stderr @@ -33,15 +33,23 @@ error[E0261]: use of undeclared lifetime name `'b` --> $DIR/issue-107090.rs:11:47 | LL | impl<'long: 'short, 'short, T> Convert<'long, 'b> for Foo<'short, 'out, T> { - | - ^^ undeclared lifetime - | | - | help: consider introducing lifetime `'b` here: `'b,` + | ^^ undeclared lifetime + | +help: consider introducing lifetime `'b` here + | +LL | impl<'b, 'long: 'short, 'short, T> Convert<'long, 'b> for Foo<'short, 'out, T> { + | +++ error[E0261]: use of undeclared lifetime name `'out` --> $DIR/issue-107090.rs:11:67 | LL | impl<'long: 'short, 'short, T> Convert<'long, 'b> for Foo<'short, 'out, T> { - | - help: consider introducing lifetime `'out` here: `'out,` ^^^^ undeclared lifetime + | ^^^^ undeclared lifetime + | +help: consider introducing lifetime `'out` here + | +LL | impl<'out, 'long: 'short, 'short, T> Convert<'long, 'b> for Foo<'short, 'out, T> { + | +++++ error[E0261]: use of undeclared lifetime name `'out` --> $DIR/issue-107090.rs:14:49 @@ -62,9 +70,12 @@ error[E0261]: use of undeclared lifetime name `'short` --> $DIR/issue-107090.rs:20:68 | LL | fn badboi<'in_, 'out, T>(x: Foo<'in_, 'out, T>, sadness: &'in_ Foo<'short, 'out, T>) -> &'out T { - | - ^^^^^^ undeclared lifetime - | | - | help: consider introducing lifetime `'short` here: `'short,` + | ^^^^^^ undeclared lifetime + | +help: consider introducing lifetime `'short` here + | +LL | fn badboi<'short, 'in_, 'out, T>(x: Foo<'in_, 'out, T>, sadness: &'in_ Foo<'short, 'out, T>) -> &'out T { + | +++++++ error: aborting due to 6 previous errors diff --git a/tests/ui/lifetimes/issue-107988.stderr b/tests/ui/lifetimes/issue-107988.stderr index c2d8c7050e97f..7d93c1d20246d 100644 --- a/tests/ui/lifetimes/issue-107988.stderr +++ b/tests/ui/lifetimes/issue-107988.stderr @@ -2,9 +2,12 @@ error[E0261]: use of undeclared lifetime name `'tcx` --> $DIR/issue-107988.rs:7:52 | LL | impl> TraitEngineExt<'tcx> for T { - | - ^^^^ undeclared lifetime - | | - | help: consider introducing lifetime `'tcx` here: `'tcx,` + | ^^^^ undeclared lifetime + | +help: consider introducing lifetime `'tcx` here + | +LL | impl<'tcx, T: ?Sized + TraitEngine<'tcx>> TraitEngineExt<'tcx> for T { + | +++++ error[E0261]: use of undeclared lifetime name `'tcx` --> $DIR/issue-107988.rs:7:30 diff --git a/tests/ui/lifetimes/missing-lifetime-in-assoc-type-1.rs b/tests/ui/lifetimes/missing-lifetime-in-assoc-type-1.rs new file mode 100644 index 0000000000000..0f220a0b62a16 --- /dev/null +++ b/tests/ui/lifetimes/missing-lifetime-in-assoc-type-1.rs @@ -0,0 +1,20 @@ +struct S; +struct T; + +impl<'a> IntoIterator for &S { + //~^ ERROR E0207 + //~| NOTE there is a named lifetime specified on the impl block you could use + //~| NOTE unconstrained lifetime parameter + //~| HELP consider using the named lifetime here instead of an implict lifetime + type Item = &T; + //~^ ERROR missing lifetime in associated type + //~| HELP consider using the lifetime from the impl block + //~| NOTE this lifetime must come from the implemented type + //~| NOTE in the trait the associated type is declared without lifetime parameters + type IntoIter = std::collections::btree_map::Values<'a, i32, T>; + + fn into_iter(self) -> Self::IntoIter { + todo!() + } +} +fn main() {} diff --git a/tests/ui/lifetimes/missing-lifetime-in-assoc-type-1.stderr b/tests/ui/lifetimes/missing-lifetime-in-assoc-type-1.stderr new file mode 100644 index 0000000000000..b4305098b01f7 --- /dev/null +++ b/tests/ui/lifetimes/missing-lifetime-in-assoc-type-1.stderr @@ -0,0 +1,30 @@ +error: missing lifetime in associated type + --> $DIR/missing-lifetime-in-assoc-type-1.rs:9:17 + | +LL | impl<'a> IntoIterator for &S { + | ---- there is a named lifetime specified on the impl block you could use +... +LL | type Item = &T; + | ^ this lifetime must come from the implemented type + | +note: in the trait the associated type is declared without lifetime parameters, so using a borrowed type for them requires that lifetime to come from the implemented type + --> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL +help: consider using the lifetime from the impl block + | +LL | type Item = &'a T; + | ++ + +error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates + --> $DIR/missing-lifetime-in-assoc-type-1.rs:4:6 + | +LL | impl<'a> IntoIterator for &S { + | ^^ unconstrained lifetime parameter + | +help: consider using the named lifetime here instead of an implict lifetime + | +LL | impl<'a> IntoIterator for &'a S { + | ++ + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0207`. diff --git a/tests/ui/lifetimes/missing-lifetime-in-assoc-type-2.rs b/tests/ui/lifetimes/missing-lifetime-in-assoc-type-2.rs new file mode 100644 index 0000000000000..d24aaaf8b10ed --- /dev/null +++ b/tests/ui/lifetimes/missing-lifetime-in-assoc-type-2.rs @@ -0,0 +1,14 @@ +struct S; +struct T; + +impl IntoIterator for &S { + type Item = &T; + //~^ ERROR missing lifetime in associated type + type IntoIter = std::collections::btree_map::Values<'a, i32, T>; + //~^ ERROR use of undeclared lifetime name `'a` + + fn into_iter(self) -> Self::IntoIter { + todo!() + } +} +fn main() {} diff --git a/tests/ui/lifetimes/missing-lifetime-in-assoc-type-2.stderr b/tests/ui/lifetimes/missing-lifetime-in-assoc-type-2.stderr new file mode 100644 index 0000000000000..9d9d2bc97d901 --- /dev/null +++ b/tests/ui/lifetimes/missing-lifetime-in-assoc-type-2.stderr @@ -0,0 +1,28 @@ +error: missing lifetime in associated type + --> $DIR/missing-lifetime-in-assoc-type-2.rs:5:17 + | +LL | type Item = &T; + | ^ this lifetime must come from the implemented type + | +note: in the trait the associated type is declared without lifetime parameters, so using a borrowed type for them requires that lifetime to come from the implemented type + --> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL +help: add a lifetime to the impl block and use it in the self type and associated type + | +LL ~ impl<'a> IntoIterator for &'a S { +LL ~ type Item = &'a T; + | + +error[E0261]: use of undeclared lifetime name `'a` + --> $DIR/missing-lifetime-in-assoc-type-2.rs:7:57 + | +LL | type IntoIter = std::collections::btree_map::Values<'a, i32, T>; + | ^^ undeclared lifetime + | +help: consider introducing lifetime `'a` here + | +LL | impl<'a> IntoIterator for &S { + | ++++ + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0261`. diff --git a/tests/ui/lifetimes/missing-lifetime-in-assoc-type-3.rs b/tests/ui/lifetimes/missing-lifetime-in-assoc-type-3.rs new file mode 100644 index 0000000000000..cf745ab97eb3a --- /dev/null +++ b/tests/ui/lifetimes/missing-lifetime-in-assoc-type-3.rs @@ -0,0 +1,14 @@ +struct S; +struct T; + +impl IntoIterator for &S { + type Item = &T; + //~^ ERROR missing lifetime in associated type + type IntoIter = std::collections::btree_map::Values; + //~^ ERROR missing lifetime specifier + + fn into_iter(self) -> Self::IntoIter { + todo!() + } +} +fn main() {} diff --git a/tests/ui/lifetimes/missing-lifetime-in-assoc-type-3.stderr b/tests/ui/lifetimes/missing-lifetime-in-assoc-type-3.stderr new file mode 100644 index 0000000000000..b5811dc8ff279 --- /dev/null +++ b/tests/ui/lifetimes/missing-lifetime-in-assoc-type-3.stderr @@ -0,0 +1,31 @@ +error: missing lifetime in associated type + --> $DIR/missing-lifetime-in-assoc-type-3.rs:5:17 + | +LL | type Item = &T; + | ^ this lifetime must come from the implemented type + | +note: in the trait the associated type is declared without lifetime parameters, so using a borrowed type for them requires that lifetime to come from the implemented type + --> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL +help: add a lifetime to the impl block and use it in the self type and associated type + | +LL ~ impl<'a> IntoIterator for &'a S { +LL ~ type Item = &'a T; + | + +error[E0106]: missing lifetime specifier + --> $DIR/missing-lifetime-in-assoc-type-3.rs:7:56 + | +LL | type IntoIter = std::collections::btree_map::Values; + | ^ expected named lifetime parameter + | +help: consider introducing a named lifetime parameter + | +LL ~ impl<'a> IntoIterator for &S { +LL | type Item = &T; +LL | +LL ~ type IntoIter = std::collections::btree_map::Values<'a, i32, T>; + | + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0106`. diff --git a/tests/ui/lifetimes/missing-lifetime-in-assoc-type-4.rs b/tests/ui/lifetimes/missing-lifetime-in-assoc-type-4.rs new file mode 100644 index 0000000000000..f63f354e002e5 --- /dev/null +++ b/tests/ui/lifetimes/missing-lifetime-in-assoc-type-4.rs @@ -0,0 +1,14 @@ +struct S; +struct T; + +impl IntoIterator for &S { + type Item = &T; + //~^ ERROR missing lifetime in associated type + type IntoIter<'a> = std::collections::btree_map::Values<'a, i32, T>; + //~^ ERROR lifetime parameters or bounds on type `IntoIter` do not match the trait declaration + + fn into_iter(self) -> Self::IntoIter { + todo!() + } +} +fn main() {} diff --git a/tests/ui/lifetimes/missing-lifetime-in-assoc-type-4.stderr b/tests/ui/lifetimes/missing-lifetime-in-assoc-type-4.stderr new file mode 100644 index 0000000000000..97afc454057b3 --- /dev/null +++ b/tests/ui/lifetimes/missing-lifetime-in-assoc-type-4.stderr @@ -0,0 +1,26 @@ +error: missing lifetime in associated type + --> $DIR/missing-lifetime-in-assoc-type-4.rs:5:17 + | +LL | type Item = &T; + | ^ this lifetime must come from the implemented type + | +note: in the trait the associated type is declared without lifetime parameters, so using a borrowed type for them requires that lifetime to come from the implemented type + --> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL +help: add a lifetime to the impl block and use it in the self type and associated type + | +LL ~ impl<'a> IntoIterator for &'a S { +LL ~ type Item = &'a T; + | + +error[E0195]: lifetime parameters or bounds on type `IntoIter` do not match the trait declaration + --> $DIR/missing-lifetime-in-assoc-type-4.rs:7:18 + | +LL | type IntoIter<'a> = std::collections::btree_map::Values<'a, i32, T>; + | ^^^^ lifetimes do not match type in trait + --> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL + | + = note: lifetimes in impl do not match this type in trait + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0195`. diff --git a/tests/ui/lifetimes/missing-lifetime-in-assoc-type-5.rs b/tests/ui/lifetimes/missing-lifetime-in-assoc-type-5.rs new file mode 100644 index 0000000000000..9e629a48de7b4 --- /dev/null +++ b/tests/ui/lifetimes/missing-lifetime-in-assoc-type-5.rs @@ -0,0 +1,20 @@ +struct S; +struct T; + +impl<'a> IntoIterator for &'_ S { + //~^ ERROR E0207 + //~| NOTE there is a named lifetime specified on the impl block you could use + //~| NOTE unconstrained lifetime parameter + //~| HELP consider using the named lifetime here instead of an implict lifetime + type Item = &T; + //~^ ERROR missing lifetime in associated type + //~| HELP consider using the lifetime from the impl block + //~| NOTE this lifetime must come from the implemented type + //~| NOTE in the trait the associated type is declared without lifetime parameters + type IntoIter = std::collections::btree_map::Values<'a, i32, T>; + + fn into_iter(self) -> Self::IntoIter { + todo!() + } +} +fn main() {} diff --git a/tests/ui/lifetimes/missing-lifetime-in-assoc-type-5.stderr b/tests/ui/lifetimes/missing-lifetime-in-assoc-type-5.stderr new file mode 100644 index 0000000000000..f845596834b8c --- /dev/null +++ b/tests/ui/lifetimes/missing-lifetime-in-assoc-type-5.stderr @@ -0,0 +1,31 @@ +error: missing lifetime in associated type + --> $DIR/missing-lifetime-in-assoc-type-5.rs:9:17 + | +LL | impl<'a> IntoIterator for &'_ S { + | ---- there is a named lifetime specified on the impl block you could use +... +LL | type Item = &T; + | ^ this lifetime must come from the implemented type + | +note: in the trait the associated type is declared without lifetime parameters, so using a borrowed type for them requires that lifetime to come from the implemented type + --> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL +help: consider using the lifetime from the impl block + | +LL | type Item = &'a T; + | ++ + +error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates + --> $DIR/missing-lifetime-in-assoc-type-5.rs:4:6 + | +LL | impl<'a> IntoIterator for &'_ S { + | ^^ unconstrained lifetime parameter + | +help: consider using the named lifetime here instead of an implict lifetime + | +LL - impl<'a> IntoIterator for &'_ S { +LL + impl<'a> IntoIterator for &'a S { + | + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0207`. diff --git a/tests/ui/lifetimes/missing-lifetime-in-assoc-type-6.rs b/tests/ui/lifetimes/missing-lifetime-in-assoc-type-6.rs new file mode 100644 index 0000000000000..036596c2aec62 --- /dev/null +++ b/tests/ui/lifetimes/missing-lifetime-in-assoc-type-6.rs @@ -0,0 +1,26 @@ +//~ NOTE in the trait the associated type is declared without lifetime parameters +struct S; +struct T; + +trait Trait { + type Item; + type IntoIter; + fn into_iter(self) -> Self::IntoIter; +} + +impl<'a> Trait for &'_ S { + //~^ ERROR E0207 + //~| NOTE there is a named lifetime specified on the impl block you could use + //~| NOTE unconstrained lifetime parameter + //~| HELP consider using the named lifetime here instead of an implict lifetime + type Item = &T; + //~^ ERROR missing lifetime in associated type + //~| HELP consider using the lifetime from the impl block + //~| NOTE this lifetime must come from the implemented type + type IntoIter = std::collections::btree_map::Values<'a, i32, T>; + + fn into_iter(self) -> Self::IntoIter { + todo!() + } +} +fn main() {} diff --git a/tests/ui/lifetimes/missing-lifetime-in-assoc-type-6.stderr b/tests/ui/lifetimes/missing-lifetime-in-assoc-type-6.stderr new file mode 100644 index 0000000000000..2e81d43674613 --- /dev/null +++ b/tests/ui/lifetimes/missing-lifetime-in-assoc-type-6.stderr @@ -0,0 +1,30 @@ +error: missing lifetime in associated type + --> $DIR/missing-lifetime-in-assoc-type-6.rs:16:17 + | +LL | impl<'a> Trait for &'_ S { + | ---- there is a named lifetime specified on the impl block you could use +... +LL | type Item = &T; + | ^ this lifetime must come from the implemented type + | + = note: in the trait the associated type is declared without lifetime parameters, so using a borrowed type for them requires that lifetime to come from the implemented type +help: consider using the lifetime from the impl block + | +LL | type Item = &'a T; + | ++ + +error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates + --> $DIR/missing-lifetime-in-assoc-type-6.rs:11:6 + | +LL | impl<'a> Trait for &'_ S { + | ^^ unconstrained lifetime parameter + | +help: consider using the named lifetime here instead of an implict lifetime + | +LL - impl<'a> Trait for &'_ S { +LL + impl<'a> Trait for &'a S { + | + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0207`. diff --git a/tests/ui/lifetimes/no_lending_iterators.rs b/tests/ui/lifetimes/no_lending_iterators.rs index 21395475fb3dc..4b9a31201247c 100644 --- a/tests/ui/lifetimes/no_lending_iterators.rs +++ b/tests/ui/lifetimes/no_lending_iterators.rs @@ -16,7 +16,7 @@ trait Bar { impl Bar for usize { type Item = &usize; - //~^ ERROR 18:17: 18:18: in the trait associated type is declared without lifetime parameters, so using a borrowed type for them requires that lifetime to come from the implemented type + //~^ ERROR missing lifetime in associated type fn poke(&mut self, item: Self::Item) { self += *item; @@ -25,7 +25,7 @@ impl Bar for usize { impl Bar for isize { type Item<'a> = &'a isize; - //~^ ERROR 27:14: 27:18: lifetime parameters or bounds on type `Item` do not match the trait declaration [E0195] + //~^ ERROR lifetime parameters or bounds on type `Item` do not match the trait declaration [E0195] fn poke(&mut self, item: Self::Item) { self += *item; diff --git a/tests/ui/lifetimes/no_lending_iterators.stderr b/tests/ui/lifetimes/no_lending_iterators.stderr index 9ceaef2f9b1aa..8c2a864d72e98 100644 --- a/tests/ui/lifetimes/no_lending_iterators.stderr +++ b/tests/ui/lifetimes/no_lending_iterators.stderr @@ -10,11 +10,15 @@ note: you can't create an `Iterator` that borrows each `Item` from itself, but y LL | impl Iterator for Data { | ^^^^ -error: in the trait associated type is declared without lifetime parameters, so using a borrowed type for them requires that lifetime to come from the implemented type +error: missing lifetime in associated type --> $DIR/no_lending_iterators.rs:18:17 | +LL | impl Bar for usize { + | - you could add a lifetime on the impl block, if the trait or the self type could have one LL | type Item = &usize; | ^ this lifetime must come from the implemented type + | + = note: in the trait the associated type is declared without lifetime parameters, so using a borrowed type for them requires that lifetime to come from the implemented type error[E0195]: lifetime parameters or bounds on type `Item` do not match the trait declaration --> $DIR/no_lending_iterators.rs:27:14 diff --git a/tests/ui/lifetimes/undeclared-lifetime-used-in-debug-macro-issue-70152.stderr b/tests/ui/lifetimes/undeclared-lifetime-used-in-debug-macro-issue-70152.stderr index 0d6ade41511fd..f90133e9fb1ab 100644 --- a/tests/ui/lifetimes/undeclared-lifetime-used-in-debug-macro-issue-70152.stderr +++ b/tests/ui/lifetimes/undeclared-lifetime-used-in-debug-macro-issue-70152.stderr @@ -1,10 +1,13 @@ error[E0261]: use of undeclared lifetime name `'b` --> $DIR/undeclared-lifetime-used-in-debug-macro-issue-70152.rs:3:9 | -LL | struct Test { - | - help: consider introducing lifetime `'b` here: `<'b>` LL | a: &'b str, | ^^ undeclared lifetime + | +help: consider introducing lifetime `'b` here + | +LL | struct Test<'b> { + | ++++ error[E0261]: use of undeclared lifetime name `'b` --> $DIR/undeclared-lifetime-used-in-debug-macro-issue-70152.rs:3:9 @@ -12,9 +15,13 @@ error[E0261]: use of undeclared lifetime name `'b` LL | #[derive(Eq, PartialEq)] | -- lifetime `'b` is missing in item created through this procedural macro LL | struct Test { - | - help: consider introducing lifetime `'b` here: `<'b>` LL | a: &'b str, | ^^ undeclared lifetime + | +help: consider introducing lifetime `'b` here + | +LL | struct Test<'b> { + | ++++ error[E0261]: use of undeclared lifetime name `'b` --> $DIR/undeclared-lifetime-used-in-debug-macro-issue-70152.rs:13:13 diff --git a/tests/ui/methods/dont-ice-on-object-lookup-w-error-region.stderr b/tests/ui/methods/dont-ice-on-object-lookup-w-error-region.stderr index 2c33941be4333..00267ce359a19 100644 --- a/tests/ui/methods/dont-ice-on-object-lookup-w-error-region.stderr +++ b/tests/ui/methods/dont-ice-on-object-lookup-w-error-region.stderr @@ -2,9 +2,12 @@ error[E0261]: use of undeclared lifetime name `'missing` --> $DIR/dont-ice-on-object-lookup-w-error-region.rs:6:20 | LL | fn project(x: Pin<&'missing mut dyn Future>) { - | - ^^^^^^^^ undeclared lifetime - | | - | help: consider introducing lifetime `'missing` here: `<'missing>` + | ^^^^^^^^ undeclared lifetime + | +help: consider introducing lifetime `'missing` here + | +LL | fn project<'missing>(x: Pin<&'missing mut dyn Future>) { + | ++++++++++ error: aborting due to 1 previous error diff --git a/tests/ui/methods/method-call-lifetime-args-unresolved.stderr b/tests/ui/methods/method-call-lifetime-args-unresolved.stderr index c72e7e0cdc37f..d3bd74a49fb3d 100644 --- a/tests/ui/methods/method-call-lifetime-args-unresolved.stderr +++ b/tests/ui/methods/method-call-lifetime-args-unresolved.stderr @@ -1,10 +1,13 @@ error[E0261]: use of undeclared lifetime name `'a` --> $DIR/method-call-lifetime-args-unresolved.rs:2:15 | -LL | fn main() { - | - help: consider introducing lifetime `'a` here: `<'a>` LL | 0.clone::<'a>(); | ^^ undeclared lifetime + | +help: consider introducing lifetime `'a` here + | +LL | fn main<'a>() { + | ++++ warning: cannot specify lifetime arguments explicitly if late bound lifetime parameters are present --> $DIR/method-call-lifetime-args-unresolved.rs:2:15 diff --git a/tests/ui/mismatched_types/issue-74918-missing-lifetime.stderr b/tests/ui/mismatched_types/issue-74918-missing-lifetime.stderr index 5020395eb6aea..dc21c2e3cf9bf 100644 --- a/tests/ui/mismatched_types/issue-74918-missing-lifetime.stderr +++ b/tests/ui/mismatched_types/issue-74918-missing-lifetime.stderr @@ -6,8 +6,9 @@ LL | type Item = IteratorChunk; | help: consider introducing a named lifetime parameter | -LL | type Item<'a> = IteratorChunk<'a, T, S>; - | ++++ +++ +LL ~ impl<'a, T, S: Iterator> Iterator for ChunkingIterator { +LL ~ type Item = IteratorChunk<'a, T, S>; + | error: aborting due to 1 previous error diff --git a/tests/ui/nll/user-annotations/region-error-ice-109072.stderr b/tests/ui/nll/user-annotations/region-error-ice-109072.stderr index d90971bed25ba..026f5b5f80a9b 100644 --- a/tests/ui/nll/user-annotations/region-error-ice-109072.stderr +++ b/tests/ui/nll/user-annotations/region-error-ice-109072.stderr @@ -2,9 +2,12 @@ error[E0261]: use of undeclared lifetime name `'missing` --> $DIR/region-error-ice-109072.rs:8:9 | LL | impl Lt<'missing> for () { - | - ^^^^^^^^ undeclared lifetime - | | - | help: consider introducing lifetime `'missing` here: `<'missing>` + | ^^^^^^^^ undeclared lifetime + | +help: consider introducing lifetime `'missing` here + | +LL | impl<'missing> Lt<'missing> for () { + | ++++++++++ error[E0261]: use of undeclared lifetime name `'missing` --> $DIR/region-error-ice-109072.rs:9:15 @@ -14,10 +17,6 @@ LL | type T = &'missing (); | help: consider introducing lifetime `'missing` here | -LL | type T<'missing> = &'missing (); - | ++++++++++ -help: consider introducing lifetime `'missing` here - | LL | impl<'missing> Lt<'missing> for () { | ++++++++++ diff --git a/tests/ui/regions/regions-in-enums.stderr b/tests/ui/regions/regions-in-enums.stderr index 66537653291c7..449763e8b5914 100644 --- a/tests/ui/regions/regions-in-enums.stderr +++ b/tests/ui/regions/regions-in-enums.stderr @@ -1,18 +1,24 @@ error[E0261]: use of undeclared lifetime name `'foo` --> $DIR/regions-in-enums.rs:13:9 | -LL | enum No0 { - | - help: consider introducing lifetime `'foo` here: `<'foo>` LL | X5(&'foo usize) | ^^^^ undeclared lifetime + | +help: consider introducing lifetime `'foo` here + | +LL | enum No0<'foo> { + | ++++++ error[E0261]: use of undeclared lifetime name `'a` --> $DIR/regions-in-enums.rs:17:9 | -LL | enum No1 { - | - help: consider introducing lifetime `'a` here: `<'a>` LL | X6(&'a usize) | ^^ undeclared lifetime + | +help: consider introducing lifetime `'a` here + | +LL | enum No1<'a> { + | ++++ error: aborting due to 2 previous errors diff --git a/tests/ui/regions/regions-in-structs.stderr b/tests/ui/regions/regions-in-structs.stderr index 5dfdc2ee93b43..c34b1ffca64f0 100644 --- a/tests/ui/regions/regions-in-structs.stderr +++ b/tests/ui/regions/regions-in-structs.stderr @@ -1,19 +1,24 @@ error[E0261]: use of undeclared lifetime name `'a` --> $DIR/regions-in-structs.rs:10:9 | -LL | struct StructDecl { - | - help: consider introducing lifetime `'a` here: `<'a>` LL | a: &'a isize, | ^^ undeclared lifetime + | +help: consider introducing lifetime `'a` here + | +LL | struct StructDecl<'a> { + | ++++ error[E0261]: use of undeclared lifetime name `'a` --> $DIR/regions-in-structs.rs:11:9 | -LL | struct StructDecl { - | - help: consider introducing lifetime `'a` here: `<'a>` -LL | a: &'a isize, LL | b: &'a isize, | ^^ undeclared lifetime + | +help: consider introducing lifetime `'a` here + | +LL | struct StructDecl<'a> { + | ++++ error: aborting due to 2 previous errors diff --git a/tests/ui/regions/regions-name-undeclared.stderr b/tests/ui/regions/regions-name-undeclared.stderr index 532603de5f783..06e6f4299deaf 100644 --- a/tests/ui/regions/regions-name-undeclared.stderr +++ b/tests/ui/regions/regions-name-undeclared.stderr @@ -50,9 +50,12 @@ LL | fn bar<'a>(x: &'a isize) { | -- lifetime parameter from outer item ... LL | type X = Option<&'a isize>; - | - ^^ use of generic parameter from outer item - | | - | help: consider introducing lifetime `'a` here: `<'a>` + | ^^ use of generic parameter from outer item + | +help: consider introducing lifetime `'a` here + | +LL | type X<'a> = Option<&'a isize>; + | ++++ error[E0401]: can't use generic parameters from outer item --> $DIR/regions-name-undeclared.rs:28:13 @@ -60,10 +63,13 @@ error[E0401]: can't use generic parameters from outer item LL | fn bar<'a>(x: &'a isize) { | -- lifetime parameter from outer item ... -LL | enum E { - | - help: consider introducing lifetime `'a` here: `<'a>` LL | E1(&'a isize) | ^^ use of generic parameter from outer item + | +help: consider introducing lifetime `'a` here + | +LL | enum E<'a> { + | ++++ error[E0401]: can't use generic parameters from outer item --> $DIR/regions-name-undeclared.rs:31:13 @@ -71,10 +77,13 @@ error[E0401]: can't use generic parameters from outer item LL | fn bar<'a>(x: &'a isize) { | -- lifetime parameter from outer item ... -LL | struct S { - | - help: consider introducing lifetime `'a` here: `<'a>` LL | f: &'a isize | ^^ use of generic parameter from outer item + | +help: consider introducing lifetime `'a` here + | +LL | struct S<'a> { + | ++++ error[E0401]: can't use generic parameters from outer item --> $DIR/regions-name-undeclared.rs:33:14 @@ -83,17 +92,23 @@ LL | fn bar<'a>(x: &'a isize) { | -- lifetime parameter from outer item ... LL | fn f(a: &'a isize) { } - | - ^^ use of generic parameter from outer item - | | - | help: consider introducing lifetime `'a` here: `<'a>` + | ^^ use of generic parameter from outer item + | +help: consider introducing lifetime `'a` here + | +LL | fn f<'a>(a: &'a isize) { } + | ++++ error[E0261]: use of undeclared lifetime name `'a` --> $DIR/regions-name-undeclared.rs:41:17 | LL | fn fn_types(a: &'a isize, - | - ^^ undeclared lifetime - | | - | help: consider introducing lifetime `'a` here: `<'a>` + | ^^ undeclared lifetime + | +help: consider introducing lifetime `'a` here + | +LL | fn fn_types<'a>(a: &'a isize, + | ++++ error[E0261]: use of undeclared lifetime name `'b` --> $DIR/regions-name-undeclared.rs:43:36 @@ -129,11 +144,13 @@ LL | fn fn_types<'b>(a: &'a isize, error[E0261]: use of undeclared lifetime name `'a` --> $DIR/regions-name-undeclared.rs:47:17 | -LL | fn fn_types(a: &'a isize, - | - help: consider introducing lifetime `'a` here: `<'a>` -... LL | c: &'a isize) | ^^ undeclared lifetime + | +help: consider introducing lifetime `'a` here + | +LL | fn fn_types<'a>(a: &'a isize, + | ++++ error[E0261]: use of undeclared lifetime name `'a` --> $DIR/regions-name-undeclared.rs:53:31 diff --git a/tests/ui/regions/regions-undeclared.stderr b/tests/ui/regions/regions-undeclared.stderr index 6bfde5524ac49..2bc0f1848032e 100644 --- a/tests/ui/regions/regions-undeclared.stderr +++ b/tests/ui/regions/regions-undeclared.stderr @@ -7,35 +7,46 @@ LL | static c_x: &'blk isize = &22; error[E0261]: use of undeclared lifetime name `'a` --> $DIR/regions-undeclared.rs:4:10 | -LL | enum EnumDecl { - | - help: consider introducing lifetime `'a` here: `<'a>` LL | Foo(&'a isize), | ^^ undeclared lifetime + | +help: consider introducing lifetime `'a` here + | +LL | enum EnumDecl<'a> { + | ++++ error[E0261]: use of undeclared lifetime name `'a` --> $DIR/regions-undeclared.rs:5:10 | -LL | enum EnumDecl { - | - help: consider introducing lifetime `'a` here: `<'a>` -LL | Foo(&'a isize), LL | Bar(&'a isize), | ^^ undeclared lifetime + | +help: consider introducing lifetime `'a` here + | +LL | enum EnumDecl<'a> { + | ++++ error[E0261]: use of undeclared lifetime name `'a` --> $DIR/regions-undeclared.rs:8:15 | LL | fn fnDecl(x: &'a isize, - | - ^^ undeclared lifetime - | | - | help: consider introducing lifetime `'a` here: `<'a>` + | ^^ undeclared lifetime + | +help: consider introducing lifetime `'a` here + | +LL | fn fnDecl<'a>(x: &'a isize, + | ++++ error[E0261]: use of undeclared lifetime name `'a` --> $DIR/regions-undeclared.rs:9:15 | -LL | fn fnDecl(x: &'a isize, - | - help: consider introducing lifetime `'a` here: `<'a>` LL | y: &'a isize) | ^^ undeclared lifetime + | +help: consider introducing lifetime `'a` here + | +LL | fn fnDecl<'a>(x: &'a isize, + | ++++ error: aborting due to 5 previous errors diff --git a/tests/ui/statics/missing_lifetime.stderr b/tests/ui/statics/missing_lifetime.stderr index e23b27f7a6a6d..102670c36428e 100644 --- a/tests/ui/statics/missing_lifetime.stderr +++ b/tests/ui/statics/missing_lifetime.stderr @@ -2,9 +2,12 @@ error[E0261]: use of undeclared lifetime name `'reborrow` --> $DIR/missing_lifetime.rs:4:15 | LL | struct Slice(&'reborrow [&'static [u8]]); - | - ^^^^^^^^^ undeclared lifetime - | | - | help: consider introducing lifetime `'reborrow` here: `<'reborrow>` + | ^^^^^^^^^ undeclared lifetime + | +help: consider introducing lifetime `'reborrow` here + | +LL | struct Slice<'reborrow>(&'reborrow [&'static [u8]]); + | +++++++++++ error: aborting due to 1 previous error diff --git a/tests/ui/suggestions/lifetimes/missing-lifetimes-in-signature.stderr b/tests/ui/suggestions/lifetimes/missing-lifetimes-in-signature.stderr index b92719e8033a0..c0548bf30daef 100644 --- a/tests/ui/suggestions/lifetimes/missing-lifetimes-in-signature.stderr +++ b/tests/ui/suggestions/lifetimes/missing-lifetimes-in-signature.stderr @@ -2,9 +2,12 @@ error[E0261]: use of undeclared lifetime name `'a` --> $DIR/missing-lifetimes-in-signature.rs:37:11 | LL | fn baz(g: G, dest: &mut T) -> impl FnOnce() + '_ - | - ^^ undeclared lifetime - | | - | help: consider introducing lifetime `'a` here: `'a,` + | ^^ undeclared lifetime + | +help: consider introducing lifetime `'a` here + | +LL | fn baz<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + | +++ warning: elided lifetime has a name --> $DIR/missing-lifetimes-in-signature.rs:102:64 diff --git a/tests/ui/traits/next-solver/dont-canonicalize-re-error.stderr b/tests/ui/traits/next-solver/dont-canonicalize-re-error.stderr index cf85c52fb42ee..989ee1c6d7c15 100644 --- a/tests/ui/traits/next-solver/dont-canonicalize-re-error.stderr +++ b/tests/ui/traits/next-solver/dont-canonicalize-re-error.stderr @@ -2,9 +2,12 @@ error[E0261]: use of undeclared lifetime name `'missing` --> $DIR/dont-canonicalize-re-error.rs:25:26 | LL | impl Constrain<'missing> for W {} - | - ^^^^^^^^ undeclared lifetime - | | - | help: consider introducing lifetime `'missing` here: `'missing,` + | ^^^^^^^^ undeclared lifetime + | +help: consider introducing lifetime `'missing` here + | +LL | impl<'missing, A: Sized> Constrain<'missing> for W {} + | +++++++++ error[E0119]: conflicting implementations of trait `Tr<'_>` for type `W<_>` --> $DIR/dont-canonicalize-re-error.rs:21:1 diff --git a/tests/ui/traits/span-bug-issue-121414.stderr b/tests/ui/traits/span-bug-issue-121414.stderr index 744806a341506..2eeda00d9cb74 100644 --- a/tests/ui/traits/span-bug-issue-121414.stderr +++ b/tests/ui/traits/span-bug-issue-121414.stderr @@ -2,9 +2,12 @@ error[E0261]: use of undeclared lifetime name `'f` --> $DIR/span-bug-issue-121414.rs:5:22 | LL | impl<'a> Bar for Foo<'f> { - | - ^^ undeclared lifetime - | | - | help: consider introducing lifetime `'f` here: `'f,` + | ^^ undeclared lifetime + | +help: consider introducing lifetime `'f` here + | +LL | impl<'f, 'a> Bar for Foo<'f> { + | +++ error: aborting due to 1 previous error