-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Recover suggestions to introduce named lifetime under NLL #96409
Merged
bors
merged 1 commit into
rust-lang:master
from
marmeladema:fix-nll-introduce-named-lifetime-suggestion
Apr 28, 2022
Merged
Changes from all commits
Commits
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 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 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 |
---|---|---|
|
@@ -6,6 +6,7 @@ use crate::infer::error_reporting::nice_region_error::util::AnonymousParamInfo; | |
use crate::infer::error_reporting::nice_region_error::NiceRegionError; | ||
use crate::infer::lexical_region_resolve::RegionResolutionError; | ||
use crate::infer::SubregionOrigin; | ||
use crate::infer::TyCtxt; | ||
|
||
use rustc_errors::{struct_span_err, Applicability, Diagnostic, ErrorGuaranteed}; | ||
use rustc_hir as hir; | ||
|
@@ -145,84 +146,83 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { | |
} | ||
} | ||
|
||
self.suggest_adding_lifetime_params(sub, ty_sup, ty_sub, &mut err); | ||
if suggest_adding_lifetime_params(self.tcx(), sub, ty_sup, ty_sub, &mut err) { | ||
err.note("each elided lifetime in input position becomes a distinct lifetime"); | ||
} | ||
|
||
let reported = err.emit(); | ||
Some(reported) | ||
} | ||
} | ||
|
||
fn suggest_adding_lifetime_params( | ||
&self, | ||
sub: Region<'tcx>, | ||
ty_sup: &Ty<'_>, | ||
ty_sub: &Ty<'_>, | ||
err: &mut Diagnostic, | ||
) { | ||
if let ( | ||
hir::Ty { kind: hir::TyKind::Rptr(lifetime_sub, _), .. }, | ||
hir::Ty { kind: hir::TyKind::Rptr(lifetime_sup, _), .. }, | ||
) = (ty_sub, ty_sup) | ||
{ | ||
if lifetime_sub.name.is_elided() && lifetime_sup.name.is_elided() { | ||
if let Some(anon_reg) = self.tcx().is_suitable_region(sub) { | ||
let hir_id = self.tcx().hir().local_def_id_to_hir_id(anon_reg.def_id); | ||
|
||
let node = self.tcx().hir().get(hir_id); | ||
let is_impl = matches!(&node, hir::Node::ImplItem(_)); | ||
let generics = match node { | ||
hir::Node::Item(&hir::Item { | ||
kind: hir::ItemKind::Fn(_, ref generics, ..), | ||
.. | ||
}) | ||
| hir::Node::TraitItem(&hir::TraitItem { ref generics, .. }) | ||
| hir::Node::ImplItem(&hir::ImplItem { ref generics, .. }) => generics, | ||
_ => return, | ||
}; | ||
|
||
let (suggestion_param_name, introduce_new) = generics | ||
.params | ||
.iter() | ||
.find(|p| matches!(p.kind, GenericParamKind::Lifetime { .. })) | ||
.and_then(|p| self.tcx().sess.source_map().span_to_snippet(p.span).ok()) | ||
.map(|name| (name, false)) | ||
.unwrap_or_else(|| ("'a".to_string(), true)); | ||
|
||
let mut suggestions = vec![ | ||
if let hir::LifetimeName::Underscore = lifetime_sub.name { | ||
(lifetime_sub.span, suggestion_param_name.clone()) | ||
} else { | ||
(lifetime_sub.span.shrink_to_hi(), suggestion_param_name.clone() + " ") | ||
}, | ||
if let hir::LifetimeName::Underscore = lifetime_sup.name { | ||
(lifetime_sup.span, suggestion_param_name.clone()) | ||
} else { | ||
(lifetime_sup.span.shrink_to_hi(), suggestion_param_name.clone() + " ") | ||
}, | ||
]; | ||
|
||
if introduce_new { | ||
let new_param_suggestion = match &generics.params { | ||
[] => (generics.span, format!("<{}>", suggestion_param_name)), | ||
[first, ..] => { | ||
(first.span.shrink_to_lo(), format!("{}, ", suggestion_param_name)) | ||
} | ||
}; | ||
|
||
suggestions.push(new_param_suggestion); | ||
} | ||
|
||
let mut sugg = String::from("consider introducing a named lifetime parameter"); | ||
if is_impl { | ||
sugg.push_str(" and update trait if needed"); | ||
} | ||
err.multipart_suggestion( | ||
sugg.as_str(), | ||
suggestions, | ||
Applicability::MaybeIncorrect, | ||
); | ||
err.note("each elided lifetime in input position becomes a distinct lifetime"); | ||
} | ||
} | ||
} | ||
pub fn suggest_adding_lifetime_params<'tcx>( | ||
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. Method moved as a free standing function |
||
tcx: TyCtxt<'tcx>, | ||
sub: Region<'tcx>, | ||
ty_sup: &Ty<'_>, | ||
ty_sub: &Ty<'_>, | ||
err: &mut Diagnostic, | ||
) -> bool { | ||
let ( | ||
hir::Ty { kind: hir::TyKind::Rptr(lifetime_sub, _), .. }, | ||
hir::Ty { kind: hir::TyKind::Rptr(lifetime_sup, _), .. }, | ||
) = (ty_sub, ty_sup) else { | ||
return false; | ||
}; | ||
|
||
if !lifetime_sub.name.is_elided() || !lifetime_sup.name.is_elided() { | ||
return false; | ||
}; | ||
|
||
let Some(anon_reg) = tcx.is_suitable_region(sub) else { | ||
return false; | ||
}; | ||
|
||
let hir_id = tcx.hir().local_def_id_to_hir_id(anon_reg.def_id); | ||
|
||
let node = tcx.hir().get(hir_id); | ||
let is_impl = matches!(&node, hir::Node::ImplItem(_)); | ||
let generics = match node { | ||
hir::Node::Item(&hir::Item { kind: hir::ItemKind::Fn(_, ref generics, ..), .. }) | ||
| hir::Node::TraitItem(&hir::TraitItem { ref generics, .. }) | ||
| hir::Node::ImplItem(&hir::ImplItem { ref generics, .. }) => generics, | ||
_ => return false, | ||
}; | ||
|
||
let (suggestion_param_name, introduce_new) = generics | ||
.params | ||
.iter() | ||
.find(|p| matches!(p.kind, GenericParamKind::Lifetime { .. })) | ||
.and_then(|p| tcx.sess.source_map().span_to_snippet(p.span).ok()) | ||
.map(|name| (name, false)) | ||
.unwrap_or_else(|| ("'a".to_string(), true)); | ||
|
||
let mut suggestions = vec![ | ||
if let hir::LifetimeName::Underscore = lifetime_sub.name { | ||
(lifetime_sub.span, suggestion_param_name.clone()) | ||
} else { | ||
(lifetime_sub.span.shrink_to_hi(), suggestion_param_name.clone() + " ") | ||
}, | ||
if let hir::LifetimeName::Underscore = lifetime_sup.name { | ||
(lifetime_sup.span, suggestion_param_name.clone()) | ||
} else { | ||
(lifetime_sup.span.shrink_to_hi(), suggestion_param_name.clone() + " ") | ||
}, | ||
]; | ||
|
||
if introduce_new { | ||
let new_param_suggestion = match &generics.params { | ||
[] => (generics.span, format!("<{}>", suggestion_param_name)), | ||
[first, ..] => (first.span.shrink_to_lo(), format!("{}, ", suggestion_param_name)), | ||
}; | ||
|
||
suggestions.push(new_param_suggestion); | ||
} | ||
|
||
let mut sugg = String::from("consider introducing a named lifetime parameter"); | ||
if is_impl { | ||
sugg.push_str(" and update trait if needed"); | ||
} | ||
err.multipart_suggestion(sugg.as_str(), suggestions, Applicability::MaybeIncorrect); | ||
|
||
true | ||
} |
This file contains 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 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 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 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 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 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 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 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 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 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 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
Oops, something went wrong.
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.
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.
Note was originally in
suggest_adding_lifetime_params
but by moving it here we can avoid having the note under NLL.However if we do want the note, I can just move it back to where it comes from
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.
Hmm. The note is kind of redundant with the NLL error output, I guess.