Unnamed repository; edit this file 'description' to name the repository.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use hir::InFile;
use itertools::Itertools;
use syntax::{AstNode, ast};

use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext, adjusted_display_range};

// Diagnostic: trait-impl-missing-assoc_item
//
// Diagnoses missing trait items in a trait impl.
pub(crate) fn trait_impl_missing_assoc_item(
    ctx: &DiagnosticsContext<'_>,
    d: &hir::TraitImplMissingAssocItems,
) -> Diagnostic {
    let missing = d.missing.iter().format_with(", ", |(name, item), f| {
        f(&match *item {
            hir::AssocItem::Function(_) => "`fn ",
            hir::AssocItem::Const(_) => "`const ",
            hir::AssocItem::TypeAlias(_) => "`type ",
        })?;
        f(&name.display(ctx.sema.db, ctx.edition))?;
        f(&"`")
    });
    Diagnostic::new(
        DiagnosticCode::RustcHardError("E0046"),
        format!("not all trait items implemented, missing: {missing}"),
        adjusted_display_range::<ast::Impl>(
            ctx,
            InFile { file_id: d.file_id, value: d.impl_ },
            &|impl_| impl_.trait_().map(|t| t.syntax().text_range()),
        ),
    )
    .stable()
}

#[cfg(test)]
mod tests {
    use crate::tests::check_diagnostics;

    #[test]
    fn trait_with_default_value() {
        check_diagnostics(
            r#"
trait Marker {
    const FLAG: bool = false;
}
struct Foo;
impl Marker for Foo {}
            "#,
        )
    }

    #[test]
    fn simple() {
        check_diagnostics(
            r#"
trait Trait {
    const C: ();
    type T;
    fn f();
}

impl Trait for () {
    const C: () = ();
    type T = ();
    fn f() {}
}

impl Trait for () {
   //^^^^^ error: not all trait items implemented, missing: `const C`
    type T = ();
    fn f() {}
}

impl Trait for () {
   //^^^^^ error: not all trait items implemented, missing: `const C`, `type T`, `fn f`
}

"#,
        );
    }

    #[test]
    fn default() {
        check_diagnostics(
            r#"
trait Trait {
    const C: ();
    type T = ();
    fn f() {}
}

impl Trait for () {
    const C: () = ();
    type T = ();
    fn f() {}
}

impl Trait for () {
   //^^^^^ error: not all trait items implemented, missing: `const C`
    type T = ();
    fn f() {}
}

impl Trait for () {
   //^^^^^ error: not all trait items implemented, missing: `const C`
     type T = ();
 }

impl Trait for () {
   //^^^^^ error: not all trait items implemented, missing: `const C`
}

"#,
        );
    }

    #[test]
    fn negative_impl() {
        check_diagnostics(
            r#"
trait Trait {
    fn item();
}

// 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#"
//- minicore: sized
trait Trait {
    type Item
    where
        Self: Sized;

    fn item()
    where
        Self: Sized;
}

trait OtherTrait {}

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 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 {}
"#,
        );
    }
}