rust/compiler/rustc_parse/src/parser
Matthias Krüger 202c70666f
Rollup merge of #103478 - SpanishPear:spanishpear/issue_103366_fix, r=TaKO8Ki
Suggest fix for misplaced generic params on fn item #103366

fixes #103366

This still has some work to go, but works for 2/3 of the initial base cases described in #1033366

simple fn:
```
error: expected identifier, found `<`
 --> shreys/test_1.rs:1:3
  |
1 | fn<T> id(x: T) -> T { x }
  |   ^ expected identifier
  |
help: help: place the generic parameter list after the function name:
  |
1 | fn id<T>(x: T) -> T { x }
  |    ~~~~

```

Complicated bounds
```
error: expected identifier, found `<`
 --> spanishpear/test_2.rs:1:3
  |
1 | fn<'a, B: 'a + std::ops::Add<Output = u32>> f(_x: B) { }
  |   ^ expected identifier
  |
help: help: place the generic parameter list after the function name:
  |
1 | fn f<'a, B: 'a + std::ops::Add<Output = u32>>(_x: B) { }
  |    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```

Opening a draft PR for comments on approach, particularly I have the following questions:
 -  [x]  Is it okay to be using `err.span_suggestion` over struct derives? I struggled to get the initial implementation (particularly the correct suggestion message) on struct derives, although I think given what I've learned since starting, I could attempt re-doing it with that approach.
  -  [x] in the case where the snippet cannot be obtained from a span, is the `help` but no suggestion okay? I think yes (also, when does this case occur?)
  -  [x] are there any red flags for the generalisation of this work for relevant item kinds (i.e. `struct`, `enum`, `trait`, and `union`). My basic testing indicates it does work for those types except the help tip is currently hardcoded to `after the function name` - which should change dependent on the item.
  - [x] I am planning to not show the suggestion if there is already a `<` after the item identifier, (i.e. if there are already generics, as after a function name per the original issue). Any major objections?
  - [x] Is the style of error okay? I wasn't sure if there was a way to make it display nicer, or if thats handled by span_suggestion

These aren't blocking questions, and I will keep working on:
  - check if there is a `<` after the ident (and if so, not showing the suggestion)
  - generalize the help message
  - figuring out how to write/run/etc ui tests (including reading the docs for them)
  - logic cleanups
2023-02-14 18:02:50 +01:00
..
attr.rs compiler: remove unnecessary imports and qualified paths 2022-12-10 18:45:34 +01:00
attr_wrapper.rs Remove TokenCursorFrame. 2023-02-03 10:06:52 +11:00
diagnostics.rs Rollup merge of #103478 - SpanishPear:spanishpear/issue_103366_fix, r=TaKO8Ki 2023-02-14 18:02:50 +01:00
expr.rs Migrate rustc_parse to derive diagnostics 2023-02-06 14:40:35 +00:00
generics.rs Recover from default value for a lifetime in generic parameters. 2023-02-04 17:04:09 -06:00
item.rs Migrate rustc_parse to derive diagnostics 2023-02-06 14:40:35 +00:00
mod.rs Rollup merge of #107551 - fee1-dead-contrib:rm_const_fnmut_helper, r=oli-obk 2023-02-03 14:15:22 -08:00
nonterminal.rs rustc_parse: migrate more to diagnostic structs 2023-02-01 21:50:34 +01:00
pat.rs Rollup merge of #107493 - clubby789:range-fat-arrow-followup, r=estebank 2023-02-02 17:14:05 +01:00
path.rs recover more unbraced const args 2023-01-27 19:26:04 +01:00
stmt.rs rustc_parse: remove huge error imports 2023-02-05 03:48:17 +01:00
ty.rs Do not eagerly recover for bad impl-trait in macros 2023-02-08 17:56:08 +00:00