Skip to content

Commit 4e9a95e

Browse files
authored
Unrolled build for rust-lang#119939
Rollup merge of rust-lang#119939 - clubby789:static-const-generic-note, r=compiler-errors Improve 'generic param from outer item' error for `Self` and inside `static`/`const` items Fixes rust-lang#109596 Fixes rust-lang#119936
2 parents 256b6fb + 2cfc817 commit 4e9a95e

16 files changed

+118
-43
lines changed

compiler/rustc_resolve/messages.ftl

+12-2
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,27 @@ resolve_forward_declared_generic_param =
118118
.label = defaulted generic parameters cannot be forward declared
119119
120120
resolve_generic_params_from_outer_item =
121-
can't use generic parameters from outer item
122-
.label = use of generic parameter from outer item
121+
can't use {$is_self ->
122+
[true] `Self`
123+
*[false] generic parameters
124+
} from outer item
125+
.label = use of {$is_self ->
126+
[true] `Self`
127+
*[false] generic parameter
128+
} from outer item
123129
.refer_to_type_directly = refer to the type directly here instead
124130
.suggestion = try introducing a local generic parameter here
125131
132+
resolve_generic_params_from_outer_item_const = a `const` is a separate item from the item that contains it
133+
126134
resolve_generic_params_from_outer_item_const_param = const parameter from outer item
127135
128136
resolve_generic_params_from_outer_item_self_ty_alias = `Self` type implicitly declared here, by this `impl`
129137
130138
resolve_generic_params_from_outer_item_self_ty_param = can't use `Self` here
131139
140+
resolve_generic_params_from_outer_item_static = a `static` is a separate item from the item that contains it
141+
132142
resolve_generic_params_from_outer_item_ty_param = type parameter from outer item
133143
134144

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
@@ -45,6 +45,17 @@ pub(crate) struct GenericParamsFromOuterItem {
4545
pub(crate) refer_to_type_directly: Option<Span>,
4646
#[subdiagnostic]
4747
pub(crate) sugg: Option<GenericParamsFromOuterItemSugg>,
48+
#[subdiagnostic]
49+
pub(crate) static_or_const: Option<GenericParamsFromOuterItemStaticOrConst>,
50+
pub(crate) is_self: bool,
51+
}
52+
53+
#[derive(Subdiagnostic)]
54+
pub(crate) enum GenericParamsFromOuterItemStaticOrConst {
55+
#[note(resolve_generic_params_from_outer_item_static)]
56+
Static,
57+
#[note(resolve_generic_params_from_outer_item_const)]
58+
Const,
4859
}
4960

5061
#[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
@@ -185,7 +185,7 @@ pub(crate) enum RibKind<'a> {
185185
FnOrCoroutine,
186186

187187
/// We passed through an item scope. Disallow upvars.
188-
Item(HasGenericParams),
188+
Item(HasGenericParams, DefKind),
189189

190190
/// We're in a constant item. Can't refer to dynamic stuff.
191191
///
@@ -225,7 +225,7 @@ impl RibKind<'_> {
225225
| RibKind::MacroDefinition(_)
226226
| RibKind::ConstParamTy
227227
| RibKind::InlineAsmSym => false,
228-
RibKind::AssocItem | RibKind::Item(_) | RibKind::ForwardGenericParamBan => true,
228+
RibKind::AssocItem | RibKind::Item(..) | RibKind::ForwardGenericParamBan => true,
229229
}
230230
}
231231

@@ -869,11 +869,12 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
869869
}
870870
fn visit_foreign_item(&mut self, foreign_item: &'ast ForeignItem) {
871871
self.resolve_doc_links(&foreign_item.attrs, MaybeExported::Ok(foreign_item.id));
872+
let def_kind = self.r.local_def_kind(foreign_item.id);
872873
match foreign_item.kind {
873874
ForeignItemKind::TyAlias(box TyAlias { ref generics, .. }) => {
874875
self.with_generic_param_rib(
875876
&generics.params,
876-
RibKind::Item(HasGenericParams::Yes(generics.span)),
877+
RibKind::Item(HasGenericParams::Yes(generics.span), def_kind),
877878
LifetimeRibKind::Generics {
878879
binder: foreign_item.id,
879880
kind: LifetimeBinderKind::Item,
@@ -885,7 +886,7 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
885886
ForeignItemKind::Fn(box Fn { ref generics, .. }) => {
886887
self.with_generic_param_rib(
887888
&generics.params,
888-
RibKind::Item(HasGenericParams::Yes(generics.span)),
889+
RibKind::Item(HasGenericParams::Yes(generics.span), def_kind),
889890
LifetimeRibKind::Generics {
890891
binder: foreign_item.id,
891892
kind: LifetimeBinderKind::Function,
@@ -895,7 +896,7 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
895896
);
896897
}
897898
ForeignItemKind::Static(..) => {
898-
self.with_static_rib(|this| {
899+
self.with_static_rib(def_kind, |this| {
899900
visit::walk_foreign_item(this, foreign_item);
900901
});
901902
}
@@ -2266,10 +2267,11 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
22662267

22672268
fn resolve_adt(&mut self, item: &'ast Item, generics: &'ast Generics) {
22682269
debug!("resolve_adt");
2270+
let kind = self.r.local_def_kind(item.id);
22692271
self.with_current_self_item(item, |this| {
22702272
this.with_generic_param_rib(
22712273
&generics.params,
2272-
RibKind::Item(HasGenericParams::Yes(generics.span)),
2274+
RibKind::Item(HasGenericParams::Yes(generics.span), kind),
22732275
LifetimeRibKind::Generics {
22742276
binder: item.id,
22752277
kind: LifetimeBinderKind::Item,
@@ -2343,11 +2345,12 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
23432345
let name = item.ident.name;
23442346
debug!("(resolving item) resolving {} ({:?})", name, item.kind);
23452347

2348+
let def_kind = self.r.local_def_kind(item.id);
23462349
match item.kind {
23472350
ItemKind::TyAlias(box TyAlias { ref generics, .. }) => {
23482351
self.with_generic_param_rib(
23492352
&generics.params,
2350-
RibKind::Item(HasGenericParams::Yes(generics.span)),
2353+
RibKind::Item(HasGenericParams::Yes(generics.span), def_kind),
23512354
LifetimeRibKind::Generics {
23522355
binder: item.id,
23532356
kind: LifetimeBinderKind::Item,
@@ -2360,7 +2363,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
23602363
ItemKind::Fn(box Fn { ref generics, .. }) => {
23612364
self.with_generic_param_rib(
23622365
&generics.params,
2363-
RibKind::Item(HasGenericParams::Yes(generics.span)),
2366+
RibKind::Item(HasGenericParams::Yes(generics.span), def_kind),
23642367
LifetimeRibKind::Generics {
23652368
binder: item.id,
23662369
kind: LifetimeBinderKind::Function,
@@ -2399,7 +2402,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
23992402
// Create a new rib for the trait-wide type parameters.
24002403
self.with_generic_param_rib(
24012404
&generics.params,
2402-
RibKind::Item(HasGenericParams::Yes(generics.span)),
2405+
RibKind::Item(HasGenericParams::Yes(generics.span), def_kind),
24032406
LifetimeRibKind::Generics {
24042407
binder: item.id,
24052408
kind: LifetimeBinderKind::Item,
@@ -2420,7 +2423,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
24202423
// Create a new rib for the trait-wide type parameters.
24212424
self.with_generic_param_rib(
24222425
&generics.params,
2423-
RibKind::Item(HasGenericParams::Yes(generics.span)),
2426+
RibKind::Item(HasGenericParams::Yes(generics.span), def_kind),
24242427
LifetimeRibKind::Generics {
24252428
binder: item.id,
24262429
kind: LifetimeBinderKind::Item,
@@ -2454,7 +2457,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
24542457
}
24552458

24562459
ItemKind::Static(box ast::StaticItem { ref ty, ref expr, .. }) => {
2457-
self.with_static_rib(|this| {
2460+
self.with_static_rib(def_kind, |this| {
24582461
this.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Static), |this| {
24592462
this.visit_ty(ty);
24602463
});
@@ -2469,11 +2472,14 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
24692472
ItemKind::Const(box ast::ConstItem { ref generics, ref ty, ref expr, .. }) => {
24702473
self.with_generic_param_rib(
24712474
&generics.params,
2472-
RibKind::Item(if self.r.tcx.features().generic_const_items {
2473-
HasGenericParams::Yes(generics.span)
2474-
} else {
2475-
HasGenericParams::No
2476-
}),
2475+
RibKind::Item(
2476+
if self.r.tcx.features().generic_const_items {
2477+
HasGenericParams::Yes(generics.span)
2478+
} else {
2479+
HasGenericParams::No
2480+
},
2481+
def_kind,
2482+
),
24772483
LifetimeRibKind::Generics {
24782484
binder: item.id,
24792485
kind: LifetimeBinderKind::ConstItem,
@@ -2558,7 +2564,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
25582564
let mut add_bindings_for_ns = |ns| {
25592565
let parent_rib = self.ribs[ns]
25602566
.iter()
2561-
.rfind(|r| matches!(r.kind, RibKind::Item(_)))
2567+
.rfind(|r| matches!(r.kind, RibKind::Item(..)))
25622568
.expect("associated item outside of an item");
25632569
seen_bindings.extend(parent_rib.bindings.keys().map(|ident| (*ident, ident.span)));
25642570
};
@@ -2693,8 +2699,8 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
26932699
self.label_ribs.pop();
26942700
}
26952701

2696-
fn with_static_rib(&mut self, f: impl FnOnce(&mut Self)) {
2697-
let kind = RibKind::Item(HasGenericParams::No);
2702+
fn with_static_rib(&mut self, def_kind: DefKind, f: impl FnOnce(&mut Self)) {
2703+
let kind = RibKind::Item(HasGenericParams::No, def_kind);
26982704
self.with_rib(ValueNS, kind, |this| this.with_rib(TypeNS, kind, f))
26992705
}
27002706

@@ -2875,7 +2881,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
28752881
// If applicable, create a rib for the type parameters.
28762882
self.with_generic_param_rib(
28772883
&generics.params,
2878-
RibKind::Item(HasGenericParams::Yes(generics.span)),
2884+
RibKind::Item(HasGenericParams::Yes(generics.span), self.r.local_def_kind(item_id)),
28792885
LifetimeRibKind::Generics {
28802886
span: generics.span,
28812887
binder: item_id,

compiler/rustc_resolve/src/lib.rs

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

1220+
fn local_def_kind(&self, node: NodeId) -> DefKind {
1221+
self.tcx.def_kind(self.local_def_id(node))
1222+
}
1223+
12201224
/// Adds a definition with a parent definition.
12211225
fn create_def(
12221226
&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]: type 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)