2019-05-31 21:52:35 +00:00
|
|
|
use super::{InferCtxt, FixupError, FixupResult, Span};
|
|
|
|
use super::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
|
2019-03-08 01:20:33 +00:00
|
|
|
use crate::mir::interpret::ConstValue;
|
2019-10-18 22:57:48 +00:00
|
|
|
use crate::ty::{self, Ty, Const, TyCtxt, TypeFoldable, InferConst};
|
2019-02-05 17:20:45 +00:00
|
|
|
use crate::ty::fold::{TypeFolder, TypeVisitor};
|
2012-08-13 22:06:13 +00:00
|
|
|
|
2014-12-01 17:27:27 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2019-05-28 19:25:21 +00:00
|
|
|
// OPPORTUNISTIC VAR RESOLVER
|
2012-08-13 22:06:13 +00:00
|
|
|
|
2019-05-28 19:25:21 +00:00
|
|
|
/// The opportunistic resolver can be used at any time. It simply replaces
|
2019-05-11 18:08:26 +00:00
|
|
|
/// type/const variables that have been unified with the things they have
|
2019-04-30 21:27:33 +00:00
|
|
|
/// been unified with (similar to `shallow_resolve`, but deep). This is
|
2014-12-01 17:27:27 +00:00
|
|
|
/// useful for printing messages etc but also required at various
|
|
|
|
/// points for correctness.
|
2019-06-14 16:39:39 +00:00
|
|
|
pub struct OpportunisticVarResolver<'a, 'tcx> {
|
2019-06-13 21:48:52 +00:00
|
|
|
infcx: &'a InferCtxt<'a, 'tcx>,
|
2012-08-13 22:06:13 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 21:48:52 +00:00
|
|
|
impl<'a, 'tcx> OpportunisticVarResolver<'a, 'tcx> {
|
2018-11-29 20:13:04 +00:00
|
|
|
#[inline]
|
2019-06-13 21:48:52 +00:00
|
|
|
pub fn new(infcx: &'a InferCtxt<'a, 'tcx>) -> Self {
|
2019-05-11 18:08:26 +00:00
|
|
|
OpportunisticVarResolver { infcx }
|
2013-01-08 22:00:45 +00:00
|
|
|
}
|
2012-08-13 22:06:13 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 21:48:52 +00:00
|
|
|
impl<'a, 'tcx> TypeFolder<'tcx> for OpportunisticVarResolver<'a, 'tcx> {
|
|
|
|
fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
|
2013-10-29 10:03:32 +00:00
|
|
|
self.infcx.tcx
|
|
|
|
}
|
|
|
|
|
2014-09-29 19:11:30 +00:00
|
|
|
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
|
2019-10-18 22:57:48 +00:00
|
|
|
if !t.has_infer_types() && !t.has_infer_consts() {
|
2014-12-01 17:27:27 +00:00
|
|
|
t // micro-optimize -- if there is nothing in this type that this fold affects...
|
|
|
|
} else {
|
2019-05-11 17:54:14 +00:00
|
|
|
let t = self.infcx.shallow_resolve(t);
|
|
|
|
t.super_fold_with(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_const(&mut self, ct: &'tcx Const<'tcx>) -> &'tcx Const<'tcx> {
|
2019-10-18 22:57:48 +00:00
|
|
|
if !ct.has_infer_consts() {
|
2019-05-11 17:54:14 +00:00
|
|
|
ct // micro-optimize -- if there is nothing in this const that this fold affects...
|
|
|
|
} else {
|
|
|
|
let ct = self.infcx.shallow_resolve(ct);
|
|
|
|
ct.super_fold_with(self)
|
2012-11-08 02:40:34 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-13 22:06:13 +00:00
|
|
|
}
|
2014-12-01 15:11:59 +00:00
|
|
|
|
2015-12-06 18:21:23 +00:00
|
|
|
/// The opportunistic type and region resolver is similar to the
|
2017-08-15 19:45:21 +00:00
|
|
|
/// opportunistic type resolver, but also opportunistically resolves
|
2015-12-06 18:21:23 +00:00
|
|
|
/// regions. It is useful for canonicalization.
|
2019-06-14 16:39:39 +00:00
|
|
|
pub struct OpportunisticTypeAndRegionResolver<'a, 'tcx> {
|
2019-06-13 21:48:52 +00:00
|
|
|
infcx: &'a InferCtxt<'a, 'tcx>,
|
2015-12-06 18:21:23 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 21:48:52 +00:00
|
|
|
impl<'a, 'tcx> OpportunisticTypeAndRegionResolver<'a, 'tcx> {
|
|
|
|
pub fn new(infcx: &'a InferCtxt<'a, 'tcx>) -> Self {
|
2018-11-06 20:05:44 +00:00
|
|
|
OpportunisticTypeAndRegionResolver { infcx }
|
2015-12-06 18:21:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 21:48:52 +00:00
|
|
|
impl<'a, 'tcx> TypeFolder<'tcx> for OpportunisticTypeAndRegionResolver<'a, 'tcx> {
|
|
|
|
fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
|
2015-12-06 18:21:23 +00:00
|
|
|
self.infcx.tcx
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
|
|
|
|
if !t.needs_infer() {
|
|
|
|
t // micro-optimize -- if there is nothing in this type that this fold affects...
|
|
|
|
} else {
|
2019-04-30 21:27:33 +00:00
|
|
|
let t0 = self.infcx.shallow_resolve(t);
|
2016-01-06 02:01:28 +00:00
|
|
|
t0.super_fold_with(self)
|
2015-12-06 18:21:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 08:45:53 +00:00
|
|
|
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
|
2016-08-25 20:58:52 +00:00
|
|
|
match *r {
|
2017-11-05 11:25:23 +00:00
|
|
|
ty::ReVar(rid) =>
|
2017-11-05 12:22:39 +00:00
|
|
|
self.infcx.borrow_region_constraints()
|
|
|
|
.opportunistic_resolve_var(self.tcx(), rid),
|
2017-11-05 11:25:23 +00:00
|
|
|
_ =>
|
|
|
|
r,
|
2015-12-06 18:21:23 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-08 01:20:33 +00:00
|
|
|
|
2019-03-18 20:55:19 +00:00
|
|
|
fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
|
2019-03-08 01:20:33 +00:00
|
|
|
if !ct.needs_infer() {
|
|
|
|
ct // micro-optimize -- if there is nothing in this const that this fold affects...
|
|
|
|
} else {
|
2019-04-30 21:27:33 +00:00
|
|
|
let c0 = self.infcx.shallow_resolve(ct);
|
2019-03-08 01:20:33 +00:00
|
|
|
c0.super_fold_with(self)
|
|
|
|
}
|
|
|
|
}
|
2015-12-06 18:21:23 +00:00
|
|
|
}
|
|
|
|
|
2017-09-02 12:35:07 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// UNRESOLVED TYPE FINDER
|
|
|
|
|
2019-03-11 18:46:20 +00:00
|
|
|
/// The unresolved type **finder** walks a type searching for
|
|
|
|
/// type variables that don't yet have a value. The first unresolved type is stored.
|
|
|
|
/// It does not construct the fully resolved type (which might
|
2017-09-02 12:35:07 +00:00
|
|
|
/// involve some hashing and so forth).
|
2019-06-14 16:39:39 +00:00
|
|
|
pub struct UnresolvedTypeFinder<'a, 'tcx> {
|
2019-06-13 21:48:52 +00:00
|
|
|
infcx: &'a InferCtxt<'a, 'tcx>,
|
2019-03-11 18:46:20 +00:00
|
|
|
|
|
|
|
/// Used to find the type parameter name and location for error reporting.
|
2019-06-13 22:32:15 +00:00
|
|
|
pub first_unresolved: Option<(Ty<'tcx>, Option<Span>)>,
|
2017-09-02 12:35:07 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 21:48:52 +00:00
|
|
|
impl<'a, 'tcx> UnresolvedTypeFinder<'a, 'tcx> {
|
|
|
|
pub fn new(infcx: &'a InferCtxt<'a, 'tcx>) -> Self {
|
2019-03-11 18:46:20 +00:00
|
|
|
UnresolvedTypeFinder { infcx, first_unresolved: None }
|
2017-09-02 12:35:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 21:48:52 +00:00
|
|
|
impl<'a, 'tcx> TypeVisitor<'tcx> for UnresolvedTypeFinder<'a, 'tcx> {
|
2017-09-02 12:35:07 +00:00
|
|
|
fn visit_ty(&mut self, t: Ty<'tcx>) -> bool {
|
2019-04-30 21:27:33 +00:00
|
|
|
let t = self.infcx.shallow_resolve(t);
|
2017-09-02 12:35:07 +00:00
|
|
|
if t.has_infer_types() {
|
2019-09-16 18:08:35 +00:00
|
|
|
if let ty::Infer(infer_ty) = t.kind {
|
2017-09-02 12:35:07 +00:00
|
|
|
// Since we called `shallow_resolve` above, this must
|
|
|
|
// be an (as yet...) unresolved inference variable.
|
2019-03-11 18:46:20 +00:00
|
|
|
let ty_var_span =
|
|
|
|
if let ty::TyVar(ty_vid) = infer_ty {
|
|
|
|
let ty_vars = self.infcx.type_variables.borrow();
|
2019-05-31 21:52:35 +00:00
|
|
|
if let TypeVariableOrigin {
|
|
|
|
kind: TypeVariableOriginKind::TypeParameterDefinition(_),
|
|
|
|
span,
|
|
|
|
} = *ty_vars.var_origin(ty_vid)
|
2019-03-11 18:46:20 +00:00
|
|
|
{
|
|
|
|
Some(span)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
self.first_unresolved = Some((t, ty_var_span));
|
|
|
|
true // Halt visiting.
|
2017-09-02 12:35:07 +00:00
|
|
|
} else {
|
|
|
|
// Otherwise, visit its contents.
|
|
|
|
t.super_visit_with(self)
|
|
|
|
}
|
|
|
|
} else {
|
2019-03-11 18:46:20 +00:00
|
|
|
// All type variables in inference types must already be resolved,
|
|
|
|
// - no need to visit the contents, continue visiting.
|
2017-09-02 12:35:07 +00:00
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-11 18:46:20 +00:00
|
|
|
|
2014-12-01 15:11:59 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2014-12-01 17:27:27 +00:00
|
|
|
// FULL TYPE RESOLUTION
|
|
|
|
|
|
|
|
/// Full type resolution replaces all type and region variables with
|
|
|
|
/// their concrete results. If any variable cannot be replaced (never unified, etc)
|
|
|
|
/// then an `Err` result is returned.
|
2019-06-13 22:32:15 +00:00
|
|
|
pub fn fully_resolve<'a, 'tcx, T>(infcx: &InferCtxt<'a, 'tcx>, value: &T) -> FixupResult<'tcx, T>
|
|
|
|
where
|
|
|
|
T: TypeFoldable<'tcx>,
|
2014-12-01 17:27:27 +00:00
|
|
|
{
|
|
|
|
let mut full_resolver = FullTypeResolver { infcx: infcx, err: None };
|
|
|
|
let result = value.fold_with(&mut full_resolver);
|
|
|
|
match full_resolver.err {
|
|
|
|
None => Ok(result),
|
|
|
|
Some(e) => Err(e),
|
|
|
|
}
|
2014-12-01 15:11:59 +00:00
|
|
|
}
|
|
|
|
|
2014-12-01 17:27:27 +00:00
|
|
|
// N.B. This type is not public because the protocol around checking the
|
|
|
|
// `err` field is not enforcable otherwise.
|
2019-06-14 16:39:39 +00:00
|
|
|
struct FullTypeResolver<'a, 'tcx> {
|
2019-06-13 21:48:52 +00:00
|
|
|
infcx: &'a InferCtxt<'a, 'tcx>,
|
2019-03-08 01:20:33 +00:00
|
|
|
err: Option<FixupError<'tcx>>,
|
2014-12-01 15:11:59 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 21:48:52 +00:00
|
|
|
impl<'a, 'tcx> TypeFolder<'tcx> for FullTypeResolver<'a, 'tcx> {
|
|
|
|
fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
|
2014-12-01 15:11:59 +00:00
|
|
|
self.infcx.tcx
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
|
2017-08-14 10:23:56 +00:00
|
|
|
if !t.needs_infer() && !ty::keep_local(&t) {
|
2014-12-01 15:11:59 +00:00
|
|
|
t // micro-optimize -- if there is nothing in this type that this fold affects...
|
2018-09-28 15:35:31 +00:00
|
|
|
// ^ we need to have the `keep_local` check to un-default
|
|
|
|
// defaulted tuples.
|
2014-12-01 15:11:59 +00:00
|
|
|
} else {
|
2019-04-30 21:27:33 +00:00
|
|
|
let t = self.infcx.shallow_resolve(t);
|
2019-09-16 18:08:35 +00:00
|
|
|
match t.kind {
|
2018-08-22 00:35:02 +00:00
|
|
|
ty::Infer(ty::TyVar(vid)) => {
|
2015-07-11 02:16:35 +00:00
|
|
|
self.err = Some(FixupError::UnresolvedTy(vid));
|
2014-12-25 12:20:48 +00:00
|
|
|
self.tcx().types.err
|
2014-12-01 17:27:27 +00:00
|
|
|
}
|
2018-08-22 00:35:02 +00:00
|
|
|
ty::Infer(ty::IntVar(vid)) => {
|
2015-07-11 02:16:35 +00:00
|
|
|
self.err = Some(FixupError::UnresolvedIntTy(vid));
|
2014-12-25 12:20:48 +00:00
|
|
|
self.tcx().types.err
|
2014-12-01 17:27:27 +00:00
|
|
|
}
|
2018-08-22 00:35:02 +00:00
|
|
|
ty::Infer(ty::FloatVar(vid)) => {
|
2015-07-11 02:16:35 +00:00
|
|
|
self.err = Some(FixupError::UnresolvedFloatTy(vid));
|
2014-12-25 12:20:48 +00:00
|
|
|
self.tcx().types.err
|
2014-12-01 17:27:27 +00:00
|
|
|
}
|
2018-08-22 00:35:02 +00:00
|
|
|
ty::Infer(_) => {
|
2016-03-25 00:14:29 +00:00
|
|
|
bug!("Unexpected type in full type resolver: {:?}", t);
|
2014-12-01 17:27:27 +00:00
|
|
|
}
|
|
|
|
_ => {
|
2016-01-06 02:01:28 +00:00
|
|
|
t.super_fold_with(self)
|
2014-12-01 17:27:27 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-01 15:11:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 08:45:53 +00:00
|
|
|
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
|
2016-08-25 20:58:52 +00:00
|
|
|
match *r {
|
2017-11-05 10:48:16 +00:00
|
|
|
ty::ReVar(rid) => self.infcx.lexical_region_resolutions
|
|
|
|
.borrow()
|
|
|
|
.as_ref()
|
|
|
|
.expect("region resolution not performed")
|
|
|
|
.resolve_var(rid),
|
2016-08-25 20:58:52 +00:00
|
|
|
_ => r,
|
2014-12-01 17:27:27 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-08 01:20:33 +00:00
|
|
|
|
2019-03-18 20:55:19 +00:00
|
|
|
fn fold_const(&mut self, c: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> {
|
2019-03-08 01:20:33 +00:00
|
|
|
if !c.needs_infer() && !ty::keep_local(&c) {
|
|
|
|
c // micro-optimize -- if there is nothing in this const that this fold affects...
|
|
|
|
// ^ we need to have the `keep_local` check to un-default
|
|
|
|
// defaulted tuples.
|
|
|
|
} else {
|
2019-04-30 21:27:33 +00:00
|
|
|
let c = self.infcx.shallow_resolve(c);
|
2019-03-18 20:55:19 +00:00
|
|
|
match c.val {
|
|
|
|
ConstValue::Infer(InferConst::Var(vid)) => {
|
|
|
|
self.err = Some(FixupError::UnresolvedConst(vid));
|
2019-05-01 22:09:53 +00:00
|
|
|
return self.tcx().consts.err;
|
2019-03-18 20:55:19 +00:00
|
|
|
}
|
|
|
|
ConstValue::Infer(InferConst::Fresh(_)) => {
|
|
|
|
bug!("Unexpected const in full const resolver: {:?}", c);
|
2019-03-08 01:20:33 +00:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
c.super_fold_with(self)
|
|
|
|
}
|
|
|
|
}
|
2014-12-01 17:27:27 +00:00
|
|
|
}
|