Skip to content

Commit

Permalink
Auto merge of #59331 - varkor:TypeVariableOrigin-refactor, r=eddyb
Browse files Browse the repository at this point in the history
Refactor `TypeVariableOrigin`

Removes some unused variants and extracts the common `Span` field.

As suggested in #59008 (comment).

r? @eddyb
  • Loading branch information
bors committed Jun 6, 2019
2 parents 7cdaffd + 66b840c commit 1bec46c
Show file tree
Hide file tree
Showing 21 changed files with 251 additions and 99 deletions.
18 changes: 14 additions & 4 deletions src/librustc/infer/canonical/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, ConstVariableOriginKind};
use crate::mir::interpret::ConstValue;
use rustc_data_structures::indexed_vec::IndexVec;
use rustc_macros::HashStable;
Expand Down Expand Up @@ -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)
)
}
Expand Down Expand Up @@ -403,10 +407,16 @@ impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> {
CanonicalVarKind::Const(ui) => {
self.next_const_var_in_universe(
self.next_ty_var_in_universe(
TypeVariableOrigin::MiscVariable(span),
TypeVariableOrigin {
kind: TypeVariableOriginKind::MiscVariable,
span,
},
universe_map(ui),
),
ConstVariableOrigin::MiscVariable(span),
ConstVariableOrigin {
kind: ConstVariableOriginKind::MiscVariable,
span,
},
universe_map(ui),
).into()
}
Expand Down
8 changes: 6 additions & 2 deletions src/librustc/infer/combine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ use super::{InferCtxt, MiscVariable, TypeTrace};
use super::lub::Lub;
use super::sub::Sub;
use super::type_variable::TypeVariableValue;
use super::unify_key::{ConstVarValue, ConstVariableValue, ConstVariableOrigin};
use super::unify_key::{ConstVarValue, ConstVariableValue};
use super::unify_key::{ConstVariableOrigin, ConstVariableOriginKind};

use crate::hir::def_id::DefId;
use crate::mir::interpret::ConstValue;
Expand Down Expand Up @@ -165,7 +166,10 @@ impl<'infcx, 'gcx, 'tcx> InferCtxt<'infcx, 'gcx, 'tcx> {
self.const_unification_table
.borrow_mut()
.unify_var_value(vid, ConstVarValue {
origin: ConstVariableOrigin::ConstInference(DUMMY_SP),
origin: ConstVariableOrigin {
kind: ConstVariableOriginKind::ConstInference,
span: DUMMY_SP,
},
val: ConstVariableValue::Known { value },
})
.map_err(|e| const_unification_error(vid_is_expected, e))?;
Expand Down
10 changes: 5 additions & 5 deletions src/librustc/infer/error_reporting/need_type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -122,8 +122,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
let mut printer = ty::print::FmtPrinter::new(self.tcx, &mut s, Namespace::TypeNS);
let ty_vars = self.type_variables.borrow();
let getter = move |ty_vid| {
if let TypeVariableOrigin::TypeParameterDefinition(_, name) =
*ty_vars.var_origin(ty_vid) {
if let TypeVariableOriginKind::TypeParameterDefinition(name) =
ty_vars.var_origin(ty_vid).kind {
return Some(name.to_string());
}
None
Expand Down
12 changes: 9 additions & 3 deletions src/librustc/infer/lattice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
}
Expand Down
26 changes: 20 additions & 6 deletions src/librustc/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ 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::unify_key::{ToType, ConstVariableOrigin};
use self::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use self::unify_key::{ToType, ConstVariableOrigin, ConstVariableOriginKind};

pub mod at;
pub mod canonical;
Expand Down Expand Up @@ -1110,13 +1110,19 @@ 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()
}
GenericParamDefKind::Const { .. } => {
let origin = ConstVariableOrigin::ConstParameterDefinition(span, param.name);
let origin = ConstVariableOrigin {
kind: ConstVariableOriginKind::ConstParameterDefinition(param.name),
span,
};
let const_var_id =
self.const_unification_table
.borrow_mut()
Expand Down Expand Up @@ -1412,8 +1418,16 @@ 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_c = |_, ty| self.next_const_var(ty, ConstVariableOrigin::MiscVariable(span));
let fld_t = |_| {
self.next_ty_var(TypeVariableOrigin {
kind: TypeVariableOriginKind::MiscVariable,
span,
})
};
let fld_c = |_, ty| self.next_const_var(ty, ConstVariableOrigin {
kind: ConstVariableOriginKind:: MiscVariable,
span,
});
self.tcx.replace_bound_vars(value, fld_r, fld_t, fld_c)
}

Expand Down
9 changes: 5 additions & 4 deletions src/librustc/infer/nll_relate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions src/librustc/infer/opaque_types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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!(
Expand Down
9 changes: 6 additions & 3 deletions src/librustc/infer/resolve.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::{InferCtxt, FixupError, FixupResult, Span, type_variable::TypeVariableOrigin};
use super::{InferCtxt, FixupError, FixupResult, Span};
use super::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use crate::mir::interpret::ConstValue;
use crate::ty::{self, Ty, Const, TyCtxt, TypeFoldable, InferConst, TypeFlags};
use crate::ty::fold::{TypeFolder, TypeVisitor};
Expand Down Expand Up @@ -123,8 +124,10 @@ impl<'a, 'gcx, 'tcx> TypeVisitor<'tcx> for UnresolvedTypeFinder<'a, 'gcx, 'tcx>
let ty_var_span =
if let ty::TyVar(ty_vid) = infer_ty {
let ty_vars = self.infcx.type_variables.borrow();
if let TypeVariableOrigin::TypeParameterDefinition(span, _name)
= *ty_vars.var_origin(ty_vid)
if let TypeVariableOrigin {
kind: TypeVariableOriginKind::TypeParameterDefinition(_),
span,
} = *ty_vars.var_origin(ty_vid)
{
Some(span)
} else {
Expand Down
37 changes: 20 additions & 17 deletions src/librustc/infer/type_variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +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),
DivergingStmt(Span),
DivergingBlockExpr(Span),
DivergingFn(Span),
LatticeVariable(Span),
Generalized(ty::TyVid),
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 {
Expand Down
21 changes: 15 additions & 6 deletions src/librustc/infer/unify_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,19 @@ impl ToType for FloatVarValue {

// Generic consts.

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

/// Reasons to create a const inference variable
#[derive(Copy, Clone, Debug)]
pub enum ConstVariableOrigin {
MiscVariable(Span),
ConstInference(Span),
ConstParameterDefinition(Span, InternedString),
SubstitutionPlaceholder(Span),
pub enum ConstVariableOriginKind {
MiscVariable,
ConstInference,
ConstParameterDefinition(InternedString),
SubstitutionPlaceholder,
}

#[derive(Copy, Clone, Debug)]
Expand Down Expand Up @@ -159,7 +165,10 @@ impl<'tcx> UnifyValue for ConstVarValue<'tcx> {
}?;

Ok(ConstVarValue {
origin: ConstVariableOrigin::ConstInference(DUMMY_SP),
origin: ConstVariableOrigin {
kind: ConstVariableOriginKind::ConstInference,
span: DUMMY_SP,
},
val,
})
}
Expand Down
9 changes: 7 additions & 2 deletions src/librustc/traits/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
}
Expand Down
14 changes: 11 additions & 3 deletions src/librustc/traits/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down
7 changes: 5 additions & 2 deletions src/librustc_mir/borrow_check/nll/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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,
Expand Down
Loading

0 comments on commit 1bec46c

Please sign in to comment.