Skip to main content

ide_diagnostics/handlers/
generic_default_refers_to_self.rs

1use crate::{Diagnostic, DiagnosticCode, DiagnosticsContext};
2
3// Diagnostic: generic-default-refers-to-self
4//
5// This diagnostic is shown when a generic default refers to `Self`
6pub(crate) fn generic_default_refers_to_self(
7    ctx: &DiagnosticsContext<'_, '_>,
8    d: &hir::GenericDefaultRefersToSelf,
9) -> Diagnostic {
10    Diagnostic::new_with_syntax_node_ptr(
11        ctx,
12        DiagnosticCode::RustcHardError("E0735"),
13        "generic parameters cannot use `Self` in their defaults",
14        d.segment.map(Into::into),
15    )
16    .stable()
17}
18
19#[cfg(test)]
20mod tests {
21    use crate::tests::check_diagnostics;
22
23    #[test]
24    fn plain_self() {
25        check_diagnostics(
26            r#"
27struct Foo<T = Self>(T);
28            // ^^^^ error: generic parameters cannot use `Self` in their defaults
29"#,
30        );
31    }
32
33    #[test]
34    fn self_as_generic() {
35        check_diagnostics(
36            r#"
37struct Wrapper<T>(T);
38struct Foo<T = Wrapper<Self>>(T);
39                    // ^^^^ error: generic parameters cannot use `Self` in their defaults
40"#,
41        );
42    }
43}