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

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.

compiler/rustc_middle/src/ty/print/mod.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -193,17 +193,22 @@ pub trait Printer<'tcx>: Sized {
193193
.params
194194
.iter()
195195
.rev()
196-
.take_while(|param| {
197-
match param.kind {
198-
ty::GenericParamDefKind::Lifetime => false,
199-
ty::GenericParamDefKind::Type { has_default, .. } => {
200-
has_default
201-
&& substs[param.index as usize]
202-
== GenericArg::from(
203-
self.tcx().type_of(param.def_id).subst(self.tcx(), substs),
204-
)
205-
}
206-
ty::GenericParamDefKind::Const => false, // FIXME(const_generics_defaults)
196+
.take_while(|param| match param.kind {
197+
ty::GenericParamDefKind::Lifetime => false,
198+
ty::GenericParamDefKind::Type { has_default, .. } => {
199+
has_default
200+
&& substs[param.index as usize]
201+
== GenericArg::from(
202+
self.tcx().type_of(param.def_id).subst(self.tcx(), substs),
203+
)
204+
}
205+
ty::GenericParamDefKind::Const { has_default } => {
206+
has_default
207+
&& substs[param.index as usize]
208+
== GenericArg::from(crate::ty::Const::from_anon_const(
209+
self.tcx(),
210+
param.def_id.expect_local(),
211+
))
207212
}
208213
})
209214
.count();

compiler/rustc_mir/src/monomorphize/collector.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1175,7 +1175,8 @@ fn create_mono_items_for_default_impls<'tcx>(
11751175
let substs =
11761176
InternalSubsts::for_item(tcx, method.def_id, |param, _| match param.kind {
11771177
GenericParamDefKind::Lifetime => tcx.lifetimes.re_erased.into(),
1178-
GenericParamDefKind::Type { .. } | GenericParamDefKind::Const => {
1178+
GenericParamDefKind::Type { .. }
1179+
| GenericParamDefKind::Const { .. } => {
11791180
trait_ref.substs[param.index as usize]
11801181
}
11811182
});

compiler/rustc_privacy/src/lib.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -928,8 +928,13 @@ impl ReachEverythingInTheInterfaceVisitor<'_, 'tcx> {
928928
self.visit(self.ev.tcx.type_of(param.def_id));
929929
}
930930
}
931-
GenericParamDefKind::Const => {
931+
GenericParamDefKind::Const { has_default, .. } => {
932932
self.visit(self.ev.tcx.type_of(param.def_id));
933+
if has_default {
934+
// how should the error case be handled here?
935+
// let default_const = self.ev.tcx.const_eval_poly(param.def_id).unwrap();
936+
// self.visit(default_const);
937+
}
933938
}
934939
}
935940
}
@@ -1741,7 +1746,7 @@ impl SearchInterfaceForPrivateItemsVisitor<'tcx> {
17411746
self.visit(self.tcx.type_of(param.def_id));
17421747
}
17431748
}
1744-
GenericParamDefKind::Const => {
1749+
GenericParamDefKind::Const { .. } => {
17451750
self.visit(self.tcx.type_of(param.def_id));
17461751
}
17471752
}

compiler/rustc_resolve/src/late.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,7 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
616616
self.visit_ty(ty);
617617
self.ribs[TypeNS].pop().unwrap();
618618
self.ribs[ValueNS].pop().unwrap();
619+
// FIXME(const_generics:default) do something with default here?
619620
}
620621
}
621622
}

compiler/rustc_resolve/src/late/lifetimes.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2004,7 +2004,9 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
20042004
GenericParamDefKind::Type { object_lifetime_default, .. } => {
20052005
Some(object_lifetime_default)
20062006
}
2007-
GenericParamDefKind::Lifetime | GenericParamDefKind::Const => None,
2007+
GenericParamDefKind::Lifetime | GenericParamDefKind::Const { .. } => {
2008+
None
2009+
}
20082010
})
20092011
.collect()
20102012
})

compiler/rustc_save_analysis/src/sig.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -614,11 +614,12 @@ impl<'hir> Sig for hir::Generics<'hir> {
614614
start: offset + text.len(),
615615
end: offset + text.len() + param_text.as_str().len(),
616616
});
617-
if let hir::GenericParamKind::Const { ref ty, ref default } = param.kind {
617+
if let hir::GenericParamKind::Const { ref ty, default } = param.kind {
618618
param_text.push_str(": ");
619619
param_text.push_str(&ty_to_string(&ty));
620-
if let Some(ref _default) = default {
621-
// FIXME(const_generics_defaults): push the `default` value here
620+
if let Some(default) = default {
621+
param_text.push_str(" = ");
622+
param_text.push_str(&id_to_string(&scx.tcx.hir(), default.hir_id));
622623
}
623624
}
624625
if !param.bounds.is_empty() {

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ symbols! {
384384
const_fn_fn_ptr_basics,
385385
const_fn_transmute,
386386
const_fn_union,
387+
const_generic_defaults,
387388
const_generics,
388389
const_generics_defaults,
389390
const_if_match,

compiler/rustc_trait_selection/src/traits/error_reporting/on_unimplemented.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
176176

177177
for param in generics.params.iter() {
178178
let value = match param.kind {
179-
GenericParamDefKind::Type { .. } | GenericParamDefKind::Const => {
179+
GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => {
180180
trait_ref.substs[param.index as usize].to_string()
181181
}
182182
GenericParamDefKind::Lifetime => continue,

compiler/rustc_trait_selection/src/traits/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ fn vtable_methods<'tcx>(
483483
let substs = trait_ref.map_bound(|trait_ref| {
484484
InternalSubsts::for_item(tcx, def_id, |param, _| match param.kind {
485485
GenericParamDefKind::Lifetime => tcx.lifetimes.re_erased.into(),
486-
GenericParamDefKind::Type { .. } | GenericParamDefKind::Const => {
486+
GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => {
487487
trait_ref.substs[param.index as usize]
488488
}
489489
})

compiler/rustc_trait_selection/src/traits/on_unimplemented.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ impl<'tcx> OnUnimplementedFormatString {
337337
.iter()
338338
.filter_map(|param| {
339339
let value = match param.kind {
340-
GenericParamDefKind::Type { .. } | GenericParamDefKind::Const => {
340+
GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => {
341341
trait_ref.substs[param.index as usize].to_string()
342342
}
343343
GenericParamDefKind::Lifetime => return None,

compiler/rustc_traits/src/chalk/db.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ fn bound_vars_for_item(tcx: TyCtxt<'tcx>, def_id: DefId) -> SubstsRef<'tcx> {
739739
tcx.mk_region(ty::RegionKind::ReLateBound(ty::INNERMOST, br)).into()
740740
}
741741

742-
ty::GenericParamDefKind::Const => tcx
742+
ty::GenericParamDefKind::Const { .. } => tcx
743743
.mk_const(ty::Const {
744744
val: ty::ConstKind::Bound(ty::INNERMOST, ty::BoundVar::from(param.index)),
745745
ty: tcx.type_of(param.def_id),

compiler/rustc_typeck/src/astconv/generics.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
6464
kind: hir::TyKind::Path(rustc_hir::QPath::Resolved(_, path)),
6565
..
6666
}),
67-
GenericParamDefKind::Const,
67+
GenericParamDefKind::Const { .. },
6868
) => match path.res {
6969
Res::Err => {
7070
add_braces_suggestion(arg, &mut err);
@@ -93,7 +93,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
9393
},
9494
(
9595
GenericArg::Type(hir::Ty { kind: hir::TyKind::Path(_), .. }),
96-
GenericParamDefKind::Const,
96+
GenericParamDefKind::Const { .. },
9797
) => add_braces_suggestion(arg, &mut err),
9898
(
9999
GenericArg::Type(hir::Ty { kind: hir::TyKind::Array(_, len), .. }),
@@ -236,7 +236,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
236236
match (arg, &param.kind, arg_count.explicit_late_bound) {
237237
(GenericArg::Lifetime(_), GenericParamDefKind::Lifetime, _)
238238
| (GenericArg::Type(_), GenericParamDefKind::Type { .. }, _)
239-
| (GenericArg::Const(_), GenericParamDefKind::Const, _) => {
239+
| (GenericArg::Const(_), GenericParamDefKind::Const { .. }, _) => {
240240
substs.push(ctx.provided_kind(param, arg));
241241
args.next();
242242
params.next();
@@ -282,7 +282,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
282282
GenericParamDefKind::Type { .. } => {
283283
ParamKindOrd::Type
284284
}
285-
GenericParamDefKind::Const => {
285+
GenericParamDefKind::Const { .. } => {
286286
ParamKindOrd::Const {
287287
unordered: tcx
288288
.features()

0 commit comments

Comments
 (0)