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
}

0 commit comments

Comments
 (0)