-
Notifications
You must be signed in to change notification settings - Fork 34
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
Implement unsafe trait support (continued) #155
Merged
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
570e95f
implement `unsafe trait` support
yoshuawuyts c86992d
add rustfmt to toolchain components
lqd 8ec6589
fix spaces breaking rustfmt and format everything
lqd c9534a0
elide defaulted fields in debug output
nikomatsakis 0e5464c
pretty-print `Safety`
lqd 866b4da
match trait safety errors from rustc
lqd f562ada
add decl safety tests
lqd 72062fc
slight `check_trait_impl` cleanup
lqd e03258c
implement safety check for negative impls
lqd 4d615b1
add tests for safe/unsafe negative impls
lqd 53d0e43
add documentation to `Safety`
lqd 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,10 +17,8 @@ use formality_types::{ | |
}; | ||
|
||
impl super::Check<'_> { | ||
#[context("check_trait_impl({v:?})")] | ||
pub(super) fn check_trait_impl(&self, v: &TraitImpl) -> Fallible<()> { | ||
let TraitImpl { binder, safety } = v; | ||
|
||
#[context("check_trait_impl({trait_impl:?})")] | ||
pub(super) fn check_trait_impl(&self, trait_impl: &TraitImpl) -> Fallible<()> { | ||
let mut env = Env::default(); | ||
|
||
let TraitImplBoundData { | ||
|
@@ -29,7 +27,7 @@ impl super::Check<'_> { | |
trait_parameters, | ||
where_clauses, | ||
impl_items, | ||
} = env.instantiate_universally(binder); | ||
} = env.instantiate_universally(&trait_impl.binder); | ||
|
||
let trait_ref = trait_id.with(self_ty, trait_parameters); | ||
|
||
|
@@ -45,7 +43,7 @@ impl super::Check<'_> { | |
trait_items, | ||
} = trait_decl.binder.instantiate_with(&trait_ref.parameters)?; | ||
|
||
self.check_safety_matches(&trait_decl, safety)?; | ||
self.check_safety_matches(&trait_decl, &trait_impl)?; | ||
|
||
for impl_item in &impl_items { | ||
self.check_trait_impl_item(&env, &where_clauses, &trait_items, impl_item)?; | ||
|
@@ -74,8 +72,8 @@ impl super::Check<'_> { | |
} | ||
|
||
/// Validate that the declared safety of an impl matches the one from the trait declaration. | ||
fn check_safety_matches(&self, trait_decl: &Trait, trait_impl: &Safety) -> Fallible<()> { | ||
if trait_decl.safety != *trait_impl { | ||
fn check_safety_matches(&self, trait_decl: &Trait, trait_impl: &TraitImpl) -> Fallible<()> { | ||
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. ...and I like this version of the fn better |
||
if trait_decl.safety != trait_impl.safety { | ||
match trait_decl.safety { | ||
Safety::Safe => bail!("implementing the trait `{:?}` is not unsafe", trait_decl.id), | ||
Safety::Unsafe => bail!( | ||
|
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.
JFYI I use struct patterns a lot as a deliberate style choice -- I want compilation errors when/if add'l fields are added to the struct -- but in this case it doesn't seem very critical.
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.
It's more for the result when opening
binder
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.
Make sense. I would add this back in this PR but it's already merged :) I'll open another PR.
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.
Even if it's not critical per se, I've re-introduced it in #156 here for consistency and easier evolvability, as well as to the negative impl counterpart.