-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #56074 - matthewjasper:forbid-recursive-impl-trait, r=<…
…try> Forbid recursive impl trait There is no type T, such that `T = [T; 2]`, but impl Trait could sometimes be to circumvented this. This patch makes it a hard error for an opaque type to resolve to such a "type". Before this can be merged it needs - [ ] A better error message - [ ] A crater run (?) to see if this any real-world code
- Loading branch information
Showing
7 changed files
with
318 additions
and
11 deletions.
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
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
19 changes: 15 additions & 4 deletions
19
src/test/ui/impl-trait/infinite-impl-trait-issue-38064.stderr
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 |
---|---|---|
@@ -1,7 +1,18 @@ | ||
error[E0275]: overflow evaluating the requirement `impl Quux` | ||
error: recursive opaque type | ||
--> $DIR/infinite-impl-trait-issue-38064.rs:18:13 | ||
| | ||
= help: consider adding a `#![recursion_limit="128"]` attribute to your crate | ||
LL | fn foo() -> impl Quux { //~ recursive opaque type | ||
| ^^^^^^^^^ resolves to self-referential type | ||
| | ||
= note: resolved type is `foo::Foo<bar::Bar<impl Quux>>` | ||
|
||
error: recursive opaque type | ||
--> $DIR/infinite-impl-trait-issue-38064.rs:24:13 | ||
| | ||
LL | fn bar() -> impl Quux { //~ recursive opaque type | ||
| ^^^^^^^^^ resolves to self-referential type | ||
| | ||
= note: resolved type is `bar::Bar<foo::Foo<impl Quux>>` | ||
|
||
error: aborting due to previous error | ||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0275`. |
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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Test that impl trait does not allow creating recursive types that are | ||
// otherwise forbidden. | ||
|
||
#![feature(await_macro, async_await, futures_api, generators)] | ||
|
||
fn option(i: i32) -> impl Sized { //~ ERROR | ||
if i < 0 { | ||
None | ||
} else { | ||
Some((option(i - 1), i)) | ||
} | ||
} | ||
|
||
fn tuple() -> impl Sized { //~ ERROR | ||
(tuple(),) | ||
} | ||
|
||
fn array() -> impl Sized { //~ ERROR | ||
[array()] | ||
} | ||
|
||
fn ptr() -> impl Sized { //~ ERROR | ||
&ptr() as *const _ | ||
} | ||
|
||
fn fn_ptr() -> impl Sized { //~ ERROR | ||
fn_ptr as fn() -> _ | ||
} | ||
|
||
fn closure_capture() -> impl Sized { //~ ERROR | ||
let x = closure_capture(); | ||
move || { x; } | ||
} | ||
|
||
fn closure_ref_capture() -> impl Sized { //~ ERROR | ||
let x = closure_ref_capture(); | ||
move || { &x; } | ||
} | ||
|
||
fn closure_sig() -> impl Sized { //~ ERROR | ||
|| closure_sig() | ||
} | ||
|
||
fn generator_sig() -> impl Sized { //~ ERROR | ||
|| generator_sig() | ||
} | ||
|
||
fn generator_capture() -> impl Sized { //~ ERROR | ||
let x = generator_capture(); | ||
move || { yield; x; } | ||
} | ||
|
||
fn substs_change<T>() -> impl Sized { //~ ERROR | ||
(substs_change::<&T>(),) | ||
} | ||
|
||
fn generator_hold() -> impl Sized { //~ ERROR | ||
move || { | ||
let x = generator_hold(); | ||
yield; | ||
x; | ||
} | ||
} | ||
|
||
async fn recursive_async_function() -> () { //~ ERROR | ||
await!(recursive_async_function()); | ||
} | ||
|
||
fn use_fn_ptr() -> impl Sized { // OK, error already reported | ||
fn_ptr() | ||
} | ||
|
||
fn mutual_recursion() -> impl Sync { //~ ERROR | ||
mutual_recursion_b() | ||
} | ||
|
||
fn mutual_recursion_b() -> impl Sized { //~ ERROR | ||
mutual_recursion() | ||
} | ||
|
||
fn main() {} |
122 changes: 122 additions & 0 deletions
122
src/test/ui/impl-trait/recursive-impl-trait-type.stderr
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 |
---|---|---|
@@ -0,0 +1,122 @@ | ||
error: recursive opaque type | ||
--> $DIR/recursive-impl-trait-type.rs:6:22 | ||
| | ||
LL | fn option(i: i32) -> impl Sized { //~ ERROR | ||
| ^^^^^^^^^^ resolves to self-referential type | ||
| | ||
= note: resolved type is `std::option::Option<(impl Sized, i32)>` | ||
|
||
error: recursive opaque type | ||
--> $DIR/recursive-impl-trait-type.rs:14:15 | ||
| | ||
LL | fn tuple() -> impl Sized { //~ ERROR | ||
| ^^^^^^^^^^ resolves to self-referential type | ||
| | ||
= note: resolved type is `(impl Sized,)` | ||
|
||
error: recursive opaque type | ||
--> $DIR/recursive-impl-trait-type.rs:18:15 | ||
| | ||
LL | fn array() -> impl Sized { //~ ERROR | ||
| ^^^^^^^^^^ resolves to self-referential type | ||
| | ||
= note: resolved type is `[impl Sized; 1]` | ||
|
||
error: recursive opaque type | ||
--> $DIR/recursive-impl-trait-type.rs:22:13 | ||
| | ||
LL | fn ptr() -> impl Sized { //~ ERROR | ||
| ^^^^^^^^^^ resolves to self-referential type | ||
| | ||
= note: resolved type is `*const impl Sized` | ||
|
||
error: recursive opaque type | ||
--> $DIR/recursive-impl-trait-type.rs:26:16 | ||
| | ||
LL | fn fn_ptr() -> impl Sized { //~ ERROR | ||
| ^^^^^^^^^^ resolves to self-referential type | ||
| | ||
= note: resolved type is `fn() -> impl Sized` | ||
|
||
error: recursive opaque type | ||
--> $DIR/recursive-impl-trait-type.rs:30:25 | ||
| | ||
LL | fn closure_capture() -> impl Sized { //~ ERROR | ||
| ^^^^^^^^^^ resolves to self-referential type | ||
| | ||
= note: resolved type is `[closure@$DIR/recursive-impl-trait-type.rs:32:5: 32:19 x:impl Sized]` | ||
|
||
error: recursive opaque type | ||
--> $DIR/recursive-impl-trait-type.rs:35:29 | ||
| | ||
LL | fn closure_ref_capture() -> impl Sized { //~ ERROR | ||
| ^^^^^^^^^^ resolves to self-referential type | ||
| | ||
= note: resolved type is `[closure@$DIR/recursive-impl-trait-type.rs:37:5: 37:20 x:impl Sized]` | ||
|
||
error: recursive opaque type | ||
--> $DIR/recursive-impl-trait-type.rs:40:21 | ||
| | ||
LL | fn closure_sig() -> impl Sized { //~ ERROR | ||
| ^^^^^^^^^^ resolves to self-referential type | ||
| | ||
= note: resolved type is `[closure@$DIR/recursive-impl-trait-type.rs:41:5: 41:21]` | ||
|
||
error: recursive opaque type | ||
--> $DIR/recursive-impl-trait-type.rs:44:23 | ||
| | ||
LL | fn generator_sig() -> impl Sized { //~ ERROR | ||
| ^^^^^^^^^^ resolves to self-referential type | ||
| | ||
= note: resolved type is `[closure@$DIR/recursive-impl-trait-type.rs:45:5: 45:23]` | ||
|
||
error: recursive opaque type | ||
--> $DIR/recursive-impl-trait-type.rs:48:27 | ||
| | ||
LL | fn generator_capture() -> impl Sized { //~ ERROR | ||
| ^^^^^^^^^^ resolves to self-referential type | ||
| | ||
= note: resolved type is `[generator@$DIR/recursive-impl-trait-type.rs:50:5: 50:26 x:impl Sized {()}]` | ||
|
||
error: recursive opaque type | ||
--> $DIR/recursive-impl-trait-type.rs:53:26 | ||
| | ||
LL | fn substs_change<T>() -> impl Sized { //~ ERROR | ||
| ^^^^^^^^^^ resolves to self-referential type | ||
| | ||
= note: resolved type is `(impl Sized,)` | ||
|
||
error: recursive opaque type | ||
--> $DIR/recursive-impl-trait-type.rs:57:24 | ||
| | ||
LL | fn generator_hold() -> impl Sized { //~ ERROR | ||
| ^^^^^^^^^^ resolves to self-referential type | ||
| | ||
= note: resolved type is `[generator@$DIR/recursive-impl-trait-type.rs:58:5: 62:6 {impl Sized, ()}]` | ||
|
||
error: recursive opaque type | ||
--> $DIR/recursive-impl-trait-type.rs:65:40 | ||
| | ||
LL | async fn recursive_async_function() -> () { //~ ERROR | ||
| ^^ resolves to self-referential type | ||
| | ||
= note: resolved type is `std::future::GenFuture<[static generator@$DIR/recursive-impl-trait-type.rs:65:43: 67:2 {impl std::future::Future, ()}]>` | ||
|
||
error: recursive opaque type | ||
--> $DIR/recursive-impl-trait-type.rs:73:26 | ||
| | ||
LL | fn mutual_recursion() -> impl Sync { //~ ERROR | ||
| ^^^^^^^^^ resolves to self-referential type | ||
| | ||
= note: type resolves to itself | ||
|
||
error: recursive opaque type | ||
--> $DIR/recursive-impl-trait-type.rs:77:28 | ||
| | ||
LL | fn mutual_recursion_b() -> impl Sized { //~ ERROR | ||
| ^^^^^^^^^^ resolves to self-referential type | ||
| | ||
= note: type resolves to itself | ||
|
||
error: aborting due to 15 previous errors | ||
|