From 0e7952c0e642c92026a856ea83c6d06fd49b2f0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Le=C3=B3n=20Orell=20Valerian=20Liehr?= Date: Mon, 11 Mar 2024 18:04:03 +0100 Subject: [PATCH] Treat weak alias types more like ADTs when computing implied bounds --- .../src/outlives/implicit_infer.rs | 2 +- .../rustc_hir_analysis/src/outlives/utils.rs | 6 ++-- compiler/rustc_type_ir/src/outlives.rs | 3 +- .../implied-outlives-bounds-1.print.stderr | 11 +++++++ .../implied-outlives-bounds-1.rs | 20 +++++++++++++ .../implied_lifetime_wf_check3.rs | 2 -- .../implied_lifetime_wf_check3.stderr | 30 ++----------------- .../implied_lifetime_wf_check4_static.rs | 1 - .../implied_lifetime_wf_check4_static.stderr | 16 +--------- 9 files changed, 41 insertions(+), 50 deletions(-) create mode 100644 tests/ui/lazy-type-alias/implied-outlives-bounds-1.print.stderr create mode 100644 tests/ui/lazy-type-alias/implied-outlives-bounds-1.rs diff --git a/compiler/rustc_hir_analysis/src/outlives/implicit_infer.rs b/compiler/rustc_hir_analysis/src/outlives/implicit_infer.rs index c2377b4781c28..5f5304acc544c 100644 --- a/compiler/rustc_hir_analysis/src/outlives/implicit_infer.rs +++ b/compiler/rustc_hir_analysis/src/outlives/implicit_infer.rs @@ -299,7 +299,7 @@ fn check_explicit_predicates<'tcx>( } } -/// Check the inferred predicates declared on the type. +/// Check the inferred predicates of the type. /// /// ### Example /// diff --git a/compiler/rustc_hir_analysis/src/outlives/utils.rs b/compiler/rustc_hir_analysis/src/outlives/utils.rs index 0c9f5ba8b6fa8..e452637391211 100644 --- a/compiler/rustc_hir_analysis/src/outlives/utils.rs +++ b/compiler/rustc_hir_analysis/src/outlives/utils.rs @@ -21,7 +21,7 @@ pub(crate) fn insert_outlives_predicate<'tcx>( ) { // If the `'a` region is bound within the field type itself, we // don't want to propagate this constraint to the header. - if !is_free_region(outlived_region) { + if !is_early_bound_region(outlived_region) { return; } @@ -132,7 +132,7 @@ pub(crate) fn insert_outlives_predicate<'tcx>( } GenericArgKind::Lifetime(r) => { - if !is_free_region(r) { + if !is_early_bound_region(r) { return; } required_predicates.entry(ty::OutlivesPredicate(kind, outlived_region)).or_insert(span); @@ -144,7 +144,7 @@ pub(crate) fn insert_outlives_predicate<'tcx>( } } -fn is_free_region(region: Region<'_>) -> bool { +fn is_early_bound_region(region: Region<'_>) -> bool { // First, screen for regions that might appear in a type header. match *region { // These correspond to `T: 'a` relationships: diff --git a/compiler/rustc_type_ir/src/outlives.rs b/compiler/rustc_type_ir/src/outlives.rs index 0e94e989b978e..09de4651fec6f 100644 --- a/compiler/rustc_type_ir/src/outlives.rs +++ b/compiler/rustc_type_ir/src/outlives.rs @@ -148,7 +148,7 @@ impl TypeVisitor for OutlivesCollector<'_, I> { // trait-ref. Therefore, if we see any higher-ranked regions, // we simply fallback to the most restrictive rule, which // requires that `Pi: 'a` for all `i`. - ty::Alias(_, alias_ty) => { + ty::Alias(ty::Projection | ty::Opaque | ty::Inherent, alias_ty) => { if !alias_ty.has_escaping_bound_vars() { // best case: no escaping regions, so push the // projection and skip the subtree (thus generating no @@ -195,6 +195,7 @@ impl TypeVisitor for OutlivesCollector<'_, I> { } ty::Adt(_, _) + | ty::Alias(ty::Weak, _) | ty::Foreign(_) | ty::Array(_, _) | ty::Pat(_, _) diff --git a/tests/ui/lazy-type-alias/implied-outlives-bounds-1.print.stderr b/tests/ui/lazy-type-alias/implied-outlives-bounds-1.print.stderr new file mode 100644 index 0000000000000..6fa9aa0a3a7aa --- /dev/null +++ b/tests/ui/lazy-type-alias/implied-outlives-bounds-1.print.stderr @@ -0,0 +1,11 @@ +error: rustc_outlives + --> $DIR/implied-outlives-bounds-1.rs:16:1 + | +LL | struct Type<'a, K, V>(&'a mut Alias); + | ^^^^^^^^^^^^^^^^^^^^^ + | + = note: K: 'a + = note: V: 'a + +error: aborting due to 1 previous error + diff --git a/tests/ui/lazy-type-alias/implied-outlives-bounds-1.rs b/tests/ui/lazy-type-alias/implied-outlives-bounds-1.rs new file mode 100644 index 0000000000000..2d7af8ef83cf7 --- /dev/null +++ b/tests/ui/lazy-type-alias/implied-outlives-bounds-1.rs @@ -0,0 +1,20 @@ +// Check that we infer the outlives-predicates `K: 'a`, `V: 'a` for `Type` +// from the weak alias `Alias`. +// This mirrors the behavior of ADTs instead of other kinds of alias types +// like projections and opaque types. +// If we were to mirror the semantics of the latter, we would infer the +// outlives-predicate `Alias: 'a` instead which is not what we want. + +//@ revisions: default print +//@[default] check-pass + +#![feature(lazy_type_alias)] +#![cfg_attr(print, feature(rustc_attrs))] +#![allow(incomplete_features)] + +#[cfg_attr(print, rustc_outlives)] +struct Type<'a, K, V>(&'a mut Alias); //[print]~ ERROR rustc_outlives + +type Alias = (K, V); + +fn main() {} diff --git a/tests/ui/type-alias-impl-trait/implied_lifetime_wf_check3.rs b/tests/ui/type-alias-impl-trait/implied_lifetime_wf_check3.rs index fb251e9bde155..bea457bad398e 100644 --- a/tests/ui/type-alias-impl-trait/implied_lifetime_wf_check3.rs +++ b/tests/ui/type-alias-impl-trait/implied_lifetime_wf_check3.rs @@ -12,7 +12,6 @@ where test_lifetime_param::Ty<'a>: 'static, { test_lifetime_param::assert_static::<'a>() - //~^ ERROR: lifetime may not live long enough } mod test_higher_kinded_lifetime_param { @@ -50,7 +49,6 @@ where test_type_param::Ty: 'static, { test_type_param::assert_static::() - //~^ ERROR: parameter type `A` may not live long enough } mod test_implied_from_fn_sig { diff --git a/tests/ui/type-alias-impl-trait/implied_lifetime_wf_check3.stderr b/tests/ui/type-alias-impl-trait/implied_lifetime_wf_check3.stderr index b7c9c131c7d1a..cb05ccb1d4d85 100644 --- a/tests/ui/type-alias-impl-trait/implied_lifetime_wf_check3.stderr +++ b/tests/ui/type-alias-impl-trait/implied_lifetime_wf_check3.stderr @@ -1,14 +1,5 @@ error: lifetime may not live long enough - --> $DIR/implied_lifetime_wf_check3.rs:14:5 - | -LL | fn test_lifetime_param_test<'a>() - | -- lifetime `'a` defined here -... -LL | test_lifetime_param::assert_static::<'a>() - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ requires that `'a` must outlive `'static` - -error: lifetime may not live long enough - --> $DIR/implied_lifetime_wf_check3.rs:29:5 + --> $DIR/implied_lifetime_wf_check3.rs:28:5 | LL | fn test_higher_kinded_lifetime_param_test<'a>() | -- lifetime `'a` defined here @@ -17,27 +8,12 @@ LL | test_higher_kinded_lifetime_param::assert_static::<'a>() | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ requires that `'a` must outlive `'static` error: lifetime may not live long enough - --> $DIR/implied_lifetime_wf_check3.rs:36:9 + --> $DIR/implied_lifetime_wf_check3.rs:35:9 | LL | fn test<'a>() { | -- lifetime `'a` defined here LL | assert_static::<'a>() | ^^^^^^^^^^^^^^^^^^^ requires that `'a` must outlive `'static` -error[E0310]: the parameter type `A` may not live long enough - --> $DIR/implied_lifetime_wf_check3.rs:52:5 - | -LL | test_type_param::assert_static::() - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | | - | the parameter type `A` must be valid for the static lifetime... - | ...so that the type `A` will meet its required lifetime bounds - | -help: consider adding an explicit lifetime bound - | -LL | fn test_type_param_test() - | +++++++++ - -error: aborting due to 4 previous errors +error: aborting due to 2 previous errors -For more information about this error, try `rustc --explain E0310`. diff --git a/tests/ui/type-alias-impl-trait/implied_lifetime_wf_check4_static.rs b/tests/ui/type-alias-impl-trait/implied_lifetime_wf_check4_static.rs index 7b2bbc995307e..b163321a263a3 100644 --- a/tests/ui/type-alias-impl-trait/implied_lifetime_wf_check4_static.rs +++ b/tests/ui/type-alias-impl-trait/implied_lifetime_wf_check4_static.rs @@ -15,7 +15,6 @@ where Ty: 'static, { assert_static::() - //~^ ERROR: the parameter type `A` may not live long enough } fn main() {} diff --git a/tests/ui/type-alias-impl-trait/implied_lifetime_wf_check4_static.stderr b/tests/ui/type-alias-impl-trait/implied_lifetime_wf_check4_static.stderr index f23b978d0b640..e11038dfcea04 100644 --- a/tests/ui/type-alias-impl-trait/implied_lifetime_wf_check4_static.stderr +++ b/tests/ui/type-alias-impl-trait/implied_lifetime_wf_check4_static.stderr @@ -17,20 +17,6 @@ help: consider adding an explicit lifetime bound LL | pub type Ty = impl Sized + 'static; | +++++++++ -error[E0310]: the parameter type `A` may not live long enough - --> $DIR/implied_lifetime_wf_check4_static.rs:17:5 - | -LL | assert_static::() - | ^^^^^^^^^^^^^^^^^^ - | | - | the parameter type `A` must be valid for the static lifetime... - | ...so that the type `A` will meet its required lifetime bounds - | -help: consider adding an explicit lifetime bound - | -LL | fn test() - | +++++++++ - -error: aborting due to 2 previous errors +error: aborting due to 1 previous error For more information about this error, try `rustc --explain E0310`.