Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit a609fb4

Browse files
committed
Auto merge of rust-lang#80547 - lqd:const_generics_defaults, r=varkor
In which we start to parse const generics defaults As discussed in this [zulip topic](https://rust-lang.zulipchat.com/#narrow/stream/260443-project-const-generics/topic/const.20generic.20defaults), this PR extracts the parsing parts from `@JulianKnodt's` PR rust-lang#75384 for a better user-experience using the newly stabilized `min_const_generics` (albeit temporary) as shown in rust-lang#80507: trying to use default values on const generics currently results in parse errors, as if the user didn't use the correct syntax (which is somewhat true but also misleading). This PR extracts (and slightly modifies in a couple places) `@JulianKnodt's` parsing code (with attribution if I've done everything correctly), AST and HIR changes, and feature gate setup. This feature is now marked as "incomplete" and thus will also print out the expected "const generics default values are unstable" error instead of a syntax error. Note that, as I've only extracted the parsing part, the actual feature will not work at all if enabled. There will be ICEs, and inference errors on the const generics default values themselves. Fixes rust-lang#80507. Once this merges, I'll: - modify the const generics tracking issue to refer to the `const_generics_defaults` gate rather than the older temporary name it uses there. - create the GH `F-const_generics_defaults` label r? `@varkor`
2 parents 18d27b2 + 942b7ce commit a609fb4

File tree

37 files changed

+160
-53
lines changed

37 files changed

+160
-53
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,8 @@ pub enum GenericParamKind {
368368
ty: P<Ty>,
369369
/// Span of the `const` keyword.
370370
kw_span: Span,
371+
/// Optional default value for the const generic param
372+
default: Option<AnonConst>,
371373
},
372374
}
373375

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -790,8 +790,9 @@ pub fn noop_flat_map_generic_param<T: MutVisitor>(
790790
GenericParamKind::Type { default } => {
791791
visit_opt(default, |default| vis.visit_ty(default));
792792
}
793-
GenericParamKind::Const { ty, kw_span: _ } => {
793+
GenericParamKind::Const { ty, kw_span: _, default } => {
794794
vis.visit_ty(ty);
795+
visit_opt(default, |default| vis.visit_anon_const(default));
795796
}
796797
}
797798
smallvec![param]

compiler/rustc_ast/src/visit.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,12 @@ pub fn walk_generic_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a Generi
578578
match param.kind {
579579
GenericParamKind::Lifetime => (),
580580
GenericParamKind::Type { ref default } => walk_list!(visitor, visit_ty, default),
581-
GenericParamKind::Const { ref ty, .. } => visitor.visit_ty(ty),
581+
GenericParamKind::Const { ref ty, ref default, .. } => {
582+
visitor.visit_ty(ty);
583+
if let Some(default) = default {
584+
visitor.visit_anon_const(default);
585+
}
586+
}
582587
}
583588
}
584589

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2242,13 +2242,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
22422242

22432243
(hir::ParamName::Plain(param.ident), kind)
22442244
}
2245-
GenericParamKind::Const { ref ty, kw_span: _ } => {
2245+
GenericParamKind::Const { ref ty, kw_span: _, ref default } => {
22462246
let ty = self
22472247
.with_anonymous_lifetime_mode(AnonymousLifetimeMode::ReportError, |this| {
22482248
this.lower_ty(&ty, ImplTraitContext::disallowed())
22492249
});
2250+
let default = default.as_ref().map(|def| self.lower_anon_const(def));
22502251

2251-
(hir::ParamName::Plain(param.ident), hir::GenericParamKind::Const { ty })
2252+
(hir::ParamName::Plain(param.ident), hir::GenericParamKind::Const { ty, default })
22522253
}
22532254
};
22542255

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ fn validate_generic_param_order(
733733
let (ord_kind, ident) = match &param.kind {
734734
GenericParamKind::Lifetime => (ParamKindOrd::Lifetime, ident),
735735
GenericParamKind::Type { default: _ } => (ParamKindOrd::Type, ident),
736-
GenericParamKind::Const { ref ty, kw_span: _ } => {
736+
GenericParamKind::Const { ref ty, kw_span: _, default: _ } => {
737737
let ty = pprust::ty_to_string(ty);
738738
let unordered = sess.features_untracked().const_generics;
739739
(ParamKindOrd::Const { unordered }, Some(format!("const {}: {}", param.ident, ty)))
@@ -774,8 +774,8 @@ fn validate_generic_param_order(
774774
}
775775
GenericParamKind::Type { default: None } => (),
776776
GenericParamKind::Lifetime => (),
777-
// FIXME(const_generics:defaults)
778-
GenericParamKind::Const { ty: _, kw_span: _ } => (),
777+
// FIXME(const_generics_defaults)
778+
GenericParamKind::Const { ty: _, kw_span: _, default: _ } => (),
779779
}
780780
first = false;
781781
}

compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,10 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session) {
619619
extended_key_value_attributes,
620620
"arbitrary expressions in key-value attributes are unstable"
621621
);
622+
gate_all!(
623+
const_generics_defaults,
624+
"default values for const generic parameters are experimental"
625+
);
622626
if sess.parse_sess.span_diagnostic.err_count() == 0 {
623627
// Errors for `destructuring_assignment` can get quite noisy, especially where `_` is
624628
// involved, so we only emit errors where there are no other parsing errors.

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2668,13 +2668,17 @@ impl<'a> State<'a> {
26682668
s.print_type(default)
26692669
}
26702670
}
2671-
ast::GenericParamKind::Const { ref ty, kw_span: _ } => {
2671+
ast::GenericParamKind::Const { ref ty, kw_span: _, ref default } => {
26722672
s.word_space("const");
26732673
s.print_ident(param.ident);
26742674
s.s.space();
26752675
s.word_space(":");
26762676
s.print_type(ty);
2677-
s.print_type_bounds(":", &param.bounds)
2677+
s.print_type_bounds(":", &param.bounds);
2678+
if let Some(ref _default) = default {
2679+
// FIXME(const_generics_defaults): print the `default` value here
2680+
todo!();
2681+
}
26782682
}
26792683
}
26802684
});

compiler/rustc_builtin_macros/src/deriving/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ fn inject_impl_of_structural_trait(
145145
*default = None;
146146
ast::GenericArg::Type(cx.ty_ident(span, param.ident))
147147
}
148-
ast::GenericParamKind::Const { ty: _, kw_span: _ } => {
148+
ast::GenericParamKind::Const { ty: _, kw_span: _, default } => {
149+
*default = None;
149150
ast::GenericArg::Const(cx.const_ident(span, param.ident))
150151
}
151152
})

compiler/rustc_feature/src/active.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ declare_features! (
581581
/// Allows `if let` guard in match arms.
582582
(active, if_let_guard, "1.47.0", Some(51114), None),
583583

584-
/// Allows non-trivial generic constants which have to be manually propageted upwards.
584+
/// Allows non-trivial generic constants which have to be manually propagated upwards.
585585
(active, const_evaluatable_checked, "1.48.0", Some(76560), None),
586586

587587
/// Allows basic arithmetic on floating point types in a `const fn`.
@@ -623,6 +623,9 @@ declare_features! (
623623
/// `:pat2018` and `:pat2021` macro matchers.
624624
(active, edition_macro_pats, "1.51.0", Some(54883), None),
625625

626+
/// Allows const generics to have default values (e.g. `struct Foo<const N: usize = 3>(...);`).
627+
(active, const_generics_defaults, "1.51.0", Some(44580), None),
628+
626629
// -------------------------------------------------------------------------
627630
// feature-group-end: actual feature gates
628631
// -------------------------------------------------------------------------
@@ -647,6 +650,7 @@ pub const INCOMPLETE_FEATURES: &[Symbol] = &[
647650
sym::repr128,
648651
sym::unsized_locals,
649652
sym::capture_disjoint_fields,
653+
sym::const_generics_defaults,
650654
];
651655

652656
/// Some features are not allowed to be used together at the same time, if

compiler/rustc_hir/src/hir.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,8 @@ pub enum GenericParamKind<'hir> {
418418
},
419419
Const {
420420
ty: &'hir Ty<'hir>,
421+
/// Optional default value for the const generic param
422+
default: Option<AnonConst>,
421423
},
422424
}
423425

0 commit comments

Comments
 (0)