Skip to content

Commit b0feb5b

Browse files
lcnrJulianKnodt
authored andcommitted
progress, stuff compiles now
1 parent 8ef8138 commit b0feb5b

25 files changed

+108
-76
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 9 additions & 17 deletions
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",
@@ -1174,17 +1177,6 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11741177
}
11751178
}
11761179
}
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.help("add `#![feature(const_generic_defaults)]` to the crate attributes to enable");
1184-
err.emit();
1185-
break;
1186-
}
1187-
}
11881180
}
11891181

11901182
validate_generic_param_order(

compiler/rustc_error_codes/src/error_codes/E0128.md

Lines changed: 1 addition & 1 deletion
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

Lines changed: 8 additions & 1 deletion
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_middle/src/hir/map/collector.rs

Lines changed: 4 additions & 0 deletions
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/ty/consts.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ impl<'tcx> Const<'tcx> {
4444
let hir_id = tcx.hir().local_def_id_to_hir_id(def.did);
4545

4646
let body_id = match tcx.hir().get(hir_id) {
47-
hir::Node::AnonConst(ac) => ac.body,
47+
hir::Node::AnonConst(ac)
48+
| hir::Node::GenericParam(hir::GenericParam {
49+
kind: hir::GenericParamKind::Const { ty: _, default: Some(ac) },
50+
..
51+
}) => ac.body,
4852
_ => span_bug!(
4953
tcx.def_span(def.did.to_def_id()),
5054
"from_anon_const can only process anonymous constants"

compiler/rustc_resolve/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ enum ResolutionError<'a> {
228228
),
229229
/// Error E0530: `X` bindings cannot shadow `Y`s.
230230
BindingShadowsSomethingUnacceptable(&'static str, Symbol, &'a NameBinding<'a>),
231-
/// Error E0128: type parameters with a default cannot use forward-declared identifiers.
231+
/// Error E0128: generic parameters with a default cannot use forward-declared identifiers.
232232
ForwardDeclaredTyParam, // FIXME(const_generics_defaults)
233233
/// ERROR E0770: the type of const parameters must not depend on other generic parameters.
234234
ParamInTyOfConstParam(Symbol),
@@ -238,7 +238,7 @@ enum ResolutionError<'a> {
238238
///
239239
/// This error is only emitted when using `min_const_generics`.
240240
ParamInNonTrivialAnonConst { name: Symbol, is_type: bool },
241-
/// Error E0735: type parameters with a default cannot use `Self`
241+
/// Error E0735: generic parameters with a default cannot use `Self`
242242
SelfInTyParamDefault,
243243
/// Error E0767: use of unreachable label
244244
UnreachableLabel { name: Symbol, definition_span: Span, suggestion: Option<LabelSuggestion> },

compiler/rustc_typeck/src/astconv/generics.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,9 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
499499
let expected_min = if infer_args {
500500
0
501501
} else {
502-
param_counts.consts + named_type_param_count - default_counts.types
502+
param_counts.consts + named_type_param_count
503+
- default_counts.types
504+
- default_counts.consts
503505
};
504506

505507
check_generics(

compiler/rustc_typeck/src/astconv/mod.rs

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -505,34 +505,17 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
505505
}
506506
}
507507
GenericParamDefKind::Const { has_default } => {
508-
let ty = tcx.at(self.span).type_of(param.def_id);
509508
if !infer_args && has_default {
510-
let c = substs.unwrap()[param.index as usize].expect_const();
511-
ty::subst::GenericArg::from(c)
512-
} else if infer_args {
513-
self.astconv.ct_infer(ty, Some(param), self.span).into()
509+
ty::Const::from_anon_const(tcx, param.def_id.expect_local()).into()
514510
} else {
515-
// We've already errored above about the mismatch.
516-
tcx.const_error(ty).into()
517-
}
518-
// FIXME(const_generic_defaults)
519-
/*
520-
if !infer_args && has_default {
521-
/*
522-
if default_needs_object_self(param) {
523-
missing_type_params.push(param.name.to_string());
524-
tcx.const_error(ty).into()
511+
let ty = tcx.at(self.span).type_of(param.def_id);
512+
if infer_args {
513+
self.astconv.ct_infer(ty, Some(param), self.span).into()
525514
} else {
515+
// We've already errored above about the mismatch.
516+
tcx.const_error(ty).into()
526517
}
527-
*/
528-
} else if infer_args {
529-
// No const parameters were provided, we can infer all.
530-
self.astconv.ct_infer(ty, Some(param), self.span).into()
531-
} else {
532-
// We've already errored above about the mismatch.
533-
tcx.const_error(ty).into()
534518
}
535-
*/
536519
}
537520
}
538521
}

compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,12 +1444,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14441444
}
14451445
}
14461446
GenericParamDefKind::Const { has_default, .. } => {
1447-
if infer_args || !has_default {
1448-
return self.fcx.var_for_def(self.span, param);
1447+
if !infer_args && has_default {
1448+
ty::Const::from_anon_const(tcx, param.def_id.expect_local()).into()
1449+
} else {
1450+
self.fcx.var_for_def(self.span, param)
14491451
}
1450-
// FIXME(const_generic_defaults)
1451-
// No const parameters were provided, we have to infer them.
1452-
todo!()
14531452
}
14541453
}
14551454
}

compiler/rustc_typeck/src/check/wfcheck.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ fn check_where_clauses<'tcx, 'fcx>(
759759
fcx.tcx.mk_param_from_def(param)
760760
}
761761

762-
GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => {
762+
GenericParamDefKind::Type { .. } => {
763763
// If the param has a default, ...
764764
if is_our_default(param) {
765765
let default_ty = fcx.tcx.type_of(param.def_id);
@@ -772,6 +772,16 @@ fn check_where_clauses<'tcx, 'fcx>(
772772

773773
fcx.tcx.mk_param_from_def(param)
774774
}
775+
GenericParamDefKind::Const { .. } => {
776+
if is_our_default(param) {
777+
let default_ct = ty::Const::from_anon_const(tcx, param.def_id.expect_local());
778+
// Const params have to currently be concrete.
779+
assert!(!default_ct.needs_subst());
780+
default_ct.into()
781+
} else {
782+
fcx.tcx.mk_param_from_def(param)
783+
}
784+
}
775785
}
776786
});
777787

0 commit comments

Comments
 (0)