-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Prevent opaque types in impl headers #87382
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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
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
23 changes: 23 additions & 0 deletions
23
src/test/ui/type-alias-impl-trait/issue-84660-trait-impl-for-tait.rs
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,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(min_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 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:1 | ||
| | ||
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 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 | ||
// impl header being accepted was used to create unsoundness. | ||
|
||
#![feature(min_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 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:1 | ||
| | ||
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 | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have an idea that could help with this span, not sure if it pans out. Create a new visitor struct. Implement intravisit::Visitor for it, most notably implement the Visitor::visit_qpath method and use qpath_res to get the DefId. If it is equal to the opaque type's use that span. If no span is found during visiting, fall back to the one you have right now.
First visit the
self_ty
variable, and if no span is found, visit thetr
variable.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC
qpath_res
doesn't work here since we're not in anExpr
orPat
node, but the qpath should be resolved so we can extract its res anyway. However, when visiting theself_ty
, the qpath's def_id is not the same as the opaque type's here. (It's almost as if the visitor is seeing the type alias node, while the TypeFolder sees the opaque type node)