Skip to content

Commit

Permalink
Stabilize anonymous_lifetime_in_impl_trait
Browse files Browse the repository at this point in the history
  • Loading branch information
c410-f3r committed Apr 21, 2024
1 parent f22a0c2 commit 70abdc8
Show file tree
Hide file tree
Showing 15 changed files with 234 additions and 131 deletions.
36 changes: 35 additions & 1 deletion compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1168,9 +1168,16 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
&& let hir::LifetimeName::Param(param_id) = lifetime_ref.res
&& let Some(generics) =
self.tcx.hir().get_generics(self.tcx.local_parent(param_id))
&& let Some(param) = generics.params.iter().find(|p| p.def_id == param_id)
&& let Some((param, pred)) = generics
.params
.iter()
.position(|param| param.def_id == param_id)
.and_then(|idx| {
Some((generics.params.get(idx)?, generics.predicates.get(idx)?))
})
&& param.is_elided_lifetime()
&& !self.tcx.asyncness(lifetime_ref.hir_id.owner.def_id).is_async()
&& is_gat(pred)
&& !self.tcx.features().anonymous_lifetime_in_impl_trait
{
let mut diag: rustc_errors::Diag<'_> = rustc_session::parse::feature_err(
Expand Down Expand Up @@ -2106,3 +2113,30 @@ pub fn deny_non_region_late_bound(
*arg = ResolvedArg::Error(guar);
}
}

fn is_gat(predicate: &hir::WherePredicate<'_>) -> bool {
let hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate { bounds, .. }) = predicate
else {
return false;
};
for bound in *bounds {
let hir::GenericBound::Trait(poly_trait_ref, _) = bound else {
continue;
};

for segment in poly_trait_ref.trait_ref.path.segments {
let Some(generic_args) = segment.args else {
continue;
};
if !generic_args.args.is_empty() {
continue;
}
for binding in generic_args.bindings {
if !binding.gen_args.args.is_empty() {
return true;
}
}
}
}
false
}
2 changes: 0 additions & 2 deletions tests/ui/associated-type-bounds/elision.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![feature(anonymous_lifetime_in_impl_trait)]

// The same thing should happen for constraints in dyn trait.
fn f(x: &mut dyn Iterator<Item: Iterator<Item = &'_ ()>>) -> Option<&'_ ()> { x.next() }
//~^ ERROR associated type bounds are not allowed in `dyn` types
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/associated-type-bounds/elision.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0106]: missing lifetime specifier
--> $DIR/elision.rs:4:70
--> $DIR/elision.rs:2:70
|
LL | fn f(x: &mut dyn Iterator<Item: Iterator<Item = &'_ ()>>) -> Option<&'_ ()> { x.next() }
| ------------------------------------------------ ^^ expected named lifetime parameter
Expand All @@ -11,7 +11,7 @@ LL | fn f<'a>(x: &'a mut dyn Iterator<Item: Iterator<Item = &'a ()>>) -> Option<
| ++++ ++ ~~ ~~

error: associated type bounds are not allowed in `dyn` types
--> $DIR/elision.rs:4:27
--> $DIR/elision.rs:2:27
|
LL | fn f(x: &mut dyn Iterator<Item: Iterator<Item = &'_ ()>>) -> Option<&'_ ()> { x.next() }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 0 additions & 2 deletions tests/ui/borrowck/anonymous-region-in-apit.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![feature(anonymous_lifetime_in_impl_trait)]

trait Foo<T> {
fn bar(self, baz: T);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/borrowck/anonymous-region-in-apit.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0521]: borrowed data escapes outside of closure
--> $DIR/anonymous-region-in-apit.rs:8:17
--> $DIR/anonymous-region-in-apit.rs:6:17
|
LL | fn qux(foo: impl Foo<&str>) {
| --- lifetime `'2` appears in the type of `foo`
Expand Down
1 change: 0 additions & 1 deletion tests/ui/generic-associated-types/issue-95305.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Forbid it for now but proper support might be added
// at some point in the future.

#![feature(anonymous_lifetime_in_impl_trait)]
trait Foo {
type Item<'a>;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/generic-associated-types/issue-95305.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0637]: `'_` cannot be used here
--> $DIR/issue-95305.rs:10:26
--> $DIR/issue-95305.rs:9:26
|
LL | fn foo(x: &impl Foo<Item<'_> = u32>) { }
| ^^ `'_` is a reserved lifetime name
Expand Down
61 changes: 61 additions & 0 deletions tests/ui/impl-trait/impl-trait-lifetimes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
trait Foo<T> {
fn foo(&self, _: T) { }
}

trait FooBar<'a> {
type Item;
}


mod foo {
fn fun(t: impl crate::Foo<&u32>, n: u32) {
t.foo(&n);
//~^ ERROR `n` does not live long enough
}
}

mod fun {
fn fun(t: impl Fn(&u32), n: u32) {
t(&n);
}
}

mod iterator_fun {
fn fun(t: impl Iterator<Item = impl Fn(&u32)>, n: u32) {
for elem in t {
elem(&n);
}
}
}

mod iterator_foo {
fn fun(t: impl Iterator<Item = impl crate::Foo<&u32>>, n: u32) {
for elem in t {
elem.foo(&n);
//~^ ERROR `n` does not live long enough
}
}
}

mod placeholder {
trait Placeholder<'a> {
fn foo(&self, _: &'a u32) {}
}

fn fun(t: impl Placeholder<'_>, n: u32) {
t.foo(&n);
//~^ ERROR `n` does not live long enough
}
}

mod stabilized {
trait InTrait {
fn in_trait(&self) -> impl Iterator<Item = &u32>;
}

fn foo1(_: impl Iterator<Item = &u32>) {}
fn foo2<'b>(_: impl crate::FooBar<'b, Item = &u32>) {}
}

fn main() {
}
43 changes: 43 additions & 0 deletions tests/ui/impl-trait/impl-trait-lifetimes.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
error[E0597]: `n` does not live long enough
--> $DIR/impl-trait-lifetimes.rs:12:15
|
LL | fn fun(t: impl crate::Foo<&u32>, n: u32) {
| - has type `t` - binding `n` declared here
LL | t.foo(&n);
| ------^^-
| | |
| | borrowed value does not live long enough
| argument requires that `n` is borrowed for `'1`
LL |
LL | }
| - `n` dropped here while still borrowed

error[E0597]: `n` does not live long enough
--> $DIR/impl-trait-lifetimes.rs:34:22
|
LL | fn fun(t: impl Iterator<Item = impl crate::Foo<&u32>>, n: u32) {
| - binding `n` declared here
LL | for elem in t {
LL | elem.foo(&n);
| ^^ borrowed value does not live long enough
...
LL | }
| - `n` dropped here while still borrowed

error[E0597]: `n` does not live long enough
--> $DIR/impl-trait-lifetimes.rs:46:15
|
LL | fn fun(t: impl Placeholder<'_>, n: u32) {
| - has type `t` - binding `n` declared here
LL | t.foo(&n);
| ------^^-
| | |
| | borrowed value does not live long enough
| argument requires that `n` is borrowed for `'1`
LL |
LL | }
| - `n` dropped here while still borrowed

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0597`.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//@ edition:2021
// gate-test-anonymous_lifetime_in_impl_trait

// Verify the behaviour of `feature(anonymous_lifetime_in_impl_trait)`.

mod elided {
fn f(_: impl Iterator<Item = &()>) {}
//~^ ERROR anonymous lifetimes in `impl Trait` are unstable

fn g(mut x: impl Iterator<Item = &()>) -> Option<&()> { x.next() }
//~^ ERROR anonymous lifetimes in `impl Trait` are unstable
//~^ ERROR lifetime may not live long
//~| ERROR missing lifetime specifier

// Anonymous lifetimes in async fn are already allowed.
Expand All @@ -17,16 +17,15 @@ mod elided {
// Anonymous lifetimes in async fn are already allowed.
// But that lifetime does not participate in resolution.
async fn i(mut x: impl Iterator<Item = &()>) -> Option<&()> { x.next() }
//~^ ERROR missing lifetime specifier
//~| ERROR lifetime may not live long enough
//~^ ERROR lifetime may not live long
//~| ERROR missing lifetime specifier
}

mod underscore {
fn f(_: impl Iterator<Item = &'_ ()>) {}
//~^ ERROR anonymous lifetimes in `impl Trait` are unstable

fn g(mut x: impl Iterator<Item = &'_ ()>) -> Option<&'_ ()> { x.next() }
//~^ ERROR anonymous lifetimes in `impl Trait` are unstable
//~^ ERROR lifetime may not live long
//~| ERROR missing lifetime specifier

// Anonymous lifetimes in async fn are already allowed.
Expand All @@ -36,29 +35,27 @@ mod underscore {
// Anonymous lifetimes in async fn are already allowed.
// But that lifetime does not participate in resolution.
async fn i(mut x: impl Iterator<Item = &'_ ()>) -> Option<&'_ ()> { x.next() }
//~^ ERROR missing lifetime specifier
//~| ERROR lifetime may not live long enough
//~^ ERROR lifetime may not live long
//~| ERROR missing lifetime specifier
}

mod alone_in_path {
trait Foo<'a> { fn next(&mut self) -> Option<&'a ()>; }

fn f(_: impl Foo) {}
//~^ ERROR anonymous lifetimes in `impl Trait` are unstable

fn g(mut x: impl Foo) -> Option<&()> { x.next() }
//~^ ERROR anonymous lifetimes in `impl Trait` are unstable
//~^ ERROR lifetime may not live long
//~| ERROR missing lifetime specifier
}

mod in_path {
trait Foo<'a, T> { fn next(&mut self) -> Option<&'a T>; }

fn f(_: impl Foo<()>) {}
//~^ ERROR anonymous lifetimes in `impl Trait` are unstable

fn g(mut x: impl Foo<()>) -> Option<&()> { x.next() }
//~^ ERROR anonymous lifetimes in `impl Trait` are unstable
//~^ ERROR lifetime may not live long
//~| ERROR missing lifetime specifier
}

Expand Down
Loading

0 comments on commit 70abdc8

Please sign in to comment.