-
Notifications
You must be signed in to change notification settings - Fork 13.4k
prevent opaque types from appearing in impl headers #95973
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7d5bbf5
prevent opaque types from appearing in impl headers
lqd d652cd4
add regression tests for opaque types in impl headers
lqd 734adb3
update ui tests using opaque types in impl headers
lqd 08ef70b
Compute a more precise span for opaque type impls
oli-obk 93a3cfb
Explain the span search logic
oli-obk 6d0349d
Apply suggestions from code review
oli-obk 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
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
16 changes: 9 additions & 7 deletions
16
src/test/ui/impl-trait/recursive-type-alias-impl-trait-declaration-too-subtle-2.stderr
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 |
---|---|---|
@@ -1,12 +1,14 @@ | ||
error[E0277]: can't compare `Bar` with `(Bar, i32)` | ||
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle-2.rs:14:5 | ||
error: cannot implement trait on type alias impl trait | ||
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle-2.rs:7:17 | ||
| | ||
LL | Bar | ||
| ^^^ no implementation for `Bar == (Bar, i32)` | ||
LL | impl PartialEq<(Foo, i32)> for Bar { | ||
| ^^^ | ||
| | ||
= help: the trait `PartialEq<(Bar, i32)>` is not implemented for `Bar` | ||
= help: the trait `PartialEq<(Foo, i32)>` is implemented for `Bar` | ||
note: type alias impl trait defined here | ||
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle-2.rs:3:12 | ||
| | ||
LL | type Foo = impl PartialEq<(Foo, i32)>; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0277`. |
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
25 changes: 7 additions & 18 deletions
25
src/test/ui/impl-trait/recursive-type-alias-impl-trait-declaration-too-subtle.stderr
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 |
---|---|---|
@@ -1,25 +1,14 @@ | ||
error: unconstrained opaque type | ||
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle.rs:4:16 | ||
error: cannot implement trait on type alias impl trait | ||
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle.rs:20:21 | ||
| | ||
LL | type Foo = impl PartialEq<(Foo, i32)>; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
LL | impl PartialEq<(Foo, i32)> for Bar { | ||
| ^^^ | ||
| | ||
= note: `Foo` must be used in combination with a concrete type within the same module | ||
|
||
error: unconstrained opaque type | ||
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle.rs:17:16 | ||
note: type alias impl trait defined here | ||
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle.rs:16:16 | ||
| | ||
LL | type Foo = impl PartialEq<(Foo, i32)>; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `Foo` must be used in combination with a concrete type within the same module | ||
|
||
error[E0276]: impl has stricter requirements than trait | ||
--> $DIR/recursive-type-alias-impl-trait-declaration-too-subtle.rs:23:9 | ||
| | ||
LL | fn eq(&self, _other: &(Bar, i32)) -> bool { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl has extra requirement `b::Bar: PartialEq<(b::Bar, i32)>` | ||
|
||
error: aborting due to 3 previous errors | ||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0276`. |
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
4 changes: 2 additions & 2 deletions
4
src/test/ui/type-alias-impl-trait/issue-76202-trait-impl-for-tait.stderr
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
23 changes: 23 additions & 0 deletions
23
src/test/ui/type-alias-impl-trait/issue-84660-trait-impl-for-tait.rs
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Regression test for issues #84660 and #86411: both are variations on #76202. | ||
// Tests that we don't ICE when we have an opaque type appearing anywhere in an impl header. | ||
|
||
#![feature(type_alias_impl_trait)] | ||
|
||
trait Foo {} | ||
impl Foo for () {} | ||
type Bar = impl Foo; | ||
fn _defining_use() -> Bar {} | ||
|
||
trait TraitArg<T> { | ||
fn f(); | ||
} | ||
|
||
impl TraitArg<Bar> for () { //~ ERROR cannot implement trait | ||
fn f() { | ||
println!("ho"); | ||
} | ||
} | ||
|
||
fn main() { | ||
<() as TraitArg<Bar>>::f(); | ||
} |
14 changes: 14 additions & 0 deletions
14
src/test/ui/type-alias-impl-trait/issue-84660-trait-impl-for-tait.stderr
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
error: cannot implement trait on type alias impl trait | ||
--> $DIR/issue-84660-trait-impl-for-tait.rs:15:15 | ||
| | ||
LL | impl TraitArg<Bar> for () { | ||
| ^^^ | ||
| | ||
note: type alias impl trait defined here | ||
--> $DIR/issue-84660-trait-impl-for-tait.rs:8:12 | ||
| | ||
LL | type Bar = impl Foo; | ||
| ^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
41 changes: 41 additions & 0 deletions
41
src/test/ui/type-alias-impl-trait/issue-84660-unsoundness.rs
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Another example from issue #84660, this time weaponized as a safe transmut: an opaque type in an | ||
oli-obk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// impl header being accepted was used to create unsoundness. | ||
|
||
#![feature(type_alias_impl_trait)] | ||
|
||
trait Foo {} | ||
impl Foo for () {} | ||
type Bar = impl Foo; | ||
fn _defining_use() -> Bar {} | ||
|
||
trait Trait<T, In> { | ||
type Out; | ||
fn convert(i: In) -> Self::Out; | ||
} | ||
|
||
impl<In, Out> Trait<Bar, In> for Out { //~ ERROR cannot implement trait | ||
type Out = Out; | ||
fn convert(_i: In) -> Self::Out { | ||
unreachable!(); | ||
} | ||
} | ||
|
||
impl<In, Out> Trait<(), In> for Out { | ||
type Out = In; | ||
fn convert(i: In) -> Self::Out { | ||
i | ||
} | ||
} | ||
|
||
fn transmute<In, Out>(i: In) -> Out { | ||
<Out as Trait<Bar, In>>::convert(i) | ||
} | ||
|
||
fn main() { | ||
let d; | ||
{ | ||
let x = "Hello World".to_string(); | ||
d = transmute::<&String, &String>(&x); | ||
} | ||
println!("{}", d); | ||
} |
14 changes: 14 additions & 0 deletions
14
src/test/ui/type-alias-impl-trait/issue-84660-unsoundness.stderr
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
error: cannot implement trait on type alias impl trait | ||
--> $DIR/issue-84660-unsoundness.rs:16:21 | ||
| | ||
LL | impl<In, Out> Trait<Bar, In> for Out { | ||
| ^^^ | ||
| | ||
note: type alias impl trait defined here | ||
--> $DIR/issue-84660-unsoundness.rs:8:12 | ||
| | ||
LL | type Bar = impl Foo; | ||
| ^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
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
10 changes: 7 additions & 3 deletions
10
src/test/ui/type-alias-impl-trait/nested-tait-inference3.stderr
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 |
---|---|---|
@@ -1,10 +1,14 @@ | ||
error: unconstrained opaque type | ||
error: cannot implement trait on type alias impl trait | ||
--> $DIR/nested-tait-inference3.rs:10:10 | ||
| | ||
LL | impl Foo<FooX> for () { } | ||
| ^^^^ | ||
| | ||
note: type alias impl trait defined here | ||
--> $DIR/nested-tait-inference3.rs:6:13 | ||
| | ||
LL | type FooX = impl Debug; | ||
| ^^^^^^^^^^ | ||
| | ||
= note: `FooX` must be used in combination with a concrete type within the same module | ||
|
||
error: aborting due to previous error | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.