Skip to content

Commit 8ef8138

Browse files
varkorJulianKnodt
authored andcommitted
Some refactoring
1 parent e4e5db4 commit 8ef8138

File tree

27 files changed

+74
-89
lines changed

27 files changed

+74
-89
lines changed

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-
visit_opt(default, |default| vis.visit_anon_const(default));
789788
vis.visit_ty(ty);
789+
visit_opt(default, |default| vis.visit_anon_const(default));
790790
}
791791
}
792792
smallvec![param]

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1180,7 +1180,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11801180
default.value.span,
11811181
"default values for const generic parameters are unstable",
11821182
);
1183-
err.note("to enable them use #![feature(const_generic_defaults)]");
1183+
err.help("add `#![feature(const_generic_defaults)]` to the crate attributes to enable");
11841184
err.emit();
11851185
break;
11861186
}

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2659,6 +2659,7 @@ impl<'a> State<'a> {
26592659
s.word_space(":");
26602660
s.print_type(ty);
26612661
s.print_type_bounds(":", &param.bounds);
2662+
// FIXME(const_generic_defaults)
26622663
if let Some(ref _default) = default {
26632664
// FIXME(const_generics_defaults): print the `default` value here
26642665
s.s.space();

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -963,9 +963,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
963963
.rev()
964964
.filter_map(|param| match param.kind {
965965
ty::GenericParamDefKind::Lifetime => None,
966-
967-
ty::GenericParamDefKind::Type { has_default, .. }
968-
| ty::GenericParamDefKind::Const { has_default } => {
966+
ty::GenericParamDefKind::Const { has_default }
967+
| ty::GenericParamDefKind::Type { has_default, .. } => {
969968
Some((param.def_id, has_default))
970969
}
971970
})

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,13 +1876,16 @@ impl EncodeContext<'a, 'tcx> {
18761876
default.is_some(),
18771877
);
18781878
}
1879-
GenericParamKind::Const { .. } => {
1879+
GenericParamKind::Const { ref default, .. } => {
18801880
self.encode_info_for_generic_param(
18811881
def_id.to_def_id(),
18821882
EntryKind::ConstParam,
18831883
true,
18841884
);
1885-
// FIXME(const_generics_defaults)
1885+
if default.is_some() {
1886+
self.encode_stability(def_id.to_def_id());
1887+
}
1888+
// FIXME(const_generic_defaults)
18861889
}
18871890
}
18881891
}

compiler/rustc_passes/src/stability.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -507,10 +507,9 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
507507

508508
fn visit_generic_param(&mut self, p: &'tcx hir::GenericParam<'tcx>) {
509509
let kind = match &p.kind {
510-
// FIXME(const_generics_defaults)
511-
hir::GenericParamKind::Type { default, .. } if default.is_some() => {
512-
AnnotationKind::Container
513-
}
510+
// Allow stability attributes on default generic arguments.
511+
hir::GenericParamKind::Type { default: Some(_), .. }
512+
| hir::GenericParamKind::Const { default: Some(_), .. } => AnnotationKind::Container,
514513
_ => AnnotationKind::Prohibited,
515514
};
516515

compiler/rustc_privacy/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,7 @@ impl ReachEverythingInTheInterfaceVisitor<'_, 'tcx> {
931931
GenericParamDefKind::Const { has_default, .. } => {
932932
self.visit(self.ev.tcx.type_of(param.def_id));
933933
if has_default {
934+
// FIXME(const_generic_defaults)
934935
// how should the error case be handled here?
935936
// let default_const = self.ev.tcx.const_eval_poly(param.def_id).unwrap();
936937
// self.visit(default_const);

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,12 +450,12 @@ impl<'a> Resolver<'a> {
450450
self.session,
451451
span,
452452
E0128,
453-
"type parameters with a default cannot use \
453+
"generic parameters with a default cannot use \
454454
forward declared identifiers"
455455
);
456456
err.span_label(
457457
span,
458-
"defaulted type parameters cannot be forward declared".to_string(),
458+
"defaulted generic parameters cannot be forward declared".to_string(),
459459
);
460460
err
461461
}

compiler/rustc_resolve/src/late.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,10 @@ crate enum RibKind<'a> {
132132
/// We passed through a `macro_rules!` statement
133133
MacroDefinition(DefId),
134134

135-
/// All bindings in this rib are type parameters that can't be used
136-
/// from the default of a type parameter because they're not declared
137-
/// before said type parameter. Also see the `visit_generics` override.
138-
ForwardTyParamBanRibKind,
135+
/// All bindings in this rib are generic parameters that can't be used
136+
/// from the default of a generic parameter because they're not declared
137+
/// before said generic parameter. Also see the `visit_generics` override.
138+
ForwardGenericParamBanRibKind,
139139

140140
/// We are inside of the type of a const parameter. Can't refer to any
141141
/// parameters.
@@ -154,7 +154,7 @@ impl RibKind<'_> {
154154
| ModuleRibKind(_)
155155
| MacroDefinition(_)
156156
| ConstParamTyRibKind => false,
157-
AssocItemRibKind | ItemRibKind(_) | ForwardTyParamBanRibKind => true,
157+
AssocItemRibKind | ItemRibKind(_) | ForwardGenericParamBanRibKind => true,
158158
}
159159
}
160160
}
@@ -555,15 +555,16 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
555555
// provide previous type parameters as they're built. We
556556
// put all the parameters on the ban list and then remove
557557
// them one by one as they are processed and become available.
558-
let mut default_ban_rib = Rib::new(ForwardTyParamBanRibKind);
558+
let mut default_ban_rib = Rib::new(ForwardGenericParamBanRibKind);
559559
let mut found_default = false;
560560
default_ban_rib.bindings.extend(generics.params.iter().filter_map(
561561
|param| match param.kind {
562-
GenericParamKind::Const { .. } | GenericParamKind::Lifetime { .. } => None,
563-
GenericParamKind::Type { ref default, .. } => {
564-
found_default |= default.is_some();
565-
found_default.then_some((Ident::with_dummy_span(param.ident.name), Res::Err))
562+
GenericParamKind::Type { default: Some(_), .. }
563+
| GenericParamKind::Const { default: Some(_), .. } => {
564+
found_default = true;
565+
Some((Ident::with_dummy_span(param.ident.name), Res::Err))
566566
}
567+
_ => None,
567568
},
568569
));
569570

@@ -591,8 +592,8 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
591592

592593
if let Some(ref ty) = default {
593594
self.ribs[TypeNS].push(default_ban_rib);
594-
self.with_rib(ValueNS, ForwardTyParamBanRibKind, |this| {
595-
// HACK: We use an empty `ForwardTyParamBanRibKind` here which
595+
self.with_rib(ValueNS, ForwardGenericParamBanRibKind, |this| {
596+
// HACK: We use an empty `ForwardGenericParamBanRibKind` here which
596597
// is only used to forbid the use of const parameters inside of
597598
// type defaults.
598599
//
@@ -616,7 +617,6 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
616617
self.visit_ty(ty);
617618
self.ribs[TypeNS].pop().unwrap();
618619
self.ribs[ValueNS].pop().unwrap();
619-
// FIXME(const_generics:default) do something with default here?
620620
}
621621
}
622622
}
@@ -866,7 +866,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
866866
| ItemRibKind(..)
867867
| ConstantItemRibKind(..)
868868
| ModuleRibKind(..)
869-
| ForwardTyParamBanRibKind
869+
| ForwardGenericParamBanRibKind
870870
| ConstParamTyRibKind => {
871871
return false;
872872
}

compiler/rustc_resolve/src/lib.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2592,8 +2592,8 @@ impl<'a> Resolver<'a> {
25922592
debug!("validate_res_from_ribs({:?})", res);
25932593
let ribs = &all_ribs[rib_index + 1..];
25942594

2595-
// An invalid forward use of a type parameter from a previous default.
2596-
if let ForwardTyParamBanRibKind = all_ribs[rib_index].kind {
2595+
// An invalid forward use of a generic parameter from a previous default.
2596+
if let ForwardGenericParamBanRibKind = all_ribs[rib_index].kind {
25972597
if record_used {
25982598
let res_error = if rib_ident.name == kw::SelfUpper {
25992599
ResolutionError::SelfInTyParamDefault
@@ -2617,7 +2617,7 @@ impl<'a> Resolver<'a> {
26172617
| ClosureOrAsyncRibKind
26182618
| ModuleRibKind(..)
26192619
| MacroDefinition(..)
2620-
| ForwardTyParamBanRibKind => {
2620+
| ForwardGenericParamBanRibKind => {
26212621
// Nothing to do. Continue.
26222622
}
26232623
ItemRibKind(_) | FnItemRibKind | AssocItemRibKind => {
@@ -2689,7 +2689,9 @@ impl<'a> Resolver<'a> {
26892689

26902690
// We only forbid constant items if we are inside of type defaults,
26912691
// for example `struct Foo<T, U = [u8; std::mem::size_of::<T>()]>`
2692-
ForwardTyParamBanRibKind => {
2692+
ForwardGenericParamBanRibKind => {
2693+
// FIXME(const_generic_defaults): we may need to distinguish between
2694+
// being in type parameter defaults and const parameter defaults
26932695
in_ty_param_default = true;
26942696
continue;
26952697
}
@@ -2782,7 +2784,9 @@ impl<'a> Resolver<'a> {
27822784

27832785
// We only forbid constant items if we are inside of type defaults,
27842786
// for example `struct Foo<T, U = [u8; std::mem::size_of::<T>()]>`
2785-
ForwardTyParamBanRibKind => {
2787+
ForwardGenericParamBanRibKind => {
2788+
// FIXME(const_generic_defaults): we may need to distinguish between
2789+
// being in type parameter defaults and const parameter defaults
27862790
in_ty_param_default = true;
27872791
continue;
27882792
}

compiler/rustc_typeck/src/astconv/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,14 +507,15 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
507507
GenericParamDefKind::Const { has_default } => {
508508
let ty = tcx.at(self.span).type_of(param.def_id);
509509
if !infer_args && has_default {
510-
let c = ty::Const::from_anon_const(tcx, param.def_id.expect_local());
510+
let c = substs.unwrap()[param.index as usize].expect_const();
511511
ty::subst::GenericArg::from(c)
512512
} else if infer_args {
513513
self.astconv.ct_infer(ty, Some(param), self.span).into()
514514
} else {
515515
// We've already errored above about the mismatch.
516516
tcx.const_error(ty).into()
517517
}
518+
// FIXME(const_generic_defaults)
518519
/*
519520
if !infer_args && has_default {
520521
/*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1447,7 +1447,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14471447
if infer_args || !has_default {
14481448
return self.fcx.var_for_def(self.span, param);
14491449
}
1450-
// FIXME(const_generics:defaults)
1450+
// FIXME(const_generic_defaults)
14511451
// No const parameters were provided, we have to infer them.
14521452
todo!()
14531453
}

compiler/rustc_typeck/src/check/wfcheck.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -713,10 +713,11 @@ fn check_where_clauses<'tcx, 'fcx>(
713713
let generics = tcx.generics_of(def_id);
714714

715715
let is_our_default = |def: &ty::GenericParamDef| match def.kind {
716-
GenericParamDefKind::Type { has_default, .. } => {
716+
GenericParamDefKind::Type { has_default, .. }
717+
| GenericParamDefKind::Const { has_default } => {
717718
has_default && def.index >= generics.parent_count as u32
718719
}
719-
_ => unreachable!(),
720+
GenericParamDefKind::Lifetime => unreachable!(),
720721
};
721722

722723
// Check that concrete defaults are well-formed. See test `type-check-defaults.rs`.
@@ -758,7 +759,7 @@ fn check_where_clauses<'tcx, 'fcx>(
758759
fcx.tcx.mk_param_from_def(param)
759760
}
760761

761-
GenericParamDefKind::Const { .. } | GenericParamDefKind::Type { .. } => {
762+
GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => {
762763
// If the param has a default, ...
763764
if is_our_default(param) {
764765
let default_ty = fcx.tcx.type_of(param.def_id);

compiler/rustc_typeck/src/collect.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,14 @@ impl Visitor<'tcx> for CollectItemTypesVisitor<'tcx> {
254254
self.tcx.ensure().type_of(def_id);
255255
}
256256
hir::GenericParamKind::Type { .. } => {}
257-
hir::GenericParamKind::Const { .. } => {
257+
hir::GenericParamKind::Const { default, .. } => {
258258
let def_id = self.tcx.hir().local_def_id(param.hir_id);
259259
self.tcx.ensure().type_of(def_id);
260260
// FIXME(const_generics_defaults)
261+
if let Some(default) = default {
262+
let def_id = self.tcx.hir().local_def_id(default.hir_id);
263+
self.tcx.ensure().type_of(def_id);
264+
}
261265
}
262266
}
263267
}

compiler/rustc_typeck/src/collect/type_of.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
8383
return generics
8484
.params
8585
.iter()
86-
.filter(|param| matches!(param.kind, ty::GenericParamDefKind::Const { ..
87-
}))
86+
.filter(|param| matches!(param.kind, ty::GenericParamDefKind::Const { .. }))
8887
.nth(arg_index)
8988
.map(|param| param.def_id);
9089
}

src/test/ui/const-generics/defaults/const-default.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
#![allow(incomplete_features)]
66

77

8-
#[derive(Default)]
9-
pub struct ConstDefault<const N: usize = 3> {
10-
items: [u32; N]
11-
}
8+
pub struct ConstDefault<const N: usize = 3> {}
129

1310
pub fn main() {
1411
let s = ConstDefault::default();
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
#![feature(const_generic_defaults)]
1+
#![crate_type = "lib"]
2+
#![feature(const_generics_defaults)]
23
#![feature(min_const_generics)]
4+
#![allow(incomplete_features)]
35

4-
fn foo<const SIZE: usize = 5>() {}
5-
//~^ ERROR default values for const generic parameters are experimental
6-
7-
fn main() {}
6+
fn foo<const SIZE: usize = 5usize>() {}
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
error[E0658]: default values for const generic parameters are experimental
2-
--> $DIR/default_function_param.rs:1:26
1+
error[E0282]: type annotations needed
2+
--> $DIR/default_function_param.rs:6:28
33
|
44
LL | fn foo<const SIZE: usize = 5>() {}
5-
| ^^^
6-
|
7-
= note: see issue #44580 <https://github.com/rust-lang/rust/issues/44580> for more information
8-
= help: add `#![feature(const_generics_defaults)]` to the crate attributes to enable
5+
| ^ cannot infer type for type `{integer}`
96

107
error: aborting due to previous error
118

12-
For more information about this error, try `rustc --explain E0658`.
9+
For more information about this error, try `rustc --explain E0282`.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
trait Foo<const KIND: bool = true> {}
22
//~^ ERROR default values for const generic parameters are experimental
3+
4+
fn main() {}

src/test/ui/error-codes/E0128.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0128]: type parameters with a default cannot use forward declared identifiers
1+
error[E0128]: generic parameters with a default cannot use forward declared identifiers
22
--> $DIR/E0128.rs:1:14
33
|
44
LL | struct Foo<T=U, U=()> {
5-
| ^ defaulted type parameters cannot be forward declared
5+
| ^ defaulted generic parameters cannot be forward declared
66

77
error: aborting due to previous error
88

src/test/ui/feature-gate/feature-gate-const_generic_defaults.rs

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/test/ui/feature-gate/feature-gate-const_generic_defaults.stderr

Lines changed: 0 additions & 10 deletions
This file was deleted.

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,5 @@ error: type parameters with a default must be trailing
1010
LL | struct Foo<A, B = Vec<C>, C>(A, B, C);
1111
| ^
1212

13-
error[E0128]: type parameters with a default cannot use forward declared identifiers
14-
--> $DIR/generic-non-trailing-defaults.rs:6:23
15-
|
16-
LL | struct Foo<A, B = Vec<C>, C>(A, B, C);
17-
| ^ defaulted type parameters cannot be forward declared
18-
19-
error: aborting due to 3 previous errors
13+
error: aborting due to 2 previous errors
2014

21-
For more information about this error, try `rustc --explain E0128`.

src/test/ui/generics/generic-type-params-forward-mention.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0128]: type parameters with a default cannot use forward declared identifiers
1+
error[E0128]: generic parameters with a default cannot use forward declared identifiers
22
--> $DIR/generic-type-params-forward-mention.rs:2:23
33
|
44
LL | struct Foo<T = Option<U>, U = bool>(T, U);
5-
| ^ defaulted type parameters cannot be forward declared
5+
| ^ defaulted generic parameters cannot be forward declared
66

77
error: aborting due to previous error
88

src/test/ui/issues/issue-18183.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0128]: type parameters with a default cannot use forward declared identifiers
1+
error[E0128]: generic parameters with a default cannot use forward declared identifiers
22
--> $DIR/issue-18183.rs:1:20
33
|
44
LL | pub struct Foo<Bar=Bar>(Bar);
5-
| ^^^ defaulted type parameters cannot be forward declared
5+
| ^^^ defaulted generic parameters cannot be forward declared
66

77
error: aborting due to previous error
88

0 commit comments

Comments
 (0)