Skip to content

Commit d113ff8

Browse files
varkoryodaldevoid
andcommitted
Handle generic consts in relate and infer
Co-Authored-By: Gabriel Smith <yodaldevoid@users.noreply.github.com>
1 parent 69423b3 commit d113ff8

File tree

3 files changed

+78
-16
lines changed

3 files changed

+78
-16
lines changed

src/librustc/infer/canonical/query_response.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::infer::canonical::{
1616
use crate::infer::region_constraints::{Constraint, RegionConstraintData};
1717
use crate::infer::InferCtxtBuilder;
1818
use crate::infer::{InferCtxt, InferOk, InferResult};
19+
use crate::mir::interpret::ConstValue;
1920
use rustc_data_structures::indexed_vec::Idx;
2021
use rustc_data_structures::indexed_vec::IndexVec;
2122
use std::fmt::Debug;
@@ -25,7 +26,7 @@ use crate::traits::TraitEngine;
2526
use crate::traits::{Obligation, ObligationCause, PredicateObligation};
2627
use crate::ty::fold::TypeFoldable;
2728
use crate::ty::subst::{Kind, UnpackedKind};
28-
use crate::ty::{self, BoundVar, Lift, Ty, TyCtxt};
29+
use crate::ty::{self, BoundVar, InferConst, Lift, Ty, TyCtxt};
2930
use crate::util::captures::Captures;
3031

3132
impl<'cx, 'gcx, 'tcx> InferCtxtBuilder<'cx, 'gcx, 'tcx> {
@@ -479,8 +480,17 @@ impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> {
479480
opt_values[br.assert_bound_var()] = Some(*original_value);
480481
}
481482
}
482-
UnpackedKind::Const(..) => {
483-
unimplemented!() // FIXME(const_generics)
483+
UnpackedKind::Const(result_value) => {
484+
if let ty::LazyConst::Evaluated(ty::Const {
485+
val: ConstValue::Infer(InferConst::Canonical(debrujin, b)),
486+
..
487+
}) = result_value {
488+
// ...in which case we would set `canonical_vars[0]` to `Some(const X)`.
489+
490+
// We only allow a `ty::INNERMOST` index in substitutions.
491+
assert_eq!(*debrujin, ty::INNERMOST);
492+
opt_values[*b] = Some(*original_value);
493+
}
484494
}
485495
}
486496
}

src/librustc/infer/resolve.rs

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use super::{InferCtxt, FixupError, FixupResult, Span, type_variable::TypeVariableOrigin};
2-
use crate::ty::{self, Ty, TyCtxt, TypeFoldable};
2+
use crate::mir::interpret::ConstValue;
3+
use crate::ty::{self, Ty, TyCtxt, TypeFoldable, InferConst};
34
use crate::ty::fold::{TypeFolder, TypeVisitor};
45

56
///////////////////////////////////////////////////////////////////////////
67
// OPPORTUNISTIC TYPE RESOLVER
78

89
/// The opportunistic type resolver can be used at any time. It simply replaces
910
/// type variables that have been unified with the things they have
10-
/// been unified with (similar to `shallow_resolve`, but deep). This is
11+
/// been unified with (similar to `shallow_resolve_type`, but deep). This is
1112
/// useful for printing messages etc but also required at various
1213
/// points for correctness.
1314
pub struct OpportunisticTypeResolver<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
@@ -30,7 +31,7 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for OpportunisticTypeResolver<'a, 'g
3031
if !t.has_infer_types() {
3132
t // micro-optimize -- if there is nothing in this type that this fold affects...
3233
} else {
33-
let t0 = self.infcx.shallow_resolve(t);
34+
let t0 = self.infcx.shallow_resolve_type(t);
3435
t0.super_fold_with(self)
3536
}
3637
}
@@ -58,7 +59,7 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for OpportunisticTypeAndRegionResolv
5859
if !t.needs_infer() {
5960
t // micro-optimize -- if there is nothing in this type that this fold affects...
6061
} else {
61-
let t0 = self.infcx.shallow_resolve(t);
62+
let t0 = self.infcx.shallow_resolve_type(t);
6263
t0.super_fold_with(self)
6364
}
6465
}
@@ -72,6 +73,15 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for OpportunisticTypeAndRegionResolv
7273
r,
7374
}
7475
}
76+
77+
fn fold_const(&mut self, ct: &'tcx ty::LazyConst<'tcx>) -> &'tcx ty::LazyConst<'tcx> {
78+
if !ct.needs_infer() {
79+
ct // micro-optimize -- if there is nothing in this const that this fold affects...
80+
} else {
81+
let c0 = self.infcx.shallow_resolve_const(ct);
82+
c0.super_fold_with(self)
83+
}
84+
}
7585
}
7686

7787
///////////////////////////////////////////////////////////////////////////
@@ -96,7 +106,7 @@ impl<'a, 'gcx, 'tcx> UnresolvedTypeFinder<'a, 'gcx, 'tcx> {
96106

97107
impl<'a, 'gcx, 'tcx> TypeVisitor<'tcx> for UnresolvedTypeFinder<'a, 'gcx, 'tcx> {
98108
fn visit_ty(&mut self, t: Ty<'tcx>) -> bool {
99-
let t = self.infcx.shallow_resolve(t);
109+
let t = self.infcx.shallow_resolve_type(t);
100110
if t.has_infer_types() {
101111
if let ty::Infer(infer_ty) = t.sty {
102112
// Since we called `shallow_resolve` above, this must
@@ -136,7 +146,7 @@ impl<'a, 'gcx, 'tcx> TypeVisitor<'tcx> for UnresolvedTypeFinder<'a, 'gcx, 'tcx>
136146
/// their concrete results. If any variable cannot be replaced (never unified, etc)
137147
/// then an `Err` result is returned.
138148
pub fn fully_resolve<'a, 'gcx, 'tcx, T>(infcx: &InferCtxt<'a, 'gcx, 'tcx>,
139-
value: &T) -> FixupResult<T>
149+
value: &T) -> FixupResult<'tcx, T>
140150
where T : TypeFoldable<'tcx>
141151
{
142152
let mut full_resolver = FullTypeResolver { infcx: infcx, err: None };
@@ -151,7 +161,7 @@ pub fn fully_resolve<'a, 'gcx, 'tcx, T>(infcx: &InferCtxt<'a, 'gcx, 'tcx>,
151161
// `err` field is not enforcable otherwise.
152162
struct FullTypeResolver<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
153163
infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
154-
err: Option<FixupError>,
164+
err: Option<FixupError<'tcx>>,
155165
}
156166

157167
impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for FullTypeResolver<'a, 'gcx, 'tcx> {
@@ -165,7 +175,7 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for FullTypeResolver<'a, 'gcx, 'tcx>
165175
// ^ we need to have the `keep_local` check to un-default
166176
// defaulted tuples.
167177
} else {
168-
let t = self.infcx.shallow_resolve(t);
178+
let t = self.infcx.shallow_resolve_type(t);
169179
match t.sty {
170180
ty::Infer(ty::TyVar(vid)) => {
171181
self.err = Some(FixupError::UnresolvedTy(vid));
@@ -199,4 +209,30 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for FullTypeResolver<'a, 'gcx, 'tcx>
199209
_ => r,
200210
}
201211
}
212+
213+
fn fold_const(&mut self, c: &'tcx ty::LazyConst<'tcx>) -> &'tcx ty::LazyConst<'tcx> {
214+
if !c.needs_infer() && !ty::keep_local(&c) {
215+
c // micro-optimize -- if there is nothing in this const that this fold affects...
216+
// ^ we need to have the `keep_local` check to un-default
217+
// defaulted tuples.
218+
} else {
219+
let c = self.infcx.shallow_resolve_const(c);
220+
match c {
221+
ty::LazyConst::Evaluated(ty::Const { val, .. }) => {
222+
match val {
223+
ConstValue::Infer(InferConst::Var(vid)) => {
224+
self.err = Some(FixupError::UnresolvedConst(*vid));
225+
return self.tcx().types.ct_err;
226+
}
227+
ConstValue::Infer(InferConst::Fresh(_)) => {
228+
bug!("Unexpected const in full const resolver: {:?}", c);
229+
}
230+
_ => {}
231+
}
232+
}
233+
_ => {}
234+
}
235+
c.super_fold_with(self)
236+
}
237+
}
202238
}

src/librustc/ty/relate.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
use crate::hir::def_id::DefId;
88
use crate::ty::subst::{Kind, UnpackedKind, SubstsRef};
99
use crate::ty::{self, Ty, TyCtxt, TypeFoldable};
10-
use crate::ty::error::{ExpectedFound, TypeError};
11-
use crate::mir::interpret::{GlobalId, ConstValue};
10+
use crate::ty::error::{ExpectedFound, TypeError, ConstError};
11+
use crate::mir::interpret::{GlobalId, ConstValue, Scalar};
1212
use crate::util::common::ErrorReported;
1313
use syntax_pos::DUMMY_SP;
1414
use std::rc::Rc;
@@ -476,6 +476,8 @@ pub fn super_relate_tys<'a, 'gcx, 'tcx, R>(relation: &mut R,
476476
let t = relation.relate(&a_t, &b_t)?;
477477
let to_u64 = |x: ty::Const<'tcx>| -> Result<u64, ErrorReported> {
478478
match x.val {
479+
// FIXME(const_generics): this doesn't work right now,
480+
// because it tries to relate an `Infer` to a `Param`.
479481
ConstValue::Unevaluated(def_id, substs) => {
480482
// FIXME(eddyb) get the right param_env.
481483
let param_env = ty::ParamEnv::empty();
@@ -489,7 +491,7 @@ pub fn super_relate_tys<'a, 'gcx, 'tcx, R>(relation: &mut R,
489491
if let Some(instance) = instance {
490492
let cid = GlobalId {
491493
instance,
492-
promoted: None
494+
promoted: None,
493495
};
494496
if let Some(s) = tcx.const_eval(param_env.and(cid))
495497
.ok()
@@ -718,6 +720,17 @@ impl<'tcx> Relate<'tcx> for ty::Region<'tcx> {
718720
}
719721
}
720722

723+
impl<'tcx> Relate<'tcx> for &'tcx ty::LazyConst<'tcx> {
724+
fn relate<'a, 'gcx, R>(relation: &mut R,
725+
a: &&'tcx ty::LazyConst<'tcx>,
726+
b: &&'tcx ty::LazyConst<'tcx>)
727+
-> RelateResult<'tcx, &'tcx ty::LazyConst<'tcx>>
728+
where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
729+
{
730+
relation.consts(*a, *b)
731+
}
732+
}
733+
721734
impl<'tcx, T: Relate<'tcx>> Relate<'tcx> for ty::Binder<T> {
722735
fn relate<'a, 'gcx, R>(relation: &mut R,
723736
a: &ty::Binder<T>,
@@ -771,14 +784,17 @@ impl<'tcx> Relate<'tcx> for Kind<'tcx> {
771784
(UnpackedKind::Type(a_ty), UnpackedKind::Type(b_ty)) => {
772785
Ok(relation.relate(&a_ty, &b_ty)?.into())
773786
}
787+
(UnpackedKind::Const(a_ct), UnpackedKind::Const(b_ct)) => {
788+
Ok(relation.relate(&a_ct, &b_ct)?.into())
789+
}
774790
(UnpackedKind::Lifetime(unpacked), x) => {
775791
bug!("impossible case reached: can't relate: {:?} with {:?}", unpacked, x)
776792
}
777793
(UnpackedKind::Type(unpacked), x) => {
778794
bug!("impossible case reached: can't relate: {:?} with {:?}", unpacked, x)
779795
}
780-
(UnpackedKind::Const(_), _) => {
781-
unimplemented!() // FIXME(const_generics)
796+
(UnpackedKind::Const(unpacked), x) => {
797+
bug!("impossible case reached: can't relate: {:?} with {:?}", unpacked, x)
782798
}
783799
}
784800
}

0 commit comments

Comments
 (0)