Skip to content

Commit f5e9cb5

Browse files
authored
Rollup merge of #97720 - cjgillot:all-fresh, r=petrochenkov
Always create elided lifetime parameters for functions Anonymous and elided lifetimes in functions are sometimes (async fns) --and sometimes not (regular fns)-- desugared to implicit generic parameters. This difference of treatment makes it some downstream analyses more complicated to handle. This step is a pre-requisite to perform lifetime elision resolution on AST. There is currently an inconsistency in the treatment of argument-position impl-trait for functions and async fns: ```rust trait Foo<'a> {} fn foo(t: impl Foo<'_>) {} //~ ERROR missing lifetime specifier async fn async_foo(t: impl Foo<'_>) {} //~ OK fn bar(t: impl Iterator<Item = &'_ u8>) {} //~ ERROR missing lifetime specifier async fn async_bar(t: impl Iterator<Item = &'_ u8>) {} //~ OK ``` The current implementation reports "missing lifetime specifier" on `foo`, but **accepts it** in `async_foo`. This PR **proposes to accept** the anonymous lifetime in both cases as an extra generic lifetime parameter. This change would be insta-stable, so let's ping t-lang. Anonymous lifetimes in GAT bindings keep being forbidden: ```rust fn foo(t: impl Foo<Assoc<'_> = Bar<'_>>) {} ^^ ^^ forbidden ok ``` I started a discussion here: https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/Anonymous.20lifetimes.20in.20universal.20impl-trait/near/284968606 r? ``@petrochenkov``
2 parents 431c6f8 + 5a20834 commit f5e9cb5

File tree

20 files changed

+163
-56
lines changed

20 files changed

+163
-56
lines changed

compiler/rustc_feature/src/active.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ declare_features! (
148148
/// below (it has to be checked before expansion possibly makes
149149
/// macros disappear).
150150
(active, allow_internal_unstable, "1.0.0", None, None),
151+
/// Allows using anonymous lifetimes in argument-position impl-trait.
152+
(active, anonymous_lifetime_in_impl_trait, "1.63.0", None, None),
151153
/// Allows identifying the `compiler_builtins` crate.
152154
(active, compiler_builtins, "1.13.0", None, None),
153155
/// Outputs useful `assert!` messages

compiler/rustc_resolve/src/late.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,10 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
758758
// We don't need to deal with patterns in parameters, because
759759
// they are not possible for foreign or bodiless functions.
760760
self.with_lifetime_rib(
761-
LifetimeRibKind::AnonymousPassThrough(fn_id, false),
761+
LifetimeRibKind::AnonymousCreateParameter {
762+
binder: fn_id,
763+
report_in_path: false,
764+
},
762765
|this| walk_list!(this, visit_param, &sig.decl.inputs),
763766
);
764767
self.with_lifetime_rib(
@@ -792,18 +795,13 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
792795
// generic parameters. This is especially useful for `async fn`, where
793796
// these fresh generic parameters can be applied to the opaque `impl Trait`
794797
// return type.
795-
let rib = if async_node_id.is_some() {
796-
// Only emit a hard error for `async fn`, since this kind of
797-
// elision has always been allowed in regular `fn`s.
798+
this.with_lifetime_rib(
798799
LifetimeRibKind::AnonymousCreateParameter {
799800
binder: fn_id,
800-
report_in_path: true,
801-
}
802-
} else {
803-
LifetimeRibKind::AnonymousPassThrough(fn_id, false)
804-
};
805-
this.with_lifetime_rib(
806-
rib,
801+
// Only emit a hard error for `async fn`, since this kind of
802+
// elision has always been allowed in regular `fn`s.
803+
report_in_path: async_node_id.is_some(),
804+
},
807805
// Add each argument to the rib.
808806
|this| this.resolve_params(&declaration.inputs),
809807
);

compiler/rustc_resolve/src/late/lifetimes.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1677,14 +1677,29 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
16771677
break None;
16781678
}
16791679

1680-
Scope::Binder { ref lifetimes, scope_type, s, .. } => {
1680+
Scope::Binder { ref lifetimes, scope_type, s, where_bound_origin, .. } => {
16811681
if let Some(&def) = lifetimes.get(&region_def_id) {
16821682
break Some(def.shifted(late_depth));
16831683
}
16841684
match scope_type {
16851685
BinderScopeType::Normal => late_depth += 1,
16861686
BinderScopeType::Concatenating => {}
16871687
}
1688+
// Fresh lifetimes in APIT used to be allowed in async fns and forbidden in
1689+
// regular fns.
1690+
if let Some(hir::PredicateOrigin::ImplTrait) = where_bound_origin
1691+
&& let hir::LifetimeName::Param(_, hir::ParamName::Fresh) = lifetime_ref.name
1692+
&& let hir::IsAsync::NotAsync = self.tcx.asyncness(lifetime_ref.hir_id.owner)
1693+
&& !self.tcx.features().anonymous_lifetime_in_impl_trait
1694+
{
1695+
rustc_session::parse::feature_err(
1696+
&self.tcx.sess.parse_sess,
1697+
sym::anonymous_lifetime_in_impl_trait,
1698+
lifetime_ref.span,
1699+
"anonymous lifetimes in `impl Trait` are unstable",
1700+
).emit();
1701+
return;
1702+
}
16881703
scope = s;
16891704
}
16901705

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ symbols! {
341341
always,
342342
and,
343343
and_then,
344+
anonymous_lifetime_in_impl_trait,
344345
any,
345346
append_const_msg,
346347
arbitrary_enum_discriminant,

src/test/ui-fulldeps/internal-lints/rustc_pass_by_value.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl CustomStruct {
9393

9494
fn test_alias(
9595
value: CustomAlias,
96-
reference: &CustomAlias, //~ ERROR passing `CustomAlias<>` by reference
96+
reference: &CustomAlias, //~ ERROR passing `CustomAlias<'_>` by reference
9797
) {
9898
}
9999
}

src/test/ui-fulldeps/internal-lints/rustc_pass_by_value.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ error: passing `CustomStruct` by reference
9494
LL | reference: &CustomStruct,
9595
| ^^^^^^^^^^^^^ help: try passing by value: `CustomStruct`
9696

97-
error: passing `CustomAlias<>` by reference
97+
error: passing `CustomAlias<'_>` by reference
9898
--> $DIR/rustc_pass_by_value.rs:96:20
9999
|
100100
LL | reference: &CustomAlias,
101-
| ^^^^^^^^^^^^ help: try passing by value: `CustomAlias<>`
101+
| ^^^^^^^^^^^^ help: try passing by value: `CustomAlias<'_>`
102102

103103
error: passing `WithParameters<T, 1>` by reference
104104
--> $DIR/rustc_pass_by_value.rs:110:20

src/test/ui/generic-associated-types/bugs/issue-87748.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ error[E0478]: lifetime bound not satisfied
44
LL | fn do_sth(_: u32) {}
55
| ^^^^^^^^^^^^^^^^^
66
|
7-
note: lifetime parameter instantiated with the anonymous lifetime #2 defined here
7+
note: lifetime parameter instantiated with the anonymous lifetime as defined here
88
--> $DIR/issue-87748.rs:18:5
99
|
1010
LL | fn do_sth(_: u32) {}
1111
| ^^^^^^^^^^^^^^^^^
12-
note: but lifetime parameter must outlive the anonymous lifetime #1 defined here
12+
note: but lifetime parameter must outlive the anonymous lifetime as defined here
1313
--> $DIR/issue-87748.rs:18:5
1414
|
1515
LL | fn do_sth(_: u32) {}

src/test/ui/generic-associated-types/issue-95305.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33
// at some point in the future.
44

55
#![feature(generic_associated_types)]
6-
6+
#![feature(anonymous_lifetime_in_impl_trait)]
77
trait Foo {
88
type Item<'a>;
99
}
1010

1111
fn foo(x: &impl Foo<Item<'_> = u32>) { }
1212
//~^ ERROR `'_` cannot be used here [E0637]
1313

14+
// Ok: the anonymous lifetime is bound to the function.
1415
fn bar(x: &impl for<'a> Foo<Item<'a> = &'_ u32>) { }
15-
//~^ ERROR missing lifetime specifier
16+
17+
// Ok: the anonymous lifetime is bound to the function.
18+
fn baz(x: &impl for<'a> Foo<Item<'a> = &u32>) { }
1619

1720
fn main() {}

src/test/ui/generic-associated-types/issue-95305.stderr

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,6 @@ error[E0637]: `'_` cannot be used here
44
LL | fn foo(x: &impl Foo<Item<'_> = u32>) { }
55
| ^^ `'_` is a reserved lifetime name
66

7-
error[E0106]: missing lifetime specifier
8-
--> $DIR/issue-95305.rs:14:41
9-
|
10-
LL | fn bar(x: &impl for<'a> Foo<Item<'a> = &'_ u32>) { }
11-
| ^^ expected named lifetime parameter
12-
|
13-
help: consider using the `'a` lifetime
14-
|
15-
LL | fn bar(x: &impl for<'a> Foo<Item<'a> = &'a u32>) { }
16-
| ~~
17-
18-
error: aborting due to 2 previous errors
7+
error: aborting due to previous error
198

20-
Some errors have detailed explanations: E0106, E0637.
21-
For more information about an error, try `rustc --explain E0106`.
9+
For more information about this error, try `rustc --explain E0637`.

src/test/ui/issues/issue-37884.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | fn next(&'a mut self) -> Option<Self::Item>
66
|
77
= note: expected fn pointer `fn(&mut RepeatMut<'a, T>) -> Option<_>`
88
found fn pointer `fn(&'a mut RepeatMut<'a, T>) -> Option<_>`
9-
note: the anonymous lifetime #1 defined here...
9+
note: the anonymous lifetime as defined here...
1010
--> $DIR/issue-37884.rs:6:5
1111
|
1212
LL | fn next(&'a mut self) -> Option<Self::Item>

0 commit comments

Comments
 (0)