Skip to content

Commit 9cd6c68

Browse files
committed
cleanup effect var handling
1 parent 8c0b4f6 commit 9cd6c68

File tree

7 files changed

+52
-71
lines changed

7 files changed

+52
-71
lines changed

compiler/rustc_infer/src/infer/canonical/canonicalizer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ impl<'cx, 'tcx> TypeFolder<TyCtxt<'tcx>> for Canonicalizer<'cx, 'tcx> {
480480
}
481481
ty::ConstKind::Infer(InferConst::EffectVar(vid)) => {
482482
match self.infcx.unwrap().probe_effect_var(vid) {
483-
Some(value) => return self.fold_const(value.as_const(self.tcx)),
483+
Some(value) => return self.fold_const(value),
484484
None => {
485485
return self.canonicalize_const_var(
486486
CanonicalVarInfo { kind: CanonicalVarKind::Effect },

compiler/rustc_infer/src/infer/canonical/mod.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use crate::infer::{ConstVariableOrigin, ConstVariableOriginKind};
2525
use crate::infer::{InferCtxt, RegionVariableOrigin, TypeVariableOrigin, TypeVariableOriginKind};
2626
use rustc_index::IndexVec;
27+
use rustc_middle::infer::unify_key::EffectVarValue;
2728
use rustc_middle::ty::fold::TypeFoldable;
2829
use rustc_middle::ty::GenericArg;
2930
use rustc_middle::ty::{self, List, Ty, TyCtxt};
@@ -152,7 +153,12 @@ impl<'tcx> InferCtxt<'tcx> {
152153
)
153154
.into(),
154155
CanonicalVarKind::Effect => {
155-
let vid = self.inner.borrow_mut().effect_unification_table().new_key(None).vid;
156+
let vid = self
157+
.inner
158+
.borrow_mut()
159+
.effect_unification_table()
160+
.new_key(EffectVarValue::Unknown)
161+
.vid;
156162
ty::Const::new_infer(self.tcx, ty::InferConst::EffectVar(vid), self.tcx.types.bool)
157163
.into()
158164
}

compiler/rustc_infer/src/infer/freshen.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,8 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for TypeFreshener<'a, 'tcx> {
151151
self.freshen_const(opt_ct, ty::InferConst::Var(v), ty::InferConst::Fresh, ct.ty())
152152
}
153153
ty::ConstKind::Infer(ty::InferConst::EffectVar(v)) => {
154-
let opt_ct = self
155-
.infcx
156-
.inner
157-
.borrow_mut()
158-
.effect_unification_table()
159-
.probe_value(v)
160-
.map(|effect| effect.as_const(self.infcx.tcx));
154+
let opt_ct =
155+
self.infcx.inner.borrow_mut().effect_unification_table().probe_value(v).known();
161156
self.freshen_const(
162157
opt_ct,
163158
ty::InferConst::EffectVar(v),

compiler/rustc_infer/src/infer/mod.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub use self::ValuePairs::*;
88
pub use relate::combine::ObligationEmittingRelation;
99
use rustc_data_structures::captures::Captures;
1010
use rustc_data_structures::undo_log::UndoLogs;
11+
use rustc_middle::infer::unify_key::EffectVarValue;
1112
use rustc_middle::infer::unify_key::{ConstVidKey, EffectVidKey};
1213

1314
use self::opaque_types::OpaqueTypeStorage;
@@ -25,8 +26,8 @@ use rustc_data_structures::unify as ut;
2526
use rustc_errors::{DiagCtxt, DiagnosticBuilder, ErrorGuaranteed};
2627
use rustc_hir::def_id::{DefId, LocalDefId};
2728
use rustc_middle::infer::canonical::{Canonical, CanonicalVarValues};
29+
use rustc_middle::infer::unify_key::ConstVariableValue;
2830
use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind, ToType};
29-
use rustc_middle::infer::unify_key::{ConstVariableValue, EffectVarValue};
3031
use rustc_middle::mir::interpret::{ErrorHandled, EvalToValTreeResult};
3132
use rustc_middle::mir::ConstraintCategory;
3233
use rustc_middle::traits::{select, DefiningAnchor};
@@ -818,7 +819,7 @@ impl<'tcx> InferCtxt<'tcx> {
818819

819820
(0..table.len())
820821
.map(|i| ty::EffectVid::from_usize(i))
821-
.filter(|&vid| table.probe_value(vid).is_none())
822+
.filter(|&vid| table.probe_value(vid).is_unknown())
822823
.map(|v| {
823824
ty::Const::new_infer(self.tcx, ty::InferConst::EffectVar(v), self.tcx.types.bool)
824825
})
@@ -1236,7 +1237,8 @@ impl<'tcx> InferCtxt<'tcx> {
12361237
}
12371238

12381239
pub fn var_for_effect(&self, param: &ty::GenericParamDef) -> GenericArg<'tcx> {
1239-
let effect_vid = self.inner.borrow_mut().effect_unification_table().new_key(None).vid;
1240+
let effect_vid =
1241+
self.inner.borrow_mut().effect_unification_table().new_key(EffectVarValue::Unknown).vid;
12401242
let ty = self
12411243
.tcx
12421244
.type_of(param.def_id)
@@ -1416,8 +1418,8 @@ impl<'tcx> InferCtxt<'tcx> {
14161418
}
14171419
}
14181420

1419-
pub fn probe_effect_var(&self, vid: EffectVid) -> Option<EffectVarValue<'tcx>> {
1420-
self.inner.borrow_mut().effect_unification_table().probe_value(vid)
1421+
pub fn probe_effect_var(&self, vid: EffectVid) -> Option<ty::Const<'tcx>> {
1422+
self.inner.borrow_mut().effect_unification_table().probe_value(vid).known()
14211423
}
14221424

14231425
/// Attempts to resolve all type/region/const variables in
@@ -1893,7 +1895,8 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for ShallowResolver<'a, 'tcx> {
18931895
.borrow_mut()
18941896
.effect_unification_table()
18951897
.probe_value(vid)
1896-
.map_or(ct, |val| val.as_const(self.infcx.tcx)),
1898+
.known()
1899+
.unwrap_or(ct),
18971900
_ => ct,
18981901
}
18991902
}

compiler/rustc_infer/src/infer/relate/combine.rs

+6-32
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,7 @@ impl<'tcx> InferCtxt<'tcx> {
202202
ty::ConstKind::Infer(InferConst::EffectVar(a_vid)),
203203
ty::ConstKind::Infer(InferConst::EffectVar(b_vid)),
204204
) => {
205-
self.inner
206-
.borrow_mut()
207-
.effect_unification_table()
208-
.unify_var_var(a_vid, b_vid)
209-
.map_err(|a| effect_unification_error(self.tcx, relation.a_is_expected(), a))?;
205+
self.inner.borrow_mut().effect_unification_table().union(a_vid, b_vid);
210206
return Ok(a);
211207
}
212208

@@ -233,19 +229,11 @@ impl<'tcx> InferCtxt<'tcx> {
233229
}
234230

235231
(ty::ConstKind::Infer(InferConst::EffectVar(vid)), _) => {
236-
return self.unify_effect_variable(
237-
relation.a_is_expected(),
238-
vid,
239-
EffectVarValue::Const(b),
240-
);
232+
return Ok(self.unify_effect_variable(vid, b));
241233
}
242234

243235
(_, ty::ConstKind::Infer(InferConst::EffectVar(vid))) => {
244-
return self.unify_effect_variable(
245-
!relation.a_is_expected(),
246-
vid,
247-
EffectVarValue::Const(a),
248-
);
236+
return Ok(self.unify_effect_variable(vid, a));
249237
}
250238

251239
(ty::ConstKind::Unevaluated(..), _) | (_, ty::ConstKind::Unevaluated(..))
@@ -366,18 +354,12 @@ impl<'tcx> InferCtxt<'tcx> {
366354
Ok(Ty::new_float(self.tcx, val))
367355
}
368356

369-
fn unify_effect_variable(
370-
&self,
371-
vid_is_expected: bool,
372-
vid: ty::EffectVid,
373-
val: EffectVarValue<'tcx>,
374-
) -> RelateResult<'tcx, ty::Const<'tcx>> {
357+
fn unify_effect_variable(&self, vid: ty::EffectVid, val: ty::Const<'tcx>) -> ty::Const<'tcx> {
375358
self.inner
376359
.borrow_mut()
377360
.effect_unification_table()
378-
.unify_var_value(vid, Some(val))
379-
.map_err(|e| effect_unification_error(self.tcx, vid_is_expected, e))?;
380-
Ok(val.as_const(self.tcx))
361+
.union_value(vid, EffectVarValue::Known(val));
362+
val
381363
}
382364
}
383365

@@ -579,11 +561,3 @@ fn float_unification_error<'tcx>(
579561
let (ty::FloatVarValue(a), ty::FloatVarValue(b)) = v;
580562
TypeError::FloatMismatch(ExpectedFound::new(a_is_expected, a, b))
581563
}
582-
583-
fn effect_unification_error<'tcx>(
584-
_tcx: TyCtxt<'tcx>,
585-
_a_is_expected: bool,
586-
(_a, _b): (EffectVarValue<'tcx>, EffectVarValue<'tcx>),
587-
) -> TypeError<'tcx> {
588-
bug!("unexpected effect unification error")
589-
}

compiler/rustc_infer/src/infer/resolve.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,13 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for EagerResolver<'_, 'tcx> {
237237
}
238238
ty::ConstKind::Infer(ty::InferConst::EffectVar(vid)) => {
239239
debug_assert_eq!(c.ty(), self.infcx.tcx.types.bool);
240-
match self.infcx.probe_effect_var(vid) {
241-
Some(c) => c.as_const(self.infcx.tcx),
242-
None => ty::Const::new_infer(
240+
self.infcx.probe_effect_var(vid).unwrap_or_else(|| {
241+
ty::Const::new_infer(
243242
self.infcx.tcx,
244243
ty::InferConst::EffectVar(self.infcx.root_effect_var(vid)),
245244
self.infcx.tcx.types.bool,
246-
),
247-
}
245+
)
246+
})
248247
}
249248
_ => {
250249
if c.has_infer() {

compiler/rustc_middle/src/infer/unify_key.rs

+23-19
Original file line numberDiff line numberDiff line change
@@ -194,33 +194,37 @@ impl<'tcx> UnifyValue for ConstVariableValue<'tcx> {
194194
/// values for the effect inference variable
195195
#[derive(Clone, Copy, Debug)]
196196
pub enum EffectVarValue<'tcx> {
197-
/// The host effect is on, enabling access to syscalls, filesystem access, etc.
198-
Host,
199-
/// The host effect is off. Execution is restricted to const operations only.
200-
NoHost,
201-
Const(ty::Const<'tcx>),
197+
Unknown,
198+
Known(ty::Const<'tcx>),
202199
}
203200

204201
impl<'tcx> EffectVarValue<'tcx> {
205-
pub fn as_const(self, tcx: TyCtxt<'tcx>) -> ty::Const<'tcx> {
202+
pub fn known(self) -> Option<ty::Const<'tcx>> {
206203
match self {
207-
EffectVarValue::Host => tcx.consts.true_,
208-
EffectVarValue::NoHost => tcx.consts.false_,
209-
EffectVarValue::Const(c) => c,
204+
EffectVarValue::Unknown => None,
205+
EffectVarValue::Known(value) => Some(value),
206+
}
207+
}
208+
209+
pub fn is_unknown(self) -> bool {
210+
match self {
211+
EffectVarValue::Unknown => true,
212+
EffectVarValue::Known(_) => false,
210213
}
211214
}
212215
}
213216

214217
impl<'tcx> UnifyValue for EffectVarValue<'tcx> {
215-
type Error = (EffectVarValue<'tcx>, EffectVarValue<'tcx>);
218+
type Error = NoError;
216219
fn unify_values(value1: &Self, value2: &Self) -> Result<Self, Self::Error> {
217-
match (value1, value2) {
218-
(EffectVarValue::Host, EffectVarValue::Host) => Ok(EffectVarValue::Host),
219-
(EffectVarValue::NoHost, EffectVarValue::NoHost) => Ok(EffectVarValue::NoHost),
220-
(EffectVarValue::NoHost | EffectVarValue::Host, _)
221-
| (_, EffectVarValue::NoHost | EffectVarValue::Host) => Err((*value1, *value2)),
222-
(EffectVarValue::Const(_), EffectVarValue::Const(_)) => {
223-
bug!("equating two const variables, both of which have known values")
220+
match (*value1, *value2) {
221+
(EffectVarValue::Unknown, EffectVarValue::Unknown) => Ok(EffectVarValue::Unknown),
222+
(EffectVarValue::Unknown, EffectVarValue::Known(val))
223+
| (EffectVarValue::Known(val), EffectVarValue::Unknown) => {
224+
Ok(EffectVarValue::Known(val))
225+
}
226+
(EffectVarValue::Known(_), EffectVarValue::Known(_)) => {
227+
bug!("equating known inference variables: {value1:?} {value2:?}")
224228
}
225229
}
226230
}
@@ -229,7 +233,7 @@ impl<'tcx> UnifyValue for EffectVarValue<'tcx> {
229233
#[derive(PartialEq, Copy, Clone, Debug)]
230234
pub struct EffectVidKey<'tcx> {
231235
pub vid: ty::EffectVid,
232-
pub phantom: PhantomData<EffectVarValue<'tcx>>,
236+
pub phantom: PhantomData<ty::Const<'tcx>>,
233237
}
234238

235239
impl<'tcx> From<ty::EffectVid> for EffectVidKey<'tcx> {
@@ -239,7 +243,7 @@ impl<'tcx> From<ty::EffectVid> for EffectVidKey<'tcx> {
239243
}
240244

241245
impl<'tcx> UnifyKey for EffectVidKey<'tcx> {
242-
type Value = Option<EffectVarValue<'tcx>>;
246+
type Value = EffectVarValue<'tcx>;
243247
#[inline]
244248
fn index(&self) -> u32 {
245249
self.vid.as_u32()

0 commit comments

Comments
 (0)