-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Tweak output of missing lifetime on associated type #135602
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
estebank
wants to merge
8
commits into
rust-lang:master
Choose a base branch
from
estebank:issue-135589
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+759
−148
Open
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
eb6d6f9
Add tests for some cases mentioned in #135589
estebank b4507bf
Detect case of missing lifetime in assoc type
estebank a6cb0b5
On unconstrained lifetime on `impl` block, suggest using it if there'…
estebank 2c7160b
Do not suggest introducing lifetime in impl assoc type
estebank 119ea42
Always point at trait assoc item when generics don't match
estebank 4756c5d
Look at the current `impl` before suggesting adding a lifetime
estebank a46faeb
Tweak wording in associated type with anon lifetime error
estebank b4682f4
Make "add lifetime" suggestion verbose
estebank File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -20,7 +20,8 @@ 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, StashKey, Suggestions, | ||||||||||||||||||||||||||||
pluralize, | ||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||
use rustc_hir::def::Namespace::{self, *}; | ||||||||||||||||||||||||||||
use rustc_hir::def::{self, CtorKind, DefKind, LifetimeRes, NonMacroAttrKind, PartialRes, PerNS}; | ||||||||||||||||||||||||||||
|
@@ -1870,9 +1871,13 @@ 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 mut err = self.r.dcx().create_err( | ||||||||||||||||||||||||||||
errors::AnonymousLivetimeNonGatReportError { | ||||||||||||||||||||||||||||
lifetime: lifetime.ident.span, | ||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||
self.point_at_impl_lifetimes(&mut err, i, lifetime.ident.span); | ||||||||||||||||||||||||||||
err.emit(); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||
self.r.dcx().emit_err(errors::ElidedAnonymousLivetimeReportError { | ||||||||||||||||||||||||||||
|
@@ -1909,6 +1914,47 @@ 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() | ||||||||||||||||||||||||||||
Comment on lines
+1949
to
+1955
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit:
Suggested change
|
||||||||||||||||||||||||||||
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 { | ||||||||||||||||||||||||||||
err.span_label( | ||||||||||||||||||||||||||||
span, | ||||||||||||||||||||||||||||
"you could add a lifetime on the impl block, if the trait or the self type can \ | ||||||||||||||||||||||||||||
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(); | ||||||||||||||||||||||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
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 | ||
type Item = &T; | ||
//~^ ERROR in the trait 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() {} |
23 changes: 23 additions & 0 deletions
23
tests/ui/lifetimes/missing-lifetime-in-assoc-type-1.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
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 | ||
--> $DIR/missing-lifetime-in-assoc-type-1.rs:8: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 | ||
| | ||
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 | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0207`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
struct S; | ||
struct T; | ||
|
||
impl IntoIterator for &S { | ||
type Item = &T; | ||
//~^ ERROR in the trait 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() {} |
26 changes: 26 additions & 0 deletions
26
tests/ui/lifetimes/missing-lifetime-in-assoc-type-2.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
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 | ||
--> $DIR/missing-lifetime-in-assoc-type-2.rs:5:17 | ||
| | ||
LL | impl IntoIterator for &S { | ||
| - you could add a lifetime on the impl block, if the trait or the self type can have one | ||
LL | type Item = &T; | ||
| ^ this lifetime must come from the implemented type | ||
|
||
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 | type IntoIter<'a> = std::collections::btree_map::Values<'a, i32, T>; | ||
| ++++ | ||
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`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
struct S; | ||
struct T; | ||
|
||
impl IntoIterator for &S { | ||
type Item = &T; | ||
//~^ ERROR in the trait associated type | ||
type IntoIter = std::collections::btree_map::Values<i32, T>; | ||
//~^ ERROR missing lifetime specifier | ||
|
||
fn into_iter(self) -> Self::IntoIter { | ||
todo!() | ||
} | ||
} | ||
fn main() {} |
22 changes: 22 additions & 0 deletions
22
tests/ui/lifetimes/missing-lifetime-in-assoc-type-3.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
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 | ||
--> $DIR/missing-lifetime-in-assoc-type-3.rs:5:17 | ||
| | ||
LL | impl IntoIterator for &S { | ||
| - you could add a lifetime on the impl block, if the trait or the self type can have one | ||
LL | type Item = &T; | ||
| ^ this lifetime must come from the implemented type | ||
|
||
error[E0106]: missing lifetime specifier | ||
--> $DIR/missing-lifetime-in-assoc-type-3.rs:7:56 | ||
| | ||
LL | type IntoIter = std::collections::btree_map::Values<i32, T>; | ||
| ^ expected named lifetime parameter | ||
| | ||
help: consider introducing a named lifetime parameter | ||
| | ||
LL | type IntoIter<'a> = 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`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
struct S; | ||
struct T; | ||
|
||
impl IntoIterator for &S { | ||
type Item = &T; | ||
//~^ ERROR in the trait 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() {} |
17 changes: 17 additions & 0 deletions
17
tests/ui/lifetimes/missing-lifetime-in-assoc-type-4.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
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 | ||
--> $DIR/missing-lifetime-in-assoc-type-4.rs:5:17 | ||
| | ||
LL | impl IntoIterator for &S { | ||
| - you could add a lifetime on the impl block, if the trait or the self type can have one | ||
LL | type Item = &T; | ||
| ^ this lifetime must come from the implemented type | ||
|
||
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 | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0195`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the
skip
? Could you add a comment explaining it?