Skip to content
Permalink
Browse files

Refactor `TypeVariableOrigin` into `TypeVariableOrigin` and `TypeVari…

…ableOriginKind`
  • Loading branch information...
varkor committed Mar 21, 2019
1 parent 05f5051 commit 4dcc49c68509d46d57a5f8001c6de191d7d15e51
@@ -21,7 +21,8 @@
//!
//! [c]: https://rust-lang.github.io/rustc-guide/traits/canonicalization.html

use crate::infer::{InferCtxt, RegionVariableOrigin, TypeVariableOrigin, ConstVariableOrigin};
use crate::infer::{InferCtxt, RegionVariableOrigin, TypeVariableOrigin, TypeVariableOriginKind};
use crate::infer::ConstVariableOrigin;
use crate::mir::interpret::ConstValue;
use rustc_data_structures::indexed_vec::IndexVec;
use rustc_macros::HashStable;
@@ -365,7 +366,10 @@ impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> {
let ty = match ty_kind {
CanonicalTyVarKind::General(ui) => {
self.next_ty_var_in_universe(
TypeVariableOrigin::MiscVariable(span),
TypeVariableOrigin {
kind: TypeVariableOriginKind::MiscVariable,
span,
},
universe_map(ui)
)
}
@@ -2,7 +2,7 @@ use crate::hir::def::Namespace;
use crate::hir::{self, Local, Pat, Body, HirId};
use crate::hir::intravisit::{self, Visitor, NestedVisitorMap};
use crate::infer::InferCtxt;
use crate::infer::type_variable::TypeVariableOrigin;
use crate::infer::type_variable::TypeVariableOriginKind;
use crate::ty::{self, Ty, Infer, TyVar};
use crate::ty::print::Print;
use syntax::source_map::CompilerDesugaringKind;
@@ -83,8 +83,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
) -> String {
if let ty::Infer(ty::TyVar(ty_vid)) = ty.sty {
let ty_vars = self.type_variables.borrow();
if let TypeVariableOrigin::TypeParameterDefinition(_, name) =
*ty_vars.var_origin(ty_vid) {
if let TypeVariableOriginKind::TypeParameterDefinition(name) =
ty_vars.var_origin(ty_vid).kind {
return name.to_string();
}
}
@@ -20,7 +20,7 @@
//! a lattice.

use super::InferCtxt;
use super::type_variable::TypeVariableOrigin;
use super::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};

use crate::traits::ObligationCause;
use crate::ty::TyVar;
@@ -79,12 +79,18 @@ pub fn super_lattice_tys<'a, 'gcx, 'tcx, L>(this: &mut L,
// iterate on the subtype obligations that are returned, but I
// think this suffices. -nmatsakis
(&ty::Infer(TyVar(..)), _) => {
let v = infcx.next_ty_var(TypeVariableOrigin::LatticeVariable(this.cause().span));
let v = infcx.next_ty_var(TypeVariableOrigin {
kind: TypeVariableOriginKind::LatticeVariable,
span: this.cause().span,
});
this.relate_bound(v, b, a)?;
Ok(v)
}
(_, &ty::Infer(TyVar(..))) => {
let v = infcx.next_ty_var(TypeVariableOrigin::LatticeVariable(this.cause().span));
let v = infcx.next_ty_var(TypeVariableOrigin {
kind: TypeVariableOriginKind::LatticeVariable,
span: this.cause().span,
});
this.relate_bound(v, a, b)?;
Ok(v)
}
@@ -39,7 +39,7 @@ use self::lexical_region_resolve::LexicalRegionResolutions;
use self::outlives::env::OutlivesEnvironment;
use self::region_constraints::{GenericKind, RegionConstraintData, VarInfos, VerifyBound};
use self::region_constraints::{RegionConstraintCollector, RegionSnapshot};
use self::type_variable::TypeVariableOrigin;
use self::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use self::unify_key::{ToType, ConstVariableOrigin};

pub mod at;
@@ -1110,7 +1110,10 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
let ty_var_id = self.type_variables.borrow_mut().new_var(
self.universe(),
false,
TypeVariableOrigin::TypeParameterDefinition(span, param.name),
TypeVariableOrigin {
kind: TypeVariableOriginKind::TypeParameterDefinition(param.name),
span,
},
);

self.tcx.mk_ty_var(ty_var_id).into()
@@ -1412,7 +1415,12 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
T: TypeFoldable<'tcx>
{
let fld_r = |br| self.next_region_var(LateBoundRegion(span, br, lbrct));
let fld_t = |_| self.next_ty_var(TypeVariableOrigin::MiscVariable(span));
let fld_t = |_| {
self.next_ty_var(TypeVariableOrigin {
kind: TypeVariableOriginKind::MiscVariable,
span,
})
};
let fld_c = |_, ty| self.next_const_var(ty, ConstVariableOrigin::MiscVariable(span));
self.tcx.replace_bound_vars(value, fld_r, fld_t, fld_c)
}
@@ -270,15 +270,16 @@ where
projection_ty: ty::ProjectionTy<'tcx>,
value_ty: Ty<'tcx>,
) -> Ty<'tcx> {
use crate::infer::type_variable::TypeVariableOrigin;
use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use crate::traits::WhereClause;
use syntax_pos::DUMMY_SP;

match value_ty.sty {
ty::Projection(other_projection_ty) => {
let var = self
.infcx
.next_ty_var(TypeVariableOrigin::MiscVariable(DUMMY_SP));
let var = self.infcx.next_ty_var(TypeVariableOrigin {
kind: TypeVariableOriginKind::MiscVariable,
span: DUMMY_SP,
});
self.relate_projection_ty(projection_ty, var);
self.relate_projection_ty(other_projection_ty, var);
var
@@ -4,7 +4,7 @@ use syntax_pos::Span;
use crate::hir::def_id::DefId;
use crate::hir;
use crate::hir::Node;
use crate::infer::{self, InferCtxt, InferOk, TypeVariableOrigin};
use crate::infer::{self, InferCtxt, InferOk, TypeVariableOrigin, TypeVariableOriginKind};
use crate::infer::outlives::free_region_map::FreeRegionRelations;
use crate::traits::{self, PredicateObligation};
use crate::ty::{self, Ty, TyCtxt, GenericParamDefKind};
@@ -864,7 +864,10 @@ impl<'a, 'gcx, 'tcx> Instantiator<'a, 'gcx, 'tcx> {
return opaque_defn.concrete_ty;
}
let span = tcx.def_span(def_id);
let ty_var = infcx.next_ty_var(TypeVariableOrigin::TypeInference(span));
let ty_var = infcx.next_ty_var(TypeVariableOrigin {
kind: TypeVariableOriginKind::TypeInference,
span,
});

let predicates_of = tcx.predicates_of(def_id);
debug!(
@@ -37,22 +37,28 @@ pub struct TypeVariableTable<'tcx> {
sub_relations: ut::UnificationTable<ut::InPlace<ty::TyVid>>,
}

#[derive(Copy, Clone, Debug)]
pub struct TypeVariableOrigin {
pub kind: TypeVariableOriginKind,
pub span: Span,
}

/// Reasons to create a type inference variable
#[derive(Copy, Clone, Debug)]
pub enum TypeVariableOrigin {
MiscVariable(Span),
NormalizeProjectionType(Span),
TypeInference(Span),
TypeParameterDefinition(Span, InternedString),

/// one of the upvars or closure kind parameters in a `ClosureSubsts`
/// (before it has been determined)
ClosureSynthetic(Span),
SubstitutionPlaceholder(Span),
AutoDeref(Span),
AdjustmentType(Span),
DivergingFn(Span),
LatticeVariable(Span),
pub enum TypeVariableOriginKind {
MiscVariable,
NormalizeProjectionType,
TypeInference,
TypeParameterDefinition(InternedString),

/// One of the upvars or closure kind parameters in a `ClosureSubsts`
/// (before it has been determined).
ClosureSynthetic,
SubstitutionPlaceholder,
AutoDeref,
AdjustmentType,
DivergingFn,
LatticeVariable,
}

struct TypeVariableData {
@@ -21,7 +21,7 @@ use crate::hir;
use crate::hir::Node;
use crate::hir::def_id::DefId;
use crate::infer::{self, InferCtxt};
use crate::infer::type_variable::TypeVariableOrigin;
use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use crate::session::DiagnosticMessageId;
use crate::ty::{self, AdtKind, ToPredicate, ToPolyTraitRef, Ty, TyCtxt, TypeFoldable};
use crate::ty::GenericParamDefKind;
@@ -1464,7 +1464,12 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
let infcx = self.infcx;
self.var_map.entry(ty).or_insert_with(||
infcx.next_ty_var(
TypeVariableOrigin::TypeParameterDefinition(DUMMY_SP, name)))
TypeVariableOrigin {
kind: TypeVariableOriginKind::TypeParameterDefinition(name),
span: DUMMY_SP,
}
)
)
} else {
ty.super_fold_with(self)
}
@@ -14,7 +14,7 @@ use super::util;

use crate::hir::def_id::DefId;
use crate::infer::{InferCtxt, InferOk, LateBoundRegionConversionTime};
use crate::infer::type_variable::TypeVariableOrigin;
use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use crate::mir::interpret::{GlobalId, ConstValue};
use rustc_data_structures::snapshot_map::{Snapshot, SnapshotMap};
use rustc_macros::HashStable;
@@ -475,7 +475,11 @@ pub fn normalize_projection_type<'a, 'b, 'gcx, 'tcx>(
let tcx = selcx.infcx().tcx;
let def_id = projection_ty.item_def_id;
let ty_var = selcx.infcx().next_ty_var(
TypeVariableOrigin::NormalizeProjectionType(tcx.def_span(def_id)));
TypeVariableOrigin {
kind: TypeVariableOriginKind::NormalizeProjectionType,
span: tcx.def_span(def_id),
},
);
let projection = ty::Binder::dummy(ty::ProjectionPredicate {
projection_ty,
ty: ty_var
@@ -810,7 +814,11 @@ fn normalize_to_error<'a, 'gcx, 'tcx>(selcx: &mut SelectionContext<'a, 'gcx, 'tc
let tcx = selcx.infcx().tcx;
let def_id = projection_ty.item_def_id;
let new_value = selcx.infcx().next_ty_var(
TypeVariableOrigin::NormalizeProjectionType(tcx.def_span(def_id)));
TypeVariableOrigin {
kind: TypeVariableOriginKind::NormalizeProjectionType,
span: tcx.def_span(def_id),
},
);
Normalized {
value: new_value,
obligations: vec![trait_obligation]
@@ -26,7 +26,7 @@ use rustc::hir::def_id::DefId;
use rustc::infer::canonical::QueryRegionConstraint;
use rustc::infer::outlives::env::RegionBoundPairs;
use rustc::infer::{InferCtxt, InferOk, LateBoundRegionConversionTime, NLLRegionVariableOrigin};
use rustc::infer::type_variable::TypeVariableOrigin;
use rustc::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use rustc::mir::interpret::{InterpError::BoundsCheck, ConstValue};
use rustc::mir::tcx::PlaceTy;
use rustc::mir::visit::{PlaceContext, Visitor, NonMutatingUseContext};
@@ -2209,7 +2209,10 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
if let ty::RawPtr(_) | ty::FnPtr(_) = ty_left.sty {
let ty_right = right.ty(mir, tcx);
let common_ty = self.infcx.next_ty_var(
TypeVariableOrigin::MiscVariable(mir.source_info(location).span),
TypeVariableOrigin {
kind: TypeVariableOriginKind::MiscVariable,
span: mir.source_info(location).span,
}
);
self.sub_types(
common_ty,
@@ -6,7 +6,7 @@ use rustc::hir::{self, PatKind, Pat, ExprKind};
use rustc::hir::def::{Res, DefKind, CtorKind};
use rustc::hir::pat_util::EnumerateAndAdjustIterator;
use rustc::infer;
use rustc::infer::type_variable::TypeVariableOrigin;
use rustc::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use rustc::traits::{ObligationCause, ObligationCauseCode};
use rustc::ty::{self, Ty, TypeFoldable};
use rustc::ty::subst::Kind;
@@ -311,9 +311,14 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
let max_len = cmp::max(expected_len, elements.len());

let element_tys_iter = (0..max_len).map(|_| {
// FIXME: `MiscVariable` for now -- obtaining the span and name information
// from all tuple elements isn't trivial.
Kind::from(self.next_ty_var(TypeVariableOrigin::TypeInference(pat.span)))
Kind::from(self.next_ty_var(
// FIXME: `MiscVariable` for now -- obtaining the span and name information
// from all tuple elements isn't trivial.
TypeVariableOrigin {
kind: TypeVariableOriginKind::TypeInference,
span: pat.span,
},
))
});
let element_tys = tcx.mk_substs(element_tys_iter);
let pat_ty = tcx.mk_ty(ty::Tuple(element_tys));
@@ -339,7 +344,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
}
}
PatKind::Box(ref inner) => {
let inner_ty = self.next_ty_var(TypeVariableOrigin::TypeInference(inner.span));
let inner_ty = self.next_ty_var(TypeVariableOrigin {
kind: TypeVariableOriginKind::TypeInference,
span: inner.span,
});
let uniq_ty = tcx.mk_box(inner_ty);

if self.check_dereferencable(pat.span, expected, &inner) {
@@ -372,7 +380,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
}
_ => {
let inner_ty = self.next_ty_var(
TypeVariableOrigin::TypeInference(inner.span));
TypeVariableOrigin {
kind: TypeVariableOriginKind::TypeInference,
span: inner.span,
}
);
let mt = ty::TypeAndMut { ty: inner_ty, mutbl: mutbl };
let region = self.next_region_var(infer::PatternRegion(pat.span));
let rptr_ty = tcx.mk_ref(region, mt);
@@ -685,7 +697,10 @@ https://doc.rust-lang.org/reference/types.html#trait-objects");
// arm for inconsistent arms or to the whole match when a `()` type
// is required).
Expectation::ExpectHasType(ety) if ety != self.tcx.mk_unit() => ety,
_ => self.next_ty_var(TypeVariableOrigin::MiscVariable(expr.span)),
_ => self.next_ty_var(TypeVariableOrigin {
kind: TypeVariableOriginKind::MiscVariable,
span: expr.span,
}),
};
CoerceMany::with_coercion_sites(coerce_first, arms)
};
@@ -8,7 +8,7 @@ use hir::def_id::{DefId, LOCAL_CRATE};
use rustc::ty::adjustment::{Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability};
use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
use rustc::{infer, traits};
use rustc::infer::type_variable::TypeVariableOrigin;
use rustc::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use rustc_target::spec::abi;
use syntax::ast::Ident;
use syntax_pos::Span;
@@ -193,9 +193,12 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
let opt_input_types = opt_arg_exprs.map(|arg_exprs| [self.tcx.mk_tup(
arg_exprs
.iter()
.map(|e| self.next_ty_var(
TypeVariableOrigin::TypeInference(e.span)
))
.map(|e| {
self.next_ty_var(TypeVariableOrigin {
kind: TypeVariableOriginKind::TypeInference,
span: e.span,
})
})
)]);
let opt_input_types = opt_input_types.as_ref().map(AsRef::as_ref);

@@ -7,7 +7,7 @@ use crate::middle::region;
use rustc::hir::def_id::DefId;
use rustc::infer::{InferOk, InferResult};
use rustc::infer::LateBoundRegionConversionTime;
use rustc::infer::type_variable::TypeVariableOrigin;
use rustc::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use rustc::traits::Obligation;
use rustc::traits::error_reporting::ArgKind;
use rustc::ty::{self, Ty, GenericParamDefKind};
@@ -102,7 +102,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
span_bug!(expr.span, "closure has lifetime param")
}
GenericParamDefKind::Type { .. } => {
self.infcx.next_ty_var(TypeVariableOrigin::ClosureSynthetic(expr.span)).into()
self.infcx.next_ty_var(TypeVariableOrigin {
kind: TypeVariableOriginKind::ClosureSynthetic,
span: expr.span,
}).into()
}
GenericParamDefKind::Const => {
span_bug!(expr.span, "closure has const param")

0 comments on commit 4dcc49c

Please sign in to comment.
You can’t perform that action at this time.