ide_diagnostics/handlers/
generic_args_prohibited.rs

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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
use either::Either;
use hir::GenericArgsProhibitedReason;
use ide_db::assists::Assist;
use ide_db::source_change::SourceChange;
use ide_db::text_edit::TextEdit;
use syntax::{ast, AstNode, TextRange};

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

// Diagnostic: generic-args-prohibited
//
// This diagnostic is shown when generic arguments are provided for a type that does not accept
// generic arguments.
pub(crate) fn generic_args_prohibited(
    ctx: &DiagnosticsContext<'_>,
    d: &hir::GenericArgsProhibited,
) -> Diagnostic {
    Diagnostic::new_with_syntax_node_ptr(
        ctx,
        DiagnosticCode::RustcHardError("E0109"),
        describe_reason(d.reason),
        d.args.map(Into::into),
    )
    .with_fixes(fixes(ctx, d))
}

fn describe_reason(reason: GenericArgsProhibitedReason) -> String {
    let kind = match reason {
        GenericArgsProhibitedReason::Module => "modules",
        GenericArgsProhibitedReason::TyParam => "type parameters",
        GenericArgsProhibitedReason::SelfTy => "`Self`",
        GenericArgsProhibitedReason::PrimitiveTy => "builtin types",
        GenericArgsProhibitedReason::EnumVariant => {
            return "you can specify generic arguments on either the enum or the variant, but not both"
                .to_owned();
        }
    };
    format!("generic arguments are not allowed on {kind}")
}

fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::GenericArgsProhibited) -> Option<Vec<Assist>> {
    let file_id = d.args.file_id.file_id()?;
    let syntax = d.args.to_node(ctx.sema.db);
    let range = match &syntax {
        Either::Left(_) => syntax.syntax().text_range(),
        Either::Right(param_list) => {
            let path_segment = ast::PathSegment::cast(param_list.syntax().parent()?)?;
            let start = if let Some(coloncolon) = path_segment.coloncolon_token() {
                coloncolon.text_range().start()
            } else {
                param_list.syntax().text_range().start()
            };
            let end = if let Some(ret_type) = path_segment.ret_type() {
                ret_type.syntax().text_range().end()
            } else {
                param_list.syntax().text_range().end()
            };
            TextRange::new(start, end)
        }
    };
    Some(vec![fix(
        "remove_generic_args",
        "Remove these generics",
        SourceChange::from_text_edit(file_id, TextEdit::delete(range)),
        syntax.syntax().text_range(),
    )])
}

#[cfg(test)]
mod tests {
    // This diagnostic was the first to be emitted in ty lowering, so the tests here also test
    // diagnostics in ty lowering in general (which is why there are so many of them).

    use crate::tests::{check_diagnostics, check_fix};

    #[test]
    fn primitives() {
        check_diagnostics(
            r#"
//- /core.rs crate:core library
#![rustc_coherence_is_core]
impl str {
    pub fn trim() {}
}

//- /lib.rs crate:foo deps:core
fn bar<T>() {}

fn foo() {
    let _: (bool<()>, ());
             // ^^^^ 💡 error: generic arguments are not allowed on builtin types
    let _ = <str<'_>>::trim;
             // ^^^^ 💡 error: generic arguments are not allowed on builtin types
    bar::<u32<{ const { 1 + 1 } }>>();
          // ^^^^^^^^^^^^^^^^^^^^^ 💡 error: generic arguments are not allowed on builtin types
}
            "#,
        );
    }

    #[test]
    fn modules() {
        check_diagnostics(
            r#"
pub mod foo {
    pub mod bar {
        pub struct Baz;

        impl Baz {
            pub fn qux() {}
        }
    }
}

fn foo() {
    let _: foo::<'_>::bar::Baz;
           // ^^^^^^ 💡 error: generic arguments are not allowed on modules
    let _ = <foo::bar<()>::Baz>::qux;
                  // ^^^^ 💡 error: generic arguments are not allowed on modules
}
        "#,
        );
    }

    #[test]
    fn type_parameters() {
        check_diagnostics(
            r#"
fn foo<T, U>() {
    let _: T<'a>;
         // ^^^^ 💡 error: generic arguments are not allowed on type parameters
    let _: U::<{ 1 + 2 }>;
         // ^^^^^^^^^^^^^ 💡 error: generic arguments are not allowed on type parameters
}
        "#,
        );
    }

    #[test]
    fn fn_like_generic_args() {
        check_diagnostics(
            r#"
fn foo() {
    let _: bool(bool, i32) -> ();
            // ^^^^^^^^^^^ 💡 error: generic arguments are not allowed on builtin types
}
        "#,
        );
    }

    #[test]
    fn fn_signature() {
        check_diagnostics(
            r#"
fn foo(
    _a: bool<'_>,
         // ^^^^ 💡 error: generic arguments are not allowed on builtin types
    _b: i32::<i64>,
        // ^^^^^^^ 💡 error: generic arguments are not allowed on builtin types
    _c: &(&str<1>)
           // ^^^ 💡 error: generic arguments are not allowed on builtin types
) -> ((), i32<bool>) {
          // ^^^^^^ 💡 error: generic arguments are not allowed on builtin types
    ((), 0)
}
        "#,
        );
    }

    #[test]
    fn const_static_type() {
        check_diagnostics(
            r#"
const A: i32<bool> = 0;
         // ^^^^^^ 💡 error: generic arguments are not allowed on builtin types
static A: i32::<{ 1 + 3 }> = 0;
          // ^^^^^^^^^^^^^ 💡 error: generic arguments are not allowed on builtin types
        "#,
        );
    }

    #[test]
    fn fix() {
        check_fix(
            r#"
fn foo() {
    let _: bool<'_, (), { 1 + 1 }>$0;
}"#,
            r#"
fn foo() {
    let _: bool;
}"#,
        );
        check_fix(
            r#"
fn foo() {
    let _: bool::$0<'_, (), { 1 + 1 }>;
}"#,
            r#"
fn foo() {
    let _: bool;
}"#,
        );
        check_fix(
            r#"
fn foo() {
    let _: bool(i$032);
}"#,
            r#"
fn foo() {
    let _: bool;
}"#,
        );
        check_fix(
            r#"
fn foo() {
    let _: bool$0(i32) -> i64;
}"#,
            r#"
fn foo() {
    let _: bool;
}"#,
        );
        check_fix(
            r#"
fn foo() {
    let _: bool::(i$032) -> i64;
}"#,
            r#"
fn foo() {
    let _: bool;
}"#,
        );
        check_fix(
            r#"
fn foo() {
    let _: bool::(i32)$0;
}"#,
            r#"
fn foo() {
    let _: bool;
}"#,
        );
    }

    #[test]
    fn in_fields() {
        check_diagnostics(
            r#"
struct A(bool<i32>);
          // ^^^^^ 💡 error: generic arguments are not allowed on builtin types
struct B { v: bool<(), 1> }
               // ^^^^^^^ 💡 error: generic arguments are not allowed on builtin types
union C {
    a: bool<i32>,
        // ^^^^^ 💡 error: generic arguments are not allowed on builtin types
    b: i32<bool>,
       // ^^^^^^ 💡 error: generic arguments are not allowed on builtin types
       }
enum D {
    A(bool<i32>),
       // ^^^^^ 💡 error: generic arguments are not allowed on builtin types
    B { v: i32<bool> },
           // ^^^^^^ 💡 error: generic arguments are not allowed on builtin types
}
        "#,
        );
    }

    #[test]
    fn in_generics() {
        check_diagnostics(
            r#"
mod foo {
    pub trait Trait {}
}

struct A<A: foo::<()>::Trait>(A)
            // ^^^^^^ 💡 error: generic arguments are not allowed on modules
    where bool<i32>: foo::Trait;
           // ^^^^^ 💡 error: generic arguments are not allowed on builtin types
union B<A: foo::<()>::Trait>
           // ^^^^^^ 💡 error: generic arguments are not allowed on modules
    where bool<i32>: foo::Trait
           // ^^^^^ 💡 error: generic arguments are not allowed on builtin types
{ a: A }
enum C<A: foo::<()>::Trait>
          // ^^^^^^ 💡 error: generic arguments are not allowed on modules
    where bool<i32>: foo::Trait
           // ^^^^^ 💡 error: generic arguments are not allowed on builtin types
{}

fn f<A: foo::<()>::Trait>()
        // ^^^^^^ 💡 error: generic arguments are not allowed on modules
    where bool<i32>: foo::Trait
           // ^^^^^ 💡 error: generic arguments are not allowed on builtin types
{}

type D<A: foo::<()>::Trait> = A
          // ^^^^^^ 💡 error: generic arguments are not allowed on modules
    where bool<i32>: foo::Trait;
           // ^^^^^ 💡 error: generic arguments are not allowed on builtin types

trait E<A: foo::<()>::Trait>
           // ^^^^^^ 💡 error: generic arguments are not allowed on modules
    where bool<i32>: foo::Trait
           // ^^^^^ 💡 error: generic arguments are not allowed on builtin types
{
    fn f<B: foo::<()>::Trait>()
            // ^^^^^^ 💡 error: generic arguments are not allowed on modules
        where bool<i32>: foo::Trait
               // ^^^^^ 💡 error: generic arguments are not allowed on builtin types
    {}

    type D<B: foo::<()>::Trait> = A
              // ^^^^^^ 💡 error: generic arguments are not allowed on modules
        where bool<i32>: foo::Trait;
               // ^^^^^ 💡 error: generic arguments are not allowed on builtin types
}

impl<A: foo::<()>::Trait> E for ()
        // ^^^^^^ 💡 error: generic arguments are not allowed on modules
    where bool<i32>: foo::Trait
           // ^^^^^ 💡 error: generic arguments are not allowed on builtin types
{
    fn f<B: foo::<()>::Trait>()
            // ^^^^^^ 💡 error: generic arguments are not allowed on modules
        where bool<i32>: foo::Trait
               // ^^^^^ 💡 error: generic arguments are not allowed on builtin types
    {}

    type D<B: foo::<()>::Trait> = A
              // ^^^^^^ 💡 error: generic arguments are not allowed on modules
        where bool<i32>: foo::Trait;
               // ^^^^^ 💡 error: generic arguments are not allowed on builtin types
}
        "#,
        );
    }

    #[test]
    fn assoc_items() {
        check_diagnostics(
            r#"
struct Foo;

trait Trait {
    fn f() -> bool<i32> { true }
               // ^^^^^ 💡 error: generic arguments are not allowed on builtin types
    type T = i32<bool>;
             // ^^^^^^ 💡 error: generic arguments are not allowed on builtin types
}

impl Trait for Foo {
    fn f() -> bool<i32> { true }
               // ^^^^^ 💡 error: generic arguments are not allowed on builtin types
    type T = i32<bool>;
             // ^^^^^^ 💡 error: generic arguments are not allowed on builtin types
}

impl Foo {
    fn f() -> bool<i32> { true }
               // ^^^^^ 💡 error: generic arguments are not allowed on builtin types
    type T = i32<bool>;
             // ^^^^^^ 💡 error: generic arguments are not allowed on builtin types
}
        "#,
        );
    }

    #[test]
    fn const_param_ty() {
        check_diagnostics(
            r#"
fn foo<
    const A: bool<i32>,
              // ^^^^^ 💡 error: generic arguments are not allowed on builtin types
    B,
    C,
    const D: bool<i32>,
              // ^^^^^ 💡 error: generic arguments are not allowed on builtin types
    const E: bool<i32>,
              // ^^^^^ 💡 error: generic arguments are not allowed on builtin types
>() {}
        "#,
        );
    }

    #[test]
    fn generic_defaults() {
        check_diagnostics(
            r#"
struct Foo<A = bool<i32>>(A);
                // ^^^^^ 💡 error: generic arguments are not allowed on builtin types
        "#,
        );
    }

    #[test]
    fn impl_self_ty() {
        check_diagnostics(
            r#"
struct Foo<A>(A);
trait Trait {}
impl Foo<bool<i32>> {}
          // ^^^^^ 💡 error: generic arguments are not allowed on builtin types
impl Trait for Foo<bool<i32>> {}
                    // ^^^^^ 💡 error: generic arguments are not allowed on builtin types
        "#,
        );
    }

    #[test]
    fn impl_trait() {
        check_diagnostics(
            r#"
mod foo {
    pub trait Trait {}
}
impl foo::<()>::Trait for () {}
     // ^^^^^^ 💡 error: generic arguments are not allowed on modules
        "#,
        );
    }

    #[test]
    fn type_alias() {
        check_diagnostics(
            r#"
pub trait Trait {
    type Assoc;
}
type T = bool<i32>;
          // ^^^^^ 💡 error: generic arguments are not allowed on builtin types
impl Trait for () {
    type Assoc = i32<bool>;
                 // ^^^^^^ 💡 error: generic arguments are not allowed on builtin types
}
        "#,
        );
    }
}