Skip to content

Commit 109cccb

Browse files
committed
Auto merge of rust-lang#105350 - compiler-errors:faster-binder-relate, r=oli-obk
Fast-path some binder relations A simpler approach than rust-lang#104598 Fixes rust-lang#104583 r? types
2 parents 71ec145 + 2025a96 commit 109cccb

File tree

9 files changed

+63
-5
lines changed

9 files changed

+63
-5
lines changed

compiler/rustc_infer/src/infer/equate.rs

+5
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ impl<'tcx> TypeRelation<'tcx> for Equate<'_, '_, 'tcx> {
178178
where
179179
T: Relate<'tcx>,
180180
{
181+
// A binder is equal to itself if it's structually equal to itself
182+
if a == b {
183+
return Ok(a);
184+
}
185+
181186
if a.skip_binder().has_escaping_bound_vars() || b.skip_binder().has_escaping_bound_vars() {
182187
self.fields.higher_ranked_sub(a, b, self.a_is_expected)?;
183188
self.fields.higher_ranked_sub(b, a, self.a_is_expected)?;

compiler/rustc_infer/src/infer/glb.rs

+5
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ impl<'tcx> TypeRelation<'tcx> for Glb<'_, '_, 'tcx> {
103103
where
104104
T: Relate<'tcx>,
105105
{
106+
// GLB of a binder and itself is just itself
107+
if a == b {
108+
return Ok(a);
109+
}
110+
106111
debug!("binders(a={:?}, b={:?})", a, b);
107112
if a.skip_binder().has_escaping_bound_vars() || b.skip_binder().has_escaping_bound_vars() {
108113
// When higher-ranked types are involved, computing the GLB is

compiler/rustc_infer/src/infer/lub.rs

+5
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ impl<'tcx> TypeRelation<'tcx> for Lub<'_, '_, 'tcx> {
103103
where
104104
T: Relate<'tcx>,
105105
{
106+
// LUB of a binder and itself is just itself
107+
if a == b {
108+
return Ok(a);
109+
}
110+
106111
debug!("binders(a={:?}, b={:?})", a, b);
107112
if a.skip_binder().has_escaping_bound_vars() || b.skip_binder().has_escaping_bound_vars() {
108113
// When higher-ranked types are involved, computing the LUB is

compiler/rustc_infer/src/infer/sub.rs

+5
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,11 @@ impl<'tcx> TypeRelation<'tcx> for Sub<'_, '_, 'tcx> {
213213
where
214214
T: Relate<'tcx>,
215215
{
216+
// A binder is always a subtype of itself if it's structually equal to itself
217+
if a == b {
218+
return Ok(a);
219+
}
220+
216221
self.fields.higher_ranked_sub(a, b, self.a_is_expected)?;
217222
Ok(a)
218223
}

compiler/rustc_middle/src/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ pub struct ImplHeader<'tcx> {
235235
pub predicates: Vec<Predicate<'tcx>>,
236236
}
237237

238-
#[derive(Copy, Clone, Debug, TypeFoldable, TypeVisitable)]
238+
#[derive(Copy, Clone, PartialEq, Eq, Debug, TypeFoldable, TypeVisitable)]
239239
pub enum ImplSubject<'tcx> {
240240
Trait(TraitRef<'tcx>),
241241
Inherent(Ty<'tcx>),

compiler/rustc_middle/src/ty/relate.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ pub trait TypeRelation<'tcx>: Sized {
106106
T: Relate<'tcx>;
107107
}
108108

109-
pub trait Relate<'tcx>: TypeFoldable<'tcx> + Copy {
109+
pub trait Relate<'tcx>: TypeFoldable<'tcx> + PartialEq + Copy {
110110
fn relate<R: TypeRelation<'tcx>>(
111111
relation: &mut R,
112112
a: Self,
@@ -351,7 +351,7 @@ impl<'tcx> Relate<'tcx> for ty::ExistentialTraitRef<'tcx> {
351351
}
352352
}
353353

354-
#[derive(Copy, Debug, Clone, TypeFoldable, TypeVisitable)]
354+
#[derive(PartialEq, Copy, Debug, Clone, TypeFoldable, TypeVisitable)]
355355
struct GeneratorWitness<'tcx>(&'tcx ty::List<Ty<'tcx>>);
356356

357357
impl<'tcx> Relate<'tcx> for GeneratorWitness<'tcx> {

compiler/rustc_middle/src/ty/sty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ static_assert_size!(TyKind<'_>, 32);
217217
/// * `GR`: The "return type", which is the type of value returned upon
218218
/// completion of the generator.
219219
/// * `GW`: The "generator witness".
220-
#[derive(Copy, Clone, Debug, TypeFoldable, TypeVisitable, Lift)]
220+
#[derive(Copy, Clone, PartialEq, Eq, Debug, TypeFoldable, TypeVisitable, Lift)]
221221
pub struct ClosureSubsts<'tcx> {
222222
/// Lifetime and type parameters from the enclosing function,
223223
/// concatenated with a tuple containing the types of the upvars.
@@ -348,7 +348,7 @@ impl<'tcx> ClosureSubsts<'tcx> {
348348
}
349349

350350
/// Similar to `ClosureSubsts`; see the above documentation for more.
351-
#[derive(Copy, Clone, Debug, TypeFoldable, TypeVisitable, Lift)]
351+
#[derive(Copy, Clone, PartialEq, Eq, Debug, TypeFoldable, TypeVisitable, Lift)]
352352
pub struct GeneratorSubsts<'tcx> {
353353
pub substs: SubstsRef<'tcx>,
354354
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// normalize-stderr-test: "long-type-\d+" -> "long-type-hash"
2+
3+
fn id(
4+
f: &dyn Fn(u32),
5+
) -> &dyn Fn(
6+
&dyn Fn(
7+
&dyn Fn(
8+
&dyn Fn(&dyn Fn(&dyn Fn(&dyn Fn(&dyn Fn(&dyn Fn(&dyn Fn(&dyn Fn(&dyn Fn(u32))))))))),
9+
),
10+
),
11+
) {
12+
f
13+
//~^ ERROR mismatched types
14+
}
15+
16+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/hang-on-deeply-nested-dyn.rs:12:5
3+
|
4+
LL | ) -> &dyn Fn(
5+
| ______-
6+
LL | | &dyn Fn(
7+
LL | | &dyn Fn(
8+
LL | | &dyn Fn(&dyn Fn(&dyn Fn(&dyn Fn(&dyn Fn(&dyn Fn(&dyn Fn(&dyn Fn(&dyn Fn(u32))))))))),
9+
LL | | ),
10+
LL | | ),
11+
LL | | ) {
12+
| |_- expected `&dyn for<'a> Fn(&'a (dyn for<'a> Fn(&'a (dyn for<'a> Fn(&'a (dyn for<'a> Fn(&'a (dyn for<'a> Fn(&'a (dyn for<'a> Fn(&'a (dyn for<'a> Fn(&'a (dyn for<'a> Fn(&'a (dyn for<'a> Fn(&'a (dyn for<'a> Fn(&'a (dyn for<'a> Fn(&'a (dyn Fn(u32) + 'a)) + 'a)) + 'a)) + 'a)) + 'a)) + 'a)) + 'a)) + 'a)) + 'a)) + 'a)) + 'a))` because of return type
13+
LL | f
14+
| ^ expected reference, found `u32`
15+
|
16+
= note: expected reference `&dyn for<'a> Fn(&'a (dyn for<'a> Fn(&'a (dyn for<'a> Fn(&'a (dyn for<'a> Fn(&'a ...) + 'a)) + 'a)) + 'a))`
17+
the full type name has been written to '$TEST_BUILD_DIR/higher-rank-trait-bounds/hang-on-deeply-nested-dyn/hang-on-deeply-nested-dyn.long-type-hash.txt'
18+
found reference `&dyn Fn(u32)`
19+
20+
error: aborting due to previous error
21+
22+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)