Skip to content
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

test that we do not support higher-ranked regions in opaque type inference #121386

Merged
merged 6 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions tests/ui/rfcs/impl-trait/higher-ranked-regions-diag.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Regression test for #97099.
// This was an ICE because `impl Sized` captures the lifetime 'a.

// check-fail
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oli-obk Oli did you forgor to update these UI test directives to use //@?

Copy link
Member

@lqd lqd Feb 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check-fail should be the default for these tests anyways, so we could remove the directives rather than use //@


trait Trait<E> {
type Assoc;
}

struct Foo;

impl<'a> Trait<&'a ()> for Foo {
type Assoc = ();
}

fn foo() -> impl for<'a> Trait<&'a ()> {
Foo
}

fn bar() -> impl for<'a> Trait<&'a (), Assoc = impl Sized> {
foo()
//~^ ERROR hidden type for `impl Sized` captures lifetime that does not appear in bounds
}

fn main() {}
13 changes: 13 additions & 0 deletions tests/ui/rfcs/impl-trait/higher-ranked-regions-diag.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error[E0700]: hidden type for `impl Sized` captures lifetime that does not appear in bounds
--> $DIR/higher-ranked-regions-diag.rs:21:5
|
LL | fn bar() -> impl for<'a> Trait<&'a (), Assoc = impl Sized> {
| -- ---------- opaque type defined here
| |
| hidden type `<impl for<'a> Trait<&'a ()> as Trait<&'a ()>>::Assoc` captures the lifetime `'a` as defined here
LL | foo()
| ^^^^^

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0700`.
84 changes: 84 additions & 0 deletions tests/ui/rfcs/type-alias-impl-trait/higher-ranked-regions-basic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Basic tests for opaque type inference under for<_> binders.

// check-fail

#![feature(type_alias_impl_trait)]

trait Trait<'a> {
type Ty;
}
impl<'a, T> Trait<'a> for T {
type Ty = &'a ();
}

mod basic_pass {
use super::*;
type Opq<'a> = impl Sized + 'a;
fn test() -> impl for<'a> Trait<'a, Ty = Opq<'a>> {}
//~^ ERROR: expected generic lifetime parameter, found `'a`
}

mod capture_rpit {
use super::*;
fn test() -> impl for<'a> Trait<'a, Ty = impl Sized> {}
//~^ ERROR hidden type for `impl Sized` captures lifetime that does not appear in bounds
}

mod capture_tait {
use super::*;
type Opq0 = impl Sized;
type Opq1<'a> = impl for<'b> Trait<'b, Ty = Opq0>;
type Opq2 = impl for<'a> Trait<'a, Ty = Opq1<'a>>;
fn test() -> Opq2 {}
//~^ ERROR hidden type for `capture_tait::Opq0` captures lifetime that does not appear in bounds
}

mod capture_tait_complex_pass {
use super::*;
type Opq0<'a> = impl Sized;
type Opq1<'a> = impl for<'b> Trait<'b, Ty = Opq0<'b>>; // <- Note 'b
//~^ ERROR: concrete type differs from previous defining opaque type use
type Opq2 = impl for<'a> Trait<'a, Ty = Opq1<'a>>;
fn test() -> Opq2 {}
//~^ ERROR: expected generic lifetime parameter, found `'a`
}

// Same as the above, but make sure that different placeholder regions are not equal.
mod capture_tait_complex_fail {
use super::*;
type Opq0<'a> = impl Sized;
type Opq1<'a> = impl for<'b> Trait<'b, Ty = Opq0<'a>>; // <- Note 'a
type Opq2 = impl for<'a> Trait<'a, Ty = Opq1<'a>>;
fn test() -> Opq2 {}
//~^ ERROR hidden type for `capture_tait_complex_fail::Opq0<'a>` captures lifetime that does not appear in bounds
}

// non-defining use because 'static is used.
mod constrain_fail0 {
use super::*;
type Opq0<'a, 'b> = impl Sized;
fn test() -> impl for<'a> Trait<'a, Ty = Opq0<'a, 'static>> {}
//~^ ERROR non-defining opaque type use in defining scope
//~| ERROR: expected generic lifetime parameter, found `'a`
}

// non-defining use because generic lifetime is used multiple times.
mod constrain_fail {
use super::*;
type Opq0<'a, 'b> = impl Sized;
fn test() -> impl for<'a> Trait<'a, Ty = Opq0<'a, 'a>> {}
//~^ ERROR non-defining opaque type use in defining scope
//~| ERROR: expected generic lifetime parameter, found `'a`
}

mod constrain_pass {
use super::*;
type Opq0<'a, 'b> = impl Sized;
type Opq1<'a> = impl for<'b> Trait<'b, Ty = Opq0<'a, 'b>>;
//~^ ERROR concrete type differs
type Opq2 = impl for<'a> Trait<'a, Ty = Opq1<'a>>;
fn test() -> Opq2 {}
//~^ ERROR: expected generic lifetime parameter, found `'a`
}

fn main() {}
125 changes: 125 additions & 0 deletions tests/ui/rfcs/type-alias-impl-trait/higher-ranked-regions-basic.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
error[E0792]: expected generic lifetime parameter, found `'a`
--> $DIR/higher-ranked-regions-basic.rs:17:55
|
LL | type Opq<'a> = impl Sized + 'a;
| -- this generic parameter must be used with a generic lifetime parameter
LL | fn test() -> impl for<'a> Trait<'a, Ty = Opq<'a>> {}
| ^^

error[E0700]: hidden type for `impl Sized` captures lifetime that does not appear in bounds
--> $DIR/higher-ranked-regions-basic.rs:23:58
|
LL | fn test() -> impl for<'a> Trait<'a, Ty = impl Sized> {}
| -- ---------- ^^
| | |
| | opaque type defined here
| hidden type `&'a ()` captures the lifetime `'a` as defined here

error[E0700]: hidden type for `capture_tait::Opq0` captures lifetime that does not appear in bounds
--> $DIR/higher-ranked-regions-basic.rs:32:23
|
LL | type Opq0 = impl Sized;
| ---------- opaque type defined here
LL | type Opq1<'a> = impl for<'b> Trait<'b, Ty = Opq0>;
| -- hidden type `&'b ()` captures the lifetime `'b` as defined here
LL | type Opq2 = impl for<'a> Trait<'a, Ty = Opq1<'a>>;
LL | fn test() -> Opq2 {}
| ^^

error[E0792]: expected generic lifetime parameter, found `'a`
--> $DIR/higher-ranked-regions-basic.rs:42:23
|
LL | type Opq1<'a> = impl for<'b> Trait<'b, Ty = Opq0<'b>>; // <- Note 'b
| -- this generic parameter must be used with a generic lifetime parameter
...
LL | fn test() -> Opq2 {}
| ^^

error: concrete type differs from previous defining opaque type use
--> $DIR/higher-ranked-regions-basic.rs:39:21
|
LL | type Opq1<'a> = impl for<'b> Trait<'b, Ty = Opq0<'b>>; // <- Note 'b
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&'a ()`, got `{type error}`
|
note: previous use here
--> $DIR/higher-ranked-regions-basic.rs:41:17
|
LL | type Opq2 = impl for<'a> Trait<'a, Ty = Opq1<'a>>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0700]: hidden type for `capture_tait_complex_fail::Opq0<'a>` captures lifetime that does not appear in bounds
--> $DIR/higher-ranked-regions-basic.rs:52:23
|
LL | type Opq0<'a> = impl Sized;
| ---------- opaque type defined here
LL | type Opq1<'a> = impl for<'b> Trait<'b, Ty = Opq0<'a>>; // <- Note 'a
| -- hidden type `&'b ()` captures the lifetime `'b` as defined here
LL | type Opq2 = impl for<'a> Trait<'a, Ty = Opq1<'a>>;
LL | fn test() -> Opq2 {}
| ^^

error[E0792]: non-defining opaque type use in defining scope
--> $DIR/higher-ranked-regions-basic.rs:60:41
|
LL | fn test() -> impl for<'a> Trait<'a, Ty = Opq0<'a, 'static>> {}
| ^^^^^^^^^^^^^^^^^^^^^^ argument `'static` is not a generic parameter
|
note: for this opaque type
--> $DIR/higher-ranked-regions-basic.rs:59:25
|
LL | type Opq0<'a, 'b> = impl Sized;
| ^^^^^^^^^^

error[E0792]: expected generic lifetime parameter, found `'a`
--> $DIR/higher-ranked-regions-basic.rs:60:65
|
LL | type Opq0<'a, 'b> = impl Sized;
| -- this generic parameter must be used with a generic lifetime parameter
LL | fn test() -> impl for<'a> Trait<'a, Ty = Opq0<'a, 'static>> {}
| ^^

error: non-defining opaque type use in defining scope
--> $DIR/higher-ranked-regions-basic.rs:69:41
|
LL | fn test() -> impl for<'a> Trait<'a, Ty = Opq0<'a, 'a>> {}
| ^^^^^^^^^^^^^^^^^ generic argument `'a` used twice
|
note: for this opaque type
--> $DIR/higher-ranked-regions-basic.rs:68:25
|
LL | type Opq0<'a, 'b> = impl Sized;
| ^^^^^^^^^^

error[E0792]: expected generic lifetime parameter, found `'a`
--> $DIR/higher-ranked-regions-basic.rs:69:60
|
LL | type Opq0<'a, 'b> = impl Sized;
| -- this generic parameter must be used with a generic lifetime parameter
LL | fn test() -> impl for<'a> Trait<'a, Ty = Opq0<'a, 'a>> {}
| ^^

error[E0792]: expected generic lifetime parameter, found `'a`
--> $DIR/higher-ranked-regions-basic.rs:80:23
|
LL | type Opq1<'a> = impl for<'b> Trait<'b, Ty = Opq0<'a, 'b>>;
| -- this generic parameter must be used with a generic lifetime parameter
...
LL | fn test() -> Opq2 {}
| ^^

error: concrete type differs from previous defining opaque type use
--> $DIR/higher-ranked-regions-basic.rs:77:21
|
LL | type Opq1<'a> = impl for<'b> Trait<'b, Ty = Opq0<'a, 'b>>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&'a ()`, got `{type error}`
|
note: previous use here
--> $DIR/higher-ranked-regions-basic.rs:79:17
|
LL | type Opq2 = impl for<'a> Trait<'a, Ty = Opq1<'a>>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 12 previous errors

Some errors have detailed explanations: E0700, E0792.
For more information about an error, try `rustc --explain E0700`.
21 changes: 21 additions & 0 deletions tests/ui/rfcs/type-alias-impl-trait/higher-ranked-regions-gat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Regression test for #97098.

#![feature(type_alias_impl_trait)]

pub trait Trait {
type Assoc<'a>;
}

pub type Foo = impl for<'a> Trait<Assoc<'a> = FooAssoc<'a>>;
pub type FooAssoc<'a> = impl Sized;
//~^ ERROR: concrete type differs from previous defining opaque type use

struct Struct;
impl Trait for Struct {
type Assoc<'a> = &'a u32;
}

const FOO: Foo = Struct;
//~^ ERROR: expected generic lifetime parameter, found `'a`

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
error[E0792]: expected generic lifetime parameter, found `'a`
--> $DIR/higher-ranked-regions-gat.rs:18:18
|
LL | pub type FooAssoc<'a> = impl Sized;
| -- this generic parameter must be used with a generic lifetime parameter
...
LL | const FOO: Foo = Struct;
| ^^^^^^

error: concrete type differs from previous defining opaque type use
Copy link
Contributor

@lcnr lcnr Feb 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you yeet this error if there's a {type error} in the expected found? 😁

--> $DIR/higher-ranked-regions-gat.rs:10:25
|
LL | pub type FooAssoc<'a> = impl Sized;
| ^^^^^^^^^^ expected `&'a u32`, got `{type error}`
|
note: previous use here
--> $DIR/higher-ranked-regions-gat.rs:9:16
|
LL | pub type Foo = impl for<'a> Trait<Assoc<'a> = FooAssoc<'a>>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0792`.
39 changes: 39 additions & 0 deletions tests/ui/type-alias-impl-trait/hkl_forbidden.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#![feature(type_alias_impl_trait)]

fn id(s: &str) -> &str {
s
}

type Opaque<'a> = impl Sized + 'a;

fn test(s: &str) -> (impl Fn(&str) -> Opaque<'_>, impl Fn(&str) -> Opaque<'_>) {
(id, id) //~ ERROR expected generic lifetime parameter, found `'_`
}

fn id2<'a, 'b>(s: (&'a str, &'b str)) -> (&'a str, &'b str) {
s
}

type Opaque2<'a> = impl Sized + 'a;

fn test2() -> impl for<'a, 'b> Fn((&'a str, &'b str)) -> (Opaque2<'a>, Opaque2<'b>) {
id2 //~ ERROR expected generic lifetime parameter, found `'a`
}

type Opaque3<'a> = impl Sized + 'a;

fn test3(s: &str) -> (impl Fn(&str) -> Opaque3<'_>, Opaque3<'_>) {
(id, s) //~ ERROR expected generic lifetime parameter, found `'_`
}

type Opaque4<'a> = impl Sized + 'a;
fn test4(s: &str) -> (Opaque4<'_>, impl Fn(&str) -> Opaque4<'_>) {
(s, id) //~ ERROR expected generic lifetime parameter, found `'_`
}

type Inner<'a> = impl Sized;
fn outer_impl() -> impl for<'a> Fn(&'a ()) -> Inner<'a> {
|x| x //~ ERROR expected generic lifetime parameter, found `'a`
}

fn main() {}
48 changes: 48 additions & 0 deletions tests/ui/type-alias-impl-trait/hkl_forbidden.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
error[E0792]: expected generic lifetime parameter, found `'_`
--> $DIR/hkl_forbidden.rs:10:5
|
LL | type Opaque<'a> = impl Sized + 'a;
| -- this generic parameter must be used with a generic lifetime parameter
...
LL | (id, id)
| ^^^^^^^^

error[E0792]: expected generic lifetime parameter, found `'a`
--> $DIR/hkl_forbidden.rs:20:5
|
LL | type Opaque2<'a> = impl Sized + 'a;
| -- this generic parameter must be used with a generic lifetime parameter
...
LL | id2
| ^^^

error[E0792]: expected generic lifetime parameter, found `'_`
--> $DIR/hkl_forbidden.rs:26:5
|
LL | type Opaque3<'a> = impl Sized + 'a;
| -- this generic parameter must be used with a generic lifetime parameter
...
LL | (id, s)
| ^^^^^^^

error[E0792]: expected generic lifetime parameter, found `'_`
--> $DIR/hkl_forbidden.rs:31:5
|
LL | type Opaque4<'a> = impl Sized + 'a;
| -- this generic parameter must be used with a generic lifetime parameter
LL | fn test4(s: &str) -> (Opaque4<'_>, impl Fn(&str) -> Opaque4<'_>) {
LL | (s, id)
| ^^^^^^^

error[E0792]: expected generic lifetime parameter, found `'a`
--> $DIR/hkl_forbidden.rs:36:5
|
LL | type Inner<'a> = impl Sized;
| -- this generic parameter must be used with a generic lifetime parameter
LL | fn outer_impl() -> impl for<'a> Fn(&'a ()) -> Inner<'a> {
LL | |x| x
| ^^^^^

error: aborting due to 5 previous errors

For more information about this error, try `rustc --explain E0792`.
Loading
Loading