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
14 changes: 9 additions & 5 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,8 @@ impl<'tcx> ForbidMCGParamUsesFolder<'tcx> {
fn error(&self) -> ErrorGuaranteed {
let msg = if self.is_self_alias {
"generic `Self` types are currently not permitted in anonymous constants"
} else if self.tcx.features().opaque_generic_const_args() {
"generic parameters in const blocks are only allowed as the direct value of a `type const`"
} else {
"generic parameters may not be used in const operations"
};
Expand All @@ -403,11 +405,13 @@ impl<'tcx> ForbidMCGParamUsesFolder<'tcx> {
diag.span_note(impl_.self_ty.span, "not a concrete type");
}
}
if self.tcx.features().min_generic_const_args()
&& !self.tcx.features().opaque_generic_const_args()
{
diag.help("add `#![feature(opaque_generic_const_args)]` to allow generic expressions as the RHS of const items");
}
if self.tcx.features().min_generic_const_args() {
if !self.tcx.features().opaque_generic_const_args() {
diag.help("add `#![feature(opaque_generic_const_args)]` to allow generic expressions as the RHS of const items");
} else {
diag.help("consider factoring the expression into a `type const` item and use it as the const argument instead");
}
};
diag.emit()
}
}
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 @@ -1001,12 +1001,14 @@ 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 { is_ogca, name, param_kind: is_type } => {
self.dcx().create_err(errs::ParamInNonTrivialAnonConst {
span,
name,
param_kind: is_type,
help: self.tcx.sess.is_nightly_build(),
is_ogca,
help_ogca: is_ogca,
})
}
ResolutionError::ParamInEnumDiscriminant { name, param_kind: is_type } => self
Expand Down
12 changes: 11 additions & 1 deletion compiler/rustc_resolve/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,12 @@ pub(crate) struct SelfInConstGenericTy {
}

#[derive(Diagnostic)]
#[diag("generic parameters may not be used in const operations")]
#[diag(
"{$is_ogca ->
[true] generic parameters in const blocks are only allowed as the direct value of a `type const`
*[false] generic parameters may not be used in const operations
}"
)]
pub(crate) struct ParamInNonTrivialAnonConst {
#[primary_span]
#[label("cannot perform const operation using `{$name}`")]
Expand All @@ -415,6 +420,11 @@ pub(crate) struct ParamInNonTrivialAnonConst {
pub(crate) param_kind: ParamKindInNonTrivialAnonConst,
#[help("add `#![feature(generic_const_exprs)]` to allow generic const expressions")]
pub(crate) help: bool,
pub(crate) is_ogca: bool,
#[help(
"consider factoring the expression into a `type const` item and use it as the const argument instead"
)]
pub(crate) help_ogca: bool,
Comment on lines +423 to +427
Copy link
Member

Choose a reason for hiding this comment

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

does merging these two fields not work?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no, fluent parsing reqire item inside structure, and when there is #[help] macro associated with it, it doesn't recognize it as argument anymore and give ICE as it cant find arg

}

#[derive(Debug)]
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_resolve/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1554,6 +1554,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
}
NoConstantGenericsReason::NonTrivialConstArg => {
ResolutionError::ParamInNonTrivialAnonConst {
is_ogca: self
.tcx
.features()
.opaque_generic_const_args(),
name: rib_ident.name,
param_kind: ParamKindInNonTrivialAnonConst::Type,
}
Expand Down Expand Up @@ -1645,6 +1649,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
}
NoConstantGenericsReason::NonTrivialConstArg => {
ResolutionError::ParamInNonTrivialAnonConst {
is_ogca: self
.tcx
.features()
.opaque_generic_const_args(),
name: rib_ident.name,
param_kind: ParamKindInNonTrivialAnonConst::Const {
name: rib_ident.name,
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3726,6 +3726,8 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
name: lifetime_ref.ident.name,
param_kind: errors::ParamKindInNonTrivialAnonConst::Lifetime,
help: self.r.tcx.sess.is_nightly_build(),
is_ogca: self.r.tcx.features().opaque_generic_const_args(),
help_ogca: self.r.tcx.features().opaque_generic_const_args(),
})
.emit()
}
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,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 {
is_ogca: bool,
name: Symbol,
param_kind: ParamKindInNonTrivialAnonConst,
},
/// generic parameters must not be used inside enum discriminants.
///
/// This error is emitted even with `generic_const_exprs`.
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/const-generics/ogca/generic-param-rhs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![feature(min_generic_const_args, opaque_generic_const_args)]
#![expect(incomplete_features)]

fn foo<const N: usize>() {}
fn bar<const N: usize>() {
foo::<const { N + 1 }>();
//~^ ERROR: generic parameters in const blocks are only allowed as the direct value of a `type const`
}
fn main(){}
10 changes: 10 additions & 0 deletions tests/ui/const-generics/ogca/generic-param-rhs.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: generic parameters in const blocks are only allowed as the direct value of a `type const`
--> $DIR/generic-param-rhs.rs:6:19
|
LL | foo::<const { N + 1 }>();
| ^
|
= help: consider factoring the expression into a `type const` item and use it as the const argument instead

error: aborting due to 1 previous error

3 changes: 1 addition & 2 deletions tests/ui/const-generics/ogca/rhs-but-not-root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

// Anon consts must be the root of the RHS to be OGCA.
type const FOO<const N: usize>: usize = ID::<const { N + 1 }>;
//~^ ERROR generic parameters may not be used in const operations

//~^ ERROR generic parameters in const blocks are only allowed as the direct value of a `type const`
type const ID<const N: usize>: usize = N;

fn main() {}
4 changes: 3 additions & 1 deletion tests/ui/const-generics/ogca/rhs-but-not-root.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
error: generic parameters may not be used in const operations
error: generic parameters in const blocks are only allowed as the direct value of a `type const`
--> $DIR/rhs-but-not-root.rs:7:54
|
LL | type const FOO<const N: usize>: usize = ID::<const { N + 1 }>;
| ^
|
= help: consider factoring the expression into a `type const` item and use it as the const argument instead

error: aborting due to 1 previous error

Loading