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

compiler/rustc_typeck/src/collect.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1527,7 +1527,7 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
15271527
|lint| {
15281528
lint.build(
15291529
"defaults for type parameters are only allowed in \
1530-
`struct`, `enum`, `type`, or `trait` definitions.",
1530+
`struct`, `enum`, `type`, or `trait` definitions",
15311531
)
15321532
.emit();
15331533
},
@@ -1554,6 +1554,14 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
15541554
Some(param_def)
15551555
}
15561556
GenericParamKind::Const { default, .. } => {
1557+
if !allow_defaults && default.is_some() {
1558+
tcx.sess.span_err(
1559+
param.span,
1560+
"defaults for const parameters are only allowed in \
1561+
`struct`, `enum`, `type`, or `trait` definitions",
1562+
);
1563+
}
1564+
15571565
let param_def = ty::GenericParamDef {
15581566
index: type_start + i as u32,
15591567
name: param.name.ident().name,

compiler/rustc_typeck/src/collect/type_of.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,12 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
436436
.discr_type()
437437
.to_ty(tcx),
438438

439+
Node::GenericParam(&GenericParam {
440+
hir_id: param_hir_id,
441+
kind: GenericParamKind::Const { default: Some(ct), .. },
442+
..
443+
}) if ct.hir_id == hir_id => tcx.type_of(tcx.hir().local_def_id(param_hir_id)),
444+
439445
x => tcx.ty_error_with_message(
440446
DUMMY_SP,
441447
&format!("unexpected const parent in type_of_def_id(): {:?}", x),
Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
1-
// check-pass
1+
// run-pass
22

33
#![feature(const_generics)]
4-
#![feature(const_generic_defaults)]
4+
#![feature(const_generics_defaults)]
55
#![allow(incomplete_features)]
66

77

8-
pub struct ConstDefault<const N: usize = 3> {}
8+
pub struct ConstDefault<const N: usize = 3>;
9+
10+
impl<const N: usize> ConstDefault<N> {
11+
fn foo(self) -> usize {
12+
N
13+
}
14+
}
15+
16+
impl ConstDefault {
17+
fn new() -> Self {
18+
ConstDefault
19+
}
20+
21+
fn bar(self) {}
22+
}
923

1024
pub fn main() {
11-
let s = ConstDefault::default();
25+
let s = ConstDefault::new();
26+
assert_eq!(s.foo(), 3);
27+
28+
let w = ConstDefault::<3>;
29+
w.bar();
1230
}

src/test/ui/const-generics/defaults/wrong-order.full.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: type parameters with a default must be trailing
1+
error: generic parameters with a default must be trailing
22
--> $DIR/wrong-order.rs:4:10
33
|
44
LL | struct A<T = u32, const N: usize> {

src/test/ui/const-generics/defaults/wrong-order.min.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: type parameters with a default must be trailing
1+
error: generic parameters with a default must be trailing
22
--> $DIR/wrong-order.rs:4:10
33
|
44
LL | struct A<T = u32, const N: usize> {

src/test/ui/const-generics/defaults/wrong-order.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![cfg_attr(full, feature(const_generics))] //[full]~WARN the feature `const_generics` is incomplete
33

44
struct A<T = u32, const N: usize> {
5-
//~^ ERROR type parameters with a default must be trailing
5+
//~^ ERROR generic parameters with a default must be trailing
66
arg: T,
77
}
88

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![crate_type = "lib"]
22
#![feature(const_generics_defaults)]
3-
#![feature(min_const_generics)]
43
#![allow(incomplete_features)]
54

65
fn foo<const SIZE: usize = 5usize>() {}
6+
//~^ ERROR defaults for const parameters are
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
error[E0282]: type annotations needed
2-
--> $DIR/default_function_param.rs:6:28
1+
error: defaults for const parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions
2+
--> $DIR/default_function_param.rs:5:14
33
|
4-
LL | fn foo<const SIZE: usize = 5>() {}
5-
| ^ cannot infer type for type `{integer}`
4+
LL | fn foo<const SIZE: usize = 5usize>() {}
5+
| ^^^^
66

77
error: aborting due to previous error
88

9-
For more information about this error, try `rustc --explain E0282`.

src/test/ui/const-generics/params-in-ct-in-ty-param-lazy-norm.full.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: type parameters with a default must be trailing
1+
error: generic parameters with a default must be trailing
22
--> $DIR/params-in-ct-in-ty-param-lazy-norm.rs:11:12
33
|
44
LL | struct Bar<T = [u8; N], const N: usize>(T);

src/test/ui/const-generics/params-in-ct-in-ty-param-lazy-norm.min.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: type parameters with a default must be trailing
1+
error: generic parameters with a default must be trailing
22
--> $DIR/params-in-ct-in-ty-param-lazy-norm.rs:11:12
33
|
44
LL | struct Bar<T = [u8; N], const N: usize>(T);

src/test/ui/const-generics/params-in-ct-in-ty-param-lazy-norm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ struct Foo<T, U = [u8; std::mem::size_of::<T>()]>(T, U);
1010
// FIXME(const_generics_defaults): We still don't know how to deal with type defaults.
1111
struct Bar<T = [u8; N], const N: usize>(T);
1212
//~^ ERROR constant values inside of type parameter defaults
13-
//~| ERROR type parameters with a default
13+
//~| ERROR generic parameters with a default
1414

1515
fn main() {}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
struct Heap;
22

33
struct Vec<A = Heap, T>(A, T);
4-
//~^ ERROR type parameters with a default must be trailing
4+
//~^ ERROR generic parameters with a default must be trailing
55

66
struct Foo<A, B = Vec<C>, C>(A, B, C);
7-
//~^ ERROR type parameters with a default must be trailing
8-
//~| ERROR type parameters with a default cannot use forward declared identifiers
7+
//~^ ERROR generic parameters with a default must be trailing
8+
//~| ERROR generic parameters with a default cannot use forward declared identifiers
99

1010
fn main() {}

src/test/ui/generics/generic-non-trailing-defaults.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error: type parameters with a default must be trailing
1+
error: generic parameters with a default must be trailing
22
--> $DIR/generic-non-trailing-defaults.rs:3:12
33
|
44
LL | struct Vec<A = Heap, T>(A, T);
55
| ^
66

7-
error: type parameters with a default must be trailing
7+
error: generic parameters with a default must be trailing
88
--> $DIR/generic-non-trailing-defaults.rs:6:15
99
|
1010
LL | struct Foo<A, B = Vec<C>, C>(A, B, C);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Ensure that we get an error and not an ICE for this problematic case.
22
struct Foo<T = Option<U>, U = bool>(T, U);
3-
//~^ ERROR type parameters with a default cannot use forward declared identifiers
3+
//~^ ERROR generic parameters with a default cannot use forward declared identifiers
44
fn main() {
55
let x: Foo;
66
}

0 commit comments

Comments
 (0)