Skip to content

False positive "not all trait items implemented" (E0046) for items with Self: Sized for unsized impls #19962

Closed
@zheland

Description

@zheland

With rust-lang/rust#135480 (on stable since 1.87.0) it is no longer required to implement items with Self: Sized in impls for unsized types.

But rust-analyzer still produces error "not all trait items implemented" (E0046) for such implementations because it checks missing trait items using its own analysis (trait-impl-missing-assoc_item).

Versions

Reproducible in VSCode on both Release and Pre-Release versions:

  • rust-analyzer version: 0.3.2490-standalone
  • rust-analyzer version: 0.4.2491-standalone

with: rustc -V: rustc 1.87.0 (17067e9ac 2025-05-09)

Example

The following example:

#[allow(dead_code)]
trait Trait {
    type Item
    where
        Self: Sized;

    fn item()
    where
        Self: Sized;
}

impl Trait for () {
    type Item = ();
    fn item() {}
}

// Items with Self: Sized bound not required to be implemented for unsized types
impl Trait for str {}
impl Trait for dyn std::fmt::Debug {}

compiles on Rust 1.87.0 (stable) without any errors:

$ cargo check
    Checking test v0.1.0
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.02s

but rust-analyzer emits errors:

not all trait items implemented, missing: `type Item`, `fn item` rust-analyzer(E0046) [Ln 18, Col 6]
not all trait items implemented, missing: `type Item`, `fn item` rust-analyzer(E0046) [Ln 19, Col 6]

Example test

When adding a similar test to rust-analyzer master (bf6d445) trait_impl_missing_assoc_item tests:

diff --git a/crates/ide-diagnostics/src/handlers/trait_impl_missing_assoc_item.rs b/crates/ide-diagnostics/src/handlers/trait_impl_missing_assoc_item.rs
index fa7ba90a75..12245c3189 100644
--- a/crates/ide-diagnostics/src/handlers/trait_impl_missing_assoc_item.rs
+++ b/crates/ide-diagnostics/src/handlers/trait_impl_missing_assoc_item.rs
@@ -124,6 +124,32 @@ trait Trait {
 
 // Negative impls don't require any items (in fact, the forbid providing any)
 impl !Trait for () {}
+"#,
+        )
+    }
+
+    #[test]
+    fn impl_sized_for_unsized() {
+        check_diagnostics(
+            r#"
+trait Trait {
+    type Item
+    where
+        Self: Sized;
+
+    fn item()
+    where
+        Self: Sized;
+}
+
+impl Trait for () {
+    type Item = ();
+    fn item() {}
+}
+
+// Items with Self: Sized bound not required to be implemented for unsized types.
+impl Trait for str {}
+impl Trait for dyn std::fmt::Debug {}
 "#,
         )
     }

tests fail with the following error:

$ cargo test -p ide-diagnostics impl_sized_for_unsized
...
Diagnostic test failed.
False negatives: []
False positives: [(LineCol { line: 16, col: 5 }, 254..259, "error: not all trait items implemented, missing: `type Item`, `fn item`"), (LineCol { line: 17, col: 5 }, 276..281, "error: not all trait items implemented, missing: `type Item`, `fn item`")]

Workaround

Add to VSCode settings:

"rust-analyzer.diagnostics.disabled": ["E0046"]

This seem to only disable trait-impl-missing-assoc_item error implemented and provided internally by rust-analyzer keeping diagnostics about missing item implementation coming from Rust (cargo check).

Metadata

Metadata

Labels

A-diagnosticsdiagnostics / error reportingBroken WindowBugs / technical debt to be addressed immediatelyC-bugCategory: bug

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions