Skip to content

Fast-path some relations via strict equality #104598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions compiler/rustc_hir_analysis/src/check/dropck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ impl<'tcx> TypeRelation<'tcx> for SimpleEqRelation<'tcx> {
self.param_env
}

#[inline]
fn fast_equate(&self) -> bool {
true
}

fn tag(&self) -> &'static str {
"dropck::SimpleEqRelation"
}
Expand Down
10 changes: 10 additions & 0 deletions compiler/rustc_infer/src/infer/combine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,11 @@ impl<'tcx> TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
self.param_env
}

#[inline]
fn fast_equate(&self) -> bool {
false
}

fn tag(&self) -> &'static str {
"Generalizer"
}
Expand Down Expand Up @@ -802,6 +807,11 @@ impl<'tcx> TypeRelation<'tcx> for ConstInferUnifier<'_, 'tcx> {
self.param_env
}

#[inline]
fn fast_equate(&self) -> bool {
false
}

fn tag(&self) -> &'static str {
"ConstInferUnifier"
}
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_infer/src/infer/equate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ impl<'tcx> TypeRelation<'tcx> for Equate<'_, '_, 'tcx> {
self.fields.tcx()
}

#[inline]
fn fast_equate(&self) -> bool {
true
}

fn param_env(&self) -> ty::ParamEnv<'tcx> {
self.fields.param_env
}
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2945,6 +2945,11 @@ impl<'tcx> TypeRelation<'tcx> for SameTypeModuloInfer<'_, 'tcx> {
"SameTypeModuloInfer"
}

#[inline]
fn fast_equate(&self) -> bool {
true
}

fn a_is_expected(&self) -> bool {
true
}
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_infer/src/infer/glb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ impl<'tcx> TypeRelation<'tcx> for Glb<'_, '_, 'tcx> {
self.fields.tcx()
}

#[inline]
fn fast_equate(&self) -> bool {
true
}

fn param_env(&self) -> ty::ParamEnv<'tcx> {
self.fields.param_env
}
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_infer/src/infer/lub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ impl<'tcx> TypeRelation<'tcx> for Lub<'_, '_, 'tcx> {
self.fields.tcx()
}

#[inline]
fn fast_equate(&self) -> bool {
true
}

fn param_env(&self) -> ty::ParamEnv<'tcx> {
self.fields.param_env
}
Expand Down
10 changes: 10 additions & 0 deletions compiler/rustc_infer/src/infer/nll_relate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,11 @@ where
self.delegate.param_env()
}

#[inline]
fn fast_equate(&self) -> bool {
false
}

fn tag(&self) -> &'static str {
"nll::subtype"
}
Expand Down Expand Up @@ -901,6 +906,11 @@ where
self.delegate.param_env()
}

#[inline]
fn fast_equate(&self) -> bool {
false
}

fn tag(&self) -> &'static str {
"nll::generalizer"
}
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_infer/src/infer/outlives/test_type_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ impl<'tcx> TypeRelation<'tcx> for Match<'tcx> {
fn param_env(&self) -> ty::ParamEnv<'tcx> {
self.param_env
}
#[inline]
fn fast_equate(&self) -> bool {
false
}
fn a_is_expected(&self) -> bool {
true
} // irrelevant
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_infer/src/infer/sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ impl<'tcx> TypeRelation<'tcx> for Sub<'_, '_, 'tcx> {
self.fields.param_env
}

#[inline]
fn fast_equate(&self) -> bool {
true
}

fn a_is_expected(&self) -> bool {
self.a_is_expected
}
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_middle/src/ty/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ impl<'tcx> TypeRelation<'tcx> for Match<'tcx> {
fn param_env(&self) -> ty::ParamEnv<'tcx> {
self.param_env
}
#[inline]
fn fast_equate(&self) -> bool {
false
}
fn a_is_expected(&self) -> bool {
true
} // irrelevant
Expand Down
23 changes: 21 additions & 2 deletions compiler/rustc_middle/src/ty/relate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ pub trait TypeRelation<'tcx>: Sized {
/// Returns a static string we can use for printouts.
fn tag(&self) -> &'static str;

/// Returns whether or not structural equality can be used to fast-path this relation
fn fast_equate(&self) -> bool;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Annotate inline this declaration instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#[inline] means nothing on trait methods without a body.


/// Returns `true` if the value `a` is the "expected" type in the
/// relation. Just affects error messages.
fn a_is_expected(&self) -> bool;
Expand Down Expand Up @@ -140,6 +143,10 @@ pub fn relate_substs<'tcx, R: TypeRelation<'tcx>>(
a_subst: SubstsRef<'tcx>,
b_subst: SubstsRef<'tcx>,
) -> RelateResult<'tcx, SubstsRef<'tcx>> {
if relation.fast_equate() && a_subst == b_subst {
return Ok(a_subst);
}

relation.tcx().mk_substs(iter::zip(a_subst, b_subst).map(|(a, b)| {
relation.relate_with_variance(ty::Invariant, ty::VarianceDiagInfo::default(), a, b)
}))
Expand All @@ -152,6 +159,10 @@ pub fn relate_substs_with_variances<'tcx, R: TypeRelation<'tcx>>(
a_subst: SubstsRef<'tcx>,
b_subst: SubstsRef<'tcx>,
) -> RelateResult<'tcx, SubstsRef<'tcx>> {
if relation.fast_equate() && a_subst == b_subst {
return Ok(a_subst);
}

let tcx = relation.tcx();

let mut cached_ty = None;
Expand Down Expand Up @@ -345,7 +356,7 @@ impl<'tcx> Relate<'tcx> for ty::ExistentialTraitRef<'tcx> {
}
}

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

impl<'tcx> Relate<'tcx> for GeneratorWitness<'tcx> {
Expand All @@ -354,6 +365,10 @@ impl<'tcx> Relate<'tcx> for GeneratorWitness<'tcx> {
a: GeneratorWitness<'tcx>,
b: GeneratorWitness<'tcx>,
) -> RelateResult<'tcx, GeneratorWitness<'tcx>> {
if relation.fast_equate() && a == b {
return Ok(a);
}

assert_eq!(a.0.len(), b.0.len());
let tcx = relation.tcx();
let types = tcx.mk_type_list(iter::zip(a.0, b.0).map(|(a, b)| relation.relate(a, b)))?;
Expand Down Expand Up @@ -655,6 +670,10 @@ impl<'tcx> Relate<'tcx> for &'tcx ty::List<ty::Binder<'tcx, ty::ExistentialPredi
a: Self,
b: Self,
) -> RelateResult<'tcx, Self> {
if relation.fast_equate() && a == b {
return Ok(a);
}

let tcx = relation.tcx();

// FIXME: this is wasteful, but want to do a perf run to see how slow it is.
Expand Down Expand Up @@ -799,9 +818,9 @@ impl<'tcx> Relate<'tcx> for ty::TraitPredicate<'tcx> {
b: ty::TraitPredicate<'tcx>,
) -> RelateResult<'tcx, ty::TraitPredicate<'tcx>> {
Ok(ty::TraitPredicate {
trait_ref: relation.relate(a.trait_ref, b.trait_ref)?,
constness: relation.relate(a.constness, b.constness)?,
polarity: relation.relate(a.polarity, b.polarity)?,
trait_ref: relation.relate(a.trait_ref, b.trait_ref)?,
})
}
}
Expand Down