1
Fork 0

Improve local generic parameter suggestions.

This commit is contained in:
Camille GILLOT 2022-07-14 15:09:30 +02:00
parent d7d701a9dc
commit 613dc2204d
16 changed files with 90 additions and 81 deletions

View file

@ -511,7 +511,7 @@ impl<'a> Resolver<'a> {
err.span_label(span, "use of generic parameter from outer function"); err.span_label(span, "use of generic parameter from outer function");
let sm = self.session.source_map(); let sm = self.session.source_map();
match outer_res { let def_id = match outer_res {
Res::SelfTy { trait_: maybe_trait_defid, alias_to: maybe_impl_defid } => { Res::SelfTy { trait_: maybe_trait_defid, alias_to: maybe_impl_defid } => {
if let Some(impl_span) = if let Some(impl_span) =
maybe_impl_defid.and_then(|(def_id, _)| self.opt_span(def_id)) maybe_impl_defid.and_then(|(def_id, _)| self.opt_span(def_id))
@ -536,11 +536,13 @@ impl<'a> Resolver<'a> {
if let Some(span) = self.opt_span(def_id) { if let Some(span) = self.opt_span(def_id) {
err.span_label(span, "type parameter from outer function"); err.span_label(span, "type parameter from outer function");
} }
def_id
} }
Res::Def(DefKind::ConstParam, def_id) => { Res::Def(DefKind::ConstParam, def_id) => {
if let Some(span) = self.opt_span(def_id) { if let Some(span) = self.opt_span(def_id) {
err.span_label(span, "const parameter from outer function"); err.span_label(span, "const parameter from outer function");
} }
def_id
} }
_ => { _ => {
bug!( bug!(
@ -548,28 +550,23 @@ impl<'a> Resolver<'a> {
DefKind::TyParam or DefKind::ConstParam" DefKind::TyParam or DefKind::ConstParam"
); );
} }
} };
if let HasGenericParams::Yes = has_generic_params { if let HasGenericParams::Yes(span) = has_generic_params {
// Try to retrieve the span of the function signature and generate a new // Try to retrieve the span of the function signature and generate a new
// message with a local type or const parameter. // message with a local type or const parameter.
let sugg_msg = "try using a local generic parameter instead"; let sugg_msg = "try using a local generic parameter instead";
if let Some((sugg_span, snippet)) = sm.generate_local_type_param_snippet(span) { let name = self.opt_name(def_id).unwrap_or(sym::T);
// Suggest the modification to the user let (span, snippet) = if span.is_empty() {
err.span_suggestion( let snippet = format!("<{}>", name);
sugg_span, (span, snippet)
sugg_msg,
snippet,
Applicability::MachineApplicable,
);
} else if let Some(sp) = sm.generate_fn_name_span(span) {
err.span_label(
sp,
"try adding a local generic parameter in this method instead",
);
} else { } else {
err.help("try using a local generic parameter instead"); let span = sm.span_through_char(span, '<').shrink_to_hi();
} let snippet = format!("{}, ", name);
(span, snippet)
};
// Suggest the modification to the user
err.span_suggestion(span, sugg_msg, snippet, Applicability::MachineApplicable);
} }
err err

View file

@ -1170,10 +1170,11 @@ impl<'a> Resolver<'a> {
let has_generic_params: HasGenericParams = match rib.kind { let has_generic_params: HasGenericParams = match rib.kind {
NormalRibKind NormalRibKind
| ClosureOrAsyncRibKind | ClosureOrAsyncRibKind
| AssocItemRibKind
| ModuleRibKind(..) | ModuleRibKind(..)
| MacroDefinition(..) | MacroDefinition(..)
| InlineAsmSymRibKind | InlineAsmSymRibKind
| FnItemRibKind
| AssocItemRibKind
| ForwardGenericParamBanRibKind => { | ForwardGenericParamBanRibKind => {
// Nothing to do. Continue. // Nothing to do. Continue.
continue; continue;
@ -1211,7 +1212,6 @@ impl<'a> Resolver<'a> {
// This was an attempt to use a type parameter outside its scope. // This was an attempt to use a type parameter outside its scope.
ItemRibKind(has_generic_params) => has_generic_params, ItemRibKind(has_generic_params) => has_generic_params,
FnItemRibKind => HasGenericParams::Yes,
ConstParamTyRibKind => { ConstParamTyRibKind => {
if let Some(span) = finalize { if let Some(span) = finalize {
self.report_error( self.report_error(
@ -1248,10 +1248,11 @@ impl<'a> Resolver<'a> {
let has_generic_params = match rib.kind { let has_generic_params = match rib.kind {
NormalRibKind NormalRibKind
| ClosureOrAsyncRibKind | ClosureOrAsyncRibKind
| AssocItemRibKind
| ModuleRibKind(..) | ModuleRibKind(..)
| MacroDefinition(..) | MacroDefinition(..)
| InlineAsmSymRibKind | InlineAsmSymRibKind
| FnItemRibKind
| AssocItemRibKind
| ForwardGenericParamBanRibKind => continue, | ForwardGenericParamBanRibKind => continue,
ConstantItemRibKind(trivial, _) => { ConstantItemRibKind(trivial, _) => {
@ -1278,7 +1279,6 @@ impl<'a> Resolver<'a> {
} }
ItemRibKind(has_generic_params) => has_generic_params, ItemRibKind(has_generic_params) => has_generic_params,
FnItemRibKind => HasGenericParams::Yes,
ConstParamTyRibKind => { ConstParamTyRibKind => {
if let Some(span) = finalize { if let Some(span) = finalize {
self.report_error( self.report_error(

View file

@ -93,7 +93,7 @@ enum PatBoundCtx {
/// Does this the item (from the item rib scope) allow generic parameters? /// Does this the item (from the item rib scope) allow generic parameters?
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
pub(crate) enum HasGenericParams { pub(crate) enum HasGenericParams {
Yes, Yes(Span),
No, No,
} }
@ -758,7 +758,7 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
self.with_lifetime_rib(LifetimeRibKind::Item, |this| { self.with_lifetime_rib(LifetimeRibKind::Item, |this| {
this.with_generic_param_rib( this.with_generic_param_rib(
&generics.params, &generics.params,
ItemRibKind(HasGenericParams::Yes), ItemRibKind(HasGenericParams::Yes(generics.span)),
LifetimeRibKind::Generics { LifetimeRibKind::Generics {
binder: foreign_item.id, binder: foreign_item.id,
kind: LifetimeBinderKind::Item, kind: LifetimeBinderKind::Item,
@ -772,7 +772,7 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
self.with_lifetime_rib(LifetimeRibKind::Item, |this| { self.with_lifetime_rib(LifetimeRibKind::Item, |this| {
this.with_generic_param_rib( this.with_generic_param_rib(
&generics.params, &generics.params,
ItemRibKind(HasGenericParams::Yes), ItemRibKind(HasGenericParams::Yes(generics.span)),
LifetimeRibKind::Generics { LifetimeRibKind::Generics {
binder: foreign_item.id, binder: foreign_item.id,
kind: LifetimeBinderKind::Function, kind: LifetimeBinderKind::Function,
@ -2078,7 +2078,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
self.with_current_self_item(item, |this| { self.with_current_self_item(item, |this| {
this.with_generic_param_rib( this.with_generic_param_rib(
&generics.params, &generics.params,
ItemRibKind(HasGenericParams::Yes), ItemRibKind(HasGenericParams::Yes(generics.span)),
LifetimeRibKind::Generics { LifetimeRibKind::Generics {
binder: item.id, binder: item.id,
kind: LifetimeBinderKind::Item, kind: LifetimeBinderKind::Item,
@ -2148,7 +2148,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
ItemKind::TyAlias(box TyAlias { ref generics, .. }) => { ItemKind::TyAlias(box TyAlias { ref generics, .. }) => {
self.with_generic_param_rib( self.with_generic_param_rib(
&generics.params, &generics.params,
ItemRibKind(HasGenericParams::Yes), ItemRibKind(HasGenericParams::Yes(generics.span)),
LifetimeRibKind::Generics { LifetimeRibKind::Generics {
binder: item.id, binder: item.id,
kind: LifetimeBinderKind::Item, kind: LifetimeBinderKind::Item,
@ -2161,7 +2161,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
ItemKind::Fn(box Fn { ref generics, .. }) => { ItemKind::Fn(box Fn { ref generics, .. }) => {
self.with_generic_param_rib( self.with_generic_param_rib(
&generics.params, &generics.params,
ItemRibKind(HasGenericParams::Yes), ItemRibKind(HasGenericParams::Yes(generics.span)),
LifetimeRibKind::Generics { LifetimeRibKind::Generics {
binder: item.id, binder: item.id,
kind: LifetimeBinderKind::Function, kind: LifetimeBinderKind::Function,
@ -2193,7 +2193,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
// Create a new rib for the trait-wide type parameters. // Create a new rib for the trait-wide type parameters.
self.with_generic_param_rib( self.with_generic_param_rib(
&generics.params, &generics.params,
ItemRibKind(HasGenericParams::Yes), ItemRibKind(HasGenericParams::Yes(generics.span)),
LifetimeRibKind::Generics { LifetimeRibKind::Generics {
binder: item.id, binder: item.id,
kind: LifetimeBinderKind::Item, kind: LifetimeBinderKind::Item,
@ -2217,7 +2217,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
// Create a new rib for the trait-wide type parameters. // Create a new rib for the trait-wide type parameters.
self.with_generic_param_rib( self.with_generic_param_rib(
&generics.params, &generics.params,
ItemRibKind(HasGenericParams::Yes), ItemRibKind(HasGenericParams::Yes(generics.span)),
LifetimeRibKind::Generics { LifetimeRibKind::Generics {
binder: item.id, binder: item.id,
kind: LifetimeBinderKind::Item, kind: LifetimeBinderKind::Item,
@ -2605,7 +2605,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
// If applicable, create a rib for the type parameters. // If applicable, create a rib for the type parameters.
self.with_generic_param_rib( self.with_generic_param_rib(
&generics.params, &generics.params,
ItemRibKind(HasGenericParams::Yes), ItemRibKind(HasGenericParams::Yes(generics.span)),
LifetimeRibKind::Generics { LifetimeRibKind::Generics {
span: generics.span, span: generics.span,
binder: item_id, binder: item_id,

View file

@ -1945,6 +1945,16 @@ impl<'a> Resolver<'a> {
def_id.as_local().map(|def_id| self.source_span[def_id]) def_id.as_local().map(|def_id| self.source_span[def_id])
} }
/// Retrieves the name of the given `DefId`.
#[inline]
pub fn opt_name(&self, def_id: DefId) -> Option<Symbol> {
let def_key = match def_id.as_local() {
Some(def_id) => self.definitions.def_key(def_id),
None => self.cstore().def_key(def_id),
};
def_key.get_opt_name()
}
/// Checks if an expression refers to a function marked with /// Checks if an expression refers to a function marked with
/// `#[rustc_legacy_const_generics]` and returns the argument index list /// `#[rustc_legacy_const_generics]` and returns the argument index list
/// from the attribute. /// from the attribute.

View file

@ -280,6 +280,7 @@ symbols! {
StructuralPartialEq, StructuralPartialEq,
SubdiagnosticMessage, SubdiagnosticMessage,
Sync, Sync,
T,
Target, Target,
ToOwned, ToOwned,
ToString, ToString,

View file

@ -4,7 +4,7 @@ error[E0401]: can't use generic parameters from outer function
LL | fn foo<const X: u32>() { LL | fn foo<const X: u32>() {
| - const parameter from outer function | - const parameter from outer function
LL | fn bar() -> u32 { LL | fn bar() -> u32 {
| --- try adding a local generic parameter in this method instead | - help: try using a local generic parameter instead: `<X>`
LL | X LL | X
| ^ use of generic parameter from outer function | ^ use of generic parameter from outer function

View file

@ -4,9 +4,9 @@ error[E0401]: can't use generic parameters from outer function
LL | fn foo<T>(x: T) { LL | fn foo<T>(x: T) {
| - type parameter from outer function | - type parameter from outer function
LL | fn bfnr<U, V: Baz<U>, W: Fn()>(y: T) { LL | fn bfnr<U, V: Baz<U>, W: Fn()>(y: T) {
| --------------------------- ^ use of generic parameter from outer function | - ^ use of generic parameter from outer function
| | | |
| help: try using a local generic parameter instead: `bfnr<U, V: Baz<U>, W: Fn(), T>` | help: try using a local generic parameter instead: `T,`
error[E0401]: can't use generic parameters from outer function error[E0401]: can't use generic parameters from outer function
--> $DIR/E0401.rs:9:16 --> $DIR/E0401.rs:9:16
@ -15,7 +15,7 @@ LL | fn foo<T>(x: T) {
| - type parameter from outer function | - type parameter from outer function
... ...
LL | fn baz<U, LL | fn baz<U,
| --- try adding a local generic parameter in this method instead | - help: try using a local generic parameter instead: `T,`
... ...
LL | (y: T) { LL | (y: T) {
| ^ use of generic parameter from outer function | ^ use of generic parameter from outer function

View file

@ -5,9 +5,9 @@ LL | impl<T> Struct<T> {
| - type parameter from outer function | - type parameter from outer function
LL | const CONST: fn() = || { LL | const CONST: fn() = || {
LL | struct _Obligation where T:; LL | struct _Obligation where T:;
| ^ use of generic parameter from outer function | - ^ use of generic parameter from outer function
| | |
= help: try using a local generic parameter instead | help: try using a local generic parameter instead: `<T>`
error: aborting due to previous error error: aborting due to previous error

View file

@ -2,10 +2,9 @@ error[E0401]: can't use generic parameters from outer function
--> $DIR/issue-3214.rs:3:12 --> $DIR/issue-3214.rs:3:12
| |
LL | fn foo<T>() { LL | fn foo<T>() {
| --- - type parameter from outer function | - type parameter from outer function
| |
| try adding a local generic parameter in this method instead
LL | struct Foo { LL | struct Foo {
| - help: try using a local generic parameter instead: `<T>`
LL | x: T, LL | x: T,
| ^ use of generic parameter from outer function | ^ use of generic parameter from outer function

View file

@ -2,11 +2,11 @@ error[E0401]: can't use generic parameters from outer function
--> $DIR/issue-5997-enum.rs:2:16 --> $DIR/issue-5997-enum.rs:2:16
| |
LL | fn f<Z>() -> bool { LL | fn f<Z>() -> bool {
| - - type parameter from outer function | - type parameter from outer function
| |
| try adding a local generic parameter in this method instead
LL | enum E { V(Z) } LL | enum E { V(Z) }
| ^ use of generic parameter from outer function | - ^ use of generic parameter from outer function
| |
| help: try using a local generic parameter instead: `<Z>`
error: aborting due to previous error error: aborting due to previous error

View file

@ -2,11 +2,11 @@ error[E0401]: can't use generic parameters from outer function
--> $DIR/issue-5997-struct.rs:2:14 --> $DIR/issue-5997-struct.rs:2:14
| |
LL | fn f<T>() -> bool { LL | fn f<T>() -> bool {
| - - type parameter from outer function | - type parameter from outer function
| |
| try adding a local generic parameter in this method instead
LL | struct S(T); LL | struct S(T);
| ^ use of generic parameter from outer function | -^ use of generic parameter from outer function
| |
| help: try using a local generic parameter instead: `<T>`
error: aborting due to previous error error: aborting due to previous error

View file

@ -4,9 +4,9 @@ error[E0401]: can't use generic parameters from outer function
LL | fn hd<U>(v: Vec<U> ) -> U { LL | fn hd<U>(v: Vec<U> ) -> U {
| - type parameter from outer function | - type parameter from outer function
LL | fn hd1(w: [U]) -> U { return w[0]; } LL | fn hd1(w: [U]) -> U { return w[0]; }
| --- ^ use of generic parameter from outer function | - ^ use of generic parameter from outer function
| | | |
| help: try using a local generic parameter instead: `hd1<U>` | help: try using a local generic parameter instead: `<U>`
error[E0401]: can't use generic parameters from outer function error[E0401]: can't use generic parameters from outer function
--> $DIR/nested-ty-params.rs:3:23 --> $DIR/nested-ty-params.rs:3:23
@ -14,9 +14,9 @@ error[E0401]: can't use generic parameters from outer function
LL | fn hd<U>(v: Vec<U> ) -> U { LL | fn hd<U>(v: Vec<U> ) -> U {
| - type parameter from outer function | - type parameter from outer function
LL | fn hd1(w: [U]) -> U { return w[0]; } LL | fn hd1(w: [U]) -> U { return w[0]; }
| --- ^ use of generic parameter from outer function | - ^ use of generic parameter from outer function
| | | |
| help: try using a local generic parameter instead: `hd1<U>` | help: try using a local generic parameter instead: `<U>`
error: aborting due to 2 previous errors error: aborting due to 2 previous errors

View file

@ -4,9 +4,9 @@ error[E0401]: can't use generic parameters from outer function
LL | fn foo<T>() { LL | fn foo<T>() {
| - type parameter from outer function | - type parameter from outer function
LL | fn bar(b: T) { } LL | fn bar(b: T) { }
| --- ^ use of generic parameter from outer function | - ^ use of generic parameter from outer function
| | | |
| help: try using a local generic parameter instead: `bar<T>` | help: try using a local generic parameter instead: `<T>`
error: aborting due to previous error error: aborting due to previous error

View file

@ -3,22 +3,22 @@ error[E0401]: can't use generic parameters from outer function
| |
LL | fn siphash<T>() { LL | fn siphash<T>() {
| - type parameter from outer function | - type parameter from outer function
... LL |
LL | trait U {
| - help: try using a local generic parameter instead: `<T>`
LL | fn g(&self, x: T) -> T; LL | fn g(&self, x: T) -> T;
| - ^ use of generic parameter from outer function | ^ use of generic parameter from outer function
| |
| help: try using a local generic parameter instead: `g<T>`
error[E0401]: can't use generic parameters from outer function error[E0401]: can't use generic parameters from outer function
--> $DIR/issue-3021-c.rs:4:30 --> $DIR/issue-3021-c.rs:4:30
| |
LL | fn siphash<T>() { LL | fn siphash<T>() {
| - type parameter from outer function | - type parameter from outer function
... LL |
LL | trait U {
| - help: try using a local generic parameter instead: `<T>`
LL | fn g(&self, x: T) -> T; LL | fn g(&self, x: T) -> T;
| - ^ use of generic parameter from outer function | ^ use of generic parameter from outer function
| |
| help: try using a local generic parameter instead: `g<T>`
error: aborting due to 2 previous errors error: aborting due to 2 previous errors

View file

@ -4,8 +4,8 @@ error[E0401]: can't use generic parameters from outer function
LL | trait TraitA<A> { LL | trait TraitA<A> {
| - type parameter from outer function | - type parameter from outer function
LL | fn outer(&self) { LL | fn outer(&self) {
| ----- try adding a local generic parameter in this method instead
LL | enum Foo<B> { LL | enum Foo<B> {
| - help: try using a local generic parameter instead: `A,`
LL | Variance(A) LL | Variance(A)
| ^ use of generic parameter from outer function | ^ use of generic parameter from outer function
@ -15,9 +15,10 @@ error[E0401]: can't use generic parameters from outer function
LL | trait TraitB<A> { LL | trait TraitB<A> {
| - type parameter from outer function | - type parameter from outer function
LL | fn outer(&self) { LL | fn outer(&self) {
| ----- try adding a local generic parameter in this method instead
LL | struct Foo<B>(A); LL | struct Foo<B>(A);
| ^ use of generic parameter from outer function | - ^ use of generic parameter from outer function
| |
| help: try using a local generic parameter instead: `A,`
error[E0401]: can't use generic parameters from outer function error[E0401]: can't use generic parameters from outer function
--> $DIR/resolve-type-param-in-item-in-trait.rs:23:28 --> $DIR/resolve-type-param-in-item-in-trait.rs:23:28
@ -25,9 +26,10 @@ error[E0401]: can't use generic parameters from outer function
LL | trait TraitC<A> { LL | trait TraitC<A> {
| - type parameter from outer function | - type parameter from outer function
LL | fn outer(&self) { LL | fn outer(&self) {
| ----- try adding a local generic parameter in this method instead
LL | struct Foo<B> { a: A } LL | struct Foo<B> { a: A }
| ^ use of generic parameter from outer function | - ^ use of generic parameter from outer function
| |
| help: try using a local generic parameter instead: `A,`
error[E0401]: can't use generic parameters from outer function error[E0401]: can't use generic parameters from outer function
--> $DIR/resolve-type-param-in-item-in-trait.rs:30:22 --> $DIR/resolve-type-param-in-item-in-trait.rs:30:22
@ -36,9 +38,9 @@ LL | trait TraitD<A> {
| - type parameter from outer function | - type parameter from outer function
LL | fn outer(&self) { LL | fn outer(&self) {
LL | fn foo<B>(a: A) { } LL | fn foo<B>(a: A) { }
| ------ ^ use of generic parameter from outer function | - ^ use of generic parameter from outer function
| | | |
| help: try using a local generic parameter instead: `foo<B, A>` | help: try using a local generic parameter instead: `A,`
error: aborting due to 4 previous errors error: aborting due to 4 previous errors

View file

@ -4,9 +4,9 @@ error[E0401]: can't use generic parameters from outer function
LL | fn foo<T>(x: T) { LL | fn foo<T>(x: T) {
| - type parameter from outer function | - type parameter from outer function
LL | fn bar(f: Box<dyn FnMut(T) -> T>) { } LL | fn bar(f: Box<dyn FnMut(T) -> T>) { }
| --- ^ use of generic parameter from outer function | - ^ use of generic parameter from outer function
| | | |
| help: try using a local generic parameter instead: `bar<T>` | help: try using a local generic parameter instead: `<T>`
error[E0401]: can't use generic parameters from outer function error[E0401]: can't use generic parameters from outer function
--> $DIR/type-arg-out-of-scope.rs:3:35 --> $DIR/type-arg-out-of-scope.rs:3:35
@ -14,9 +14,9 @@ error[E0401]: can't use generic parameters from outer function
LL | fn foo<T>(x: T) { LL | fn foo<T>(x: T) {
| - type parameter from outer function | - type parameter from outer function
LL | fn bar(f: Box<dyn FnMut(T) -> T>) { } LL | fn bar(f: Box<dyn FnMut(T) -> T>) { }
| --- ^ use of generic parameter from outer function | - ^ use of generic parameter from outer function
| | | |
| help: try using a local generic parameter instead: `bar<T>` | help: try using a local generic parameter instead: `<T>`
error: aborting due to 2 previous errors error: aborting due to 2 previous errors