Skip to content
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 11 commits into from
Nov 12, 2023

Conversation

lqd
Copy link
Member

@lqd lqd commented Nov 11, 2023

This PR supersedes #128:

  • rebases most of @yoshuawuyts's work over Niko's recent parser changes
  • also integrates Niko's pretty-printer fix on optional fields, that way the spurious separators present in the previous PR are now gone
  • matches rustc's error messages on safety mismatches
  • adds E0198 check for negative impls
  • adds a bunch of tests for the different combinations of safe/unsafe trait, safe/unsafe/negative impl
  • optional: also adds rustfmt as a required component in the toolchain, just so it's easier to format things. And with that, removes some formatting weirdness that break rustfmt, and formats stray rogue files. I can remove these commits from the PR if you prefer not to have them.

#[term]
#[derive(Default)]
pub enum Safety {
#[default]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know you could do this, nifty.

#[derive(Default)]
pub enum Safety {
#[default]
Safe,
Unsafe,
}

impl fmt::Debug for Safety {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this the default behavior that #[term] would generate?

Copy link
Member Author

@lqd lqd Nov 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I didn't know this, or test that it would. I'll try, and open a PR to clean this up.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've fixed this in #156

@@ -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;
Copy link
Contributor

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.

Copy link
Contributor

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

Copy link
Member Author

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.

Copy link
Member Author

@lqd lqd Nov 12, 2023

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.

@@ -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<()> {
Copy link
Contributor

@nikomatsakis nikomatsakis Nov 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...and I like this version of the fn better

@nikomatsakis nikomatsakis merged commit ddbb916 into rust-lang:main Nov 12, 2023
3 checks passed
@lqd lqd deleted the unsafe-traits branch November 12, 2023 11:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants