Skip to content

Commit 3706595

Browse files
authored
Merge pull request #20770 from ChayimFriedman2/unsafe-impl-unresolved
fix: Ignore impl trait safety errors when the trait is unresolved
2 parents 3f6d9e2 + 572e703 commit 3706595

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

crates/hir/src/lib.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ impl Module {
754754
GenericDef::Impl(impl_def).diagnostics(db, acc);
755755

756756
let loc = impl_def.id.lookup(db);
757-
let source_map = db.impl_signature_with_source_map(impl_def.id).1;
757+
let (impl_signature, source_map) = db.impl_signature_with_source_map(impl_def.id);
758758
expr_store_diagnostics(db, acc, &source_map);
759759

760760
let file_id = loc.id.file_id;
@@ -783,10 +783,17 @@ impl Module {
783783
}
784784

785785
let trait_ = impl_def.trait_(db);
786-
let trait_is_unsafe = trait_.is_some_and(|t| t.is_unsafe(db));
786+
let mut trait_is_unsafe = trait_.is_some_and(|t| t.is_unsafe(db));
787787
let impl_is_negative = impl_def.is_negative(db);
788788
let impl_is_unsafe = impl_def.is_unsafe(db);
789789

790+
let trait_is_unresolved = trait_.is_none() && impl_signature.target_trait.is_some();
791+
if trait_is_unresolved {
792+
// Ignore trait safety errors when the trait is unresolved, as otherwise we'll treat it as safe,
793+
// which may not be correct.
794+
trait_is_unsafe = impl_is_unsafe;
795+
}
796+
790797
let drop_maybe_dangle = (|| {
791798
// FIXME: This can be simplified a lot by exposing hir-ty's utils.rs::Generics helper
792799
let trait_ = trait_?;

crates/ide-diagnostics/src/handlers/trait_impl_incorrect_safety.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,13 @@ struct S;
127127
"#,
128128
);
129129
}
130+
131+
#[test]
132+
fn unsafe_unresolved_trait() {
133+
check_diagnostics(
134+
r#"
135+
unsafe impl TestTrait for u32 {}
136+
"#,
137+
);
138+
}
130139
}

0 commit comments

Comments
 (0)