Skip to content

Commit 56e541d

Browse files
committed
Auto merge of #50801 - eddyb:param-things, r=nikomatsakis
Quick refactoring around Substs & friends. r? @nikomatsakis
2 parents 6e6a4b1 + 73f6210 commit 56e541d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+246
-281
lines changed

src/librustc/ich/impls_ty.rs

+20-12
Original file line numberDiff line numberDiff line change
@@ -739,8 +739,7 @@ impl<'a> HashStable<StableHashingContext<'a>> for ty::Generics {
739739
ref parent_count,
740740
ref params,
741741

742-
// Reverse map to each `TypeParamDef`'s `index` field, from
743-
// `def_id.index` (`def_id.krate` is the same as the item's).
742+
// Reverse map to each param's `index` field, from its `def_id`.
744743
param_def_id_to_index: _, // Don't hash this
745744
has_self,
746745
has_late_bound_regions,
@@ -754,11 +753,6 @@ impl<'a> HashStable<StableHashingContext<'a>> for ty::Generics {
754753
}
755754
}
756755

757-
impl_stable_hash_for!(enum ty::GenericParamDefKind {
758-
Lifetime,
759-
Type(ty)
760-
});
761-
762756
impl_stable_hash_for!(struct ty::GenericParamDef {
763757
name,
764758
def_id,
@@ -767,11 +761,25 @@ impl_stable_hash_for!(struct ty::GenericParamDef {
767761
kind
768762
});
769763

770-
impl_stable_hash_for!(struct ty::TypeParamDef {
771-
has_default,
772-
object_lifetime_default,
773-
synthetic
774-
});
764+
impl<'a> HashStable<StableHashingContext<'a>> for ty::GenericParamDefKind {
765+
fn hash_stable<W: StableHasherResult>(&self,
766+
hcx: &mut StableHashingContext<'a>,
767+
hasher: &mut StableHasher<W>) {
768+
mem::discriminant(self).hash_stable(hcx, hasher);
769+
match *self {
770+
ty::GenericParamDefKind::Lifetime => {}
771+
ty::GenericParamDefKind::Type {
772+
has_default,
773+
ref object_lifetime_default,
774+
ref synthetic,
775+
} => {
776+
has_default.hash_stable(hcx, hasher);
777+
object_lifetime_default.hash_stable(hcx, hasher);
778+
synthetic.hash_stable(hcx, hasher);
779+
}
780+
}
781+
}
782+
}
775783

776784
impl<'a, 'gcx, T> HashStable<StableHashingContext<'a>>
777785
for ::middle::resolve_lifetime::Set1<T>

src/librustc/infer/canonical.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -256,11 +256,11 @@ impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> {
256256

257257
CanonicalTyVarKind::Float => self.tcx.mk_float_var(self.next_float_var_id()),
258258
};
259-
Kind::from(ty)
259+
ty.into()
260260
}
261261

262262
CanonicalVarKind::Region => {
263-
Kind::from(self.next_region_var(RegionVariableOrigin::MiscVariable(span)))
263+
self.next_region_var(RegionVariableOrigin::MiscVariable(span)).into()
264264
}
265265
}
266266
}
@@ -555,7 +555,7 @@ impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for Canonicalizer<'cx, 'gcx, 'tcx>
555555
opportunistically resolved to {:?}",
556556
vid, r
557557
);
558-
let cvar = self.canonical_var(info, Kind::from(r));
558+
let cvar = self.canonical_var(info, r.into());
559559
self.tcx().mk_region(ty::ReCanonical(cvar))
560560
}
561561

@@ -570,7 +570,7 @@ impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for Canonicalizer<'cx, 'gcx, 'tcx>
570570
let info = CanonicalVarInfo {
571571
kind: CanonicalVarKind::Region,
572572
};
573-
let cvar = self.canonical_var(info, Kind::from(r));
573+
let cvar = self.canonical_var(info, r.into());
574574
self.tcx().mk_region(ty::ReCanonical(cvar))
575575
} else {
576576
r
@@ -750,7 +750,7 @@ impl<'cx, 'gcx, 'tcx> Canonicalizer<'cx, 'gcx, 'tcx> {
750750
let info = CanonicalVarInfo {
751751
kind: CanonicalVarKind::Ty(ty_kind),
752752
};
753-
let cvar = self.canonical_var(info, Kind::from(ty_var));
753+
let cvar = self.canonical_var(info, ty_var.into());
754754
self.tcx().mk_infer(ty::InferTy::CanonicalTy(cvar))
755755
}
756756
}

src/librustc/infer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
915915
// region parameter definition.
916916
self.next_region_var(EarlyBoundRegion(span, param.name)).into()
917917
}
918-
GenericParamDefKind::Type(_) => {
918+
GenericParamDefKind::Type {..} => {
919919
// Create a type inference variable for the given
920920
// type parameter definition. The substitutions are
921921
// for actual parameters that may be referred to by

src/librustc/middle/resolve_lifetime.rs

+7-11
Original file line numberDiff line numberDiff line change
@@ -1658,18 +1658,14 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
16581658
self.xcrate_object_lifetime_defaults
16591659
.entry(def_id)
16601660
.or_insert_with(|| {
1661-
tcx.generics_of(def_id)
1662-
.params
1663-
.iter()
1664-
.filter_map(|param| {
1665-
match param.kind {
1666-
GenericParamDefKind::Type(ty) => {
1667-
Some(ty.object_lifetime_default)
1668-
}
1669-
GenericParamDefKind::Lifetime => None,
1661+
tcx.generics_of(def_id).params.iter().filter_map(|param| {
1662+
match param.kind {
1663+
GenericParamDefKind::Type { object_lifetime_default, .. } => {
1664+
Some(object_lifetime_default)
16701665
}
1671-
})
1672-
.collect()
1666+
GenericParamDefKind::Lifetime => None,
1667+
}
1668+
}).collect()
16731669
})
16741670
};
16751671
unsubst

src/librustc/traits/error_reporting.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
383383

384384
for param in generics.params.iter() {
385385
let value = match param.kind {
386-
GenericParamDefKind::Type(_) => {
386+
GenericParamDefKind::Type {..} => {
387387
trait_ref.substs[param.index as usize].to_string()
388388
},
389389
GenericParamDefKind::Lifetime => continue,
@@ -652,14 +652,10 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
652652
&& fallback_has_occurred
653653
{
654654
let predicate = trait_predicate.map_bound(|mut trait_pred| {
655-
{
656-
let trait_ref = &mut trait_pred.trait_ref;
657-
let never_substs = trait_ref.substs;
658-
let mut unit_substs = Vec::with_capacity(never_substs.len());
659-
unit_substs.push(self.tcx.mk_nil().into());
660-
unit_substs.extend(&never_substs[1..]);
661-
trait_ref.substs = self.tcx.intern_substs(&unit_substs);
662-
}
655+
trait_pred.trait_ref.substs = self.tcx.mk_substs_trait(
656+
self.tcx.mk_nil(),
657+
&trait_pred.trait_ref.substs[1..],
658+
);
663659
trait_pred
664660
});
665661
let unit_obligation = Obligation {

src/librustc/traits/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ fn vtable_methods<'a, 'tcx>(
838838
Substs::for_item(tcx, def_id, |param, _| {
839839
match param.kind {
840840
GenericParamDefKind::Lifetime => tcx.types.re_erased.into(),
841-
GenericParamDefKind::Type(_) => {
841+
GenericParamDefKind::Type {..} => {
842842
trait_ref.substs[param.index as usize]
843843
}
844844
}

src/librustc/traits/on_unimplemented.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ impl<'a, 'gcx, 'tcx> OnUnimplementedFormatString {
289289
let generics = tcx.generics_of(trait_ref.def_id);
290290
let generic_map = generics.params.iter().filter_map(|param| {
291291
let value = match param.kind {
292-
GenericParamDefKind::Type(_) => {
292+
GenericParamDefKind::Type {..} => {
293293
trait_ref.substs[param.index as usize].to_string()
294294
},
295295
GenericParamDefKind::Lifetime => return None

src/librustc/traits/select.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use dep_graph::{DepNodeIndex, DepKind};
3737
use hir::def_id::DefId;
3838
use infer;
3939
use infer::{InferCtxt, InferOk, TypeFreshener};
40-
use ty::subst::{Kind, Subst, Substs};
40+
use ty::subst::{Subst, Substs};
4141
use ty::{self, ToPredicate, ToPolyTraitRef, Ty, TyCtxt, TypeFoldable};
4242
use ty::fast_reject;
4343
use ty::relate::TypeRelation;
@@ -3019,7 +3019,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
30193019
// with a potentially unsized trailing field.
30203020
let params = substs_a.iter().enumerate().map(|(i, &k)| {
30213021
if ty_params.contains(i) {
3022-
Kind::from(tcx.types.err)
3022+
tcx.types.err.into()
30233023
} else {
30243024
k
30253025
}
@@ -3058,24 +3058,24 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
30583058
obligation.predicate.def_id(),
30593059
obligation.recursion_depth + 1,
30603060
inner_source,
3061-
&[inner_target]));
3061+
&[inner_target.into()]));
30623062
}
30633063

30643064
// (.., T) -> (.., U).
30653065
(&ty::TyTuple(tys_a), &ty::TyTuple(tys_b)) => {
30663066
assert_eq!(tys_a.len(), tys_b.len());
30673067

30683068
// The last field of the tuple has to exist.
3069-
let (a_last, a_mid) = if let Some(x) = tys_a.split_last() {
3069+
let (&a_last, a_mid) = if let Some(x) = tys_a.split_last() {
30703070
x
30713071
} else {
30723072
return Err(Unimplemented);
30733073
};
3074-
let b_last = tys_b.last().unwrap();
3074+
let &b_last = tys_b.last().unwrap();
30753075

30763076
// Check that the source tuple with the target's
30773077
// last element is equal to the target.
3078-
let new_tuple = tcx.mk_tup(a_mid.iter().chain(Some(b_last)));
3078+
let new_tuple = tcx.mk_tup(a_mid.iter().cloned().chain(iter::once(b_last)));
30793079
let InferOk { obligations, .. } =
30803080
self.infcx.at(&obligation.cause, obligation.param_env)
30813081
.eq(target, new_tuple)
@@ -3089,7 +3089,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
30893089
obligation.predicate.def_id(),
30903090
obligation.recursion_depth + 1,
30913091
a_last,
3092-
&[b_last]));
3092+
&[b_last.into()]));
30933093
}
30943094

30953095
_ => bug!()

src/librustc/traits/util.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
use hir::def_id::DefId;
12-
use ty::subst::{Subst, Substs};
12+
use ty::subst::{Kind, Subst, Substs};
1313
use ty::{self, Ty, TyCtxt, ToPredicate, ToPolyTraitRef};
1414
use ty::outlives::Component;
1515
use util::nodemap::FxHashSet;
@@ -430,13 +430,13 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
430430
cause: ObligationCause<'tcx>,
431431
trait_def_id: DefId,
432432
recursion_depth: usize,
433-
param_ty: Ty<'tcx>,
434-
ty_params: &[Ty<'tcx>])
433+
self_ty: Ty<'tcx>,
434+
params: &[Kind<'tcx>])
435435
-> PredicateObligation<'tcx>
436436
{
437437
let trait_ref = ty::TraitRef {
438438
def_id: trait_def_id,
439-
substs: self.mk_substs_trait(param_ty, ty_params)
439+
substs: self.mk_substs_trait(self_ty, params)
440440
};
441441
predicate_for_trait_ref(cause, param_env, trait_ref, recursion_depth)
442442
}
@@ -512,7 +512,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
512512
};
513513
let trait_ref = ty::TraitRef {
514514
def_id: fn_trait_def_id,
515-
substs: self.mk_substs_trait(self_ty, &[arguments_tuple]),
515+
substs: self.mk_substs_trait(self_ty, &[arguments_tuple.into()]),
516516
};
517517
ty::Binder::bind((trait_ref, sig.skip_binder().output()))
518518
}

src/librustc/ty/context.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -2329,11 +2329,11 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
23292329
let substs = Substs::for_item(self, def_id, |param, substs| {
23302330
match param.kind {
23312331
GenericParamDefKind::Lifetime => bug!(),
2332-
GenericParamDefKind::Type(ty_param) => {
2332+
GenericParamDefKind::Type { has_default, .. } => {
23332333
if param.index == 0 {
23342334
ty.into()
23352335
} else {
2336-
assert!(ty_param.has_default);
2336+
assert!(has_default);
23372337
self.type_of(param.def_id).subst(self, substs).into()
23382338
}
23392339
}
@@ -2477,7 +2477,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
24772477
GenericParamDefKind::Lifetime => {
24782478
self.mk_region(ty::ReEarlyBound(param.to_early_bound_region_data())).into()
24792479
}
2480-
GenericParamDefKind::Type(_) => self.mk_ty_param(param.index, param.name).into(),
2480+
GenericParamDefKind::Type {..} => self.mk_ty_param(param.index, param.name).into(),
24812481
}
24822482
}
24832483

@@ -2584,11 +2584,11 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
25842584
}
25852585

25862586
pub fn mk_substs_trait(self,
2587-
s: Ty<'tcx>,
2588-
t: &[Ty<'tcx>])
2587+
self_ty: Ty<'tcx>,
2588+
rest: &[Kind<'tcx>])
25892589
-> &'tcx Substs<'tcx>
25902590
{
2591-
self.mk_substs(iter::once(s).chain(t.into_iter().cloned()).map(Kind::from))
2591+
self.mk_substs(iter::once(self_ty.into()).chain(rest.iter().cloned()))
25922592
}
25932593

25942594
pub fn mk_clauses<I: InternAs<[Clause<'tcx>], Clauses<'tcx>>>(self, iter: I) -> I::Output {
@@ -2600,7 +2600,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
26002600
}
26012601

26022602
pub fn mk_goal(self, goal: Goal<'tcx>) -> &'tcx Goal {
2603-
&self.mk_goals(iter::once(goal))[0]
2603+
&self.intern_goals(&[goal])[0]
26042604
}
26052605

26062606
pub fn lint_node<S: Into<MultiSpan>>(self,

src/librustc/ty/instance.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
use hir::def_id::DefId;
1212
use ty::{self, Ty, TypeFoldable, Substs, TyCtxt};
13-
use ty::subst::Kind;
1413
use traits;
1514
use rustc_target::spec::abi::Abi;
1615
use util::ppaux;
@@ -361,7 +360,7 @@ fn fn_once_adapter_instance<'a, 'tcx>(
361360
let sig = substs.closure_sig(closure_did, tcx);
362361
let sig = tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
363362
assert_eq!(sig.inputs().len(), 1);
364-
let substs = tcx.mk_substs([Kind::from(self_ty), sig.inputs()[0].into()].iter().cloned());
363+
let substs = tcx.mk_substs_trait(self_ty, &[sig.inputs()[0].into()]);
365364

366365
debug!("fn_once_adapter_shim: self_ty={:?} sig={:?}", self_ty, sig);
367366
Instance { def, substs }

0 commit comments

Comments
 (0)