]> git.lizzy.rs Git - rust.git/commitdiff
Auto merge of #85041 - mibac138:suggest-generics, r=estebank
authorbors <bors@rust-lang.org>
Thu, 13 May 2021 08:08:20 +0000 (08:08 +0000)
committerbors <bors@rust-lang.org>
Thu, 13 May 2021 08:08:20 +0000 (08:08 +0000)
Suggest adding a type parameter for impls

Add a new suggestion upon encountering an unknown type in a `impl` that suggests adding a new type parameter. This diagnostic suggests to add a new type parameter even though it may be a const parameter, however after adding the parameter and running rustc again a follow up error steers the user to change the type parameter to a const parameter.

```rust
struct X<const C: ()>();
impl X<C> {}
```
suggests
```
error[E0412]: cannot find type `C` in this scope
 --> bar.rs:2:8
  |
1 | struct X<const C: ()>();
  | ------------------------ similarly named struct `X` defined here
2 | impl X<C> {}
  |        ^
  |
help: a struct with a similar name exists
  |
2 | impl X<X> {}
  |        ^
help: you might be missing a type parameter
  |
2 | impl<C> X<C> {}
  |     ^^^
```
After adding a type parameter the code now becomes
```rust
struct X<const C: ()>();
impl<C> X<C> {}
```
and the error now fully steers the user towards the correct code
```
error[E0747]: type provided when a constant was expected
 --> bar.rs:2:11
  |
2 | impl<C> X<C> {}
  |           ^
  |
help: consider changing this type parameter to be a `const` generic
  |
2 | impl<const C: ()> X<C> {}
  |      ^^^^^^^^^^^
```
r? `@estebank`
Somewhat related #84946

1  2 
compiler/rustc_resolve/src/late/diagnostics.rs

index 13e457507cf32cf87e6a04e45b76a8909d798686,28d636ef1b21eab71fe7989a220c82b673e86148..03b578d4adeec235d60aa34654cfbdc1962c2845
@@@ -6,10 -6,13 +6,13 @@@ use crate::{CrateLint, Module, ModuleKi
  use crate::{PathResult, PathSource, Segment};
  
  use rustc_ast::visit::FnKind;
- use rustc_ast::{self as ast, Expr, ExprKind, Item, ItemKind, NodeId, Path, Ty, TyKind};
+ use rustc_ast::{
+     self as ast, Expr, ExprKind, GenericParam, GenericParamKind, Item, ItemKind, NodeId, Path, Ty,
+     TyKind,
+ };
  use rustc_ast_pretty::pprust::path_segment_to_string;
  use rustc_data_structures::fx::FxHashSet;
 -use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder};
 +use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder, SuggestionStyle};
  use rustc_hir as hir;
  use rustc_hir::def::Namespace::{self, *};
  use rustc_hir::def::{self, CtorKind, CtorOf, DefKind};