@@ -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.
0 commit comments