Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_resolve/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ resolve_generic_params_from_outer_item =
} from outer item
.refer_to_type_directly = refer to the type directly here instead
.suggestion = try introducing a local generic parameter here
.note = nested items are independent from their parent item for everything except for privacy and name resolution
resolve_generic_params_from_outer_item_const = a `const` is a separate item from the item that contains it
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
has_generic_params,
def_kind,
inner_item,
current_self_ty,
} => {
use errs::GenericParamsFromOuterItemLabel as Label;
let static_or_const = match def_kind {
Expand Down Expand Up @@ -595,7 +596,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
sm,
self.def_span(def_id),
)));
err.refer_to_type_directly = Some(span);
err.refer_to_type_directly =
current_self_ty.map(|snippet| errs::UseTypeDirectly { span, snippet });
return self.dcx().create_err(err);
}
Res::Def(DefKind::TyParam, def_id) => {
Expand Down
17 changes: 15 additions & 2 deletions compiler/rustc_resolve/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ use crate::{Res, fluent_generated as fluent};

#[derive(Diagnostic)]
#[diag(resolve_generic_params_from_outer_item, code = E0401)]
#[note]
pub(crate) struct GenericParamsFromOuterItem {
#[primary_span]
#[label]
pub(crate) span: Span,
#[subdiagnostic]
pub(crate) label: Option<GenericParamsFromOuterItemLabel>,
#[label(resolve_refer_to_type_directly)]
pub(crate) refer_to_type_directly: Option<Span>,
#[subdiagnostic]
pub(crate) refer_to_type_directly: Option<UseTypeDirectly>,
#[subdiagnostic]
pub(crate) sugg: Option<GenericParamsFromOuterItemSugg>,
#[subdiagnostic]
Expand Down Expand Up @@ -68,6 +69,18 @@ pub(crate) struct GenericParamsFromOuterItemSugg {
pub(crate) span: Span,
pub(crate) snippet: String,
}
#[derive(Subdiagnostic)]
#[suggestion(
resolve_refer_to_type_directly,
code = "{snippet}",
applicability = "maybe-incorrect",
style = "verbose"
)]
pub(crate) struct UseTypeDirectly {
#[primary_span]
pub(crate) span: Span,
pub(crate) snippet: String,
}

#[derive(Diagnostic)]
#[diag(resolve_name_is_already_used_as_generic_parameter, code = E0403)]
Expand Down
12 changes: 11 additions & 1 deletion compiler/rustc_resolve/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
i,
rib_ident,
*res,
finalize.map(|finalize| finalize.path_span),
finalize.map(|_| general_span),
*original_rib_ident_def,
ribs,
diag_metadata,
Expand Down Expand Up @@ -1415,6 +1415,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
has_generic_params,
def_kind,
inner_item: item,
current_self_ty: diag_metadata
.and_then(|m| m.current_self_type.as_ref())
.and_then(|ty| {
self.tcx.sess.source_map().span_to_snippet(ty.span).ok()
}),
},
);
}
Expand Down Expand Up @@ -1503,6 +1508,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
has_generic_params,
def_kind,
inner_item: item,
current_self_ty: diag_metadata
.and_then(|m| m.current_self_type.as_ref())
.and_then(|ty| {
self.tcx.sess.source_map().span_to_snippet(ty.span).ok()
}),
},
);
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ pub(crate) struct DiagMetadata<'ast> {
current_trait_assoc_items: Option<&'ast [Box<AssocItem>]>,

/// The current self type if inside an impl (used for better errors).
current_self_type: Option<Ty>,
pub(crate) current_self_type: Option<Ty>,

/// The current self item if inside an ADT (used for better errors).
current_self_item: Option<NodeId>,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ enum ResolutionError<'ra> {
has_generic_params: HasGenericParams,
def_kind: DefKind,
inner_item: Option<(Span, ast::ItemKind)>,
current_self_ty: Option<String>,
},
/// Error E0403: the name is already used for a type or const parameter in this generic
/// parameter list.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error: generic parameters may not be used in const operations
--> $DIR/associated-item-duplicate-bounds.rs:7:18
|
LL | links: [u32; A::LINKS], // Shouldn't suggest bounds already there.
| ^^^^^^^^ cannot perform const operation using `A`
| ^ cannot perform const operation using `A`
|
= note: type parameters may not be used in const expressions
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ LL | fn bar() -> u32 {
LL | X
| ^ use of generic parameter from outer item
|
= note: nested items are independent from their parent item for everything except for privacy and name resolution
help: try introducing a local generic parameter here
|
LL | fn bar<X>() -> u32 {
Expand Down
4 changes: 3 additions & 1 deletion tests/ui/delegation/target-expr.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ LL | reuse Trait::static_method {
| ------------- generic parameter used in this inner delegated function
LL |
LL | let _ = T::Default();
| ^^^^^^^^^^ use of generic parameter from outer item
| ^ use of generic parameter from outer item
|
= note: nested items are independent from their parent item for everything except for privacy and name resolution

error[E0434]: can't capture dynamic environment in a fn item
--> $DIR/target-expr.rs:26:17
Expand Down
15 changes: 11 additions & 4 deletions tests/ui/error-codes/E0401.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ LL | fn bfnr<U, V: Baz<U>, W: Fn()>(y: T) {
| |
| generic parameter used in this inner function
|
= note: nested items are independent from their parent item for everything except for privacy and name resolution
help: try introducing a local generic parameter here
|
LL | fn bfnr<T, U, V: Baz<U>, W: Fn()>(y: T) {
Expand All @@ -25,6 +26,7 @@ LL | fn baz<U,
LL | (y: T) {
| ^ use of generic parameter from outer item
|
= note: nested items are independent from their parent item for everything except for privacy and name resolution
help: try introducing a local generic parameter here
|
LL | fn baz<T, U,
Expand All @@ -37,11 +39,16 @@ LL | impl<T> Iterator for A<T> {
| ---- `Self` type implicitly declared here, by this `impl`
...
LL | fn helper(sel: &Self) -> u8 {
| ------ ^^^^
| | |
| | use of `Self` from outer item
| | refer to the type directly here instead
| ------ ^^^^ use of `Self` from outer item
| |
| `Self` used in this inner function
|
= note: nested items are independent from their parent item for everything except for privacy and name resolution
help: refer to the type directly here instead
|
LL - fn helper(sel: &Self) -> u8 {
LL + fn helper(sel: &A<T>) -> u8 {
|

error: aborting due to 3 previous errors

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ LL | enum E { V(Z) }
| |
| generic parameter used in this inner enum
|
= note: nested items are independent from their parent item for everything except for privacy and name resolution
help: try introducing a local generic parameter here
|
LL | enum E<Z> { V(Z) }
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/generics/generic-params-nested-fn-scope-error.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ LL | fn bar(w: [U]) -> U {
| |
| generic parameter used in this inner function
|
= note: nested items are independent from their parent item for everything except for privacy and name resolution
help: try introducing a local generic parameter here
|
LL | fn bar<U>(w: [U]) -> U {
Expand All @@ -23,6 +24,7 @@ LL | fn bar(w: [U]) -> U {
| |
| generic parameter used in this inner function
|
= note: nested items are independent from their parent item for everything except for privacy and name resolution
help: try introducing a local generic parameter here
|
LL | fn bar<U>(w: [U]) -> U {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/generics/invalid-type-param-default.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0128]: generic parameter defaults cannot reference parameters before they
--> $DIR/invalid-type-param-default.rs:12:12
|
LL | fn mdn<T = T::Item>(_: T) {}
| ^^^^^^^ cannot reference `T` before it is declared
| ^ cannot reference `T` before it is declared

error: defaults for generic parameters are not allowed here
--> $DIR/invalid-type-param-default.rs:7:8
Expand Down
1 change: 1 addition & 0 deletions tests/ui/generics/issue-98432.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ LL | struct _Obligation where T:;
| |
| generic parameter used in this inner struct
|
= note: nested items are independent from their parent item for everything except for privacy and name resolution
help: try introducing a local generic parameter here
|
LL | struct _Obligation<T> where T:;
Expand Down
1 change: 1 addition & 0 deletions tests/ui/resolve/bad-type-env-capture.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ LL | fn bar(b: T) { }
| |
| generic parameter used in this inner function
|
= note: nested items are independent from their parent item for everything except for privacy and name resolution
help: try introducing a local generic parameter here
|
LL | fn bar<T>(b: T) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ error[E0401]: can't use generic parameters from outer item
LL | fn outer<T: Tr>() { // outer function
| - type parameter from outer item
LL | const K: u32 = T::C;
| - ^^^^ use of generic parameter from outer item
| - ^ use of generic parameter from outer item
| |
| generic parameter used in this inner constant item
|
= note: nested items are independent from their parent item for everything except for privacy and name resolution
= note: a `const` is a separate item from the item that contains it

error[E0401]: can't use generic parameters from outer item
Expand All @@ -17,10 +18,11 @@ LL | impl<T> Tr for T { // outer impl block
| - type parameter from outer item
LL | const C: u32 = {
LL | const I: u32 = T::C;
| - ^^^^ use of generic parameter from outer item
| - ^ use of generic parameter from outer item
| |
| generic parameter used in this inner constant item
|
= note: nested items are independent from their parent item for everything except for privacy and name resolution
= note: a `const` is a separate item from the item that contains it

error[E0401]: can't use generic parameters from outer item
Expand All @@ -29,10 +31,11 @@ error[E0401]: can't use generic parameters from outer item
LL | struct S<T: Tr>(U32<{ // outer struct
| - type parameter from outer item
LL | const _: u32 = T::C;
| - ^^^^ use of generic parameter from outer item
| - ^ use of generic parameter from outer item
| |
| generic parameter used in this inner constant item
|
= note: nested items are independent from their parent item for everything except for privacy and name resolution
= note: a `const` is a separate item from the item that contains it

error: aborting due to 3 previous errors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ error[E0401]: can't use generic parameters from outer item
LL | fn outer<T: Tr>() { // outer function
| - type parameter from outer item
LL | const K: u32 = T::C;
| - ^^^^ use of generic parameter from outer item
| - ^ use of generic parameter from outer item
| |
| generic parameter used in this inner constant item
|
= note: nested items are independent from their parent item for everything except for privacy and name resolution
= note: a `const` is a separate item from the item that contains it
help: try introducing a local generic parameter here
|
Expand All @@ -21,10 +22,11 @@ LL | impl<T> Tr for T { // outer impl block
| - type parameter from outer item
LL | const C: u32 = {
LL | const I: u32 = T::C;
| - ^^^^ use of generic parameter from outer item
| - ^ use of generic parameter from outer item
| |
| generic parameter used in this inner constant item
|
= note: nested items are independent from their parent item for everything except for privacy and name resolution
= note: a `const` is a separate item from the item that contains it
help: try introducing a local generic parameter here
|
Expand All @@ -37,10 +39,11 @@ error[E0401]: can't use generic parameters from outer item
LL | struct S<T: Tr>(U32<{ // outer struct
| - type parameter from outer item
LL | const _: u32 = T::C;
| - ^^^^ use of generic parameter from outer item
| - ^ use of generic parameter from outer item
| |
| generic parameter used in this inner constant item
|
= note: nested items are independent from their parent item for everything except for privacy and name resolution
= note: a `const` is a separate item from the item that contains it
help: try introducing a local generic parameter here
|
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/resolve/issue-12796.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ LL | fn inner(_: &Self) {
| | use of `Self` from outer item
| | can't use `Self` here
| `Self` used in this inner function
|
= note: nested items are independent from their parent item for everything except for privacy and name resolution

error: aborting due to 1 previous error

Expand Down
2 changes: 2 additions & 0 deletions tests/ui/resolve/issue-3021-c.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ LL | trait U {
LL | fn g(&self, x: T) -> T;
| ^ use of generic parameter from outer item
|
= note: nested items are independent from their parent item for everything except for privacy and name resolution
help: try introducing a local generic parameter here
|
LL | trait U<T> {
Expand All @@ -25,6 +26,7 @@ LL | trait U {
LL | fn g(&self, x: T) -> T;
| ^ use of generic parameter from outer item
|
= note: nested items are independent from their parent item for everything except for privacy and name resolution
help: try introducing a local generic parameter here
|
LL | trait U<T> {
Expand Down
1 change: 1 addition & 0 deletions tests/ui/resolve/issue-3214.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ LL | struct Foo {
LL | x: T,
| ^ use of generic parameter from outer item
|
= note: nested items are independent from their parent item for everything except for privacy and name resolution
help: try introducing a local generic parameter here
|
LL | struct Foo<T> {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/resolve/issue-39559.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error: generic parameters may not be used in const operations
--> $DIR/issue-39559.rs:14:18
|
LL | entries: [T; D::dim()],
| ^^^^^^ cannot perform const operation using `D`
| ^ cannot perform const operation using `D`
|
= note: type parameters may not be used in const expressions
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ LL | |
LL | | }
| |_____- generic parameter used in this inner extern block
|
= note: nested items are independent from their parent item for everything except for privacy and name resolution
= note: a `static` is a separate item from the item that contains it

error: aborting due to 1 previous error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ LL | |
LL | | }
| |_____- generic parameter used in this inner extern block
|
= note: nested items are independent from their parent item for everything except for privacy and name resolution
= note: a `static` is a separate item from the item that contains it

error[E0401]: can't use generic parameters from outer item
Expand All @@ -22,6 +23,7 @@ LL | static a: *const T = Default::default();
| |
| generic parameter used in this inner static item
|
= note: nested items are independent from their parent item for everything except for privacy and name resolution
= note: a `static` is a separate item from the item that contains it

error[E0401]: can't use generic parameters from outer item
Expand All @@ -36,6 +38,7 @@ LL | |
LL | | }
| |_____- generic parameter used in this inner extern block
|
= note: nested items are independent from their parent item for everything except for privacy and name resolution
= note: a `static` is a separate item from the item that contains it

error[E0401]: can't use generic parameters from outer item
Expand All @@ -48,6 +51,7 @@ LL | static a: [u8; N] = [0; N];
| |
| generic parameter used in this inner static item
|
= note: nested items are independent from their parent item for everything except for privacy and name resolution
= note: a `static` is a separate item from the item that contains it

error[E0401]: can't use generic parameters from outer item
Expand All @@ -60,6 +64,7 @@ LL | static a: [u8; N] = [0; N];
| |
| generic parameter used in this inner static item
|
= note: nested items are independent from their parent item for everything except for privacy and name resolution
= note: a `static` is a separate item from the item that contains it

error: aborting due to 5 previous errors
Expand Down
Loading
Loading