Skip to content

Commit 2ea320a

Browse files
committed
Some refactoring
1 parent 1b679fd commit 2ea320a

File tree

16 files changed

+55
-41
lines changed

16 files changed

+55
-41
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1151,7 +1151,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11511151
default.value.span,
11521152
"default values for const generic parameters are unstable",
11531153
);
1154-
err.note("to enable them use #![feature(const_generic_defaults)]");
1154+
err.help("add `#![feature(const_generic_defaults)]` to the crate attributes to enable");
11551155
err.emit();
11561156
break;
11571157
}
@@ -1165,6 +1165,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11651165
let ident = Some(param.ident.to_string());
11661166
let (kind, ident) = match &param.kind {
11671167
GenericParamKind::Lifetime => (ParamKindOrd::Lifetime, ident),
1168+
// FIXME: here and below, take `default` into account.
11681169
GenericParamKind::Type { default: _ } => (ParamKindOrd::Type, ident),
11691170
GenericParamKind::Const { ref ty, kw_span: _, default: _ } => {
11701171
let ty = pprust::ty_to_string(ty);

compiler/rustc_ast_pretty/src/pprust/state.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2675,6 +2675,7 @@ impl<'a> State<'a> {
26752675
s.word_space(":");
26762676
s.print_type(ty);
26772677
s.print_type_bounds(":", &param.bounds);
2678+
// FIXME(const_generic_defaults)
26782679
if let Some(ref _default) = default {
26792680
s.s.space();
26802681
s.word_space("=");

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -947,11 +947,10 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
947947
.rev()
948948
.filter_map(|param| match param.kind {
949949
ty::GenericParamDefKind::Lifetime => None,
950-
951-
ty::GenericParamDefKind::Type { has_default, .. }
952-
| ty::GenericParamDefKind::Const { has_default } => {
950+
ty::GenericParamDefKind::Type { has_default, .. } => {
953951
Some((param.def_id, has_default))
954952
}
953+
ty::GenericParamDefKind::Const { .. } => None, // FIXME(const_generic_defaults)
955954
})
956955
.peekable();
957956
let has_default = {

compiler/rustc_metadata/src/rmeta/encoder.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1828,13 +1828,16 @@ impl EncodeContext<'a, 'tcx> {
18281828
self.encode_stability(def_id.to_def_id());
18291829
}
18301830
}
1831-
GenericParamKind::Const { .. } => {
1831+
GenericParamKind::Const { ref default, .. } => {
18321832
self.encode_info_for_generic_param(
18331833
def_id.to_def_id(),
18341834
EntryKind::ConstParam,
18351835
true,
18361836
);
1837-
// FIXME(const_generics:defaults)
1837+
if default.is_some() {
1838+
self.encode_stability(def_id.to_def_id());
1839+
}
1840+
// FIXME(const_generic_defaults)
18381841
}
18391842
}
18401843
}

compiler/rustc_passes/src/stability.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,9 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
443443

444444
fn visit_generic_param(&mut self, p: &'tcx hir::GenericParam<'tcx>) {
445445
let kind = match &p.kind {
446-
// FIXME(const_generics:defaults)
447-
hir::GenericParamKind::Type { default, .. } if default.is_some() => {
446+
// Allow stability attributes on default generic arguments.
447+
hir::GenericParamKind::Type { default: Some(_), .. }
448+
| hir::GenericParamKind::Const { default: Some(_), .. } => {
448449
AnnotationKind::Container
449450
}
450451
_ => AnnotationKind::Prohibited,

compiler/rustc_privacy/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,7 @@ impl ReachEverythingInTheInterfaceVisitor<'_, 'tcx> {
879879
GenericParamDefKind::Const { has_default, .. } => {
880880
self.visit(self.ev.tcx.type_of(param.def_id));
881881
if has_default {
882+
// FIXME(const_generic_defaults)
882883
// how should the error case be handled here?
883884
// let default_const = self.ev.tcx.const_eval_poly(param.def_id).unwrap();
884885
// self.visit(default_const);

compiler/rustc_resolve/src/diagnostics.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -428,17 +428,17 @@ impl<'a> Resolver<'a> {
428428
err.span_label(binding.span, msg);
429429
err
430430
}
431-
ResolutionError::ForwardDeclaredTyParam => {
431+
ResolutionError::ForwardDeclaredGenericParam => {
432432
let mut err = struct_span_err!(
433433
self.session,
434434
span,
435435
E0128,
436-
"type parameters with a default cannot use \
436+
"generic parameters with a default cannot use \
437437
forward declared identifiers"
438438
);
439439
err.span_label(
440440
span,
441-
"defaulted type parameters cannot be forward declared".to_string(),
441+
"defaulted generic parameters cannot be forward declared".to_string(),
442442
);
443443
err
444444
}

compiler/rustc_resolve/src/late.rs

+15-14
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,10 @@ crate enum RibKind<'a> {
127127
/// We passed through a `macro_rules!` statement
128128
MacroDefinition(DefId),
129129

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

135135
/// We are inside of the type of a const parameter. Can't refer to any
136136
/// parameters.
@@ -149,7 +149,7 @@ impl RibKind<'_> {
149149
| ModuleRibKind(_)
150150
| MacroDefinition(_)
151151
| ConstParamTyRibKind => false,
152-
AssocItemRibKind | ItemRibKind(_) | ForwardTyParamBanRibKind => true,
152+
AssocItemRibKind | ItemRibKind(_) | ForwardGenericParamBanRibKind => true,
153153
}
154154
}
155155
}
@@ -535,15 +535,16 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
535535
// provide previous type parameters as they're built. We
536536
// put all the parameters on the ban list and then remove
537537
// them one by one as they are processed and become available.
538-
let mut default_ban_rib = Rib::new(ForwardTyParamBanRibKind);
538+
let mut default_ban_rib = Rib::new(ForwardGenericParamBanRibKind);
539539
let mut found_default = false;
540540
default_ban_rib.bindings.extend(generics.params.iter().filter_map(
541541
|param| match param.kind {
542-
GenericParamKind::Const { .. } | GenericParamKind::Lifetime { .. } => None,
543-
GenericParamKind::Type { ref default, .. } => {
544-
found_default |= default.is_some();
545-
found_default.then_some((Ident::with_dummy_span(param.ident.name), Res::Err))
542+
GenericParamKind::Type { default: Some(_), .. }
543+
| GenericParamKind::Const { default: Some(_), .. } => {
544+
found_default = true;
545+
Some((Ident::with_dummy_span(param.ident.name), Res::Err))
546546
}
547+
_ => None,
547548
},
548549
));
549550

@@ -571,8 +572,8 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
571572

572573
if let Some(ref ty) = default {
573574
self.ribs[TypeNS].push(default_ban_rib);
574-
self.with_rib(ValueNS, ForwardTyParamBanRibKind, |this| {
575-
// HACK: We use an empty `ForwardTyParamBanRibKind` here which
575+
self.with_rib(ValueNS, ForwardGenericParamBanRibKind, |this| {
576+
// HACK: We use an empty `ForwardGenericParamBanRibKind` here which
576577
// is only used to forbid the use of const parameters inside of
577578
// type defaults.
578579
//
@@ -587,6 +588,7 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
587588
default_ban_rib.bindings.remove(&Ident::with_dummy_span(param.ident.name));
588589
}
589590
GenericParamKind::Const { ref ty, kw_span: _, default: _ } => {
591+
// FIXME(const_generic_defaults): handle `default`
590592
for bound in &param.bounds {
591593
self.visit_param_bound(bound);
592594
}
@@ -595,7 +597,6 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
595597
self.visit_ty(ty);
596598
self.ribs[TypeNS].pop().unwrap();
597599
self.ribs[ValueNS].pop().unwrap();
598-
// FIXME(const_generics:default) do something with default here?
599600
}
600601
}
601602
}
@@ -845,7 +846,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
845846
| ItemRibKind(..)
846847
| ConstantItemRibKind(_)
847848
| ModuleRibKind(..)
848-
| ForwardTyParamBanRibKind
849+
| ForwardGenericParamBanRibKind
849850
| ConstParamTyRibKind => {
850851
return false;
851852
}

compiler/rustc_resolve/src/lib.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ enum ResolutionError<'a> {
214214
/// Error E0530: `X` bindings cannot shadow `Y`s.
215215
BindingShadowsSomethingUnacceptable(&'static str, Symbol, &'a NameBinding<'a>),
216216
/// Error E0128: type parameters with a default cannot use forward-declared identifiers.
217-
ForwardDeclaredTyParam, // FIXME(const_generics:defaults)
217+
ForwardDeclaredGenericParam, // FIXME(const_generic_defaults): we need a test for this
218218
/// ERROR E0770: the type of const parameters must not depend on other generic parameters.
219219
ParamInTyOfConstParam(Symbol),
220220
/// constant values inside of type parameter defaults must not depend on generic parameters.
@@ -2547,13 +2547,13 @@ impl<'a> Resolver<'a> {
25472547
debug!("validate_res_from_ribs({:?})", res);
25482548
let ribs = &all_ribs[rib_index + 1..];
25492549

2550-
// An invalid forward use of a type parameter from a previous default.
2551-
if let ForwardTyParamBanRibKind = all_ribs[rib_index].kind {
2550+
// An invalid forward use of a generic parameter from a previous default.
2551+
if let ForwardGenericParamBanRibKind = all_ribs[rib_index].kind {
25522552
if record_used {
25532553
let res_error = if rib_ident.name == kw::SelfUpper {
25542554
ResolutionError::SelfInTyParamDefault
25552555
} else {
2556-
ResolutionError::ForwardDeclaredTyParam
2556+
ResolutionError::ForwardDeclaredGenericParam
25572557
};
25582558
self.report_error(span, res_error);
25592559
}
@@ -2572,7 +2572,7 @@ impl<'a> Resolver<'a> {
25722572
| ClosureOrAsyncRibKind
25732573
| ModuleRibKind(..)
25742574
| MacroDefinition(..)
2575-
| ForwardTyParamBanRibKind => {
2575+
| ForwardGenericParamBanRibKind => {
25762576
// Nothing to do. Continue.
25772577
}
25782578
ItemRibKind(_) | FnItemRibKind | AssocItemRibKind => {
@@ -2622,7 +2622,9 @@ impl<'a> Resolver<'a> {
26222622

26232623
// We only forbid constant items if we are inside of type defaults,
26242624
// for example `struct Foo<T, U = [u8; std::mem::size_of::<T>()]>`
2625-
ForwardTyParamBanRibKind => {
2625+
ForwardGenericParamBanRibKind => {
2626+
// FIXME(const_generic_defaults): we may need to distinguish between
2627+
// being in type parameter defaults and const parameter defaults
26262628
in_ty_param_default = true;
26272629
continue;
26282630
}
@@ -2715,7 +2717,9 @@ impl<'a> Resolver<'a> {
27152717

27162718
// We only forbid constant items if we are inside of type defaults,
27172719
// for example `struct Foo<T, U = [u8; std::mem::size_of::<T>()]>`
2718-
ForwardTyParamBanRibKind => {
2720+
ForwardGenericParamBanRibKind => {
2721+
// FIXME(const_generic_defaults): we may need to distinguish between
2722+
// being in type parameter defaults and const parameter defaults
27192723
in_ty_param_default = true;
27202724
continue;
27212725
}

compiler/rustc_save_analysis/src/dump_visitor.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1343,10 +1343,12 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> {
13431343
self.visit_ty(ty);
13441344
}
13451345
}
1346-
hir::GenericParamKind::Const { ref ty, default: _ } => {
1346+
hir::GenericParamKind::Const { ref ty, ref default } => {
13471347
self.process_bounds(param.bounds);
13481348
self.visit_ty(ty);
1349-
// Don't have to visit default here because it's known to be a const
1349+
if let Some(default) = default {
1350+
self.visit_anon_const(default);
1351+
}
13501352
}
13511353
}
13521354
}

compiler/rustc_typeck/src/astconv/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
495495
// We've already errored above about the mismatch.
496496
tcx.const_error(ty).into()
497497
}
498+
// FIXME(const_generic_defaults)
498499
/*
499500
if !infer_args && has_default {
500501
/*

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1379,7 +1379,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13791379
if infer_args || !has_default {
13801380
return self.fcx.var_for_def(self.span, param);
13811381
}
1382-
// FIXME(const_generics:defaults)
1382+
// FIXME(const_generic_defaults)
13831383
// No const parameters were provided, we have to infer them.
13841384
todo!()
13851385
}

compiler/rustc_typeck/src/check/wfcheck.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,8 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) {
286286
// We currently only check wf of const params here.
287287
hir::GenericParamKind::Lifetime { .. } | hir::GenericParamKind::Type { .. } => (),
288288

289-
// Const parameters are well formed if their
290-
// type is structural match.
289+
// Const parameters are well formed if their type is structural match.
290+
// FIXME(const_generic_defaults): we also need to check that the default is wf
291291
hir::GenericParamKind::Const { ty: hir_ty, .. } => {
292292
let ty = tcx.type_of(tcx.hir().local_def_id(param.hir_id));
293293

compiler/rustc_typeck/src/collect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ impl Visitor<'tcx> for CollectItemTypesVisitor<'tcx> {
228228
hir::GenericParamKind::Const { .. } => {
229229
let def_id = self.tcx.hir().local_def_id(param.hir_id);
230230
self.tcx.ensure().type_of(def_id);
231-
// FIXME(const_generics:defaults)
231+
// FIXME(const_generic_defaults)
232232
}
233233
}
234234
}

src/librustdoc/clean/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ impl Clean<GenericParamDef> for hir::GenericParam<'_> {
612612
GenericParamDefKind::Const {
613613
did: cx.tcx.hir().local_def_id(self.hir_id).to_def_id(),
614614
ty: ty.clean(cx),
615-
// FIXME(const_generics:default) add default field here to docs
615+
// FIXME(const_generic_defaults): add default field here for docs
616616
},
617617
),
618618
};
@@ -1386,7 +1386,7 @@ fn clean_qpath(hir_ty: &hir::Ty<'_>, cx: &DocContext<'_>) -> Type {
13861386
if let Some(ct) = const_ {
13871387
ct_substs.insert(const_param_def_id.to_def_id(), ct.clean(cx));
13881388
}
1389-
// FIXME(const_generics:defaults)
1389+
// FIXME(const_generic_defaults)
13901390
indices.consts += 1;
13911391
}
13921392
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ struct Foo<T, U = [u8; std::mem::size_of::<T>()]>(T, U);
77
//[full]~^ ERROR constant values inside of type parameter defaults
88
//[min]~^^ ERROR generic parameters may not be used in const operations
99

10-
// FIXME(const_generics:defaults): We still don't know how to we deal with type defaults.
10+
// FIXME(const_generic_defaults): We still don't know how to we deal with type defaults.
1111
struct Bar<T = [u8; N], const N: usize>(T);
1212
//~^ ERROR constant values inside of type parameter defaults
1313
//~| ERROR type parameters with a default

0 commit comments

Comments
 (0)