Skip to content

add AnonConstKind to non_trivial_const_arg diagnostics #144324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ resolve_const_param_in_enum_discriminant =
const parameters may not be used in enum discriminant values

resolve_const_param_in_non_trivial_anon_const =
const parameters may only be used as standalone arguments here, i.e. `{$name}`
const parameters may only be used as standalone arguments in {$place}, i.e. `{$name}`

resolve_constructor_private_if_any_field_private =
a constructor is private if any of the fields is private
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
ResolutionError::ParamInTyOfConstParam { name } => {
self.dcx().create_err(errs::ParamInTyOfConstParam { span, name })
}
ResolutionError::ParamInNonTrivialAnonConst { name, param_kind: is_type } => {
ResolutionError::ParamInNonTrivialAnonConst { name, param_kind: is_type, place } => {
self.dcx().create_err(errs::ParamInNonTrivialAnonConst {
span,
name,
Expand All @@ -910,6 +910,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
.sess
.is_nightly_build()
.then_some(errs::ParamInNonTrivialAnonConstHelp),
place: place.unwrap(),
})
}
ResolutionError::ParamInEnumDiscriminant { name, param_kind: is_type } => self
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_resolve/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rustc_macros::{Diagnostic, Subdiagnostic};
use rustc_span::{Ident, Span, Symbol};

use crate::Res;
use crate::late::PatternSource;
use crate::late::{AnonConstKind, PatternSource};

#[derive(Diagnostic)]
#[diag(resolve_generic_params_from_outer_item, code = E0401)]
Expand Down Expand Up @@ -384,6 +384,7 @@ pub(crate) struct ParamInNonTrivialAnonConst {
pub(crate) param_kind: ParamKindInNonTrivialAnonConst,
#[subdiagnostic]
pub(crate) help: Option<ParamInNonTrivialAnonConstHelp>,
pub(crate) place: AnonConstKind,
}

#[derive(Subdiagnostic)]
Expand Down
8 changes: 5 additions & 3 deletions compiler/rustc_resolve/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
res_err = Some((span, CannotCaptureDynamicEnvironmentInFnItem));
}
}
RibKind::ConstantItem(_, item) => {
RibKind::ConstantItem(_, item, _) => {
// Still doesn't deal with upvars
if let Some(span) = finalize {
let (span, resolution_error) = match item {
Expand Down Expand Up @@ -1260,7 +1260,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
}
}

RibKind::ConstantItem(trivial, _) => {
RibKind::ConstantItem(trivial, _, kind) => {
if let ConstantHasGenerics::No(cause) = trivial {
// HACK(min_const_generics): If we encounter `Self` in an anonymous
// constant we can't easily tell if it's generic at this stage, so
Expand Down Expand Up @@ -1291,6 +1291,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
name: rib_ident.name,
param_kind:
ParamKindInNonTrivialAnonConst::Type,
place: kind,
}
}
};
Expand Down Expand Up @@ -1350,7 +1351,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
}
}

RibKind::ConstantItem(trivial, _) => {
RibKind::ConstantItem(trivial, _, kind) => {
if let ConstantHasGenerics::No(cause) = trivial {
if let Some(span) = finalize {
let error = match cause {
Expand All @@ -1366,6 +1367,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
param_kind: ParamKindInNonTrivialAnonConst::Const {
name: rib_ident.name,
},
place: kind,
}
}
};
Expand Down
79 changes: 56 additions & 23 deletions compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub(crate) enum PatternSource {
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
enum IsRepeatExpr {
pub(crate) enum IsRepeatExpr {
No,
Yes,
}
Expand All @@ -76,13 +76,27 @@ struct IsNeverPattern;
/// Describes whether an `AnonConst` is a type level const arg or
/// some other form of anon const (i.e. inline consts or enum discriminants)
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
enum AnonConstKind {
pub(crate) enum AnonConstKind {
EnumDiscriminant,
FieldDefaultValue,
InlineConst,
ConstArg(IsRepeatExpr),
}

impl IntoDiagArg for AnonConstKind {
fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
DiagArgValue::Str(Cow::Borrowed(match self {
AnonConstKind::EnumDiscriminant => "enum discriminant",
AnonConstKind::FieldDefaultValue => "field default value",
AnonConstKind::InlineConst => "inline const",
AnonConstKind::ConstArg(is_repeat_expr) => match is_repeat_expr {
IsRepeatExpr::No => "const generic args",
IsRepeatExpr::Yes => "array repeat expression",
},
}))
}
}

impl PatternSource {
fn descr(self) -> &'static str {
match self {
Expand Down Expand Up @@ -209,7 +223,7 @@ pub(crate) enum RibKind<'ra> {
///
/// The item may reference generic parameters in trivial constant expressions.
/// All other constants aren't allowed to use generic params at all.
ConstantItem(ConstantHasGenerics, Option<(Ident, ConstantItemKind)>),
ConstantItem(ConstantHasGenerics, Option<(Ident, ConstantItemKind)>, Option<AnonConstKind>),

/// We passed through a module.
Module(Module<'ra>),
Expand Down Expand Up @@ -3017,22 +3031,31 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
&mut self,
is_repeat: IsRepeatExpr,
may_use_generics: ConstantHasGenerics,
anon_const_kind: Option<AnonConstKind>,
item: Option<(Ident, ConstantItemKind)>,
f: impl FnOnce(&mut Self),
) {
let f = |this: &mut Self| {
this.with_rib(ValueNS, RibKind::ConstantItem(may_use_generics, item), |this| {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really not sure bout this one since it is nested ConstantItems

this.with_rib(
TypeNS,
RibKind::ConstantItem(
may_use_generics.force_yes_if(is_repeat == IsRepeatExpr::Yes),
item,
),
|this| {
this.with_label_rib(RibKind::ConstantItem(may_use_generics, item), f);
},
)
})
this.with_rib(
ValueNS,
RibKind::ConstantItem(may_use_generics, item, anon_const_kind),
|this| {
this.with_rib(
TypeNS,
RibKind::ConstantItem(
may_use_generics.force_yes_if(is_repeat == IsRepeatExpr::Yes),
item,
anon_const_kind,
),
|this| {
this.with_label_rib(
RibKind::ConstantItem(may_use_generics, item, anon_const_kind),
f,
);
},
)
},
)
};

if let ConstantHasGenerics::No(cause) = may_use_generics {
Expand Down Expand Up @@ -3548,9 +3571,13 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {

fn resolve_const_body(&mut self, expr: &'ast Expr, item: Option<(Ident, ConstantItemKind)>) {
self.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Infer), |this| {
this.with_constant_rib(IsRepeatExpr::No, ConstantHasGenerics::Yes, item, |this| {
this.visit_expr(expr)
});
this.with_constant_rib(
IsRepeatExpr::No,
ConstantHasGenerics::Yes,
None,
item,
|this| this.visit_expr(expr),
);
})
}

Expand Down Expand Up @@ -4757,11 +4784,17 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
}
};

self.with_constant_rib(is_repeat_expr, may_use_generics, None, |this| {
this.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Infer), |this| {
resolve_expr(this);
});
});
self.with_constant_rib(
is_repeat_expr,
may_use_generics,
Some(anon_const_kind),
None,
|this| {
this.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Infer), |this| {
resolve_expr(this);
});
},
);
}

fn resolve_expr_field(&mut self, f: &'ast ExprField, e: &'ast Expr) {
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ use tracing::debug;
use super::NoConstantGenericsReason;
use crate::diagnostics::{ImportSuggestion, LabelSuggestion, TypoSuggestion};
use crate::late::{
AliasPossibility, LateResolutionVisitor, LifetimeBinderKind, LifetimeRes, LifetimeRibKind,
LifetimeUseSet, QSelf, RibKind,
AliasPossibility, AnonConstKind, LateResolutionVisitor, LifetimeBinderKind, LifetimeRes,
LifetimeRibKind, LifetimeUseSet, QSelf, RibKind,
};
use crate::ty::fast_reject::SimplifiedType;
use crate::{
Expand Down Expand Up @@ -3337,6 +3337,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
.sess
.is_nightly_build()
.then_some(errors::ParamInNonTrivialAnonConstHelp),
place: AnonConstKind::InlineConst,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no context here so I am just defaulting to InlineConst. I need to fix this

})
.emit();
}
Expand Down
8 changes: 7 additions & 1 deletion compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ pub mod rustdoc;

pub use macros::registered_tools_ast;

use crate::late::AnonConstKind;

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

#[derive(Debug)]
Expand Down Expand Up @@ -285,7 +287,11 @@ enum ResolutionError<'ra> {
/// generic parameters must not be used inside const evaluations.
///
/// This error is only emitted when using `min_const_generics`.
ParamInNonTrivialAnonConst { name: Symbol, param_kind: ParamKindInNonTrivialAnonConst },
ParamInNonTrivialAnonConst {
name: Symbol,
param_kind: ParamKindInNonTrivialAnonConst,
place: Option<AnonConstKind>,
},
/// generic parameters must not be used inside enum discriminants.
///
/// This error is emitted even with `generic_const_exprs`.
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/const-generics/const-arg-in-const-arg.min.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ error: generic parameters may not be used in const operations
LL | let _: [u8; bar::<N>()];
| ^ cannot perform const operation using `N`
|
= help: const parameters may only be used as standalone arguments here, i.e. `N`
= help: const parameters may only be used as standalone arguments in const generic args, i.e. `N`
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions

error: generic parameters may not be used in const operations
Expand Down Expand Up @@ -58,7 +58,7 @@ error: generic parameters may not be used in const operations
LL | let _ = [0; bar::<N>()];
| ^ cannot perform const operation using `N`
|
= help: const parameters may only be used as standalone arguments here, i.e. `N`
= help: const parameters may only be used as standalone arguments in array repeat expression, i.e. `N`
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions

error: generic parameters may not be used in const operations
Expand Down Expand Up @@ -112,7 +112,7 @@ error: generic parameters may not be used in const operations
LL | let _: Foo<{ bar::<N>() }>;
| ^ cannot perform const operation using `N`
|
= help: const parameters may only be used as standalone arguments here, i.e. `N`
= help: const parameters may only be used as standalone arguments in const generic args, i.e. `N`
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions

error: generic parameters may not be used in const operations
Expand Down Expand Up @@ -166,7 +166,7 @@ error: generic parameters may not be used in const operations
LL | let _ = Foo::<{ bar::<N>() }>;
| ^ cannot perform const operation using `N`
|
= help: const parameters may only be used as standalone arguments here, i.e. `N`
= help: const parameters may only be used as standalone arguments in const generic args, i.e. `N`
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions

error: generic parameters may not be used in const operations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error: generic parameters may not be used in const operations
LL | struct Foo<const N: usize, const M: usize = { N + 1 }>;
| ^ cannot perform const operation using `N`
|
= help: const parameters may only be used as standalone arguments here, i.e. `N`
= help: const parameters may only be used as standalone arguments in const generic args, i.e. `N`
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions

error: generic parameters may not be used in const operations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | N
LL | fn foo<const N: usize>() -> Foo<{ arg!{} arg!{} }> { loop {} }
| ------ in this macro invocation
|
= help: const parameters may only be used as standalone arguments here, i.e. `N`
= help: const parameters may only be used as standalone arguments in const generic args, i.e. `N`
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
= note: this error originates in the macro `arg` (in Nightly builds, run with -Z macro-backtrace for more info)

Expand All @@ -20,7 +20,7 @@ LL | N
LL | fn foo<const N: usize>() -> Foo<{ arg!{} arg!{} }> { loop {} }
| ------ in this macro invocation
|
= help: const parameters may only be used as standalone arguments here, i.e. `N`
= help: const parameters may only be used as standalone arguments in const generic args, i.e. `N`
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
= note: this error originates in the macro `arg` (in Nightly builds, run with -Z macro-backtrace for more info)

Expand All @@ -30,7 +30,7 @@ error: generic parameters may not be used in const operations
LL | fn bar<const N: usize>() -> [(); { empty!{}; N }] { loop {} }
| ^ cannot perform const operation using `N`
|
= help: const parameters may only be used as standalone arguments here, i.e. `N`
= help: const parameters may only be used as standalone arguments in const generic args, i.e. `N`
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions

error: aborting due to 3 previous errors
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/const-generics/early/macro_rules-braces.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ error: generic parameters may not be used in const operations
LL | let _: foo!({{ N }});
| ^ cannot perform const operation using `N`
|
= help: const parameters may only be used as standalone arguments here, i.e. `N`
= help: const parameters may only be used as standalone arguments in const generic args, i.e. `N`
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions

error: generic parameters may not be used in const operations
Expand All @@ -35,7 +35,7 @@ error: generic parameters may not be used in const operations
LL | let _: bar!({ N });
| ^ cannot perform const operation using `N`
|
= help: const parameters may only be used as standalone arguments here, i.e. `N`
= help: const parameters may only be used as standalone arguments in const generic args, i.e. `N`
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions

error: generic parameters may not be used in const operations
Expand All @@ -44,7 +44,7 @@ error: generic parameters may not be used in const operations
LL | let _: baz!({{ N }});
| ^ cannot perform const operation using `N`
|
= help: const parameters may only be used as standalone arguments here, i.e. `N`
= help: const parameters may only be used as standalone arguments in const generic args, i.e. `N`
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions

error: generic parameters may not be used in const operations
Expand All @@ -53,7 +53,7 @@ error: generic parameters may not be used in const operations
LL | let _: biz!({ N });
| ^ cannot perform const operation using `N`
|
= help: const parameters may only be used as standalone arguments here, i.e. `N`
= help: const parameters may only be used as standalone arguments in const generic args, i.e. `N`
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions

error: aborting due to 6 previous errors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | N
LL | fn foo<const N: usize>() -> A<{{ y!() }}> {
| ---- in this macro invocation
|
= help: const parameters may only be used as standalone arguments here, i.e. `N`
= help: const parameters may only be used as standalone arguments in const generic args, i.e. `N`
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
= note: this error originates in the macro `y` (in Nightly builds, run with -Z macro-backtrace for more info)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | { N }
LL | fn foo<const N: usize>() -> A<{ y!() }> {
| ---- in this macro invocation
|
= help: const parameters may only be used as standalone arguments here, i.e. `N`
= help: const parameters may only be used as standalone arguments in const generic args, i.e. `N`
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions
= note: this error originates in the macro `y` (in Nightly builds, run with -Z macro-backtrace for more info)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error: generic parameters may not be used in const operations
LL | fn foo<const N: usize>() -> A<{ { N } }> {
| ^ cannot perform const operation using `N`
|
= help: const parameters may only be used as standalone arguments here, i.e. `N`
= help: const parameters may only be used as standalone arguments in const generic args, i.e. `N`
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions

error: aborting due to 1 previous error
Expand Down
Loading
Loading