Skip to content

Commit cd73914

Browse files
committed
add AnonConstKind to non_trivial_const_arg diagnostics
1 parent 292be5c commit cd73914

File tree

7 files changed

+39
-16
lines changed

7 files changed

+39
-16
lines changed

compiler/rustc_resolve/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ resolve_const_param_in_enum_discriminant =
120120
const parameters may not be used in enum discriminant values
121121
122122
resolve_const_param_in_non_trivial_anon_const =
123-
const parameters may only be used as standalone arguments here, i.e. `{$name}`
123+
const parameters may only be used as standalone arguments in {$place}, i.e. `{$name}`
124124
125125
resolve_constructor_private_if_any_field_private =
126126
a constructor is private if any of the fields is private

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10071007
ResolutionError::ParamInTyOfConstParam { name } => {
10081008
self.dcx().create_err(errs::ParamInTyOfConstParam { span, name })
10091009
}
1010-
ResolutionError::ParamInNonTrivialAnonConst { name, param_kind: is_type } => {
1010+
ResolutionError::ParamInNonTrivialAnonConst { name, param_kind: is_type , place} => {
10111011
self.dcx().create_err(errs::ParamInNonTrivialAnonConst {
10121012
span,
10131013
name,
@@ -1017,6 +1017,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10171017
.sess
10181018
.is_nightly_build()
10191019
.then_some(errs::ParamInNonTrivialAnonConstHelp),
1020+
place: place.unwrap(),
10201021
})
10211022
}
10221023
ResolutionError::ParamInEnumDiscriminant { name, param_kind: is_type } => self

compiler/rustc_resolve/src/errors.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_errors::{
66
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
77
use rustc_span::{Ident, Span, Symbol};
88

9-
use crate::late::PatternSource;
9+
use crate::late::{PatternSource, AnonConstKind};
1010
use crate::{Res, fluent_generated as fluent};
1111

1212
#[derive(Diagnostic)]
@@ -387,6 +387,7 @@ pub(crate) struct ParamInNonTrivialAnonConst {
387387
pub(crate) param_kind: ParamKindInNonTrivialAnonConst,
388388
#[subdiagnostic]
389389
pub(crate) help: Option<ParamInNonTrivialAnonConstHelp>,
390+
pub(crate) place: AnonConstKind
390391
}
391392

392393
#[derive(Subdiagnostic)]

compiler/rustc_resolve/src/ident.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
12391239
res_err = Some((span, CannotCaptureDynamicEnvironmentInFnItem));
12401240
}
12411241
}
1242-
RibKind::ConstantItem(_, item) => {
1242+
RibKind::ConstantItem(_, item, _) => {
12431243
// Still doesn't deal with upvars
12441244
if let Some(span) = finalize {
12451245
let (span, resolution_error) = match item {
@@ -1340,7 +1340,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
13401340
}
13411341
}
13421342

1343-
RibKind::ConstantItem(trivial, _) => {
1343+
RibKind::ConstantItem(trivial, _, kind) => {
13441344
if let ConstantHasGenerics::No(cause) = trivial {
13451345
// HACK(min_const_generics): If we encounter `Self` in an anonymous
13461346
// constant we can't easily tell if it's generic at this stage, so
@@ -1371,6 +1371,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
13711371
name: rib_ident.name,
13721372
param_kind:
13731373
ParamKindInNonTrivialAnonConst::Type,
1374+
place: kind,
1375+
13741376
}
13751377
}
13761378
};
@@ -1431,7 +1433,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14311433
}
14321434
}
14331435

1434-
RibKind::ConstantItem(trivial, _) => {
1436+
RibKind::ConstantItem(trivial, _, kind) => {
14351437
if let ConstantHasGenerics::No(cause) = trivial {
14361438
if let Some(span) = finalize {
14371439
let error = match cause {
@@ -1447,6 +1449,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14471449
param_kind: ParamKindInNonTrivialAnonConst::Const {
14481450
name: rib_ident.name,
14491451
},
1452+
place: kind,
14501453
}
14511454
}
14521455
};

compiler/rustc_resolve/src/late.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub(crate) enum PatternSource {
6464
}
6565

6666
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
67-
enum IsRepeatExpr {
67+
pub(crate) enum IsRepeatExpr {
6868
No,
6969
Yes,
7070
}
@@ -74,13 +74,28 @@ struct IsNeverPattern;
7474
/// Describes whether an `AnonConst` is a type level const arg or
7575
/// some other form of anon const (i.e. inline consts or enum discriminants)
7676
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
77-
enum AnonConstKind {
77+
pub(crate) enum AnonConstKind {
7878
EnumDiscriminant,
7979
FieldDefaultValue,
8080
InlineConst,
8181
ConstArg(IsRepeatExpr),
8282
}
8383

84+
impl IntoDiagArg for AnonConstKind {
85+
fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
86+
DiagArgValue::Str(Cow::Borrowed(match self {
87+
AnonConstKind::EnumDiscriminant => "enum discriminant",
88+
AnonConstKind::FieldDefaultValue => "field default value",
89+
AnonConstKind::InlineConst => "inline const",
90+
AnonConstKind::ConstArg(is_repeat_expr) => match is_repeat_expr {
91+
IsRepeatExpr::No => "array repeat expression",
92+
IsRepeatExpr::Yes => "const generic args",
93+
}
94+
},
95+
))
96+
}
97+
}
98+
8499
impl PatternSource {
85100
fn descr(self) -> &'static str {
86101
match self {
@@ -214,7 +229,7 @@ pub(crate) enum RibKind<'ra> {
214229
///
215230
/// The item may reference generic parameters in trivial constant expressions.
216231
/// All other constants aren't allowed to use generic params at all.
217-
ConstantItem(ConstantHasGenerics, Option<(Ident, ConstantItemKind)>),
232+
ConstantItem(ConstantHasGenerics, Option<(Ident, ConstantItemKind)>, Option<AnonConstKind>),
218233

219234
/// We passed through a module item.
220235
Module(Module<'ra>),
@@ -2989,19 +3004,21 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
29893004
&mut self,
29903005
is_repeat: IsRepeatExpr,
29913006
may_use_generics: ConstantHasGenerics,
3007+
anon_const_kind: Option<AnonConstKind>,
29923008
item: Option<(Ident, ConstantItemKind)>,
29933009
f: impl FnOnce(&mut Self),
29943010
) {
29953011
let f = |this: &mut Self| {
2996-
this.with_rib(ValueNS, RibKind::ConstantItem(may_use_generics, item), |this| {
3012+
this.with_rib(ValueNS, RibKind::ConstantItem(may_use_generics, item, anon_const_kind), |this| {
29973013
this.with_rib(
29983014
TypeNS,
29993015
RibKind::ConstantItem(
30003016
may_use_generics.force_yes_if(is_repeat == IsRepeatExpr::Yes),
30013017
item,
3018+
anon_const_kind,
30023019
),
30033020
|this| {
3004-
this.with_label_rib(RibKind::ConstantItem(may_use_generics, item), f);
3021+
this.with_label_rib(RibKind::ConstantItem(may_use_generics, item, anon_const_kind), f);
30053022
},
30063023
)
30073024
})
@@ -3518,7 +3535,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
35183535

35193536
fn resolve_const_body(&mut self, expr: &'ast Expr, item: Option<(Ident, ConstantItemKind)>) {
35203537
self.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Infer), |this| {
3521-
this.with_constant_rib(IsRepeatExpr::No, ConstantHasGenerics::Yes, item, |this| {
3538+
this.with_constant_rib(IsRepeatExpr::No, ConstantHasGenerics::Yes, None, item, |this| {
35223539
this.visit_expr(expr)
35233540
});
35243541
})
@@ -4728,7 +4745,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
47284745
}
47294746
};
47304747

4731-
self.with_constant_rib(is_repeat_expr, may_use_generics, None, |this| {
4748+
self.with_constant_rib(is_repeat_expr, may_use_generics, Some(anon_const_kind), None, |this| {
47324749
this.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Infer), |this| {
47334750
resolve_expr(this);
47344751
});

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ use tracing::debug;
3333
use super::NoConstantGenericsReason;
3434
use crate::diagnostics::{ImportSuggestion, LabelSuggestion, TypoSuggestion};
3535
use crate::late::{
36-
AliasPossibility, LateResolutionVisitor, LifetimeBinderKind, LifetimeRes, LifetimeRibKind,
37-
LifetimeUseSet, QSelf, RibKind,
36+
AliasPossibility, AnonConstKind, LateResolutionVisitor, LifetimeBinderKind, LifetimeRes, LifetimeRibKind, LifetimeUseSet, QSelf, RibKind
3837
};
3938
use crate::ty::fast_reject::SimplifiedType;
4039
use crate::{
@@ -3344,6 +3343,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
33443343
.sess
33453344
.is_nightly_build()
33463345
.then_some(errors::ParamInNonTrivialAnonConstHelp),
3346+
place: AnonConstKind::InlineConst,
33473347
})
33483348
.emit();
33493349
}

compiler/rustc_resolve/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ pub mod rustdoc;
9797
pub use macros::registered_tools_ast;
9898

9999
use crate::ref_mut::{CmCell, CmRefCell};
100+
use crate::late::AnonConstKind;
100101

101102
rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
102103

@@ -301,7 +302,7 @@ enum ResolutionError<'ra> {
301302
/// generic parameters must not be used inside const evaluations.
302303
///
303304
/// This error is only emitted when using `min_const_generics`.
304-
ParamInNonTrivialAnonConst { name: Symbol, param_kind: ParamKindInNonTrivialAnonConst },
305+
ParamInNonTrivialAnonConst { name: Symbol, param_kind: ParamKindInNonTrivialAnonConst, place: Option<AnonConstKind> },
305306
/// generic parameters must not be used inside enum discriminants.
306307
///
307308
/// This error is emitted even with `generic_const_exprs`.

0 commit comments

Comments
 (0)