Skip to content
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

slightly adopt and unify const generics terminology #84926

Closed
wants to merge 3 commits into from
Closed
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_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ pub enum GenericParamKind {
ty: P<Ty>,
/// Span of the `const` keyword.
kw_span: Span,
/// Optional default value for the const generic param
/// Optional default value for the const param.
default: Option<AnonConst>,
},
}
Expand Down
5 changes: 1 addition & 4 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,10 +677,7 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session) {
extended_key_value_attributes,
"arbitrary expressions in key-value attributes are unstable"
);
gate_all!(
const_generics_defaults,
"default values for const generic parameters are experimental"
);
gate_all!(const_generics_defaults, "default values for const parameters are experimental");
if sess.parse_sess.span_diagnostic.err_count() == 0 {
// Errors for `destructuring_assignment` can get quite noisy, especially where `_` is
// involved, so we only emit errors where there are no other parsing errors.
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_error_codes/src/error_codes/E0741.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
A non-structural-match type was used as the type of a const generic parameter.
A non-structural-match type was used as the type of a const parameter.

Erroneous code example:

Expand All @@ -11,7 +11,7 @@ struct B<const X: A>; // error!
```

Only structural-match types (that is, types that derive `PartialEq` and `Eq`)
may be used as the types of const generic parameters.
may be used as the types of const parameters.

To fix the previous code example, we derive `PartialEq` and `Eq`:

Expand Down
7 changes: 3 additions & 4 deletions compiler/rustc_error_codes/src/error_codes/E0771.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
A non-`'static` lifetime was used in a const generic. This is currently not
allowed.
A non-`'static` lifetime was used in a const parameter type. This is
currently not allowed.

Erroneous code example:

Expand All @@ -9,8 +9,7 @@ Erroneous code example:
fn function_with_str<'a, const STRING: &'a str>() {} // error!
```

To fix this issue, the lifetime in the const generic need to be changed to
`'static`:
To fix this issue, considering changing the lifetime to `'static`:

```
#![feature(const_generics)]
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_feature/src/active.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ declare_features! (
/// Allows arbitrary expressions in key-value attributes at parse time.
(active, extended_key_value_attributes, "1.50.0", Some(78835), None),

/// Allows const generics to have default values (e.g. `struct Foo<const N: usize = 3>(...);`).
/// Allows const parameters to have default values (e.g. `struct Foo<const N: usize = 3>(...);`).
(active, const_generics_defaults, "1.51.0", Some(44580), None),

/// Allows references to types with interior mutability within constants
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ pub enum GenericParamKind<'hir> {
},
Const {
ty: &'hir Ty<'hir>,
/// Optional default value for the const generic param
/// Optional default value for the const param.
default: Option<AnonConst>,
},
}
Expand Down Expand Up @@ -1389,7 +1389,7 @@ pub enum ConstContext {
/// Other contexts include:
/// - Array length expressions
/// - Enum discriminants
/// - Const generics
/// - Const generic arguments
///
/// For the most part, other contexts are treated just like a regular `const`, so they are
/// lumped into the same category.
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/ty/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct Const<'tcx> {
static_assert_size!(Const<'_>, 48);

impl<'tcx> Const<'tcx> {
/// Literals and const generic parameters are eagerly converted to a constant, everything else
/// Literals and const parameters are eagerly converted to a constant, everything else
/// becomes `Unevaluated`.
pub fn from_anon_const(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> &'tcx Self {
Self::from_opt_const_arg_anon_const(tcx, ty::WithOptConstParam::unknown(def_id))
Expand Down Expand Up @@ -151,7 +151,7 @@ impl<'tcx> Const<'tcx> {
#[inline]
/// Attempts to evaluate the given constant to bits. Can fail to evaluate in the presence of
/// generics (or erroneous code) or if the value can't be represented as bits (e.g. because it
/// contains const generic parameters or pointers).
/// contains const parameters or pointers).
pub fn try_eval_bits(
&self,
tcx: TyCtxt<'tcx>,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/consts/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct Unevaluated<'tcx> {
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable)]
#[derive(Hash, HashStable)]
pub enum ConstKind<'tcx> {
/// A const generic parameter.
/// A const parameter.
Param(ty::ParamConst),

/// Infer the value of the const.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir/src/transform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ fn mir_for_ctfe<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> &'tcx Body<'tcx> {
}
}

/// Same as `mir_for_ctfe`, but used to get the MIR of a const generic parameter.
/// Same as `mir_for_ctfe`, but used to get the MIR of a const parameter.
/// The docs on `WithOptConstParam` explain this a bit more, but the TLDR is that
/// we'd get cycle errors with `mir_for_ctfe`, because typeck would need to typeck
/// the const parameter while type checking the main body, which in turn would try
Expand Down
14 changes: 4 additions & 10 deletions compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1871,17 +1871,13 @@ impl<'a> Parser<'a> {
pub fn handle_unambiguous_unbraced_const_arg(&mut self) -> PResult<'a, P<Expr>> {
let start = self.token.span;
let expr = self.parse_expr_res(Restrictions::CONST_EXPR, None).map_err(|mut err| {
err.span_label(
start.shrink_to_lo(),
"while parsing a const generic argument starting here",
);
err.span_label(start.shrink_to_lo(), "while parsing a const argument starting here");
err
})?;
if !self.expr_is_valid_const_arg(&expr) {
self.struct_span_err(
expr.span,
"expressions must be enclosed in braces to be used as const generic \
arguments",
"expressions must be enclosed in braces to be used as const arguments",
)
.multipart_suggestion(
"enclose the `const` expression in braces",
Expand Down Expand Up @@ -1939,14 +1935,12 @@ impl<'a> Parser<'a> {
Ok(expr) => {
if token::Comma == self.token.kind || self.token.kind.should_end_const_arg() {
// Avoid the following output by checking that we consumed a full const arg:
// help: expressions must be enclosed in braces to be used as const generic
// arguments
// help: expressions must be enclosed in braces to be used as const arguments
// |
// LL | let sr: Vec<{ (u32, _, _) = vec![] };
// | ^ ^
err.multipart_suggestion(
"expressions must be enclosed in braces to be used as const generic \
arguments",
"expressions must be enclosed in braces to be used as const arguments",
vec![
(start.shrink_to_lo(), "{ ".to_string()),
(expr.span.shrink_to_hi(), " }".to_string()),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/parser/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl<'a> Parser<'a> {
self.expect(&token::Colon)?;
let ty = self.parse_ty()?;

// Parse optional const generics default value, taking care of feature gating the spans
// Parse optional const parameter default value, taking care of feature gating the spans
// with the unstable syntax mechanism.
let default = if self.eat(&token::Eq) {
// The gated span goes from the `=` to the end of the const argument that follows (and
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,7 @@ impl CheckAttrVisitor<'tcx> {
"#[rustc_legacy_const_generics] functions must \
only have const generics",
)
.span_label(param.span, "non-const generic parameter")
.span_label(param.span, "not a const parameter")
.emit();
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1781,13 +1781,13 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {

// FIXME(const_generics): This patches over a ICE caused by non-'static lifetimes in const
// generics. We are disallowing this until we can decide on how we want to handle non-'static
// lifetimes in const generics. See issue #74052 for discussion.
// lifetimes in const parameter types. See issue #74052 for discussion.
crate fn emit_non_static_lt_in_const_generic_error(&self, lifetime_ref: &hir::Lifetime) {
let mut err = struct_span_err!(
self.tcx.sess,
lifetime_ref.span,
E0771,
"use of non-static lifetime `{}` in const generic",
"use of non-static lifetime `{}` in a const parameter type",
lifetime_ref
);
err.note(
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_save_analysis/src/sig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ impl<'hir> Sig for hir::Generics<'hir> {
// FIXME descend properly into bounds.
}
hir::GenericParamKind::Const { .. } => {
// Const generics cannot contain bounds.
// Const parameters cannot contain bounds.
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_typeck/src/astconv/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
if param_type.is_suggestable() {
err.span_suggestion(
tcx.def_span(src_def_id),
"consider changing this type parameter to be a `const` generic",
"consider using a const parameter instead",
format!("const {}: {}", param_name, param_type),
Applicability::MaybeIncorrect,
);
Expand Down
7 changes: 2 additions & 5 deletions compiler/rustc_typeck/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,16 +309,13 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) {
if is_ptr {
tcx.sess.span_err(
hir_ty.span,
&format!(
"using {} as const generic parameters is forbidden",
unsupported_type
),
&format!("using {} as const parameters is forbidden", unsupported_type),
)
} else {
let mut err = tcx.sess.struct_span_err(
hir_ty.span,
&format!(
"{} is forbidden as the type of a const generic parameter",
"{} is forbidden as the type of a const parameter",
unsupported_type
),
);
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_typeck/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1373,8 +1373,7 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
let parent_id = tcx.hir().get_parent_item(hir_id);
Some(tcx.hir().local_def_id(parent_id).to_def_id())
}
// FIXME(#43408) always enable this once `lazy_normalization` is
// stable enough and does not need a feature gate anymore.

Node::AnonConst(_) => {
let parent_id = tcx.hir().get_parent_item(hir_id);
let parent_def_id = tcx.hir().local_def_id(parent_id);
Expand Down Expand Up @@ -1405,6 +1404,9 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
//
// Note that we do not supply the parent generics when using
// `min_const_generics`.
//
// FIXME(#43408) always enable this once `lazy_normalization` is
// stable enough and does not need a feature gate anymore.
Some(parent_def_id.to_def_id())
} else {
let parent_node = tcx.hir().get(tcx.hir().get_parent_node(hir_id));
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/binding/const-param.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Identifier pattern referring to a const generic parameter is an error (issue #68853).
// Identifier pattern referring to a const parameter is an error (issue #68853).
// revisions: full min
#![cfg_attr(full, feature(const_generics))]
#![cfg_attr(full, allow(incomplete_features))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ LL | arr: [u8; CFG.arr_size],
= help: const parameters may only be used as standalone arguments, i.e. `CFG`
= help: use `#![feature(const_generics)]` and `#![feature(const_evaluatable_checked)]` to allow generic const expressions

error: `Config` is forbidden as the type of a const generic parameter
error: `Config` is forbidden as the type of a const parameter
--> $DIR/array-size-in-generic-struct-param.rs:17:21
|
LL | struct B<const CFG: Config> {
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/const-generics/closing-args-token.full.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: expressions must be enclosed in braces to be used as const generic arguments
error: expressions must be enclosed in braces to be used as const arguments
--> $DIR/closing-args-token.rs:10:9
|
LL | S::<5 + 2 >> 7>;
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/const-generics/closing-args-token.min.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: expressions must be enclosed in braces to be used as const generic arguments
error: expressions must be enclosed in braces to be used as const arguments
--> $DIR/closing-args-token.rs:10:9
|
LL | S::<5 + 2 >> 7>;
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/const-generics/closing-args-token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ struct T<const X: bool>;

fn bad_args_1() {
S::<5 + 2 >> 7>;
//~^ ERROR expressions must be enclosed in braces to be used as const generic arguments
//~^ ERROR expressions must be enclosed in braces to be used as const arguments
//~| ERROR comparison operators cannot be chained
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: expressions must be enclosed in braces to be used as const generic arguments
error: expressions must be enclosed in braces to be used as const arguments
--> $DIR/const-expression-parameter.rs:15:20
|
LL | i32_identity::<1 + 2>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: expressions must be enclosed in braces to be used as const generic arguments
error: expressions must be enclosed in braces to be used as const arguments
--> $DIR/const-expression-parameter.rs:15:20
|
LL | i32_identity::<1 + 2>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ error: type parameters must be declared prior to const parameters
LL | fn foo<const X: (), T>(_: &T) {}
| --------------^- help: reorder the parameters: lifetimes, then types, then consts: `<T, const X: ()>`

error: `()` is forbidden as the type of a const generic parameter
error: `()` is forbidden as the type of a const parameter
--> $DIR/const-param-before-other-params.rs:5:17
|
LL | fn bar<const X: (), 'a>(_: &'a ()) {
Expand All @@ -19,7 +19,7 @@ LL | fn bar<const X: (), 'a>(_: &'a ()) {
= note: the only supported types are integers, `bool` and `char`
= help: more complex types are supported with `#![feature(const_generics)]`

error: `()` is forbidden as the type of a const generic parameter
error: `()` is forbidden as the type of a const parameter
--> $DIR/const-param-before-other-params.rs:10:17
|
LL | fn foo<const X: (), T>(_: &T) {}
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/const-generics/const-param-before-other-params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

fn bar<const X: (), 'a>(_: &'a ()) {
//~^ ERROR lifetime parameters must be declared prior to const parameters
//[min]~^^ ERROR `()` is forbidden as the type of a const generic parameter
//[min]~^^ ERROR `()` is forbidden as the type of a const parameter
}

fn foo<const X: (), T>(_: &T) {}
//[min]~^ ERROR type parameters must be declared prior to const parameters
//[min]~^^ ERROR `()` is forbidden as the type of a const generic parameter
//[min]~^^ ERROR `()` is forbidden as the type of a const parameter

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ error[E0637]: `&` without an explicit lifetime name cannot be used here
LL | fn bar<const N: &u8>() {}
| ^ explicit lifetime name needed here

error: `&'static u8` is forbidden as the type of a const generic parameter
error: `&'static u8` is forbidden as the type of a const parameter
--> $DIR/const-param-elided-lifetime.rs:10:19
|
LL | struct A<const N: &u8>;
Expand All @@ -37,7 +37,7 @@ LL | struct A<const N: &u8>;
= note: the only supported types are integers, `bool` and `char`
= help: more complex types are supported with `#![feature(const_generics)]`

error: `&'static u8` is forbidden as the type of a const generic parameter
error: `&'static u8` is forbidden as the type of a const parameter
--> $DIR/const-param-elided-lifetime.rs:15:15
|
LL | impl<const N: &u8> A<N> {
Expand All @@ -46,7 +46,7 @@ LL | impl<const N: &u8> A<N> {
= note: the only supported types are integers, `bool` and `char`
= help: more complex types are supported with `#![feature(const_generics)]`

error: `&'static u8` is forbidden as the type of a const generic parameter
error: `&'static u8` is forbidden as the type of a const parameter
--> $DIR/const-param-elided-lifetime.rs:23:15
|
LL | impl<const N: &u8> B for A<N> {}
Expand All @@ -55,7 +55,7 @@ LL | impl<const N: &u8> B for A<N> {}
= note: the only supported types are integers, `bool` and `char`
= help: more complex types are supported with `#![feature(const_generics)]`

error: `&'static u8` is forbidden as the type of a const generic parameter
error: `&'static u8` is forbidden as the type of a const parameter
--> $DIR/const-param-elided-lifetime.rs:27:17
|
LL | fn bar<const N: &u8>() {}
Expand All @@ -64,7 +64,7 @@ LL | fn bar<const N: &u8>() {}
= note: the only supported types are integers, `bool` and `char`
= help: more complex types are supported with `#![feature(const_generics)]`

error: `&'static u8` is forbidden as the type of a const generic parameter
error: `&'static u8` is forbidden as the type of a const parameter
--> $DIR/const-param-elided-lifetime.rs:18:21
|
LL | fn foo<const M: &u8>(&self) {}
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/const-generics/const-param-elided-lifetime.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Elided lifetimes within the type of a const generic parameters is disallowed. This matches the
// Elided lifetimes within the type of a const parameters is disallowed. This matches the
// behaviour of trait bounds where `fn foo<T: Ord<&u8>>() {}` is illegal. Though we could change
// elided lifetimes within the type of a const generic parameters to be 'static, like elided
// elided lifetimes within the type of a const parameters to be 'static, like elided
// lifetimes within const/static items.
// revisions: full min

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ error[E0770]: the type of const parameters must not depend on other generic para
LL | pub struct SelfDependent<const N: [u8; N]>;
| ^ the type must not depend on the parameter `N`

error: `[u8; _]` is forbidden as the type of a const generic parameter
error: `[u8; _]` is forbidden as the type of a const parameter
--> $DIR/const-param-type-depends-on-const-param.rs:11:47
|
LL | pub struct Dependent<const N: usize, const X: [u8; N]>([(); N]);
Expand All @@ -19,7 +19,7 @@ LL | pub struct Dependent<const N: usize, const X: [u8; N]>([(); N]);
= note: the only supported types are integers, `bool` and `char`
= help: more complex types are supported with `#![feature(const_generics)]`

error: `[u8; _]` is forbidden as the type of a const generic parameter
error: `[u8; _]` is forbidden as the type of a const parameter
--> $DIR/const-param-type-depends-on-const-param.rs:15:35
|
LL | pub struct SelfDependent<const N: [u8; N]>;
Expand Down
Loading