Skip to content

Commit 5b33de3

Browse files
committed
Auto merge of #75384 - JulianKnodt:cg_def, r=varkor,lcnr
implement `feature(const_generics_defaults)` Implements const generics defaults `struct Example<const N: usize=3>`, as well as a query for getting the default of a given const-parameter's def id. There are some remaining FIXME's but they were specified as not blocking for merging this PR. This also puts the defaults behind the unstable feature gate `#![feature(const_generics_defaults)]`. ~~This currently creates a field which is always false on `GenericParamDefKind` for future use when consts are permitted to have defaults. I'm not sure if this is exactly what is best for adding default parameters, but I mimicked the style of type defaults, so hopefully this is ok.~~ r? `@lcnr`
2 parents db492ec + 33370fd commit 5b33de3

File tree

73 files changed

+517
-191
lines changed

Some content is hidden

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

73 files changed

+517
-191
lines changed

compiler/rustc_ast_lowering/src/lib.rs

-1
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

+9-6
Original file line numberDiff line numberDiff line change
@@ -1150,20 +1150,23 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11501150
}
11511151

11521152
fn visit_generics(&mut self, generics: &'a Generics) {
1153-
let mut prev_ty_default = None;
1153+
let cg_defaults = self.session.features_untracked().const_generics_defaults;
1154+
1155+
let mut prev_param_default = None;
11541156
for param in &generics.params {
11551157
match param.kind {
11561158
GenericParamKind::Lifetime => (),
1157-
GenericParamKind::Type { default: Some(_), .. } => {
1158-
prev_ty_default = Some(param.ident.span);
1159+
GenericParamKind::Type { default: Some(_), .. }
1160+
| GenericParamKind::Const { default: Some(_), .. } => {
1161+
prev_param_default = Some(param.ident.span);
11591162
}
11601163
GenericParamKind::Type { .. } | GenericParamKind::Const { .. } => {
1161-
if let Some(span) = prev_ty_default {
1164+
if let Some(span) = prev_param_default {
11621165
let mut err = self.err_handler().struct_span_err(
11631166
span,
1164-
"type parameters with a default must be trailing",
1167+
"generic parameters with a default must be trailing",
11651168
);
1166-
if matches!(param.kind, GenericParamKind::Const { .. }) {
1169+
if matches!(param.kind, GenericParamKind::Const { .. }) && !cg_defaults {
11671170
err.note(
11681171
"using type defaults and const parameters \
11691172
in the same parameter list is currently not permitted",

compiler/rustc_ast_pretty/src/pprust/state.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -2659,8 +2659,10 @@ impl<'a> State<'a> {
26592659
s.word_space(":");
26602660
s.print_type(ty);
26612661
s.print_type_bounds(":", &param.bounds);
2662-
if let Some(ref _default) = default {
2663-
// FIXME(const_generics_defaults): print the `default` value here
2662+
if let Some(ref default) = default {
2663+
s.s.space();
2664+
s.word_space("=");
2665+
s.print_expr(&default.value);
26642666
}
26652667
}
26662668
}

compiler/rustc_error_codes/src/error_codes/E0128.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ struct Foo<T = U, U = ()> {
77
field1: T,
88
field2: U,
99
}
10-
// error: type parameters with a default cannot use forward declared
10+
// error: generic parameters with a default cannot use forward declared
1111
// identifiers
1212
```
1313

compiler/rustc_hir/src/intravisit.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,9 @@ pub trait Visitor<'v>: Sized {
366366
fn visit_generic_param(&mut self, p: &'v GenericParam<'v>) {
367367
walk_generic_param(self, p)
368368
}
369+
fn visit_const_param_default(&mut self, _param: HirId, ct: &'v AnonConst) {
370+
walk_const_param_default(self, ct)
371+
}
369372
fn visit_generics(&mut self, g: &'v Generics<'v>) {
370373
walk_generics(self, g)
371374
}
@@ -869,13 +872,17 @@ pub fn walk_generic_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v Generi
869872
GenericParamKind::Const { ref ty, ref default } => {
870873
visitor.visit_ty(ty);
871874
if let Some(ref default) = default {
872-
visitor.visit_anon_const(default);
875+
visitor.visit_const_param_default(param.hir_id, default);
873876
}
874877
}
875878
}
876879
walk_list!(visitor, visit_param_bound, param.bounds);
877880
}
878881

882+
pub fn walk_const_param_default<'v, V: Visitor<'v>>(visitor: &mut V, ct: &'v AnonConst) {
883+
visitor.visit_anon_const(ct)
884+
}
885+
879886
pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics<'v>) {
880887
walk_list!(visitor, visit_generic_param, generics.params);
881888
walk_list!(visitor, visit_where_predicate, generics.where_clause.predicates);

compiler/rustc_hir_pretty/src/lib.rs

+4-2
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

+19-25
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ use rustc_hir::{Item, ItemKind, Node};
6767
use rustc_middle::ty::error::TypeError;
6868
use rustc_middle::ty::{
6969
self,
70-
subst::{Subst, SubstsRef},
70+
subst::{GenericArgKind, Subst, SubstsRef},
7171
Region, Ty, TyCtxt, TypeFoldable,
7272
};
7373
use rustc_span::{sym, BytePos, DesugaringKind, Pos, Span};
@@ -957,33 +957,27 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
957957
) -> SubstsRef<'tcx> {
958958
let generics = self.tcx.generics_of(def_id);
959959
let mut num_supplied_defaults = 0;
960-
let mut type_params = generics
961-
.params
962-
.iter()
963-
.rev()
964-
.filter_map(|param| match param.kind {
965-
ty::GenericParamDefKind::Lifetime => None,
966-
ty::GenericParamDefKind::Type { has_default, .. } => {
967-
Some((param.def_id, has_default))
968-
}
969-
ty::GenericParamDefKind::Const => None, // FIXME(const_generics_defaults)
970-
})
971-
.peekable();
972-
let has_default = {
973-
let has_default = type_params.peek().map(|(_, has_default)| has_default);
974-
*has_default.unwrap_or(&false)
975-
};
976-
if has_default {
977-
let types = substs.types().rev();
978-
for ((def_id, has_default), actual) in type_params.zip(types) {
979-
if !has_default {
980-
break;
960+
961+
let default_params = generics.params.iter().rev().filter_map(|param| match param.kind {
962+
ty::GenericParamDefKind::Type { has_default: true, .. } => Some(param.def_id),
963+
ty::GenericParamDefKind::Const { has_default: true } => Some(param.def_id),
964+
_ => None,
965+
});
966+
for (def_id, actual) in default_params.zip(substs.iter().rev()) {
967+
match actual.unpack() {
968+
GenericArgKind::Const(c) => {
969+
if self.tcx.const_param_default(def_id).subst(self.tcx, substs) != c {
970+
break;
971+
}
981972
}
982-
if self.tcx.type_of(def_id).subst(self.tcx, substs) != actual {
983-
break;
973+
GenericArgKind::Type(ty) => {
974+
if self.tcx.type_of(def_id).subst(self.tcx, substs) != ty {
975+
break;
976+
}
984977
}
985-
num_supplied_defaults += 1;
978+
_ => break,
986979
}
980+
num_supplied_defaults += 1;
987981
}
988982
let len = generics.params.len();
989983
let mut generics = generics.clone();

compiler/rustc_metadata/src/rmeta/decoder.rs

+8
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,14 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
953953
self.root.tables.expn_that_defined.get(self, id).unwrap().decode((self, sess))
954954
}
955955

956+
fn get_const_param_default(
957+
&self,
958+
tcx: TyCtxt<'tcx>,
959+
id: DefIndex,
960+
) -> rustc_middle::ty::Const<'tcx> {
961+
self.root.tables.const_defaults.get(self, id).unwrap().decode((self, tcx))
962+
}
963+
956964
/// Iterates over all the stability attributes in the given crate.
957965
fn get_lib_features(&self, tcx: TyCtxt<'tcx>) -> &'tcx [(Symbol, Option<Symbol>)] {
958966
// FIXME: For a proc macro crate, not sure whether we should return the "host"

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
122122
promoted_mir => { tcx.arena.alloc(cdata.get_promoted_mir(tcx, def_id.index)) }
123123
mir_abstract_const => { cdata.get_mir_abstract_const(tcx, def_id.index) }
124124
unused_generic_params => { cdata.get_unused_generic_params(def_id.index) }
125+
const_param_default => { tcx.mk_const(cdata.get_const_param_default(tcx, def_id.index)) }
125126
mir_const_qualif => { cdata.mir_const_qualif(def_id.index) }
126127
fn_sig => { cdata.fn_sig(def_id.index, tcx) }
127128
inherent_impls => { cdata.get_inherent_implementations_for_type(tcx, def_id.index) }

compiler/rustc_metadata/src/rmeta/encoder.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -1876,13 +1876,12 @@ impl EncodeContext<'a, 'tcx> {
18761876
default.is_some(),
18771877
);
18781878
}
1879-
GenericParamKind::Const { .. } => {
1880-
self.encode_info_for_generic_param(
1881-
def_id.to_def_id(),
1882-
EntryKind::ConstParam,
1883-
true,
1884-
);
1885-
// FIXME(const_generics_defaults)
1879+
GenericParamKind::Const { ref default, .. } => {
1880+
let def_id = def_id.to_def_id();
1881+
self.encode_info_for_generic_param(def_id, EntryKind::ConstParam, true);
1882+
if default.is_some() {
1883+
record!(self.tables.const_defaults[def_id] <- self.tcx.const_param_default(def_id))
1884+
}
18861885
}
18871886
}
18881887
}

compiler/rustc_metadata/src/rmeta/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -307,13 +307,14 @@ define_tables! {
307307
mir_for_ctfe: Table<DefIndex, Lazy!(mir::Body<'tcx>)>,
308308
promoted_mir: Table<DefIndex, Lazy!(IndexVec<mir::Promoted, mir::Body<'tcx>>)>,
309309
mir_abstract_consts: Table<DefIndex, Lazy!(&'tcx [mir::abstract_const::Node<'tcx>])>,
310+
const_defaults: Table<DefIndex, Lazy<rustc_middle::ty::Const<'tcx>>>,
310311
unused_generic_params: Table<DefIndex, Lazy<FiniteBitSet<u32>>>,
311312
// `def_keys` and `def_path_hashes` represent a lazy version of a
312313
// `DefPathTable`. This allows us to avoid deserializing an entire
313314
// `DefPathTable` up front, since we may only ever use a few
314315
// definitions from any given crate.
315316
def_keys: Table<DefIndex, Lazy<DefKey>>,
316-
def_path_hashes: Table<DefIndex, Lazy<DefPathHash>>
317+
def_path_hashes: Table<DefIndex, Lazy<DefPathHash>>,
317318
}
318319

319320
#[derive(Copy, Clone, MetadataEncodable, MetadataDecodable)]

compiler/rustc_middle/src/hir/map/collector.rs

+4
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,10 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
395395
}
396396
}
397397

398+
fn visit_const_param_default(&mut self, param: HirId, ct: &'hir AnonConst) {
399+
self.with_parent(param, |this| intravisit::walk_const_param_default(this, ct))
400+
}
401+
398402
fn visit_trait_item(&mut self, ti: &'hir TraitItem<'hir>) {
399403
self.with_dep_node_owner(ti.def_id, ti, |this, hash| {
400404
this.insert_with_hash(ti.span, ti.hir_id(), Node::TraitItem(ti), hash);

compiler/rustc_middle/src/query/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ rustc_queries! {
9393
desc { |tcx| "computing the optional const parameter of `{}`", tcx.def_path_str(key.to_def_id()) }
9494
}
9595

96+
/// Given the def_id of a const-generic parameter, computes the associated default const
97+
/// parameter. e.g. `fn example<const N: usize=3>` called on `N` would return `3`.
98+
query const_param_default(param: DefId) -> &'tcx ty::Const<'tcx> {
99+
desc { |tcx| "compute const default for a given parameter `{}`", tcx.def_path_str(param) }
100+
}
101+
96102
/// Records the type of every item.
97103
query type_of(key: DefId) -> Ty<'tcx> {
98104
desc { |tcx| "computing type of `{}`", tcx.def_path_str(key) }

compiler/rustc_middle/src/ty/consts.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::ty::{self, Ty, TyCtxt};
55
use crate::ty::{ParamEnv, ParamEnvAnd};
66
use rustc_errors::ErrorReported;
77
use rustc_hir as hir;
8-
use rustc_hir::def_id::LocalDefId;
8+
use rustc_hir::def_id::{DefId, LocalDefId};
99
use rustc_macros::HashStable;
1010

1111
mod int;
@@ -202,3 +202,18 @@ impl<'tcx> Const<'tcx> {
202202
.unwrap_or_else(|| bug!("expected usize, got {:#?}", self))
203203
}
204204
}
205+
206+
pub fn const_param_default<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> &'tcx Const<'tcx> {
207+
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
208+
let default_def_id = match tcx.hir().get(hir_id) {
209+
hir::Node::GenericParam(hir::GenericParam {
210+
kind: hir::GenericParamKind::Const { ty: _, default: Some(ac) },
211+
..
212+
}) => tcx.hir().local_def_id(ac.hir_id),
213+
_ => span_bug!(
214+
tcx.def_span(def_id),
215+
"`const_param_default` expected a generic parameter with a constant"
216+
),
217+
};
218+
Const::from_anon_const(tcx, default_def_id)
219+
}

compiler/rustc_middle/src/ty/context.rs

+2-2
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

+12-8
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

@@ -121,8 +123,8 @@ impl<'tcx> Generics {
121123
GenericParamDefKind::Type { has_default, .. } => {
122124
own_defaults.types += has_default as usize;
123125
}
124-
GenericParamDefKind::Const => {
125-
// FIXME(const_generics:defaults)
126+
GenericParamDefKind::Const { has_default } => {
127+
own_defaults.consts += has_default as usize;
126128
}
127129
}
128130
}
@@ -146,7 +148,9 @@ impl<'tcx> Generics {
146148
pub fn own_requires_monomorphization(&self) -> bool {
147149
for param in &self.params {
148150
match param.kind {
149-
GenericParamDefKind::Type { .. } | GenericParamDefKind::Const => return true,
151+
GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => {
152+
return true;
153+
}
150154
GenericParamDefKind::Lifetime => {}
151155
}
152156
}
@@ -189,7 +193,7 @@ impl<'tcx> Generics {
189193
pub fn const_param(&'tcx self, param: &ParamConst, tcx: TyCtxt<'tcx>) -> &GenericParamDef {
190194
let param = self.param_at(param.index as usize, tcx);
191195
match param.kind {
192-
GenericParamDefKind::Const => param,
196+
GenericParamDefKind::Const { .. } => param,
193197
_ => bug!("expected const parameter, but found another generic parameter"),
194198
}
195199
}

compiler/rustc_middle/src/ty/instance.rs

+1-1
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/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1949,6 +1949,7 @@ pub fn provide(providers: &mut ty::query::Providers) {
19491949
trait_impls_of: trait_def::trait_impls_of_provider,
19501950
all_local_trait_impls: trait_def::all_local_trait_impls,
19511951
type_uninhabited_from: inhabitedness::type_uninhabited_from,
1952+
const_param_default: consts::const_param_default,
19521953
..*providers
19531954
};
19541955
}

0 commit comments

Comments
 (0)