Skip to content

Commit 1b679fd

Browse files
JulianKnodtvarkor
authored andcommitted
Add has_default to GenericParamDefKind::Const
This currently creates a field which is always false on GenericParamDefKind for future use when consts are permitted to have defaults Update const_generics:default locations Previously just ignored them, now actually do something about them. Fix using type check instead of value Add parsing This adds all the necessary changes to lower const-generics defaults from parsing. Change P<Expr> to AnonConst This matches the arguments passed to instantiations of const generics, and makes it specific to just anonymous constants. Attempt to fix lowering bugs
1 parent d107a87 commit 1b679fd

File tree

48 files changed

+220
-92
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+220
-92
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ pub enum GenericParamKind {
368368
ty: P<Ty>,
369369
/// Span of the `const` keyword.
370370
kw_span: Span,
371+
default: Option<AnonConst>,
371372
},
372373
}
373374

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,8 @@ 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 } => {
794+
visit_opt(default, |default| vis.visit_anon_const(default));
794795
vis.visit_ty(ty);
795796
}
796797
}

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+
if let Some(default) = default {
583+
visitor.visit_anon_const(default);
584+
}
585+
visitor.visit_ty(ty);
586+
}
582587
}
583588
}
584589

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2242,13 +2242,15 @@ 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
});
22502250

2251-
(hir::ParamName::Plain(param.ident), hir::GenericParamKind::Const { ty })
2251+
let default = default.as_ref().map(|def| self.lower_anon_const(def));
2252+
2253+
(hir::ParamName::Plain(param.ident), hir::GenericParamKind::Const { ty, default })
22522254
}
22532255
};
22542256

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,17 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11451145
}
11461146
}
11471147
}
1148+
if !self.session.features_untracked().const_generic_defaults {
1149+
if let GenericParamKind::Const { default: Some(ref default), .. } = param.kind {
1150+
let mut err = self.err_handler().struct_span_err(
1151+
default.value.span,
1152+
"default values for const generic parameters are unstable",
1153+
);
1154+
err.note("to enable them use #![feature(const_generic_defaults)]");
1155+
err.emit();
1156+
break;
1157+
}
1158+
}
11481159
}
11491160

11501161
validate_generic_param_order(
@@ -1155,7 +1166,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11551166
let (kind, ident) = match &param.kind {
11561167
GenericParamKind::Lifetime => (ParamKindOrd::Lifetime, ident),
11571168
GenericParamKind::Type { default: _ } => (ParamKindOrd::Type, ident),
1158-
GenericParamKind::Const { ref ty, kw_span: _ } => {
1169+
GenericParamKind::Const { ref ty, kw_span: _, default: _ } => {
11591170
let ty = pprust::ty_to_string(ty);
11601171
let unordered = self.session.features_untracked().const_generics;
11611172
(

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2668,13 +2668,18 @@ 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+
s.s.space();
2680+
s.word_space("=");
2681+
// s.print_anon_const(&default);
2682+
}
26782683
}
26792684
}
26802685
});

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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,9 @@ declare_features! (
620620
/// Allows arbitrary expressions in key-value attributes at parse time.
621621
(active, extended_key_value_attributes, "1.50.0", Some(78835), None),
622622

623+
/// Allows defaults const generics (e.g. `struct Foo<const N: usize = 3>(...);`).
624+
(active, const_generic_defaults, "1.51.0", Some(44580), None),
625+
623626
// -------------------------------------------------------------------------
624627
// feature-group-end: actual feature gates
625628
// -------------------------------------------------------------------------

compiler/rustc_hir/src/hir.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ pub enum GenericParamKind<'hir> {
418418
},
419419
Const {
420420
ty: &'hir Ty<'hir>,
421+
default: Option<AnonConst>,
421422
},
422423
}
423424

compiler/rustc_hir/src/intravisit.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,12 @@ pub fn walk_generic_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v Generi
877877
match param.kind {
878878
GenericParamKind::Lifetime { .. } => {}
879879
GenericParamKind::Type { ref default, .. } => walk_list!(visitor, visit_ty, default),
880-
GenericParamKind::Const { ref ty } => visitor.visit_ty(ty),
880+
GenericParamKind::Const { ref ty, ref default } => {
881+
visitor.visit_ty(ty);
882+
if let Some(ref default) = default {
883+
visitor.visit_anon_const(default);
884+
}
885+
}
881886
}
882887
walk_list!(visitor, visit_param_bound, param.bounds);
883888
}

0 commit comments

Comments
 (0)