-
Notifications
You must be signed in to change notification settings - Fork 182
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
Add implied bounds #56
Closed
+160
−84
Closed
Changes from all commits
Commits
Show all changes
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,10 +131,11 @@ fn prove_forall() { | |
trait Marker { } | ||
impl<T> Marker for Vec<T> { } | ||
|
||
trait Clone { } | ||
impl Clone for Foo { } | ||
trait Eq<T> { } | ||
trait Ord<T> where Self: Eq<T> { } | ||
|
||
impl<T> Clone for Vec<T> where T: Clone { } | ||
impl<T> Eq<Vec<T>> for Vec<T> where T: Eq<T> { } | ||
impl<T> Ord<Vec<T>> for Vec<T> where T: Ord<T> { } | ||
} | ||
|
||
goal { | ||
|
@@ -164,36 +165,46 @@ fn prove_forall() { | |
"Unique; substitution [], lifetime constraints []" | ||
} | ||
|
||
// Here, we don't know that `T: Clone`, so we can't prove that | ||
// `Vec<T>: Clone`. | ||
// Here, we don't know that `T: Eq<T>`, so we can't prove that | ||
// `Vec<T>: Eq<Vec<T>>`. | ||
goal { | ||
forall<T> { Vec<T>: Clone } | ||
forall<T> { Vec<T>: Eq<Vec<T>> } | ||
} yields { | ||
"CannotProve" | ||
"No possible solution" | ||
} | ||
|
||
// Here, we do know that `T: Clone`, so we can. | ||
// Here, we do know that `T: Eq`, so we can. | ||
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. Nit: |
||
goal { | ||
forall<T> { | ||
if (T: Clone) { | ||
Vec<T>: Clone | ||
if (T: Eq<T>) { | ||
Vec<T>: Eq<Vec<T>> | ||
} | ||
} | ||
} yields { | ||
"Unique; substitution [], lifetime constraints []" | ||
} | ||
|
||
// This fails because we used `if_raw`, and hence we do not | ||
// know that `WF(T: Clone)` holds. | ||
// know that `WF(T: Ord<T>)` hence `T: Eq<T>` holds. | ||
goal { | ||
forall<T> { | ||
if_raw (T: Clone) { | ||
Vec<T>: Clone | ||
if_raw (T: Ord<T>) { | ||
Vec<T>: Ord<Vec<T>> | ||
} | ||
} | ||
} yields { | ||
"CannotProve" | ||
} | ||
|
||
goal { | ||
forall<T> { | ||
if (T: Ord<T>) { | ||
Vec<T>: Ord<Vec<T>> | ||
} | ||
} | ||
} yields { | ||
"Unique" | ||
} | ||
} | ||
} | ||
|
||
|
@@ -1539,10 +1550,9 @@ fn coinductive_semantics() { | |
"CannotProve" | ||
} | ||
|
||
// `WellFormed(T)` because of the hand-written impl for `Ptr<T>`. | ||
goal { | ||
forall<T> { | ||
if (WellFormed(T), T: Send) { | ||
if (T: Send) { | ||
List<T>: Send | ||
} | ||
} | ||
|
@@ -1596,3 +1606,66 @@ fn mixed_semantics() { | |
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn implied_bounds() { | ||
test! { | ||
program { | ||
trait Hash { } | ||
struct Set<K> where K: Hash { } | ||
|
||
struct NotHash<T> { } | ||
|
||
struct i32 { } | ||
impl Hash for i32 { } | ||
|
||
trait Eq<T> { } | ||
trait Ord<T> where Self: Eq<T> { } | ||
|
||
struct Ordered<U> where U: Ord<U> { } | ||
} | ||
|
||
// We know that `Set<K>` is well-formed so `K` must implement `Hash`. | ||
goal { | ||
forall<K> { | ||
if (WellFormed(Set<K>)) { | ||
K: Hash | ||
} | ||
} | ||
} yields { | ||
"Unique" | ||
} | ||
|
||
// The following function: | ||
// ``` | ||
// fn foo<T>(arg: Set<NotHash<T>>) { | ||
// /* assume that WellFormed(Set<NotHash<T>>) inside here */ | ||
// } | ||
// ``` | ||
// cannot be called whatever the value of `T` is because | ||
// there is no `Hash` impl for `NotHash<T>` hence `Set<NotHash<T>>` | ||
// cannot be well-formed. | ||
// | ||
// Since `NotHash<T>` is a local type, a local negative | ||
// reasoning is allowed and the following query fails. We can then issue | ||
// a warning saying that the function cannot be called. | ||
goal { | ||
exists<T> { | ||
WellFormed(Set<NotHash<T>>) | ||
} | ||
} yields { | ||
"No possible solution" | ||
} | ||
|
||
// Transitive implied bounds | ||
goal { | ||
forall<U> { | ||
if (WellFormed(Ordered<U>)) { | ||
U: Eq<U> | ||
} | ||
} | ||
} yields { | ||
"Unique" | ||
} | ||
} | ||
} |
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 definitely prefer this to the macro, but I'd still like some comments. =)