Skip to content

Commit 866b4da

Browse files
committed
match trait safety errors from rustc
1 parent 0e5464c commit 866b4da

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

crates/formality-check/src/impls.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use formality_prove::{Env, Safety};
66
use formality_rust::{
77
grammar::{
88
AssociatedTy, AssociatedTyBoundData, AssociatedTyValue, AssociatedTyValueBoundData, Fn,
9-
FnBoundData, ImplItem, NegTraitImpl, NegTraitImplBoundData, TraitBoundData, TraitImpl,
10-
TraitImplBoundData, TraitItem,
9+
FnBoundData, ImplItem, NegTraitImpl, NegTraitImplBoundData, Trait, TraitBoundData,
10+
TraitImpl, TraitImplBoundData, TraitItem,
1111
},
1212
prove::ToWcs,
1313
};
@@ -45,7 +45,7 @@ impl super::Check<'_> {
4545
trait_items,
4646
} = trait_decl.binder.instantiate_with(&trait_ref.parameters)?;
4747

48-
self.check_safety_matches(&trait_decl.safety, safety)?;
48+
self.check_safety_matches(&trait_decl, safety)?;
4949

5050
for impl_item in &impl_items {
5151
self.check_trait_impl_item(&env, &where_clauses, &trait_items, impl_item)?;
@@ -73,17 +73,16 @@ impl super::Check<'_> {
7373
Ok(())
7474
}
7575

76-
/// Validate `unsafe trait` and `unsafe impl` line up
77-
fn check_safety_matches(&self, trait_decl: &Safety, trait_impl: &Safety) -> Fallible<()> {
78-
match trait_decl {
79-
Safety::Safe => anyhow::ensure!(
80-
matches!(trait_impl, Safety::Safe),
81-
"implementing the trait is not `unsafe`"
82-
),
83-
Safety::Unsafe => anyhow::ensure!(
84-
matches!(trait_impl, Safety::Unsafe),
85-
"the trait requires an `unsafe impl` declaration"
86-
),
76+
/// Validate that the declared safety of an impl matches the one from the trait declaration.
77+
fn check_safety_matches(&self, trait_decl: &Trait, trait_impl: &Safety) -> Fallible<()> {
78+
if trait_decl.safety != *trait_impl {
79+
match trait_decl.safety {
80+
Safety::Safe => bail!("implementing the trait `{:?}` is not unsafe", trait_decl.id),
81+
Safety::Unsafe => bail!(
82+
"the trait `{:?}` requires an `unsafe impl` declaration",
83+
trait_decl.id
84+
),
85+
}
8786
}
8887
Ok(())
8988
}

0 commit comments

Comments
 (0)