Skip to content

Commit

Permalink
Treat weak alias types more like ADTs when computing implied bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
fmease committed Mar 11, 2024
1 parent af69f4c commit 118c6c1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 19 deletions.
6 changes: 3 additions & 3 deletions compiler/rustc_hir_analysis/src/outlives/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand All @@ -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:
Expand Down
33 changes: 17 additions & 16 deletions compiler/rustc_infer/src/infer/outlives/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ fn compute_components<'tcx>(
// 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::Inherent | ty::Opaque, 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
Expand Down Expand Up @@ -171,21 +171,22 @@ fn compute_components<'tcx>(
// the type and then visits the types that are lexically
// contained within. (The comments refer to relevant rules
// from RFC1214.)
ty::Bool | // OutlivesScalar
ty::Char | // OutlivesScalar
ty::Int(..) | // OutlivesScalar
ty::Uint(..) | // OutlivesScalar
ty::Float(..) | // OutlivesScalar
ty::Never | // ...
ty::Adt(..) | // OutlivesNominalType
ty::Foreign(..) | // OutlivesNominalType
ty::Str | // OutlivesScalar (ish)
ty::Slice(..) | // ...
ty::RawPtr(..) | // ...
ty::Ref(..) | // OutlivesReference
ty::Tuple(..) | // ...
ty::FnPtr(_) | // OutlivesFunction (*)
ty::Dynamic(..) | // OutlivesObject, OutlivesFragment (*)
ty::Bool | // OutlivesScalar
ty::Char | // OutlivesScalar
ty::Int(..) | // OutlivesScalar
ty::Uint(..) | // OutlivesScalar
ty::Float(..) | // OutlivesScalar
ty::Never | // ...
ty::Adt(..) | // OutlivesNominalType
ty::Alias(ty::Weak, _) | // OutlivesNominalType (ish)
ty::Foreign(..) | // OutlivesNominalType
ty::Str | // OutlivesScalar (ish)
ty::Slice(..) | // ...
ty::RawPtr(..) | // ...
ty::Ref(..) | // OutlivesReference
ty::Tuple(..) | // ...
ty::FnPtr(_) | // OutlivesFunction (*)
ty::Dynamic(..) | // OutlivesObject, OutlivesFragment (*)
ty::Bound(..) |
ty::Error(_) => {
// (*) Function pointers and trait objects are both binders.
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/lazy-type-alias/implied-outlives-bounds-1.print.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error: rustc_outlives
--> $DIR/implied-outlives-bounds-1.rs:16:1
|
LL | struct Type<'a, K, V>(&'a mut Alias<K, V>);
| ^^^^^^^^^^^^^^^^^^^^^
|
= note: K: 'a
= note: V: 'a

error: aborting due to 1 previous error

20 changes: 20 additions & 0 deletions tests/ui/lazy-type-alias/implied-outlives-bounds-1.rs
Original file line number Diff line number Diff line change
@@ -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<K, V>: '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<K, V>); //[print]~ ERROR rustc_outlives

type Alias<K, V> = (K, V);

fn main() {}

0 comments on commit 118c6c1

Please sign in to comment.