-
Notifications
You must be signed in to change notification settings - Fork 13.3k
do not unnecessarily leak auto traits in item bounds #139789
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
+285
−7
Merged
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
33 changes: 33 additions & 0 deletions
33
tests/ui/impl-trait/auto-trait-leakage/avoid-query-cycle-via-item-bound.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,33 @@ | ||
//@ check-pass | ||
//@ revisions: current next | ||
//@[next] compile-flags: -Znext-solver | ||
//@ ignore-compare-mode-next-solver (explicit revisions) | ||
|
||
// When proving auto trait bounds, make sure that we depend on auto trait | ||
// leakage if we can also prove it via an item bound. | ||
fn is_send<T: Send>(_: T) {} | ||
|
||
fn direct() -> impl Send { | ||
is_send(check(false)); // leaks auto traits, depends on `check` | ||
1u16 | ||
} | ||
|
||
trait Indir: Send {} | ||
impl Indir for u32 {} | ||
fn indir() -> impl Indir { | ||
is_send(check(false)); // leaks auto traits, depends on `check` | ||
1u32 | ||
} | ||
|
||
fn check(b: bool) -> impl Sized { | ||
if b { | ||
// must not leak auto traits, as we otherwise get a query cycle. | ||
is_send(direct()); | ||
is_send(indir()); | ||
} | ||
1u64 | ||
} | ||
|
||
fn main() { | ||
check(true); | ||
} |
58 changes: 58 additions & 0 deletions
58
tests/ui/impl-trait/in-trait/method-compatability-via-leakage-cycle.current.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,58 @@ | ||
error[E0391]: cycle detected when computing type of `<impl at $DIR/method-compatability-via-leakage-cycle.rs:17:1: 17:19>::{anon_assoc#0}` | ||
--> $DIR/method-compatability-via-leakage-cycle.rs:21:24 | ||
| | ||
LL | fn foo(b: bool) -> impl Sized { | ||
| ^^^^^^^^^^ | ||
| | ||
note: ...which requires comparing an impl and trait method signature, inferring any hidden `impl Trait` types in the process... | ||
--> $DIR/method-compatability-via-leakage-cycle.rs:21:24 | ||
| | ||
LL | fn foo(b: bool) -> impl Sized { | ||
| ^^^^^^^^^^ | ||
= note: ...which requires evaluating trait selection obligation `<impl at $DIR/method-compatability-via-leakage-cycle.rs:17:1: 17:19>::foo::{opaque#0}: core::marker::Send`... | ||
note: ...which requires computing type of opaque `<impl at $DIR/method-compatability-via-leakage-cycle.rs:17:1: 17:19>::foo::{opaque#0}`... | ||
--> $DIR/method-compatability-via-leakage-cycle.rs:21:24 | ||
| | ||
LL | fn foo(b: bool) -> impl Sized { | ||
| ^^^^^^^^^^ | ||
note: ...which requires borrow-checking `<impl at $DIR/method-compatability-via-leakage-cycle.rs:17:1: 17:19>::foo`... | ||
--> $DIR/method-compatability-via-leakage-cycle.rs:21:5 | ||
| | ||
LL | fn foo(b: bool) -> impl Sized { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
note: ...which requires promoting constants in MIR for `<impl at $DIR/method-compatability-via-leakage-cycle.rs:17:1: 17:19>::foo`... | ||
--> $DIR/method-compatability-via-leakage-cycle.rs:21:5 | ||
| | ||
LL | fn foo(b: bool) -> impl Sized { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
note: ...which requires checking if `<impl at $DIR/method-compatability-via-leakage-cycle.rs:17:1: 17:19>::foo` contains FFI-unwind calls... | ||
--> $DIR/method-compatability-via-leakage-cycle.rs:21:5 | ||
| | ||
LL | fn foo(b: bool) -> impl Sized { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
note: ...which requires building MIR for `<impl at $DIR/method-compatability-via-leakage-cycle.rs:17:1: 17:19>::foo`... | ||
--> $DIR/method-compatability-via-leakage-cycle.rs:21:5 | ||
| | ||
LL | fn foo(b: bool) -> impl Sized { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
note: ...which requires match-checking `<impl at $DIR/method-compatability-via-leakage-cycle.rs:17:1: 17:19>::foo`... | ||
--> $DIR/method-compatability-via-leakage-cycle.rs:21:5 | ||
| | ||
LL | fn foo(b: bool) -> impl Sized { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
note: ...which requires type-checking `<impl at $DIR/method-compatability-via-leakage-cycle.rs:17:1: 17:19>::foo`... | ||
--> $DIR/method-compatability-via-leakage-cycle.rs:21:5 | ||
| | ||
LL | fn foo(b: bool) -> impl Sized { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
= note: ...which again requires computing type of `<impl at $DIR/method-compatability-via-leakage-cycle.rs:17:1: 17:19>::{anon_assoc#0}`, completing the cycle | ||
note: cycle used when checking that `<impl at $DIR/method-compatability-via-leakage-cycle.rs:17:1: 17:19>` is well-formed | ||
--> $DIR/method-compatability-via-leakage-cycle.rs:17:1 | ||
| | ||
LL | impl Trait for u32 { | ||
| ^^^^^^^^^^^^^^^^^^ | ||
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0391`. |
122 changes: 122 additions & 0 deletions
122
tests/ui/impl-trait/in-trait/method-compatability-via-leakage-cycle.next.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[E0391]: cycle detected when computing type of `<impl at $DIR/method-compatability-via-leakage-cycle.rs:17:1: 17:19>::{anon_assoc#0}` | ||
--> $DIR/method-compatability-via-leakage-cycle.rs:21:24 | ||
| | ||
LL | fn foo(b: bool) -> impl Sized { | ||
| ^^^^^^^^^^ | ||
| | ||
note: ...which requires comparing an impl and trait method signature, inferring any hidden `impl Trait` types in the process... | ||
--> $DIR/method-compatability-via-leakage-cycle.rs:21:5 | ||
| | ||
LL | fn foo(b: bool) -> impl Sized { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
note: ...which requires computing type of `<impl at $DIR/method-compatability-via-leakage-cycle.rs:17:1: 17:19>::foo::{opaque#0}`... | ||
--> $DIR/method-compatability-via-leakage-cycle.rs:21:24 | ||
| | ||
LL | fn foo(b: bool) -> impl Sized { | ||
| ^^^^^^^^^^ | ||
note: ...which requires computing type of opaque `<impl at $DIR/method-compatability-via-leakage-cycle.rs:17:1: 17:19>::foo::{opaque#0}`... | ||
--> $DIR/method-compatability-via-leakage-cycle.rs:21:24 | ||
| | ||
LL | fn foo(b: bool) -> impl Sized { | ||
| ^^^^^^^^^^ | ||
note: ...which requires borrow-checking `<impl at $DIR/method-compatability-via-leakage-cycle.rs:17:1: 17:19>::foo`... | ||
--> $DIR/method-compatability-via-leakage-cycle.rs:21:5 | ||
| | ||
LL | fn foo(b: bool) -> impl Sized { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
note: ...which requires promoting constants in MIR for `<impl at $DIR/method-compatability-via-leakage-cycle.rs:17:1: 17:19>::foo`... | ||
--> $DIR/method-compatability-via-leakage-cycle.rs:21:5 | ||
| | ||
LL | fn foo(b: bool) -> impl Sized { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
note: ...which requires checking if `<impl at $DIR/method-compatability-via-leakage-cycle.rs:17:1: 17:19>::foo` contains FFI-unwind calls... | ||
--> $DIR/method-compatability-via-leakage-cycle.rs:21:5 | ||
| | ||
LL | fn foo(b: bool) -> impl Sized { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
note: ...which requires building MIR for `<impl at $DIR/method-compatability-via-leakage-cycle.rs:17:1: 17:19>::foo`... | ||
--> $DIR/method-compatability-via-leakage-cycle.rs:21:5 | ||
| | ||
LL | fn foo(b: bool) -> impl Sized { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
note: ...which requires match-checking `<impl at $DIR/method-compatability-via-leakage-cycle.rs:17:1: 17:19>::foo`... | ||
--> $DIR/method-compatability-via-leakage-cycle.rs:21:5 | ||
| | ||
LL | fn foo(b: bool) -> impl Sized { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
note: ...which requires type-checking `<impl at $DIR/method-compatability-via-leakage-cycle.rs:17:1: 17:19>::foo`... | ||
--> $DIR/method-compatability-via-leakage-cycle.rs:21:5 | ||
| | ||
LL | fn foo(b: bool) -> impl Sized { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
= note: ...which again requires computing type of `<impl at $DIR/method-compatability-via-leakage-cycle.rs:17:1: 17:19>::{anon_assoc#0}`, completing the cycle | ||
note: cycle used when checking that `<impl at $DIR/method-compatability-via-leakage-cycle.rs:17:1: 17:19>` is well-formed | ||
--> $DIR/method-compatability-via-leakage-cycle.rs:17:1 | ||
| | ||
LL | impl Trait for u32 { | ||
| ^^^^^^^^^^^^^^^^^^ | ||
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information | ||
|
||
error[E0391]: cycle detected when computing type of `<impl at $DIR/method-compatability-via-leakage-cycle.rs:17:1: 17:19>::{anon_assoc#0}` | ||
--> $DIR/method-compatability-via-leakage-cycle.rs:21:24 | ||
| | ||
LL | fn foo(b: bool) -> impl Sized { | ||
| ^^^^^^^^^^ | ||
| | ||
note: ...which requires comparing an impl and trait method signature, inferring any hidden `impl Trait` types in the process... | ||
--> $DIR/method-compatability-via-leakage-cycle.rs:21:5 | ||
| | ||
LL | fn foo(b: bool) -> impl Sized { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
note: ...which requires computing type of `<impl at $DIR/method-compatability-via-leakage-cycle.rs:17:1: 17:19>::foo::{opaque#0}`... | ||
--> $DIR/method-compatability-via-leakage-cycle.rs:21:24 | ||
| | ||
LL | fn foo(b: bool) -> impl Sized { | ||
| ^^^^^^^^^^ | ||
note: ...which requires computing type of opaque `<impl at $DIR/method-compatability-via-leakage-cycle.rs:17:1: 17:19>::foo::{opaque#0}`... | ||
--> $DIR/method-compatability-via-leakage-cycle.rs:21:24 | ||
| | ||
LL | fn foo(b: bool) -> impl Sized { | ||
| ^^^^^^^^^^ | ||
note: ...which requires borrow-checking `<impl at $DIR/method-compatability-via-leakage-cycle.rs:17:1: 17:19>::foo`... | ||
--> $DIR/method-compatability-via-leakage-cycle.rs:21:5 | ||
| | ||
LL | fn foo(b: bool) -> impl Sized { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
note: ...which requires promoting constants in MIR for `<impl at $DIR/method-compatability-via-leakage-cycle.rs:17:1: 17:19>::foo`... | ||
--> $DIR/method-compatability-via-leakage-cycle.rs:21:5 | ||
| | ||
LL | fn foo(b: bool) -> impl Sized { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
note: ...which requires checking if `<impl at $DIR/method-compatability-via-leakage-cycle.rs:17:1: 17:19>::foo` contains FFI-unwind calls... | ||
--> $DIR/method-compatability-via-leakage-cycle.rs:21:5 | ||
| | ||
LL | fn foo(b: bool) -> impl Sized { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
note: ...which requires building MIR for `<impl at $DIR/method-compatability-via-leakage-cycle.rs:17:1: 17:19>::foo`... | ||
--> $DIR/method-compatability-via-leakage-cycle.rs:21:5 | ||
| | ||
LL | fn foo(b: bool) -> impl Sized { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
note: ...which requires match-checking `<impl at $DIR/method-compatability-via-leakage-cycle.rs:17:1: 17:19>::foo`... | ||
--> $DIR/method-compatability-via-leakage-cycle.rs:21:5 | ||
| | ||
LL | fn foo(b: bool) -> impl Sized { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
note: ...which requires type-checking `<impl at $DIR/method-compatability-via-leakage-cycle.rs:17:1: 17:19>::foo`... | ||
--> $DIR/method-compatability-via-leakage-cycle.rs:21:5 | ||
| | ||
LL | fn foo(b: bool) -> impl Sized { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
= note: ...which again requires computing type of `<impl at $DIR/method-compatability-via-leakage-cycle.rs:17:1: 17:19>::{anon_assoc#0}`, completing the cycle | ||
note: cycle used when checking that `<impl at $DIR/method-compatability-via-leakage-cycle.rs:17:1: 17:19>` is well-formed | ||
--> $DIR/method-compatability-via-leakage-cycle.rs:17:1 | ||
| | ||
LL | impl Trait for u32 { | ||
| ^^^^^^^^^^^^^^^^^^ | ||
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information | ||
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0391`. |
30 changes: 30 additions & 0 deletions
30
tests/ui/impl-trait/in-trait/method-compatability-via-leakage-cycle.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,30 @@ | ||
//@ revisions: current next | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be marked as a known-bug. |
||
//@[next] compile-flags: -Znext-solver | ||
//@ ignore-compare-mode-next-solver (explicit revisions) | ||
//@ known-bug: #139788 | ||
|
||
// Recursively using the trait method inside of an impl in case checking | ||
// method compatability relies on opaque type leakage currently causes a | ||
// cycle error. | ||
|
||
trait Trait { | ||
// desugars to | ||
// type Assoc: Sized + Send; | ||
// fn foo(b: bool) -> Self::Assoc; | ||
fn foo(b: bool) -> impl Sized + Send; | ||
} | ||
|
||
impl Trait for u32 { | ||
// desugars to | ||
// type Assoc = impl_rpit::<Self>; | ||
// fn foo(b: bool) -> Self::Assoc { .. } | ||
fn foo(b: bool) -> impl Sized { | ||
if b { | ||
u32::foo(false) | ||
} else { | ||
1u32 | ||
} | ||
} | ||
} | ||
|
||
fn main() {} |
14 changes: 14 additions & 0 deletions
14
tests/ui/impl-trait/in-trait/method-compatability-via-leakage.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,14 @@ | ||
//@ check-pass | ||
//@ revisions: current next | ||
//@[next] compile-flags: -Znext-solver | ||
//@ ignore-compare-mode-next-solver (explicit revisions) | ||
|
||
trait Trait { | ||
fn foo() -> impl Sized + Send; | ||
} | ||
|
||
impl Trait for u32 { | ||
fn foo() -> impl Sized {} | ||
} | ||
|
||
fn main() {} |
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
Oops, something went wrong.
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.
Maybe find some place to also explicitly mention that this is sound b/c we later separately check that the hidden type implements the RPIT's bounds after opaque type inference.
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.
this feels like a more general question about alias bound candidates of opaques 🤔 so we'd probably have to put it there. To be totally honest, I am not confident that it is fully sound in the first place as long as checking well-formedness for opaque types happens in analysis mode xx
I've kept #109387 in my github notifications for multiple months now