Skip to content

Commit 7081e9b

Browse files
authored
Unrolled build for #132912
Rollup merge of #132912 - fmease:simplify-gen-param-default-users, r=compiler-errors Simplify some places that deal with generic parameter defaults
2 parents 81eef2d + d0ddba3 commit 7081e9b

File tree

4 files changed

+39
-115
lines changed

4 files changed

+39
-115
lines changed

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+23-75
Original file line numberDiff line numberDiff line change
@@ -1427,57 +1427,28 @@ fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, span: Span, def_id
14271427
let predicates = tcx.predicates_of(def_id.to_def_id());
14281428
let generics = tcx.generics_of(def_id);
14291429

1430-
let is_our_default = |def: &ty::GenericParamDef| match def.kind {
1431-
GenericParamDefKind::Type { has_default, .. }
1432-
| GenericParamDefKind::Const { has_default, .. } => {
1433-
has_default && def.index >= generics.parent_count as u32
1434-
}
1435-
GenericParamDefKind::Lifetime => {
1436-
span_bug!(tcx.def_span(def.def_id), "lifetime params can have no default")
1437-
}
1438-
};
1439-
14401430
// Check that concrete defaults are well-formed. See test `type-check-defaults.rs`.
14411431
// For example, this forbids the declaration:
14421432
//
14431433
// struct Foo<T = Vec<[u32]>> { .. }
14441434
//
14451435
// Here, the default `Vec<[u32]>` is not WF because `[u32]: Sized` does not hold.
14461436
for param in &generics.own_params {
1447-
match param.kind {
1448-
GenericParamDefKind::Type { .. } => {
1449-
if is_our_default(param) {
1450-
let ty = tcx.type_of(param.def_id).instantiate_identity();
1451-
// Ignore dependent defaults -- that is, where the default of one type
1452-
// parameter includes another (e.g., `<T, U = T>`). In those cases, we can't
1453-
// be sure if it will error or not as user might always specify the other.
1454-
if !ty.has_param() {
1455-
wfcx.register_wf_obligation(
1456-
tcx.def_span(param.def_id),
1457-
Some(WellFormedLoc::Ty(param.def_id.expect_local())),
1458-
ty.into(),
1459-
);
1460-
}
1461-
}
1462-
}
1463-
GenericParamDefKind::Const { .. } => {
1464-
if is_our_default(param) {
1465-
// FIXME(const_generics_defaults): This
1466-
// is incorrect when dealing with unused args, for example
1467-
// for `struct Foo<const N: usize, const M: usize = { 1 - 2 }>`
1468-
// we should eagerly error.
1469-
let default_ct = tcx.const_param_default(param.def_id).instantiate_identity();
1470-
if !default_ct.has_param() {
1471-
wfcx.register_wf_obligation(
1472-
tcx.def_span(param.def_id),
1473-
None,
1474-
default_ct.into(),
1475-
);
1476-
}
1477-
}
1437+
if let Some(default) = param.default_value(tcx).map(ty::EarlyBinder::instantiate_identity) {
1438+
// Ignore dependent defaults -- that is, where the default of one type
1439+
// parameter includes another (e.g., `<T, U = T>`). In those cases, we can't
1440+
// be sure if it will error or not as user might always specify the other.
1441+
// FIXME(generic_const_exprs): This is incorrect when dealing with unused const params.
1442+
// E.g: `struct Foo<const N: usize, const M: usize = { 1 - 2 }>;`. Here, we should
1443+
// eagerly error but we don't as we have `ConstKind::Unevaluated(.., [N, M])`.
1444+
if !default.has_param() {
1445+
wfcx.register_wf_obligation(
1446+
tcx.def_span(param.def_id),
1447+
matches!(param.kind, GenericParamDefKind::Type { .. })
1448+
.then(|| WellFormedLoc::Ty(param.def_id.expect_local())),
1449+
default,
1450+
);
14781451
}
1479-
// Doesn't have defaults.
1480-
GenericParamDefKind::Lifetime => {}
14811452
}
14821453
}
14831454

@@ -1490,39 +1461,16 @@ fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, span: Span, def_id
14901461
//
14911462
// First we build the defaulted generic parameters.
14921463
let args = GenericArgs::for_item(tcx, def_id.to_def_id(), |param, _| {
1493-
match param.kind {
1494-
GenericParamDefKind::Lifetime => {
1495-
// All regions are identity.
1496-
tcx.mk_param_from_def(param)
1497-
}
1498-
1499-
GenericParamDefKind::Type { .. } => {
1500-
// If the param has a default, ...
1501-
if is_our_default(param) {
1502-
let default_ty = tcx.type_of(param.def_id).instantiate_identity();
1503-
// ... and it's not a dependent default, ...
1504-
if !default_ty.has_param() {
1505-
// ... then instantiate it with the default.
1506-
return default_ty.into();
1507-
}
1508-
}
1509-
1510-
tcx.mk_param_from_def(param)
1511-
}
1512-
GenericParamDefKind::Const { .. } => {
1513-
// If the param has a default, ...
1514-
if is_our_default(param) {
1515-
let default_ct = tcx.const_param_default(param.def_id).instantiate_identity();
1516-
// ... and it's not a dependent default, ...
1517-
if !default_ct.has_param() {
1518-
// ... then instantiate it with the default.
1519-
return default_ct.into();
1520-
}
1521-
}
1522-
1523-
tcx.mk_param_from_def(param)
1524-
}
1464+
if param.index >= generics.parent_count as u32
1465+
// If the param has a default, ...
1466+
&& let Some(default) = param.default_value(tcx).map(ty::EarlyBinder::instantiate_identity)
1467+
// ... and it's not a dependent default, ...
1468+
&& !default.has_param()
1469+
{
1470+
// ... then instantiate it with the default.
1471+
return default;
15251472
}
1473+
tcx.mk_param_from_def(param)
15261474
});
15271475

15281476
// Now we build the instantiated predicates.

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+9-23
Original file line numberDiff line numberDiff line change
@@ -1306,30 +1306,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13061306
rustc_hir_analysis::hir_ty_lowering::RegionInferReason::Param(param),
13071307
)
13081308
.into(),
1309-
GenericParamDefKind::Type { has_default, .. } => {
1310-
if !infer_args && has_default {
1311-
// If we have a default, then it doesn't matter that we're not
1312-
// inferring the type arguments: we provide the default where any
1313-
// is missing.
1314-
tcx.type_of(param.def_id).instantiate(tcx, preceding_args).into()
1315-
} else {
1316-
// If no type arguments were provided, we have to infer them.
1317-
// This case also occurs as a result of some malformed input, e.g.
1318-
// a lifetime argument being given instead of a type parameter.
1319-
// Using inference instead of `Error` gives better error messages.
1320-
self.fcx.var_for_def(self.span, param)
1309+
GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => {
1310+
if !infer_args && let Some(default) = param.default_value(tcx) {
1311+
// If we have a default, then it doesn't matter that we're not inferring
1312+
// the type/const arguments: We provide the default where any is missing.
1313+
return default.instantiate(tcx, preceding_args);
13211314
}
1322-
}
1323-
GenericParamDefKind::Const { has_default, .. } => {
1324-
if has_default {
1325-
if !infer_args {
1326-
return tcx
1327-
.const_param_default(param.def_id)
1328-
.instantiate(tcx, preceding_args)
1329-
.into();
1330-
}
1331-
}
1332-
1315+
// If no type/const arguments were provided, we have to infer them.
1316+
// This case also occurs as a result of some malformed input, e.g.,
1317+
// a lifetime argument being given instead of a type/const parameter.
1318+
// Using inference instead of `Error` gives better error messages.
13331319
self.fcx.var_for_def(self.span, param)
13341320
}
13351321
}

compiler/rustc_middle/src/ty/generics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ impl GenericParamDef {
8686
tcx: TyCtxt<'tcx>,
8787
) -> Option<EarlyBinder<'tcx, ty::GenericArg<'tcx>>> {
8888
match self.kind {
89-
GenericParamDefKind::Type { has_default, .. } if has_default => {
89+
GenericParamDefKind::Type { has_default: true, .. } => {
9090
Some(tcx.type_of(self.def_id).map_bound(|t| t.into()))
9191
}
92-
GenericParamDefKind::Const { has_default, .. } if has_default => {
92+
GenericParamDefKind::Const { has_default: true, .. } => {
9393
Some(tcx.const_param_default(self.def_id).map_bound(|c| c.into()))
9494
}
9595
_ => None,

compiler/rustc_privacy/src/lib.rs

+5-15
Original file line numberDiff line numberDiff line change
@@ -782,21 +782,11 @@ impl<'tcx> Visitor<'tcx> for EmbargoVisitor<'tcx> {
782782
impl ReachEverythingInTheInterfaceVisitor<'_, '_> {
783783
fn generics(&mut self) -> &mut Self {
784784
for param in &self.ev.tcx.generics_of(self.item_def_id).own_params {
785-
match param.kind {
786-
GenericParamDefKind::Lifetime => {}
787-
GenericParamDefKind::Type { has_default, .. } => {
788-
if has_default {
789-
self.visit(self.ev.tcx.type_of(param.def_id).instantiate_identity());
790-
}
791-
}
792-
GenericParamDefKind::Const { has_default, .. } => {
793-
self.visit(self.ev.tcx.type_of(param.def_id).instantiate_identity());
794-
if has_default {
795-
self.visit(
796-
self.ev.tcx.const_param_default(param.def_id).instantiate_identity(),
797-
);
798-
}
799-
}
785+
if let GenericParamDefKind::Const { .. } = param.kind {
786+
self.visit(self.ev.tcx.type_of(param.def_id).instantiate_identity());
787+
}
788+
if let Some(default) = param.default_value(self.ev.tcx) {
789+
self.visit(default.instantiate_identity());
800790
}
801791
}
802792
self

0 commit comments

Comments
 (0)