Skip to content

Commit ca81fe2

Browse files
committed
Auto merge of #119939 - clubby789:static-const-generic-note, r=<try>
Improve 'generic param from outer item' error for `Self` and inside `static`/`const` items Fixes #109596 Fixes #119936
2 parents 8847bda + 2cfc817 commit ca81fe2

16 files changed

+118
-43
lines changed

compiler/rustc_resolve/messages.ftl

+12-2
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,27 @@ resolve_forward_declared_generic_param =
114114
.label = defaulted generic parameters cannot be forward declared
115115
116116
resolve_generic_params_from_outer_item =
117-
can't use generic parameters from outer item
118-
.label = use of generic parameter from outer item
117+
can't use {$is_self ->
118+
[true] `Self`
119+
*[false] generic parameters
120+
} from outer item
121+
.label = use of {$is_self ->
122+
[true] `Self`
123+
*[false] generic parameter
124+
} from outer item
119125
.refer_to_type_directly = refer to the type directly here instead
120126
.suggestion = try introducing a local generic parameter here
121127
128+
resolve_generic_params_from_outer_item_const = a `const` is a separate item from the item that contains it
129+
122130
resolve_generic_params_from_outer_item_const_param = const parameter from outer item
123131
124132
resolve_generic_params_from_outer_item_self_ty_alias = `Self` type implicitly declared here, by this `impl`
125133
126134
resolve_generic_params_from_outer_item_self_ty_param = can't use `Self` here
127135
136+
resolve_generic_params_from_outer_item_static = a `static` is a separate item from the item that contains it
137+
128138
resolve_generic_params_from_outer_item_ty_param = type parameter from outer item
129139
130140

compiler/rustc_resolve/src/diagnostics.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -561,13 +561,21 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
561561
resolution_error: ResolutionError<'a>,
562562
) -> DiagnosticBuilder<'_> {
563563
match resolution_error {
564-
ResolutionError::GenericParamsFromOuterItem(outer_res, has_generic_params) => {
564+
ResolutionError::GenericParamsFromOuterItem(outer_res, has_generic_params, def_kind) => {
565565
use errs::GenericParamsFromOuterItemLabel as Label;
566+
let static_or_const = match def_kind {
567+
DefKind::Static(_) => Some(errs::GenericParamsFromOuterItemStaticOrConst::Static),
568+
DefKind::Const => Some(errs::GenericParamsFromOuterItemStaticOrConst::Const),
569+
_ => None,
570+
};
571+
let is_self = matches!(outer_res, Res::SelfTyParam { .. } | Res::SelfTyAlias { .. });
566572
let mut err = errs::GenericParamsFromOuterItem {
567573
span,
568574
label: None,
569575
refer_to_type_directly: None,
570576
sugg: None,
577+
static_or_const,
578+
is_self,
571579
};
572580

573581
let sm = self.tcx.sess.source_map();

compiler/rustc_resolve/src/errors.rs

+11
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ pub(crate) struct GenericParamsFromOuterItem {
4444
pub(crate) refer_to_type_directly: Option<Span>,
4545
#[subdiagnostic]
4646
pub(crate) sugg: Option<GenericParamsFromOuterItemSugg>,
47+
#[subdiagnostic]
48+
pub(crate) static_or_const: Option<GenericParamsFromOuterItemStaticOrConst>,
49+
pub(crate) is_self: bool,
50+
}
51+
52+
#[derive(Subdiagnostic)]
53+
pub(crate) enum GenericParamsFromOuterItemStaticOrConst {
54+
#[note(resolve_generic_params_from_outer_item_static)]
55+
Static,
56+
#[note(resolve_generic_params_from_outer_item_const)]
57+
Const,
4758
}
4859

4960
#[derive(Subdiagnostic)]

compiler/rustc_resolve/src/ident.rs

+20-10
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ use rustc_span::symbol::{kw, Ident};
1010
use rustc_span::Span;
1111

1212
use crate::errors::{ParamKindInEnumDiscriminant, ParamKindInNonTrivialAnonConst};
13-
use crate::late::{
14-
ConstantHasGenerics, HasGenericParams, NoConstantGenericsReason, PathSource, Rib, RibKind,
15-
};
13+
use crate::late::{ConstantHasGenerics, NoConstantGenericsReason, PathSource, Rib, RibKind};
1614
use crate::macros::{sub_namespace_match, MacroRulesScope};
1715
use crate::BindingKey;
1816
use crate::{errors, AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, Determinacy, Finalize};
@@ -1090,7 +1088,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
10901088
| RibKind::ForwardGenericParamBan => {
10911089
// Nothing to do. Continue.
10921090
}
1093-
RibKind::Item(_) | RibKind::AssocItem => {
1091+
RibKind::Item(..) | RibKind::AssocItem => {
10941092
// This was an attempt to access an upvar inside a
10951093
// named function item. This is not allowed, so we
10961094
// report an error.
@@ -1155,7 +1153,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
11551153
}
11561154
Res::Def(DefKind::TyParam, _) | Res::SelfTyParam { .. } | Res::SelfTyAlias { .. } => {
11571155
for rib in ribs {
1158-
let has_generic_params: HasGenericParams = match rib.kind {
1156+
let (has_generic_params, def_kind) = match rib.kind {
11591157
RibKind::Normal
11601158
| RibKind::FnOrCoroutine
11611159
| RibKind::Module(..)
@@ -1213,7 +1211,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
12131211
}
12141212

12151213
// This was an attempt to use a type parameter outside its scope.
1216-
RibKind::Item(has_generic_params) => has_generic_params,
1214+
RibKind::Item(has_generic_params, def_kind) => {
1215+
(has_generic_params, def_kind)
1216+
}
12171217
RibKind::ConstParamTy => {
12181218
if let Some(span) = finalize {
12191219
self.report_error(
@@ -1231,15 +1231,19 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
12311231
if let Some(span) = finalize {
12321232
self.report_error(
12331233
span,
1234-
ResolutionError::GenericParamsFromOuterItem(res, has_generic_params),
1234+
ResolutionError::GenericParamsFromOuterItem(
1235+
res,
1236+
has_generic_params,
1237+
def_kind,
1238+
),
12351239
);
12361240
}
12371241
return Res::Err;
12381242
}
12391243
}
12401244
Res::Def(DefKind::ConstParam, _) => {
12411245
for rib in ribs {
1242-
let has_generic_params = match rib.kind {
1246+
let (has_generic_params, def_kind) = match rib.kind {
12431247
RibKind::Normal
12441248
| RibKind::FnOrCoroutine
12451249
| RibKind::Module(..)
@@ -1276,7 +1280,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
12761280
continue;
12771281
}
12781282

1279-
RibKind::Item(has_generic_params) => has_generic_params,
1283+
RibKind::Item(has_generic_params, def_kind) => {
1284+
(has_generic_params, def_kind)
1285+
}
12801286
RibKind::ConstParamTy => {
12811287
if let Some(span) = finalize {
12821288
self.report_error(
@@ -1295,7 +1301,11 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
12951301
if let Some(span) = finalize {
12961302
self.report_error(
12971303
span,
1298-
ResolutionError::GenericParamsFromOuterItem(res, has_generic_params),
1304+
ResolutionError::GenericParamsFromOuterItem(
1305+
res,
1306+
has_generic_params,
1307+
def_kind,
1308+
),
12991309
);
13001310
}
13011311
return Res::Err;

compiler/rustc_resolve/src/late.rs

+26-20
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ pub(crate) enum RibKind<'a> {
181181
FnOrCoroutine,
182182

183183
/// We passed through an item scope. Disallow upvars.
184-
Item(HasGenericParams),
184+
Item(HasGenericParams, DefKind),
185185

186186
/// We're in a constant item. Can't refer to dynamic stuff.
187187
///
@@ -221,7 +221,7 @@ impl RibKind<'_> {
221221
| RibKind::MacroDefinition(_)
222222
| RibKind::ConstParamTy
223223
| RibKind::InlineAsmSym => false,
224-
RibKind::AssocItem | RibKind::Item(_) | RibKind::ForwardGenericParamBan => true,
224+
RibKind::AssocItem | RibKind::Item(..) | RibKind::ForwardGenericParamBan => true,
225225
}
226226
}
227227

@@ -866,11 +866,12 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
866866
}
867867
fn visit_foreign_item(&mut self, foreign_item: &'ast ForeignItem) {
868868
self.resolve_doc_links(&foreign_item.attrs, MaybeExported::Ok(foreign_item.id));
869+
let def_kind = self.r.local_def_kind(foreign_item.id);
869870
match foreign_item.kind {
870871
ForeignItemKind::TyAlias(box TyAlias { ref generics, .. }) => {
871872
self.with_generic_param_rib(
872873
&generics.params,
873-
RibKind::Item(HasGenericParams::Yes(generics.span)),
874+
RibKind::Item(HasGenericParams::Yes(generics.span), def_kind),
874875
LifetimeRibKind::Generics {
875876
binder: foreign_item.id,
876877
kind: LifetimeBinderKind::Item,
@@ -882,7 +883,7 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
882883
ForeignItemKind::Fn(box Fn { ref generics, .. }) => {
883884
self.with_generic_param_rib(
884885
&generics.params,
885-
RibKind::Item(HasGenericParams::Yes(generics.span)),
886+
RibKind::Item(HasGenericParams::Yes(generics.span), def_kind),
886887
LifetimeRibKind::Generics {
887888
binder: foreign_item.id,
888889
kind: LifetimeBinderKind::Function,
@@ -892,7 +893,7 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
892893
);
893894
}
894895
ForeignItemKind::Static(..) => {
895-
self.with_static_rib(|this| {
896+
self.with_static_rib(def_kind, |this| {
896897
visit::walk_foreign_item(this, foreign_item);
897898
});
898899
}
@@ -2268,10 +2269,11 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
22682269

22692270
fn resolve_adt(&mut self, item: &'ast Item, generics: &'ast Generics) {
22702271
debug!("resolve_adt");
2272+
let kind = self.r.local_def_kind(item.id);
22712273
self.with_current_self_item(item, |this| {
22722274
this.with_generic_param_rib(
22732275
&generics.params,
2274-
RibKind::Item(HasGenericParams::Yes(generics.span)),
2276+
RibKind::Item(HasGenericParams::Yes(generics.span), kind),
22752277
LifetimeRibKind::Generics {
22762278
binder: item.id,
22772279
kind: LifetimeBinderKind::Item,
@@ -2345,11 +2347,12 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
23452347
let name = item.ident.name;
23462348
debug!("(resolving item) resolving {} ({:?})", name, item.kind);
23472349

2350+
let def_kind = self.r.local_def_kind(item.id);
23482351
match item.kind {
23492352
ItemKind::TyAlias(box TyAlias { ref generics, .. }) => {
23502353
self.with_generic_param_rib(
23512354
&generics.params,
2352-
RibKind::Item(HasGenericParams::Yes(generics.span)),
2355+
RibKind::Item(HasGenericParams::Yes(generics.span), def_kind),
23532356
LifetimeRibKind::Generics {
23542357
binder: item.id,
23552358
kind: LifetimeBinderKind::Item,
@@ -2362,7 +2365,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
23622365
ItemKind::Fn(box Fn { ref generics, .. }) => {
23632366
self.with_generic_param_rib(
23642367
&generics.params,
2365-
RibKind::Item(HasGenericParams::Yes(generics.span)),
2368+
RibKind::Item(HasGenericParams::Yes(generics.span), def_kind),
23662369
LifetimeRibKind::Generics {
23672370
binder: item.id,
23682371
kind: LifetimeBinderKind::Function,
@@ -2401,7 +2404,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
24012404
// Create a new rib for the trait-wide type parameters.
24022405
self.with_generic_param_rib(
24032406
&generics.params,
2404-
RibKind::Item(HasGenericParams::Yes(generics.span)),
2407+
RibKind::Item(HasGenericParams::Yes(generics.span), def_kind),
24052408
LifetimeRibKind::Generics {
24062409
binder: item.id,
24072410
kind: LifetimeBinderKind::Item,
@@ -2422,7 +2425,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
24222425
// Create a new rib for the trait-wide type parameters.
24232426
self.with_generic_param_rib(
24242427
&generics.params,
2425-
RibKind::Item(HasGenericParams::Yes(generics.span)),
2428+
RibKind::Item(HasGenericParams::Yes(generics.span), def_kind),
24262429
LifetimeRibKind::Generics {
24272430
binder: item.id,
24282431
kind: LifetimeBinderKind::Item,
@@ -2456,7 +2459,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
24562459
}
24572460

24582461
ItemKind::Static(box ast::StaticItem { ref ty, ref expr, .. }) => {
2459-
self.with_static_rib(|this| {
2462+
self.with_static_rib(def_kind, |this| {
24602463
this.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Static), |this| {
24612464
this.visit_ty(ty);
24622465
});
@@ -2471,11 +2474,14 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
24712474
ItemKind::Const(box ast::ConstItem { ref generics, ref ty, ref expr, .. }) => {
24722475
self.with_generic_param_rib(
24732476
&generics.params,
2474-
RibKind::Item(if self.r.tcx.features().generic_const_items {
2475-
HasGenericParams::Yes(generics.span)
2476-
} else {
2477-
HasGenericParams::No
2478-
}),
2477+
RibKind::Item(
2478+
if self.r.tcx.features().generic_const_items {
2479+
HasGenericParams::Yes(generics.span)
2480+
} else {
2481+
HasGenericParams::No
2482+
},
2483+
def_kind,
2484+
),
24792485
LifetimeRibKind::Generics {
24802486
binder: item.id,
24812487
kind: LifetimeBinderKind::ConstItem,
@@ -2560,7 +2566,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
25602566
let mut add_bindings_for_ns = |ns| {
25612567
let parent_rib = self.ribs[ns]
25622568
.iter()
2563-
.rfind(|r| matches!(r.kind, RibKind::Item(_)))
2569+
.rfind(|r| matches!(r.kind, RibKind::Item(..)))
25642570
.expect("associated item outside of an item");
25652571
seen_bindings.extend(parent_rib.bindings.keys().map(|ident| (*ident, ident.span)));
25662572
};
@@ -2695,8 +2701,8 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
26952701
self.label_ribs.pop();
26962702
}
26972703

2698-
fn with_static_rib(&mut self, f: impl FnOnce(&mut Self)) {
2699-
let kind = RibKind::Item(HasGenericParams::No);
2704+
fn with_static_rib(&mut self, def_kind: DefKind, f: impl FnOnce(&mut Self)) {
2705+
let kind = RibKind::Item(HasGenericParams::No, def_kind);
27002706
self.with_rib(ValueNS, kind, |this| this.with_rib(TypeNS, kind, f))
27012707
}
27022708

@@ -2877,7 +2883,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
28772883
// If applicable, create a rib for the type parameters.
28782884
self.with_generic_param_rib(
28792885
&generics.params,
2880-
RibKind::Item(HasGenericParams::Yes(generics.span)),
2886+
RibKind::Item(HasGenericParams::Yes(generics.span), self.r.local_def_kind(item_id)),
28812887
LifetimeRibKind::Generics {
28822888
span: generics.span,
28832889
binder: item_id,

compiler/rustc_resolve/src/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ struct BindingError {
185185
#[derive(Debug)]
186186
enum ResolutionError<'a> {
187187
/// Error E0401: can't use type or const parameters from outer item.
188-
GenericParamsFromOuterItem(Res, HasGenericParams),
188+
GenericParamsFromOuterItem(Res, HasGenericParams, DefKind),
189189
/// Error E0403: the name is already used for a type or const parameter in this generic
190190
/// parameter list.
191191
NameAlreadyUsedInParameterList(Symbol, Span),
@@ -1216,6 +1216,10 @@ impl<'tcx> Resolver<'_, 'tcx> {
12161216
self.opt_local_def_id(node).unwrap_or_else(|| panic!("no entry for node id: `{node:?}`"))
12171217
}
12181218

1219+
fn local_def_kind(&self, node: NodeId) -> DefKind {
1220+
self.tcx.def_kind(self.local_def_id(node))
1221+
}
1222+
12191223
/// Adds a definition with a parent definition.
12201224
fn create_def(
12211225
&mut self,

tests/ui/error-codes/E0401.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ LL | fn baz<U,
2020
LL | (y: T) {
2121
| ^ use of generic parameter from outer item
2222

23-
error[E0401]: can't use generic parameters from outer item
23+
error[E0401]: can't use `Self` from outer item
2424
--> $DIR/E0401.rs:24:25
2525
|
2626
LL | impl<T> Iterator for A<T> {
@@ -29,7 +29,7 @@ LL | impl<T> Iterator for A<T> {
2929
LL | fn helper(sel: &Self) -> u8 {
3030
| ^^^^
3131
| |
32-
| use of generic parameter from outer item
32+
| use of `Self` from outer item
3333
| refer to the type directly here instead
3434

3535
error[E0283]: type annotations needed

tests/ui/inner-static-type-parameter.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ LL | fn foo<T>() {
55
| - type parameter from outer item
66
LL | static a: Bar<T> = Bar::What;
77
| ^ use of generic parameter from outer item
8+
|
9+
= note: a `static` is a separate item from the item that contains it
810

911
error[E0392]: parameter `T` is never used
1012
--> $DIR/inner-static-type-parameter.rs:3:10

tests/ui/resolve/generic-params-from-outer-item-in-const-item.default.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ LL | fn outer<T: Tr>() { // outer function
55
| - type parameter from outer item
66
LL | const K: u32 = T::C;
77
| ^^^^ use of generic parameter from outer item
8+
|
9+
= note: a `const` is a separate item from the item that contains it
810

911
error[E0401]: can't use generic parameters from outer item
1012
--> $DIR/generic-params-from-outer-item-in-const-item.rs:19:24
@@ -14,6 +16,8 @@ LL | impl<T> Tr for T { // outer impl block
1416
LL | const C: u32 = {
1517
LL | const I: u32 = T::C;
1618
| ^^^^ use of generic parameter from outer item
19+
|
20+
= note: a `const` is a separate item from the item that contains it
1721

1822
error[E0401]: can't use generic parameters from outer item
1923
--> $DIR/generic-params-from-outer-item-in-const-item.rs:27:20
@@ -22,6 +26,8 @@ LL | struct S<T: Tr>(U32<{ // outer struct
2226
| - type parameter from outer item
2327
LL | const _: u32 = T::C;
2428
| ^^^^ use of generic parameter from outer item
29+
|
30+
= note: a `const` is a separate item from the item that contains it
2531

2632
error: aborting due to 3 previous errors
2733

0 commit comments

Comments
 (0)