Skip to content

Commit

Permalink
Auto merge of #118247 - spastorino:type-equality-subtyping, r=<try>
Browse files Browse the repository at this point in the history
Fix for TypeId exposes equality-by-subtyping vs normal-form-syntactic-equality unsoundness

Fixes #97156

This PR revives #97427 idea, it sits on top of #118118 because the idea uncovered some problems with IATs.

r? `@lcnr`

This is ICEing yet for `tests/ui/traits/new-solver/escaping-bound-vars-in-writeback-normalization.rs` using the new trait solver.
After #118118 and this ICE is fixed, we would need a rebase and a crater run.

Opening as a WIP for now.
  • Loading branch information
bors committed Nov 28, 2023
2 parents 49b3924 + d3841fb commit ab2dc84
Show file tree
Hide file tree
Showing 16 changed files with 141 additions and 29 deletions.
10 changes: 7 additions & 3 deletions compiler/rustc_hir_analysis/src/check/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
let name_str = intrinsic_name.as_str();

let bound_vars = tcx.mk_bound_variable_kinds(&[
ty::BoundVariableKind::Region(ty::BrAnon),
ty::BoundVariableKind::Region(ty::BrAnon),
ty::BoundVariableKind::Region(ty::BrEnv),
]);
Expand All @@ -151,7 +152,7 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
let env_region = ty::Region::new_bound(
tcx,
ty::INNERMOST,
ty::BoundRegion { var: ty::BoundVar::from_u32(1), kind: ty::BrEnv },
ty::BoundRegion { var: ty::BoundVar::from_u32(2), kind: ty::BrEnv },
);
let va_list_ty = tcx.type_of(did).instantiate(tcx, &[region.into()]);
(Ty::new_ref(tcx, env_region, ty::TypeAndMut { ty: va_list_ty, mutbl }), va_list_ty)
Expand Down Expand Up @@ -446,9 +447,12 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {

sym::raw_eq => {
let br = ty::BoundRegion { var: ty::BoundVar::from_u32(0), kind: ty::BrAnon };
let param_ty =
let param_ty_lhs =
Ty::new_imm_ref(tcx, ty::Region::new_bound(tcx, ty::INNERMOST, br), param(0));
let br = ty::BoundRegion { var: ty::BoundVar::from_u32(1), kind: ty::BrAnon };
let param_ty_rhs =
Ty::new_imm_ref(tcx, ty::Region::new_bound(tcx, ty::INNERMOST, br), param(0));
(1, vec![param_ty; 2], tcx.types.bool)
(1, vec![param_ty_lhs, param_ty_rhs], tcx.types.bool)
}

sym::black_box => (1, vec![param(0)], param(0)),
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_infer/src/infer/equate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ impl<'tcx> TypeRelation<'tcx> for Equate<'_, '_, 'tcx> {
}

if a.skip_binder().has_escaping_bound_vars() || b.skip_binder().has_escaping_bound_vars() {
self.fields.higher_ranked_sub(a, b, self.a_is_expected)?;
self.fields.higher_ranked_sub(b, a, self.a_is_expected)?;
self.fields.higher_ranked_equate(a, b, self.a_is_expected)?;
self.fields.higher_ranked_equate(b, a, self.a_is_expected)?;
} else {
// Fast path for the common case.
self.relate(a.skip_binder(), b.skip_binder())?;
Expand Down
34 changes: 34 additions & 0 deletions compiler/rustc_infer/src/infer/higher_ranked/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,40 @@ impl<'a, 'tcx> CombineFields<'a, 'tcx> {
// placeholders which **must not** be named afterwards.
Ok(())
}

#[instrument(skip(self), level = "debug")]
pub fn higher_ranked_equate<T>(
&mut self,
sub: Binder<'tcx, T>,
sup: Binder<'tcx, T>,
sub_is_expected: bool,
) -> RelateResult<'tcx, ()>
where
T: Relate<'tcx>,
{
let span = self.trace.cause.span;
// First, we instantiate each bound region in the supertype with a
// fresh placeholder region. Note that this automatically creates
// a new universe if needed.
let sup_prime = self.infcx.instantiate_binder_with_placeholders(sup);

// Next, we instantiate each bound region in the subtype
// with a fresh region variable. These region variables --
// but no other preexisting region variables -- can name
// the placeholders.
let sub_prime = self.infcx.instantiate_binder_with_fresh_vars(span, HigherRankedType, sub);

debug!("a_prime={:?}", sub_prime);
debug!("b_prime={:?}", sup_prime);

// Compare types now that bound regions have been replaced.
let result = self.equate(sub_is_expected).relate(sub_prime, sup_prime)?;

debug!("OK result={result:?}");
// NOTE: returning the result here would be dangerous as it contains
// placeholders which **must not** be named afterwards.
Ok(())
}
}

impl<'tcx> InferCtxt<'tcx> {
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/associated-inherent-types/issue-111404-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ impl<'a> Foo<fn(&'a ())> {
}

fn bar(_: fn(Foo<for<'b> fn(Foo<fn(&'b ())>::Assoc)>::Assoc)) {}
//~^ ERROR higher-ranked subtype error
//~| ERROR higher-ranked subtype error
//~^ ERROR mismatched types [E0308]
//~| ERROR mismatched types [E0308]

fn main() {}
18 changes: 12 additions & 6 deletions tests/ui/associated-inherent-types/issue-111404-1.stderr
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
error: higher-ranked subtype error
--> $DIR/issue-111404-1.rs:10:1
error[E0308]: mismatched types
--> $DIR/issue-111404-1.rs:10:11
|
LL | fn bar(_: fn(Foo<for<'b> fn(Foo<fn(&'b ())>::Assoc)>::Assoc)) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
|
= note: expected struct `Foo<fn(&())>`
found struct `Foo<for<'b> fn(&'b ())>`

error: higher-ranked subtype error
--> $DIR/issue-111404-1.rs:10:1
error[E0308]: mismatched types
--> $DIR/issue-111404-1.rs:10:11
|
LL | fn bar(_: fn(Foo<for<'b> fn(Foo<fn(&'b ())>::Assoc)>::Assoc)) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
|
= note: expected struct `Foo<fn(&())>`
found struct `Foo<for<'b> fn(&'b ())>`
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.
5 changes: 4 additions & 1 deletion tests/ui/coherence/coherence-fn-covariant-bound-vs-static.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// check-pass

// Test that impls for these two types are considered ovelapping:
//
// * `for<'r> fn(fn(&'r u32))`
Expand All @@ -15,7 +17,8 @@ trait Trait {}

impl Trait for for<'r> fn(fn(&'r ())) {}
impl<'a> Trait for fn(fn(&'a ())) {}
//~^ ERROR conflicting implementations
//~^ WARN conflicting implementations of trait `Trait` for type `for<'r> fn(fn(&'r ()))` [coherence_leak_check]
//~| WARN this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
//
// Note in particular that we do NOT get a future-compatibility warning
// here. This is because the new leak-check proposed in [MCP 295] does not
Expand Down
10 changes: 6 additions & 4 deletions tests/ui/coherence/coherence-fn-covariant-bound-vs-static.stderr
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
error[E0119]: conflicting implementations of trait `Trait` for type `for<'r> fn(fn(&'r ()))`
--> $DIR/coherence-fn-covariant-bound-vs-static.rs:17:1
warning: conflicting implementations of trait `Trait` for type `for<'r> fn(fn(&'r ()))`
--> $DIR/coherence-fn-covariant-bound-vs-static.rs:19:1
|
LL | impl Trait for for<'r> fn(fn(&'r ())) {}
| ------------------------------------- first implementation here
LL | impl<'a> Trait for fn(fn(&'a ())) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `for<'r> fn(fn(&'r ()))`
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #56105 <https://github.com/rust-lang/rust/issues/56105>
= note: this behavior recently changed as a result of a bug fix; see rust-lang/rust#56105 for details
= note: `#[warn(coherence_leak_check)]` on by default

error: aborting due to 1 previous error
warning: 1 warning emitted

For more information about this error, try `rustc --explain E0119`.
5 changes: 4 additions & 1 deletion tests/ui/coherence/coherence-fn-inputs.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// check-pass

// Test that we consider these two types completely equal:
//
// * `for<'a, 'b> fn(&'a u32, &'b u32)`
Expand All @@ -13,7 +15,8 @@
trait Trait {}
impl Trait for for<'a, 'b> fn(&'a u32, &'b u32) {}
impl Trait for for<'c> fn(&'c u32, &'c u32) {
//~^ ERROR conflicting implementations
//~^ WARN conflicting implementations of trait `Trait` for type `for<'a, 'b> fn(&'a u32, &'b u32)` [coherence_leak_check]
//~| WARN this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
//
// Note in particular that we do NOT get a future-compatibility warning
// here. This is because the new leak-check proposed in [MCP 295] does not
Expand Down
10 changes: 6 additions & 4 deletions tests/ui/coherence/coherence-fn-inputs.stderr
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
error[E0119]: conflicting implementations of trait `Trait` for type `for<'a, 'b> fn(&'a u32, &'b u32)`
--> $DIR/coherence-fn-inputs.rs:15:1
warning: conflicting implementations of trait `Trait` for type `for<'a, 'b> fn(&'a u32, &'b u32)`
--> $DIR/coherence-fn-inputs.rs:17:1
|
LL | impl Trait for for<'a, 'b> fn(&'a u32, &'b u32) {}
| ----------------------------------------------- first implementation here
LL | impl Trait for for<'c> fn(&'c u32, &'c u32) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `for<'a, 'b> fn(&'a u32, &'b u32)`
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #56105 <https://github.com/rust-lang/rust/issues/56105>
= note: this behavior recently changed as a result of a bug fix; see rust-lang/rust#56105 for details
= note: `#[warn(coherence_leak_check)]` on by default

error: aborting due to 1 previous error
warning: 1 warning emitted

For more information about this error, try `rustc --explain E0119`.
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,12 @@ LL | WHAT_A_TYPE => 0,
= note: the traits must be derived, manual `impl`s are not sufficient
= note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralEq.html for details

error: aborting due to 1 previous error
error[E0277]: the trait bound `for<'a, 'b> fn(&'a (), &'b ()): WithAssoc<T>` is not satisfied
--> $DIR/typeid-equality-by-subtyping.rs:44:51
|
LL | fn unsound<T>(x: <One as WithAssoc<T>>::Assoc) -> <Two as WithAssoc<T>>::Assoc
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `WithAssoc<T>` is not implemented for `for<'a, 'b> fn(&'a (), &'b ())`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0277`.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
//
// In particular, we test this pattern in trait solving, where it is not connected
// to any part of the source code.
//
// check-pass

trait Trait<T> {}

Expand Down Expand Up @@ -34,4 +32,5 @@ fn main() {
// This is because we can use `'static`.

foo::<()>();
//~^ ERROR implementation of `Trait` is not general enough
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error: implementation of `Trait` is not general enough
--> $DIR/hrtb-exists-forall-trait-covariant.rs:34:5
|
LL | foo::<()>();
| ^^^^^^^^^^^ implementation of `Trait` is not general enough
|
= note: `()` must implement `Trait<for<'b> fn(fn(&'b u32))>`
= note: ...but it actually implements `Trait<fn(fn(&'0 u32))>`, for some specific lifetime `'0`

error: aborting due to 1 previous error

3 changes: 1 addition & 2 deletions tests/ui/lub-glb/old-lub-glb-hr-eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// error. However, now that we handle subtyping correctly, we no
// longer get an error, because we recognize these two types as
// equivalent!
//
// check-pass

fn foo(x: fn(&u8, &u8), y: for<'a> fn(&'a u8, &'a u8)) {
// The two types above are actually equivalent. With the older
Expand All @@ -13,6 +11,7 @@ fn foo(x: fn(&u8, &u8), y: for<'a> fn(&'a u8, &'a u8)) {
let z = match 22 {
0 => x,
_ => y,
//~^ ERROR `match` arms have incompatible types [E0308]
};
}

Expand Down
19 changes: 19 additions & 0 deletions tests/ui/lub-glb/old-lub-glb-hr-eq.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
error[E0308]: `match` arms have incompatible types
--> $DIR/old-lub-glb-hr-eq.rs:13:14
|
LL | let z = match 22 {
| _____________-
LL | | 0 => x,
| | - this is found to be of type `for<'a, 'b> fn(&'a u8, &'b u8)`
LL | | _ => y,
| | ^ one type is more general than the other
LL | |
LL | | };
| |_____- `match` arms have incompatible types
|
= note: expected fn pointer `for<'a, 'b> fn(&'a u8, &'b u8)`
found fn pointer `for<'a> fn(&'a u8, &'a u8)`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// compile-flags: -Ztrait-solver=next
// check-pass

trait Trait {
type Ty;
Expand All @@ -11,6 +10,8 @@ impl Trait for for<'a> fn(&'a u8, &'a u8) {

// argument is necessary to create universes before registering the hidden type.
fn test<'a>(_: <fn(&u8, &u8) as Trait>::Ty) -> impl Sized {
//~^ ERROR the type `<for<'a, 'b> fn(&'a u8, &'b u8) as Trait>::Ty` is not well-formed
//~| ERROR the size for values of type `<for<'a, 'b> fn(&'a u8, &'b u8) as Trait>::Ty` cannot be known at compilation time [E0277]
"hidden type is `&'?0 str` with '?0 member of ['static,]"
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
error: the type `<for<'a, 'b> fn(&'a u8, &'b u8) as Trait>::Ty` is not well-formed
--> $DIR/member-constraints-in-root-universe.rs:12:16
|
LL | fn test<'a>(_: <fn(&u8, &u8) as Trait>::Ty) -> impl Sized {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0277]: the size for values of type `<for<'a, 'b> fn(&'a u8, &'b u8) as Trait>::Ty` cannot be known at compilation time
--> $DIR/member-constraints-in-root-universe.rs:12:13
|
LL | fn test<'a>(_: <fn(&u8, &u8) as Trait>::Ty) -> impl Sized {
| ^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `<for<'a, 'b> fn(&'a u8, &'b u8) as Trait>::Ty`
= help: unsized fn params are gated as an unstable feature
help: function arguments must have a statically known size, borrowed types always have a known size
|
LL | fn test<'a>(_: &<fn(&u8, &u8) as Trait>::Ty) -> impl Sized {
| +

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0277`.

0 comments on commit ab2dc84

Please sign in to comment.