Skip to content

Commit 9df3360

Browse files
skinnyBatmati865
authored andcommitted
Emit ConstEquate obligation after checking/unifying for inference variables. This means
a inference variable can be unified with an unevaluated const.
1 parent 23edfc3 commit 9df3360

File tree

6 files changed

+59
-55
lines changed

6 files changed

+59
-55
lines changed

src/librustc_infer/infer/combine.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl<'infcx, 'tcx> InferCtxt<'infcx, 'tcx> {
126126
b: &'tcx ty::Const<'tcx>,
127127
) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>>
128128
where
129-
R: TypeRelation<'tcx>,
129+
R: ConstEquateRelation<'tcx>,
130130
{
131131
debug!("{}.consts({:?}, {:?})", relation.tag(), a, b);
132132
if a == b {
@@ -164,6 +164,14 @@ impl<'infcx, 'tcx> InferCtxt<'infcx, 'tcx> {
164164
(_, ty::ConstKind::Infer(InferConst::Var(vid))) => {
165165
return self.unify_const_variable(!a_is_expected, vid, a);
166166
}
167+
(ty::ConstKind::Unevaluated(..), _) => {
168+
relation.const_equate_obligation(a, b);
169+
return Ok(b);
170+
}
171+
(_, ty::ConstKind::Unevaluated(..)) => {
172+
relation.const_equate_obligation(a, b);
173+
return Ok(a);
174+
}
167175
_ => {}
168176
}
169177

@@ -654,6 +662,13 @@ impl TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
654662
}
655663
}
656664

665+
pub trait ConstEquateRelation<'tcx>: TypeRelation<'tcx> {
666+
/// Register am obligation that both constants must be equal to each other.
667+
///
668+
/// If they aren't equal then the relation doesn't hold.
669+
fn const_equate_obligation(&mut self, a: &'tcx ty::Const<'tcx>, b: &'tcx ty::Const<'tcx>);
670+
}
671+
657672
pub trait RelateResultCompare<'tcx, T> {
658673
fn compare<F>(&self, t: T, f: F) -> RelateResult<'tcx, T>
659674
where

src/librustc_infer/infer/equate.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use super::combine::{CombineFields, RelationDir};
1+
use super::combine::{CombineFields, RelationDir, ConstEquateRelation};
22
use super::Subtype;
33

44
use rustc_middle::ty::relate::{self, Relate, RelateResult, TypeRelation};
55
use rustc_middle::ty::subst::SubstsRef;
66
use rustc_middle::ty::TyVar;
7-
use rustc_middle::ty::{self, ConstKind, Ty, TyCtxt};
7+
use rustc_middle::ty::{self, Ty, TyCtxt};
88

99
use rustc_hir::def_id::DefId;
1010

@@ -119,17 +119,7 @@ impl TypeRelation<'tcx> for Equate<'combine, 'infcx, 'tcx> {
119119
a: &'tcx ty::Const<'tcx>,
120120
b: &'tcx ty::Const<'tcx>,
121121
) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> {
122-
match (a.val, b.val) {
123-
(ConstKind::Unevaluated(..), _) => {
124-
self.fields.add_const_equate_obligation(self.a_is_expected, a, b);
125-
Ok(b)
126-
}
127-
(_, ConstKind::Unevaluated(..)) => {
128-
self.fields.add_const_equate_obligation(self.a_is_expected, a, b);
129-
Ok(a)
130-
}
131-
_ => self.fields.infcx.super_combine_consts(self, a, b),
132-
}
122+
self.fields.infcx.super_combine_consts(self, a, b)
133123
}
134124

135125
fn binders<T>(
@@ -150,3 +140,9 @@ impl TypeRelation<'tcx> for Equate<'combine, 'infcx, 'tcx> {
150140
}
151141
}
152142
}
143+
144+
impl<'tcx> ConstEquateRelation<'tcx> for Equate<'_, '_, 'tcx> {
145+
fn const_equate_obligation(&mut self, a: &'tcx ty::Const<'tcx>, b: &'tcx ty::Const<'tcx>) {
146+
self.fields.add_const_equate_obligation(self.a_is_expected, a, b);
147+
}
148+
}

src/librustc_infer/infer/glb.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use super::lattice::{self, LatticeDir};
33
use super::InferCtxt;
44
use super::Subtype;
55

6+
use crate::infer::combine::ConstEquateRelation;
67
use crate::traits::ObligationCause;
78
use rustc_middle::ty::relate::{Relate, RelateResult, TypeRelation};
89
use rustc_middle::ty::{self, Ty, TyCtxt};
@@ -79,17 +80,7 @@ impl TypeRelation<'tcx> for Glb<'combine, 'infcx, 'tcx> {
7980
a: &'tcx ty::Const<'tcx>,
8081
b: &'tcx ty::Const<'tcx>,
8182
) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> {
82-
match (a.val, b.val) {
83-
(ty::ConstKind::Unevaluated(..), _) => {
84-
self.fields.add_const_equate_obligation(self.a_is_expected, a, b);
85-
Ok(b)
86-
}
87-
(_, ty::ConstKind::Unevaluated(..)) => {
88-
self.fields.add_const_equate_obligation(self.a_is_expected, a, b);
89-
Ok(a)
90-
}
91-
_ => self.fields.infcx.super_combine_consts(self, a, b),
92-
}
83+
self.fields.infcx.super_combine_consts(self, a, b)
9384
}
9485

9586
fn binders<T>(
@@ -126,3 +117,9 @@ impl<'combine, 'infcx, 'tcx> LatticeDir<'infcx, 'tcx> for Glb<'combine, 'infcx,
126117
Ok(())
127118
}
128119
}
120+
121+
impl<'tcx> ConstEquateRelation<'tcx> for Glb<'_, '_, 'tcx> {
122+
fn const_equate_obligation(&mut self, a: &'tcx ty::Const<'tcx>, b: &'tcx ty::Const<'tcx>) {
123+
self.fields.add_const_equate_obligation(self.a_is_expected, a, b);
124+
}
125+
}

src/librustc_infer/infer/lub.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use super::lattice::{self, LatticeDir};
33
use super::InferCtxt;
44
use super::Subtype;
55

6+
use crate::infer::combine::ConstEquateRelation;
67
use crate::traits::ObligationCause;
78
use rustc_middle::ty::relate::{Relate, RelateResult, TypeRelation};
89
use rustc_middle::ty::{self, Ty, TyCtxt};
@@ -79,17 +80,7 @@ impl TypeRelation<'tcx> for Lub<'combine, 'infcx, 'tcx> {
7980
a: &'tcx ty::Const<'tcx>,
8081
b: &'tcx ty::Const<'tcx>,
8182
) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> {
82-
match (a.val, b.val) {
83-
(ty::ConstKind::Unevaluated(..), _) => {
84-
self.fields.add_const_equate_obligation(self.a_is_expected, a, b);
85-
Ok(b)
86-
}
87-
(_, ty::ConstKind::Unevaluated(..)) => {
88-
self.fields.add_const_equate_obligation(self.a_is_expected, a, b);
89-
Ok(a)
90-
}
91-
_ => self.fields.infcx.super_combine_consts(self, a, b),
92-
}
83+
self.fields.infcx.super_combine_consts(self, a, b)
9384
}
9485

9586
fn binders<T>(
@@ -110,6 +101,12 @@ impl TypeRelation<'tcx> for Lub<'combine, 'infcx, 'tcx> {
110101
}
111102
}
112103

104+
impl<'tcx> ConstEquateRelation<'tcx> for Lub<'_, '_, 'tcx> {
105+
fn const_equate_obligation(&mut self, a: &'tcx ty::Const<'tcx>, b: &'tcx ty::Const<'tcx>) {
106+
self.fields.add_const_equate_obligation(self.a_is_expected, a, b);
107+
}
108+
}
109+
113110
impl<'combine, 'infcx, 'tcx> LatticeDir<'infcx, 'tcx> for Lub<'combine, 'infcx, 'tcx> {
114111
fn infcx(&self) -> &'infcx InferCtxt<'infcx, 'tcx> {
115112
self.fields.infcx

src/librustc_infer/infer/nll_relate/mod.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
//! thing we relate in chalk are basically domain goals and their
2222
//! constituents)
2323
24+
use crate::infer::combine::ConstEquateRelation;
2425
use crate::infer::InferCtxt;
2526
use crate::infer::{ConstVarValue, ConstVariableValue};
2627
use crate::traits::DomainGoal;
@@ -606,14 +607,6 @@ where
606607
}
607608

608609
match (a.val, b.val) {
609-
(ty::ConstKind::Unevaluated(..), _) => {
610-
self.delegate.const_equate(a, b);
611-
Ok(b)
612-
}
613-
(_, ty::ConstKind::Unevaluated(..)) => {
614-
self.delegate.const_equate(a, b);
615-
Ok(a)
616-
}
617610
(_, ty::ConstKind::Infer(InferConst::Var(_))) if D::forbid_inference_vars() => {
618611
// Forbid inference variables in the RHS.
619612
bug!("unexpected inference var {:?}", b)
@@ -736,6 +729,15 @@ where
736729
}
737730
}
738731

732+
impl<'tcx, D> ConstEquateRelation<'tcx> for TypeRelating<'_, 'tcx, D>
733+
where
734+
D: TypeRelatingDelegate<'tcx>,
735+
{
736+
fn const_equate_obligation(&mut self, a: &'tcx ty::Const<'tcx>, b: &'tcx ty::Const<'tcx>) {
737+
self.delegate.const_equate(a, b);
738+
}
739+
}
740+
739741
/// When we encounter a binder like `for<..> fn(..)`, we actually have
740742
/// to walk the `fn` value to find all the values bound by the `for`
741743
/// (these are not explicitly present in the ty representation right

src/librustc_infer/infer/sub.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::combine::{CombineFields, RelationDir};
22
use super::SubregionOrigin;
33

4+
use crate::infer::combine::ConstEquateRelation;
45
use crate::traits::Obligation;
56
use rustc_middle::ty::fold::TypeFoldable;
67
use rustc_middle::ty::relate::{Cause, Relate, RelateResult, TypeRelation};
@@ -155,17 +156,7 @@ impl TypeRelation<'tcx> for Sub<'combine, 'infcx, 'tcx> {
155156
a: &'tcx ty::Const<'tcx>,
156157
b: &'tcx ty::Const<'tcx>,
157158
) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> {
158-
match (a.val, b.val) {
159-
(ty::ConstKind::Unevaluated(..), _) => {
160-
self.fields.add_const_equate_obligation(self.a_is_expected, a, b);
161-
Ok(b)
162-
}
163-
(_, ty::ConstKind::Unevaluated(..)) => {
164-
self.fields.add_const_equate_obligation(self.a_is_expected, a, b);
165-
Ok(a)
166-
}
167-
_ => self.fields.infcx.super_combine_consts(self, a, b),
168-
}
159+
self.fields.infcx.super_combine_consts(self, a, b)
169160
}
170161

171162
fn binders<T>(
@@ -179,3 +170,9 @@ impl TypeRelation<'tcx> for Sub<'combine, 'infcx, 'tcx> {
179170
self.fields.higher_ranked_sub(a, b, self.a_is_expected)
180171
}
181172
}
173+
174+
impl<'tcx> ConstEquateRelation<'tcx> for Sub<'_, '_, 'tcx> {
175+
fn const_equate_obligation(&mut self, a: &'tcx ty::Const<'tcx>, b: &'tcx ty::Const<'tcx>) {
176+
self.fields.add_const_equate_obligation(self.a_is_expected, a, b);
177+
}
178+
}

0 commit comments

Comments
 (0)