-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Handle recursion limit for subtype and well-formed predicates #117754
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
+130
−5
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Variant of #117151 when the overflow comes entirely from subtype predicates. | ||
|
||
#![allow(unreachable_code)] | ||
|
||
use std::ptr; | ||
|
||
fn main() { | ||
// Give x and y completely unconstrained types. Using a function call | ||
// or `as` cast would create a well-formed predicate. | ||
let x = return; | ||
let y = return; | ||
let mut w = (x, y); | ||
//~^ ERROR overflow evaluating the requirement | ||
// Avoid creating lifetimes, `Sized` bounds or function calls. | ||
let a = (ptr::addr_of!(y), ptr::addr_of!(x)); | ||
w = a; | ||
} |
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,9 @@ | ||
error[E0275]: overflow evaluating the requirement `_ <: *const _` | ||
--> $DIR/subtype-recursion-limit.rs:12:17 | ||
| | ||
LL | let mut w = (x, y); | ||
| ^^^^^^ | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0275`. |
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,27 @@ | ||
// Regression test for #117151, this used to hang the compiler | ||
|
||
pub type ISO<A: 'static, B: 'static> = (Box<dyn Fn(A) -> B>, Box<dyn Fn(B) -> A>); | ||
pub fn iso<A: 'static, B: 'static, F1, F2>(a: F1, b: F2) -> ISO<A, B> | ||
where | ||
F1: 'static + Fn(A) -> B, | ||
F2: 'static + Fn(B) -> A, | ||
{ | ||
(Box::new(a), Box::new(b)) | ||
} | ||
pub fn iso_un_option<A: 'static, B: 'static>(i: ISO<Option<A>, Option<B>>) -> ISO<A, B> { | ||
let (ab, ba) = (i.ab, i.ba); | ||
//~^ ERROR no field `ab` on type | ||
//~| ERROR no field `ba` on type | ||
let left = move |o_a| match o_a { | ||
//~^ ERROR overflow evaluating the requirement | ||
None => panic!("absured"), | ||
Some(a) => a, | ||
}; | ||
let right = move |o_b| match o_b { | ||
None => panic!("absurd"), | ||
Some(b) => b, | ||
}; | ||
iso(left, right) | ||
} | ||
|
||
fn main() {} |
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,22 @@ | ||
error[E0609]: no field `ab` on type `(Box<(dyn Fn(Option<A>) -> Option<B> + 'static)>, Box<(dyn Fn(Option<B>) -> Option<A> + 'static)>)` | ||
--> $DIR/well-formed-recursion-limit.rs:12:23 | ||
| | ||
LL | let (ab, ba) = (i.ab, i.ba); | ||
| ^^ unknown field | ||
|
||
error[E0609]: no field `ba` on type `(Box<(dyn Fn(Option<A>) -> Option<B> + 'static)>, Box<(dyn Fn(Option<B>) -> Option<A> + 'static)>)` | ||
--> $DIR/well-formed-recursion-limit.rs:12:29 | ||
| | ||
LL | let (ab, ba) = (i.ab, i.ba); | ||
| ^^ unknown field | ||
|
||
error[E0275]: overflow evaluating the requirement `_ <: Option<_>` | ||
--> $DIR/well-formed-recursion-limit.rs:15:33 | ||
| | ||
LL | let left = move |o_a| match o_a { | ||
| ^^^ | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
Some errors have detailed explanations: E0275, E0609. | ||
For more information about an error, try `rustc --explain E0275`. |
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,25 @@ | ||
// Test for specific details of how we handle higher-ranked subtyping to make | ||
// sure that any changes are made deliberately. | ||
// | ||
// - `let y = x` creates a `Subtype` obligation that is deferred for later. | ||
// - `w = a` sets the type of `x` to `Option<for<'a> fn(&'a ())>` and generalizes | ||
// `z` first to `Option<_>` and then to `Option<fn(&'0 ())>`. | ||
// - The various subtyping obligations are then processed. | ||
// | ||
// This requires that | ||
// 1. the `Subtype` obligation from `y = x` isn't processed while the types of | ||
// `w` and `a` are being unified. | ||
// 2. the pending subtype obligation isn't considered when determining the type | ||
// to generalize `z` to first (when related to the type of `y`). | ||
// | ||
// Found when considering fixes to #117151 | ||
// check-pass | ||
|
||
fn main() { | ||
let mut x = None; | ||
let y = x; | ||
let z = Default::default(); | ||
let mut w = (&mut x, z, z); | ||
let a = (&mut None::<fn(&())>, y, None::<fn(&'static ())>); | ||
w = a; | ||
} |
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.
This test feels a bit :/ currently the way we generalize higher ranked types is wrong when subtyping.
we currently do the following (using
x
for the infer var inx: None<_>
):Subtype(x <: y)
obligationw = a
assignment setsx
tofor<'a> fn(&'a ())
, emits aSubtype(y <: z)
obligation and generalizesz
tofn(&'0 ())
.Subtype(x <: y)
settingy
tofor<'a> fn(&'a ())
and then proveSubtype(y <: z)
which is now trivial as both types are rigid.If we were to prove the
Subtype
obligations before generalizingz
we'd get an incorrect error, e.g:It feels like this test mostly tests a very specific impl detail of our subtyping algorithm where we do not try to eagerly make progress on previous
Subtype
obligations while relating.I think the explanation here is somewhat misleading. The reason this works is because generalization does not care about subtype relations (apart from the occurs check). I would instead use sth like "Subtyping of inference variables is currently handled via delayed
Subtype
obligations. We do not immediately process them after constraining one of the involved inference variables. We'd otherwise incorrectly constrain the type ofz
as generalization does not handle binders correctly".