Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,48 @@ impl Module {
}
}

// HACK: When specialization is enabled in the current crate, and there exists
// *any* blanket impl that provides a default implementation for the missing item,
// suppress the missing associated item diagnostic.
// This can lead to false negatives when the impl in question does not actually
// specialize that blanket impl, but determining the exact specialization
// relationship here would be significantly more expensive.
if !missing.is_empty() {
let krate = self.krate(db).id;
let def_map = crate_def_map(db, krate);
if def_map.is_unstable_feature_enabled(&sym::specialization)
|| def_map.is_unstable_feature_enabled(&sym::min_specialization)
{
missing.retain(|(assoc_name, assoc_item)| {
let AssocItem::Function(_) = assoc_item else {
Copy link
Contributor

Choose a reason for hiding this comment

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

Technically the full specialization supports specialization of types as well (and also default impl), but I don't think we need to support that.

return true;
};

for &impl_ in TraitImpls::for_crate(db, krate).blanket_impls(trait_.id)
{
if impl_ == impl_id {
continue;
}

for (name, item) in &impl_.impl_items(db).items {
let AssocItemId::FunctionId(fn_) = item else {
continue;
};
if name != assoc_name {
continue;
}

if db.function_signature(*fn_).is_default() {
return false;
}
}
}

true
});
}
}

if !missing.is_empty() {
acc.push(
TraitImplMissingAssocItems {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,22 @@ impl Trait for dyn OtherTrait {}
"#,
)
}

#[test]
fn no_false_positive_on_specialization() {
check_diagnostics(
r#"
#![feature(specialization)]

pub trait Foo {
fn foo();
}

impl<T> Foo for T {
default fn foo() {}
}
impl Foo for bool {}
"#,
);
}
}