2012-12-04 00:48:01 +00:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2015-07-11 02:16:35 +00:00
|
|
|
use super::{InferCtxt, FixupError, FixupResult};
|
2016-03-22 15:30:57 +00:00
|
|
|
use ty::{self, Ty, TyCtxt, TypeFoldable};
|
2016-04-29 03:00:23 +00:00
|
|
|
use ty::fold::TypeFolder;
|
2012-08-13 22:06:13 +00:00
|
|
|
|
2014-12-01 17:27:27 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// OPPORTUNISTIC TYPE RESOLVER
|
2012-08-13 22:06:13 +00:00
|
|
|
|
2014-12-01 17:27:27 +00:00
|
|
|
/// The opportunistic type resolver can be used at any time. It simply replaces
|
|
|
|
/// type variables that have been unified with the things they have
|
|
|
|
/// been unified with (similar to `shallow_resolve`, but deep). This is
|
|
|
|
/// useful for printing messages etc but also required at various
|
|
|
|
/// points for correctness.
|
2016-05-03 02:23:22 +00:00
|
|
|
pub struct OpportunisticTypeResolver<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
|
|
|
|
infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
|
2012-08-13 22:06:13 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 03:00:23 +00:00
|
|
|
impl<'a, 'gcx, 'tcx> OpportunisticTypeResolver<'a, 'gcx, 'tcx> {
|
|
|
|
pub fn new(infcx: &'a InferCtxt<'a, 'gcx, 'tcx>) -> Self {
|
2014-12-01 17:27:27 +00:00
|
|
|
OpportunisticTypeResolver { infcx: infcx }
|
2013-01-08 22:00:45 +00:00
|
|
|
}
|
2012-08-13 22:06:13 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 03:00:23 +00:00
|
|
|
impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for OpportunisticTypeResolver<'a, 'gcx, 'tcx> {
|
|
|
|
fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, '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> {
|
2015-06-23 23:54:32 +00:00
|
|
|
if !t.has_infer_types() {
|
2014-12-01 17:27:27 +00:00
|
|
|
t // micro-optimize -- if there is nothing in this type that this fold affects...
|
|
|
|
} else {
|
|
|
|
let t0 = self.infcx.shallow_resolve(t);
|
2016-01-06 02:01:28 +00:00
|
|
|
t0.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
|
|
|
|
/// opportunistic type resolver, but also opportunistly resolves
|
|
|
|
/// regions. It is useful for canonicalization.
|
2016-05-03 02:23:22 +00:00
|
|
|
pub struct OpportunisticTypeAndRegionResolver<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
|
|
|
|
infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
|
2015-12-06 18:21:23 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 03:00:23 +00:00
|
|
|
impl<'a, 'gcx, 'tcx> OpportunisticTypeAndRegionResolver<'a, 'gcx, 'tcx> {
|
|
|
|
pub fn new(infcx: &'a InferCtxt<'a, 'gcx, 'tcx>) -> Self {
|
2015-12-06 18:21:23 +00:00
|
|
|
OpportunisticTypeAndRegionResolver { infcx: infcx }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 03:00:23 +00:00
|
|
|
impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for OpportunisticTypeAndRegionResolver<'a, 'gcx, 'tcx> {
|
|
|
|
fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, '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 {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_region(&mut self, r: ty::Region) -> ty::Region {
|
|
|
|
match r {
|
|
|
|
ty::ReVar(rid) => self.infcx.region_vars.opportunistic_resolve_var(rid),
|
|
|
|
_ => r,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2016-04-29 03:00:23 +00:00
|
|
|
pub fn fully_resolve<'a, 'gcx, 'tcx, T>(infcx: &InferCtxt<'a, 'gcx, 'tcx>,
|
|
|
|
value: &T) -> FixupResult<T>
|
2014-12-01 17:27:27 +00:00
|
|
|
where T : TypeFoldable<'tcx>
|
|
|
|
{
|
|
|
|
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.
|
2016-05-03 02:23:22 +00:00
|
|
|
struct FullTypeResolver<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
|
|
|
|
infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
|
2015-07-11 02:16:35 +00:00
|
|
|
err: Option<FixupError>,
|
2014-12-01 15:11:59 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 03:00:23 +00:00
|
|
|
impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for FullTypeResolver<'a, 'gcx, 'tcx> {
|
|
|
|
fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> {
|
2014-12-01 15:11:59 +00:00
|
|
|
self.infcx.tcx
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
|
2015-06-23 23:54:32 +00:00
|
|
|
if !t.needs_infer() {
|
2014-12-01 15:11:59 +00:00
|
|
|
t // micro-optimize -- if there is nothing in this type that this fold affects...
|
|
|
|
} else {
|
2014-12-01 17:27:27 +00:00
|
|
|
let t = self.infcx.shallow_resolve(t);
|
|
|
|
match t.sty {
|
2015-06-11 23:21:46 +00:00
|
|
|
ty::TyInfer(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
|
|
|
}
|
2015-06-11 23:21:46 +00:00
|
|
|
ty::TyInfer(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
|
|
|
}
|
2015-06-11 23:21:46 +00:00
|
|
|
ty::TyInfer(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
|
|
|
}
|
2015-06-11 23:21:46 +00:00
|
|
|
ty::TyInfer(_) => {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-01 17:27:27 +00:00
|
|
|
fn fold_region(&mut self, r: ty::Region) -> ty::Region {
|
|
|
|
match r {
|
2015-08-18 20:21:29 +00:00
|
|
|
ty::ReVar(rid) => self.infcx.region_vars.resolve_var(rid),
|
2014-12-01 17:27:27 +00:00
|
|
|
_ => r,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|