-
-
Notifications
You must be signed in to change notification settings - Fork 15.4k
Allow explicit lifetime arguments (behind new #![feature(late_bound_turbofishing)]) and prevent explicit lifetime arguments where they do not appear in a function signature
#160471
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
addiesh
wants to merge
7
commits into
rust-lang:main
Choose a base branch
from
addiesh:fishymandias
base: main
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.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3b079d5
I am Fishymandias, fish of fish
addiesh 09ff51e
you look slay today
addiesh f052a12
ignore this
addiesh 0bdf885
this is really messy I'm so sorry
addiesh 3ad2695
attempt to preserve prior behavior, test cleanup
addiesh d2e26fe
test fixes
addiesh b04af4d
review changes
addiesh 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
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
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 |
|---|---|---|
|
|
@@ -399,7 +399,7 @@ pub fn check_generic_arg_count_for_value_path( | |
|
|
||
| /// Checks that the correct number of generic arguments have been provided. | ||
| /// This is used both for datatypes and function calls. | ||
| #[instrument(skip(cx, gen_pos), level = "debug")] | ||
| #[instrument(skip(cx, gen_pos), level = "info")] | ||
| pub(crate) fn check_generic_arg_count( | ||
| cx: &dyn HirTyLowerer<'_>, | ||
| def_id: DefId, | ||
|
|
@@ -409,6 +409,8 @@ pub(crate) fn check_generic_arg_count( | |
| has_self: bool, | ||
| ) -> GenericArgCountResult { | ||
| let gen_args = seg.args(); | ||
| let tcx = cx.tcx(); | ||
| let kind = tcx.def_kind(def_id); | ||
| let default_counts = gen_params.own_defaults(); | ||
| let param_counts = gen_params.own_counts(); | ||
|
|
||
|
|
@@ -430,7 +432,33 @@ pub(crate) fn check_generic_arg_count( | |
| prohibit_assoc_item_constraint(cx, c, None); | ||
| } | ||
|
|
||
| let tcx = cx.tcx(); | ||
| // hidden lifetimes may not be specified explicitly. | ||
| // if it doesn't show up in the function signature, | ||
| // it can't be written as a lifetime arg. | ||
| // | ||
| // While most hidden lifetimes are late-bound (e.g. `fn(_: &u32)` ), | ||
| // there are some cases (complicated and involve associated types) | ||
| // where an early-bound lifetime parameter can be hidden from the function signature. | ||
|
Contributor
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. name a test in this comment that shows this |
||
| // | ||
| // see: <tests/ui/lifetimes/turbofishing-invisible-lifetimes-154490.rs> | ||
|
|
||
| let hidden_early_lifetimes = | ||
| gen_params.own_params.iter().filter(|x| x.is_anonymous_lifetime()).count(); | ||
|
|
||
| let hidden_late_lifetimes = | ||
| gen_params.own_lifetime_params.iter().filter(|x| x.is_anonymous_lifetime()).count() | ||
| - hidden_early_lifetimes; | ||
|
|
||
| debug!(?hidden_early_lifetimes, ?hidden_late_lifetimes); | ||
|
|
||
| let late_bound_lt_count = gen_params.own_late_bound_regions.len(); | ||
|
|
||
| if kind.is_fn_like() { | ||
| debug!("we are using fn-like {:?} ({gen_pos:?})", tcx.item_name(def_id)); | ||
| } | ||
| debug!(?late_bound_lt_count); | ||
| debug!("gen_args count = {}", gen_args.args.len()); | ||
| debug!("lb+eb lifetimes={:?}", gen_params.own_lifetime_params); | ||
|
|
||
| // Suppress this warning for delegations as it is compiler generated and lifetimes are | ||
| // propagated while late-bound lifetimes may be present. | ||
|
|
@@ -449,7 +477,7 @@ pub(crate) fn check_generic_arg_count( | |
| return Ok(()); | ||
| } | ||
|
|
||
| if late_bounds_ignore { | ||
| if late_bounds_ignore && !tcx.features().late_bound_turbofishing() { | ||
| return Ok(()); | ||
| } | ||
|
|
||
|
|
@@ -476,9 +504,22 @@ pub(crate) fn check_generic_arg_count( | |
| Err(reported) | ||
| }; | ||
|
|
||
| let min_expected_lifetime_args = if infer_lifetimes { 0 } else { param_counts.lifetimes }; | ||
| let max_expected_lifetime_args = param_counts.lifetimes; | ||
| let min_expected_lifetime_args = | ||
| if infer_lifetimes { 0 } else { param_counts.lifetimes - hidden_early_lifetimes }; | ||
| debug!(?min_expected_lifetime_args); | ||
|
|
||
| let mut max_expected_lifetime_args = param_counts.lifetimes - hidden_early_lifetimes; | ||
|
|
||
| // FIXME: under certain circumstances (which I have had trouble replicating) | ||
| // this leads to subtraction with overflow | ||
| if tcx.features().late_bound_turbofishing() { | ||
| max_expected_lifetime_args = | ||
| max_expected_lifetime_args + late_bound_lt_count - hidden_late_lifetimes; | ||
| } | ||
| debug!(?max_expected_lifetime_args,); | ||
|
|
||
| let num_provided_lifetime_args = gen_args.num_lifetime_args(); | ||
| debug!(?num_provided_lifetime_args,); | ||
|
|
||
| let lifetimes_correct = check_lifetime_args( | ||
| min_expected_lifetime_args, | ||
|
|
@@ -542,7 +583,7 @@ pub(crate) fn check_generic_arg_count( | |
| .map(|param| param.name) | ||
| .collect(); | ||
| if constraint_names == param_names { | ||
| let has_assoc_ty_with_same_name = if let DefKind::Trait = tcx.def_kind(def_id) { | ||
| let has_assoc_ty_with_same_name = if let DefKind::Trait = kind { | ||
| gen_args.constraints.iter().any(|constraint| { | ||
| traits::supertrait_def_ids(tcx, def_id).any(|trait_did| { | ||
| cx.probe_trait_that_defines_assoc_item( | ||
|
|
@@ -633,20 +674,27 @@ pub(crate) fn prohibit_explicit_late_bound_lifetimes( | |
| ) -> ExplicitLateBound { | ||
| struct LifetimeArgsIssue { | ||
| msg: &'static str, | ||
| note: &'static str, | ||
| } | ||
|
|
||
| impl<'a> Diagnostic<'a, ()> for LifetimeArgsIssue { | ||
| fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, ()> { | ||
| let Self { msg } = self; | ||
| Diag::new(dcx, level, msg) | ||
| let Self { msg, note } = self; | ||
| Diag::new(dcx, level, msg).with_note(note) | ||
| } | ||
| } | ||
|
|
||
| let param_counts = def.own_counts(); | ||
|
|
||
| if let Some(span_late) = def.has_late_bound_regions | ||
| // FIXME(addiesh): just turning off the diagnostic is probably not enough to solve the problem. see: | ||
| // https://rust-lang.zulipchat.com/#narrow/channel/600108-t-types.2Fearly-late-cleanup/topic/turbofishing.20elided.20lifetimes/near/607748866 | ||
| if cx.tcx().features().late_bound_turbofishing() { | ||
| ExplicitLateBound::Yes | ||
| } else if let Some(span_late) = def.own_late_bound_regions.first().copied() | ||
| && args.has_lifetime_args() | ||
| { | ||
| let gone_turbofishing = "this may change in the future; see issue #156581 <https://github.com/rust-lang/rust/issues/156581> for more information"; | ||
|
|
||
| let msg = "cannot specify lifetime arguments explicitly \ | ||
| if late bound lifetime parameters are present"; | ||
| let note = "the late bound lifetime parameter is introduced here"; | ||
|
|
@@ -657,6 +705,7 @@ pub(crate) fn prohibit_explicit_late_bound_lifetimes( | |
| { | ||
| struct_span_code_err!(cx.dcx(), span, E0794, "{}", msg) | ||
| .with_span_note(span_late, note) | ||
| .with_note(gone_turbofishing) | ||
| .emit(); | ||
| } else { | ||
| let mut multispan = MultiSpan::from_span(span); | ||
|
|
@@ -665,7 +714,7 @@ pub(crate) fn prohibit_explicit_late_bound_lifetimes( | |
| LATE_BOUND_LIFETIME_ARGUMENTS, | ||
| args.args[0].hir_id(), | ||
| multispan, | ||
| LifetimeArgsIssue { msg }, | ||
| LifetimeArgsIssue { msg, note: gone_turbofishing }, | ||
| ); | ||
| } | ||
|
|
||
|
|
||
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
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
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
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.
wouldn't we generally want to append these to
spans?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.
I thought so, but it turns out that doing it unconditionally causes cycles in a bunch of places...
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.
Ok... apparently it also just... Breaks the late-bound var checking too. why does this tiny, almost one-liner change make Literally Everything Explode??