Tweak wording in associated type with anon lifetime error

Move the previous long message to a note and use a shorter primary message:

```
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;
   |                  ++
```
This commit is contained in:
Esteban Küber 2025-01-17 01:05:15 +00:00
parent 0d7ef4f757
commit 04804c713e
19 changed files with 128 additions and 25 deletions

View file

@ -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

View file

@ -930,6 +930,8 @@ pub(crate) struct AnonymousLifetimeNonGatReportError {
#[primary_span]
#[label]
pub(crate) lifetime: Span,
#[note]
pub(crate) decl: MultiSpan,
}
#[derive(Subdiagnostic)]

View file

@ -20,21 +20,21 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
use rustc_data_structures::unord::{UnordMap, UnordSet};
use rustc_errors::codes::*;
use rustc_errors::{
Applicability, Diag, DiagArgValue, ErrorGuaranteed, IntoDiagArg, StashKey, Suggestions,
pluralize,
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, Visibility};
use rustc_middle::ty::{AssocTag, DelegationFnSig, Visibility};
use rustc_middle::{bug, span_bug};
use rustc_session::config::{CrateType, ResolveDocLinks};
use rustc_session::lint;
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 thin_vec::ThinVec;
use tracing::{debug, instrument, trace};
@ -725,6 +725,9 @@ struct DiagMetadata<'ast> {
/// The current impl items (used to suggest).
current_impl_items: Option<&'ast [Box<AssocItem>]>,
/// The current impl items (used to suggest).
current_impl_item: Option<&'ast AssocItem>,
/// When processing impl trait
currently_processing_impl_trait: Option<(TraitRef, Ty)>,
@ -1878,9 +1881,31 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
ty: ty.span,
});
} else {
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(ident) = assoc.kind.ident()
&& let Some(assoc) = assocs.find_by_ident_and_kind(
self.r.tcx,
ident,
AssocTag::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::AnonymousLifetimeNonGatReportError {
lifetime: lifetime.ident.span,
decl,
},
);
self.point_at_impl_lifetimes(&mut err, i, lifetime.ident.span);
@ -1993,7 +2018,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
} 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)
&& let ControlFlow::Break(sp) = AnonRefFinder.visit_trait_ref(&of_trait.trait_ref)
{
err.multipart_suggestion_verbose(
"add a lifetime to the impl block and use it in the trait and associated type",
@ -3354,6 +3379,8 @@ impl<'a, 'ast, 'ra, '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 {
ident,
@ -3502,6 +3529,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
panic!("unexpanded macro in resolve!")
}
}
self.diag_metadata.current_impl_item = prev;
}
fn check_trait_item<F>(

View file

@ -9,7 +9,7 @@ trait MyTrait {
impl MyTrait for &i32 {
type Output = &i32;
//~^ 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
}
impl MyTrait for &u32 {
@ -19,7 +19,7 @@ impl MyTrait for &u32 {
impl<'a> MyTrait for &f64 {
type Output = &f64;
//~^ 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
}
trait OtherTrait<'a> {
@ -27,7 +27,7 @@ trait OtherTrait<'a> {
}
impl OtherTrait<'_> for f64 {
type Output = &f64;
//~^ 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
}
// This is what you have to do:

View file

@ -1,9 +1,10 @@
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 {
@ -16,7 +17,7 @@ error[E0637]: `'_` cannot be used here
LL | type Output = &'_ i32;
| ^^ `'_` is a reserved lifetime name
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:21:19
|
LL | impl<'a> MyTrait for &f64 {
@ -24,17 +25,19 @@ LL | impl<'a> MyTrait for &f64 {
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: 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: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 {

View file

@ -7,9 +7,10 @@ impl<'a> IntoIterator for &S {
//~| NOTE unconstrained lifetime parameter
//~| HELP consider using the named lifetime here instead of an implicit lifetime
type Item = &T;
//~^ ERROR in the trait associated type
//~^ 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 {

View file

@ -1,4 +1,4 @@
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/missing-lifetime-in-assoc-type-1.rs:9:17
|
LL | impl<'a> IntoIterator for &S {
@ -7,6 +7,8 @@ LL | impl<'a> IntoIterator for &S {
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;

View file

@ -3,7 +3,7 @@ struct T;
impl IntoIterator for &S {
type Item = &T;
//~^ ERROR in the trait associated type
//~^ ERROR missing lifetime in associated type
type IntoIter = std::collections::btree_map::Values<'a, i32, T>;
//~^ ERROR use of undeclared lifetime name `'a`

View file

@ -1,9 +1,11 @@
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/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 {

View file

@ -3,7 +3,7 @@ struct T;
impl IntoIterator for &S {
type Item = &T;
//~^ ERROR in the trait associated type
//~^ ERROR missing lifetime in associated type
type IntoIter = std::collections::btree_map::Values<i32, T>;
//~^ ERROR missing lifetime specifier

View file

@ -1,9 +1,11 @@
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/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 {

View file

@ -3,7 +3,7 @@ struct T;
impl IntoIterator for &S {
type Item = &T;
//~^ ERROR in the trait associated type
//~^ ERROR missing lifetime in associated type
type IntoIter<'a> = std::collections::btree_map::Values<'a, i32, T>;
//~^ ERROR lifetime parameters or bounds on associated type `IntoIter` do not match the trait declaration

View file

@ -1,9 +1,11 @@
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/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 {

View file

@ -7,9 +7,10 @@ impl<'a> IntoIterator for &'_ S {
//~| NOTE unconstrained lifetime parameter
//~| HELP consider using the named lifetime here instead of an implicit lifetime
type Item = &T;
//~^ ERROR in the trait associated type
//~^ 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 {

View file

@ -1,4 +1,4 @@
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/missing-lifetime-in-assoc-type-5.rs:9:17
|
LL | impl<'a> IntoIterator for &'_ S {
@ -7,6 +7,8 @@ LL | impl<'a> IntoIterator for &'_ S {
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;

View file

@ -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() {}

View file

@ -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`.

View file

@ -16,7 +16,7 @@ trait Bar {
impl Bar for usize {
type Item = &usize;
//~^ 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
fn poke(&mut self, item: Self::Item) {
self += *item;

View file

@ -10,13 +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 associated type `Item` do not match the trait declaration
--> $DIR/no_lending_iterators.rs:27:14