mirror of
https://github.com/rust-lang/rust.git
synced 2026-04-30 14:52:56 +03:00
mGCA: improve diag message with ogca
This commit is contained in:
@@ -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"
|
||||
};
|
||||
@@ -403,11 +405,13 @@ fn error(&self) -> ErrorGuaranteed {
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1001,12 +1001,14 @@ pub(crate) fn into_struct_error(
|
||||
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
|
||||
|
||||
@@ -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}`")]
|
||||
@@ -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,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
||||
@@ -1554,6 +1554,10 @@ fn validate_res_from_ribs(
|
||||
}
|
||||
NoConstantGenericsReason::NonTrivialConstArg => {
|
||||
ResolutionError::ParamInNonTrivialAnonConst {
|
||||
is_ogca: self
|
||||
.tcx
|
||||
.features()
|
||||
.opaque_generic_const_args(),
|
||||
name: rib_ident.name,
|
||||
param_kind: ParamKindInNonTrivialAnonConst::Type,
|
||||
}
|
||||
@@ -1645,6 +1649,10 @@ fn validate_res_from_ribs(
|
||||
}
|
||||
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,
|
||||
|
||||
@@ -3726,6 +3726,8 @@ pub(crate) fn emit_forbidden_non_static_lifetime_error(
|
||||
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()
|
||||
}
|
||||
|
||||
@@ -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`.
|
||||
|
||||
@@ -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(){}
|
||||
@@ -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
|
||||
|
||||
@@ -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() {}
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user