Skip to content

Commit e4e5db4

Browse files
committed
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 79e5814 commit e4e5db4

File tree

39 files changed

+158
-77
lines changed

39 files changed

+158
-77
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ pub enum GenericParamKind {
385385
ty: P<Ty>,
386386
/// Span of the `const` keyword.
387387
kw_span: Span,
388+
388389
/// Optional default value for the const generic param
389390
default: Option<AnonConst>,
390391
},

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -785,8 +785,8 @@ pub fn noop_flat_map_generic_param<T: MutVisitor>(
785785
visit_opt(default, |default| vis.visit_ty(default));
786786
}
787787
GenericParamKind::Const { ty, kw_span: _, default } => {
788-
vis.visit_ty(ty);
789788
visit_opt(default, |default| vis.visit_anon_const(default));
789+
vis.visit_ty(ty);
790790
}
791791
}
792792
smallvec![param]

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2290,7 +2290,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
22902290
this.lower_ty(&ty, ImplTraitContext::disallowed())
22912291
});
22922292
let default = default.as_ref().map(|def| self.lower_anon_const(def));
2293-
22942293
(hir::ParamName::Plain(param.ident), hir::GenericParamKind::Const { ty, default })
22952294
}
22962295
};

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,17 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11741174
}
11751175
}
11761176
}
1177+
if !self.session.features_untracked().const_generics_defaults {
1178+
if let GenericParamKind::Const { default: Some(ref default), .. } = param.kind {
1179+
let mut err = self.err_handler().struct_span_err(
1180+
default.value.span,
1181+
"default values for const generic parameters are unstable",
1182+
);
1183+
err.note("to enable them use #![feature(const_generic_defaults)]");
1184+
err.emit();
1185+
break;
1186+
}
1187+
}
11771188
}
11781189

11791190
validate_generic_param_order(

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2661,6 +2661,9 @@ impl<'a> State<'a> {
26612661
s.print_type_bounds(":", &param.bounds);
26622662
if let Some(ref _default) = default {
26632663
// FIXME(const_generics_defaults): print the `default` value here
2664+
s.s.space();
2665+
s.word_space("=");
2666+
// s.print_anon_const(&default);
26642667
}
26652668
}
26662669
}

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2266,8 +2266,10 @@ impl<'a> State<'a> {
22662266
GenericParamKind::Const { ref ty, ref default } => {
22672267
self.word_space(":");
22682268
self.print_type(ty);
2269-
if let Some(ref _default) = default {
2270-
// FIXME(const_generics_defaults): print the `default` value here
2269+
if let Some(ref default) = default {
2270+
self.s.space();
2271+
self.word_space("=");
2272+
self.print_anon_const(&default)
22712273
}
22722274
}
22732275
}

compiler/rustc_infer/src/infer/error_reporting/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -963,10 +963,11 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
963963
.rev()
964964
.filter_map(|param| match param.kind {
965965
ty::GenericParamDefKind::Lifetime => None,
966-
ty::GenericParamDefKind::Type { has_default, .. } => {
966+
967+
ty::GenericParamDefKind::Type { has_default, .. }
968+
| ty::GenericParamDefKind::Const { has_default } => {
967969
Some((param.def_id, has_default))
968970
}
969-
ty::GenericParamDefKind::Const => None, // FIXME(const_generics_defaults)
970971
})
971972
.peekable();
972973
let has_default = {

compiler/rustc_middle/src/ty/context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2221,7 +2221,7 @@ impl<'tcx> TyCtxt<'tcx> {
22212221
let adt_def = self.adt_def(wrapper_def_id);
22222222
let substs =
22232223
InternalSubsts::for_item(self, wrapper_def_id, |param, substs| match param.kind {
2224-
GenericParamDefKind::Lifetime | GenericParamDefKind::Const => bug!(),
2224+
GenericParamDefKind::Lifetime | GenericParamDefKind::Const { .. } => bug!(),
22252225
GenericParamDefKind::Type { has_default, .. } => {
22262226
if param.index == 0 {
22272227
ty_param.into()
@@ -2416,7 +2416,7 @@ impl<'tcx> TyCtxt<'tcx> {
24162416
self.mk_region(ty::ReEarlyBound(param.to_early_bound_region_data())).into()
24172417
}
24182418
GenericParamDefKind::Type { .. } => self.mk_ty_param(param.index, param.name).into(),
2419-
GenericParamDefKind::Const => {
2419+
GenericParamDefKind::Const { .. } => {
24202420
self.mk_const_param(param.index, param.name, self.type_of(param.def_id)).into()
24212421
}
24222422
}

compiler/rustc_middle/src/ty/generics.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,24 @@ pub enum GenericParamDefKind {
1818
object_lifetime_default: ObjectLifetimeDefault,
1919
synthetic: Option<hir::SyntheticTyParamKind>,
2020
},
21-
Const,
21+
Const {
22+
has_default: bool,
23+
},
2224
}
2325

2426
impl GenericParamDefKind {
2527
pub fn descr(&self) -> &'static str {
2628
match self {
2729
GenericParamDefKind::Lifetime => "lifetime",
2830
GenericParamDefKind::Type { .. } => "type",
29-
GenericParamDefKind::Const => "constant",
31+
GenericParamDefKind::Const { .. } => "constant",
3032
}
3133
}
3234
pub fn to_ord(&self, tcx: TyCtxt<'_>) -> ast::ParamKindOrd {
3335
match self {
3436
GenericParamDefKind::Lifetime => ast::ParamKindOrd::Lifetime,
3537
GenericParamDefKind::Type { .. } => ast::ParamKindOrd::Type,
36-
GenericParamDefKind::Const => {
38+
GenericParamDefKind::Const { .. } => {
3739
ast::ParamKindOrd::Const { unordered: tcx.features().const_generics }
3840
}
3941
}
@@ -105,7 +107,7 @@ impl<'tcx> Generics {
105107
match param.kind {
106108
GenericParamDefKind::Lifetime => own_counts.lifetimes += 1,
107109
GenericParamDefKind::Type { .. } => own_counts.types += 1,
108-
GenericParamDefKind::Const => own_counts.consts += 1,
110+
GenericParamDefKind::Const { .. } => own_counts.consts += 1,
109111
}
110112
}
111113

@@ -118,12 +120,10 @@ impl<'tcx> Generics {
118120
for param in &self.params {
119121
match param.kind {
120122
GenericParamDefKind::Lifetime => (),
121-
GenericParamDefKind::Type { has_default, .. } => {
123+
GenericParamDefKind::Type { has_default, .. } |
124+
GenericParamDefKind::Const { has_default } => {
122125
own_defaults.types += has_default as usize;
123126
}
124-
GenericParamDefKind::Const => {
125-
// FIXME(const_generics:defaults)
126-
}
127127
}
128128
}
129129

@@ -146,7 +146,7 @@ impl<'tcx> Generics {
146146
pub fn own_requires_monomorphization(&self) -> bool {
147147
for param in &self.params {
148148
match param.kind {
149-
GenericParamDefKind::Type { .. } | GenericParamDefKind::Const => return true,
149+
GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => return true,
150150
GenericParamDefKind::Lifetime => {}
151151
}
152152
}
@@ -189,7 +189,7 @@ impl<'tcx> Generics {
189189
pub fn const_param(&'tcx self, param: &ParamConst, tcx: TyCtxt<'tcx>) -> &GenericParamDef {
190190
let param = self.param_at(param.index as usize, tcx);
191191
match param.kind {
192-
GenericParamDefKind::Const => param,
192+
GenericParamDefKind::Const { .. } => param,
193193
_ => bug!("expected const parameter, but found another generic parameter"),
194194
}
195195
}

compiler/rustc_middle/src/ty/instance.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ fn polymorphize<'tcx>(
593593
},
594594

595595
// Simple case: If parameter is a const or type parameter..
596-
ty::GenericParamDefKind::Const | ty::GenericParamDefKind::Type { .. } if
596+
ty::GenericParamDefKind::Const { .. } | ty::GenericParamDefKind::Type { .. } if
597597
// ..and is within range and unused..
598598
unused.contains(param.index).unwrap_or(false) =>
599599
// ..then use the identity for this parameter.

0 commit comments

Comments
 (0)