Auto merge of #85041 - mibac138:suggest-generics, r=estebank
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
This commit is contained in:
commit
703f2e1685
6 changed files with 190 additions and 9 deletions
|
@ -6,7 +6,10 @@ use crate::{CrateLint, Module, ModuleKind, ModuleOrUniformRoot};
|
|||
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, SuggestionStyle};
|
||||
|
@ -1600,8 +1603,8 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
|
|||
if !self.diagnostic_metadata.currently_processing_generics && !single_uppercase_char {
|
||||
return None;
|
||||
}
|
||||
match (self.diagnostic_metadata.current_item, single_uppercase_char) {
|
||||
(Some(Item { kind: ItemKind::Fn(..), ident, .. }), _) if ident.name == sym::main => {
|
||||
match (self.diagnostic_metadata.current_item, single_uppercase_char, self.diagnostic_metadata.currently_processing_generics) {
|
||||
(Some(Item { kind: ItemKind::Fn(..), ident, .. }), _, _) if ident.name == sym::main => {
|
||||
// Ignore `fn main()` as we don't want to suggest `fn main<T>()`
|
||||
}
|
||||
(
|
||||
|
@ -1613,9 +1616,11 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
|
|||
| kind @ ItemKind::Union(..),
|
||||
..
|
||||
}),
|
||||
true,
|
||||
true, _
|
||||
)
|
||||
| (Some(Item { kind, .. }), false) => {
|
||||
// Without the 2nd `true`, we'd suggest `impl <T>` for `impl T` when a type `T` isn't found
|
||||
| (Some(Item { kind: kind @ ItemKind::Impl(..), .. }), true, true)
|
||||
| (Some(Item { kind, .. }), false, _) => {
|
||||
// Likely missing type parameter.
|
||||
if let Some(generics) = kind.generics() {
|
||||
if span.overlaps(generics.span) {
|
||||
|
@ -1633,6 +1638,10 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
|
|||
let (span, sugg) = if let [.., param] = &generics.params[..] {
|
||||
let span = if let [.., bound] = ¶m.bounds[..] {
|
||||
bound.span()
|
||||
} else if let GenericParam {
|
||||
kind: GenericParamKind::Const { ty, kw_span: _, default }, ..
|
||||
} = param {
|
||||
default.as_ref().map(|def| def.value.span).unwrap_or(ty.span)
|
||||
} else {
|
||||
param.ident.span
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue