Skip to content

Commit e44fc6c

Browse files
committed
Auto merge of #48523 - varkor:generics-ty-generalisations, r=nikomatsakis
The Great Generics Generalisation: Ty Edition Part of the generic parameter refactoring effort, split off from #48149. Contains the `ty`-relative refactoring. r? @eddyb
2 parents f0fdaba + 5be2bdb commit e44fc6c

Some content is hidden

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

48 files changed

+966
-806
lines changed

src/librustc/hir/lowering.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1459,10 +1459,9 @@ impl<'a> LoweringContext<'a> {
14591459
return n;
14601460
}
14611461
assert!(!def_id.is_local());
1462-
let n = self.cstore
1463-
.item_generics_cloned_untracked(def_id, self.sess)
1464-
.regions
1465-
.len();
1462+
let item_generics =
1463+
self.cstore.item_generics_cloned_untracked(def_id, self.sess);
1464+
let n = item_generics.own_counts().lifetimes;
14661465
self.type_def_lifetime_params.insert(def_id, n);
14671466
n
14681467
});

src/librustc/ich/impls_ty.rs

+16-30
Original file line numberDiff line numberDiff line change
@@ -735,54 +735,40 @@ impl<'a> HashStable<StableHashingContext<'a>> for ty::Generics {
735735
hasher: &mut StableHasher<W>) {
736736
let ty::Generics {
737737
parent,
738-
parent_regions,
739-
parent_types,
740-
ref regions,
741-
ref types,
738+
ref parent_count,
739+
ref params,
742740

743-
// Reverse map to each `TypeParameterDef`'s `index` field, from
741+
// Reverse map to each `TypeParamDef`'s `index` field, from
744742
// `def_id.index` (`def_id.krate` is the same as the item's).
745-
type_param_to_index: _, // Don't hash this
743+
param_def_id_to_index: _, // Don't hash this
746744
has_self,
747745
has_late_bound_regions,
748746
} = *self;
749747

750748
parent.hash_stable(hcx, hasher);
751-
parent_regions.hash_stable(hcx, hasher);
752-
parent_types.hash_stable(hcx, hasher);
753-
regions.hash_stable(hcx, hasher);
754-
types.hash_stable(hcx, hasher);
749+
parent_count.hash_stable(hcx, hasher);
750+
params.hash_stable(hcx, hasher);
755751
has_self.hash_stable(hcx, hasher);
756752
has_late_bound_regions.hash_stable(hcx, hasher);
757753
}
758754
}
759755

760-
impl<'a> HashStable<StableHashingContext<'a>>
761-
for ty::RegionParameterDef {
762-
fn hash_stable<W: StableHasherResult>(&self,
763-
hcx: &mut StableHashingContext<'a>,
764-
hasher: &mut StableHasher<W>) {
765-
let ty::RegionParameterDef {
766-
name,
767-
def_id,
768-
index,
769-
pure_wrt_drop
770-
} = *self;
771-
772-
name.hash_stable(hcx, hasher);
773-
def_id.hash_stable(hcx, hasher);
774-
index.hash_stable(hcx, hasher);
775-
pure_wrt_drop.hash_stable(hcx, hasher);
776-
}
777-
}
756+
impl_stable_hash_for!(enum ty::GenericParamDefKind {
757+
Lifetime,
758+
Type(ty)
759+
});
778760

779-
impl_stable_hash_for!(struct ty::TypeParameterDef {
761+
impl_stable_hash_for!(struct ty::GenericParamDef {
780762
name,
781763
def_id,
782764
index,
765+
pure_wrt_drop,
766+
kind
767+
});
768+
769+
impl_stable_hash_for!(struct ty::TypeParamDef {
783770
has_default,
784771
object_lifetime_default,
785-
pure_wrt_drop,
786772
synthetic
787773
});
788774

src/librustc/infer/anon_types/mod.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use infer::outlives::free_region_map::FreeRegionRelations;
1414
use rustc_data_structures::fx::FxHashMap;
1515
use syntax::ast;
1616
use traits::{self, PredicateObligation};
17-
use ty::{self, Ty, TyCtxt};
17+
use ty::{self, Ty, TyCtxt, GenericParamDefKind};
1818
use ty::fold::{BottomUpFolder, TypeFoldable, TypeFolder};
1919
use ty::outlives::Component;
2020
use ty::subst::{Kind, Substs, UnpackedKind};
@@ -313,12 +313,13 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
313313
// `['a]` for the first impl trait and `'b` for the
314314
// second.
315315
let mut least_region = None;
316-
for region_def in &abstract_type_generics.regions {
317-
// Find the index of this region in the list of substitutions.
318-
let index = region_def.index as usize;
319-
316+
for param in &abstract_type_generics.params {
317+
match param.kind {
318+
GenericParamDefKind::Lifetime => {}
319+
_ => continue
320+
}
320321
// Get the value supplied for this region from the substs.
321-
let subst_arg = anon_defn.substs.region_at(index);
322+
let subst_arg = anon_defn.substs.region_at(param.index as usize);
322323

323324
// Compute the least upper bound of it with the other regions.
324325
debug!("constrain_anon_types: least_region={:?}", least_region);
@@ -616,10 +617,9 @@ impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for ReverseMapper<'cx, 'gcx, 'tcx>
616617
// during trans.
617618

618619
let generics = self.tcx.generics_of(def_id);
619-
let parent_len = generics.parent_count();
620620
let substs = self.tcx.mk_substs(substs.substs.iter().enumerate().map(
621621
|(index, &kind)| {
622-
if index < parent_len {
622+
if index < generics.parent_count {
623623
// Accommodate missing regions in the parent kinds...
624624
self.fold_kind_mapping_missing_regions_to_empty(kind)
625625
} else {

src/librustc/infer/mod.rs

+33-34
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ use hir::def_id::DefId;
2121
use middle::free_region::RegionRelations;
2222
use middle::region;
2323
use middle::lang_items;
24-
use ty::subst::Substs;
24+
use ty::subst::{Kind, Substs};
2525
use ty::{TyVid, IntVid, FloatVid};
26-
use ty::{self, Ty, TyCtxt};
26+
use ty::{self, Ty, TyCtxt, GenericParamDefKind};
2727
use ty::error::{ExpectedFound, TypeError, UnconstrainedNumeric};
2828
use ty::fold::TypeFoldable;
2929
use ty::relate::RelateResult;
@@ -905,34 +905,35 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
905905
self.next_region_var(RegionVariableOrigin::NLL(origin))
906906
}
907907

908-
/// Create a region inference variable for the given
909-
/// region parameter definition.
910-
pub fn region_var_for_def(&self,
911-
span: Span,
912-
def: &ty::RegionParameterDef)
913-
-> ty::Region<'tcx> {
914-
self.next_region_var(EarlyBoundRegion(span, def.name))
915-
}
916-
917-
/// Create a type inference variable for the given
918-
/// type parameter definition. The substitutions are
919-
/// for actual parameters that may be referred to by
920-
/// the default of this type parameter, if it exists.
921-
/// E.g. `struct Foo<A, B, C = (A, B)>(...);` when
922-
/// used in a path such as `Foo::<T, U>::new()` will
923-
/// use an inference variable for `C` with `[T, U]`
924-
/// as the substitutions for the default, `(T, U)`.
925-
pub fn type_var_for_def(&self,
926-
span: Span,
927-
def: &ty::TypeParameterDef)
928-
-> Ty<'tcx> {
929-
let ty_var_id = self.type_variables
930-
.borrow_mut()
931-
.new_var(self.universe(),
932-
false,
933-
TypeVariableOrigin::TypeParameterDefinition(span, def.name));
934-
935-
self.tcx.mk_var(ty_var_id)
908+
pub fn var_for_def(&self,
909+
span: Span,
910+
param: &ty::GenericParamDef)
911+
-> Kind<'tcx> {
912+
match param.kind {
913+
GenericParamDefKind::Lifetime => {
914+
// Create a region inference variable for the given
915+
// region parameter definition.
916+
self.next_region_var(EarlyBoundRegion(span, param.name)).into()
917+
}
918+
GenericParamDefKind::Type(_) => {
919+
// Create a type inference variable for the given
920+
// type parameter definition. The substitutions are
921+
// for actual parameters that may be referred to by
922+
// the default of this type parameter, if it exists.
923+
// E.g. `struct Foo<A, B, C = (A, B)>(...);` when
924+
// used in a path such as `Foo::<T, U>::new()` will
925+
// use an inference variable for `C` with `[T, U]`
926+
// as the substitutions for the default, `(T, U)`.
927+
let ty_var_id =
928+
self.type_variables
929+
.borrow_mut()
930+
.new_var(self.universe(),
931+
false,
932+
TypeVariableOrigin::TypeParameterDefinition(span, param.name));
933+
934+
self.tcx.mk_var(ty_var_id).into()
935+
}
936+
}
936937
}
937938

938939
/// Given a set of generics defined on a type or impl, returns a substitution mapping each
@@ -941,10 +942,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
941942
span: Span,
942943
def_id: DefId)
943944
-> &'tcx Substs<'tcx> {
944-
Substs::for_item(self.tcx, def_id, |def, _| {
945-
self.region_var_for_def(span, def)
946-
}, |def, _| {
947-
self.type_var_for_def(span, def)
945+
Substs::for_item(self.tcx, def_id, |param, _| {
946+
self.var_for_def(span, param)
948947
})
949948
}
950949

src/librustc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#![cfg_attr(stage0, feature(dyn_trait))]
5050
#![feature(from_ref)]
5151
#![feature(fs_read_write)]
52+
#![feature(iterator_find_map)]
5253
#![cfg_attr(windows, feature(libc))]
5354
#![cfg_attr(stage0, feature(macro_lifetime_matcher))]
5455
#![feature(macro_vis_matcher)]

src/librustc/middle/resolve_lifetime.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
2020
use hir::map::Map;
2121
use hir::ItemLocalId;
2222
use hir::LifetimeName;
23-
use ty::{self, TyCtxt};
23+
use ty::{self, TyCtxt, GenericParamDefKind};
2424

2525
use errors::DiagnosticBuilder;
2626
use rustc::lint;
@@ -667,8 +667,8 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
667667
for lt_def in generics.lifetimes() {
668668
let (lt_name, region) = Region::early(&self.tcx.hir, &mut index, &lt_def);
669669
if let hir::LifetimeName::Underscore = lt_name {
670-
// Pick the elided lifetime "definition" if one exists and use it to make an
671-
// elision scope.
670+
// Pick the elided lifetime "definition" if one exists and use it to make
671+
// an elision scope.
672672
elision = Some(region);
673673
} else {
674674
lifetimes.insert(lt_name, region);
@@ -1659,9 +1659,16 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
16591659
.entry(def_id)
16601660
.or_insert_with(|| {
16611661
tcx.generics_of(def_id)
1662-
.types
1662+
.params
16631663
.iter()
1664-
.map(|def| def.object_lifetime_default)
1664+
.filter_map(|param| {
1665+
match param.kind {
1666+
GenericParamDefKind::Type(ty) => {
1667+
Some(ty.object_lifetime_default)
1668+
}
1669+
GenericParamDefKind::Lifetime => None,
1670+
}
1671+
})
16651672
.collect()
16661673
})
16671674
};

src/librustc/traits/auto_trait.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,14 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
222222
});
223223

224224
let names_map: FxHashSet<String> = generics
225-
.regions
225+
.params
226226
.iter()
227-
.map(|l| l.name.to_string())
227+
.filter_map(|param| {
228+
match param.kind {
229+
ty::GenericParamDefKind::Lifetime => Some(param.name.to_string()),
230+
_ => None
231+
}
232+
})
228233
.collect();
229234

230235
let body_ids: FxHashSet<_> = infcx

src/librustc/traits/error_reporting.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use std::fmt;
3636
use syntax::ast;
3737
use session::DiagnosticMessageId;
3838
use ty::{self, AdtKind, ToPredicate, ToPolyTraitRef, Ty, TyCtxt, TypeFoldable};
39+
use ty::GenericParamDefKind;
3940
use ty::error::ExpectedFound;
4041
use ty::fast_reject;
4142
use ty::fold::TypeFolder;
@@ -378,12 +379,15 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
378379
flags.push(("_Self".to_string(), Some(self.tcx.type_of(def.did).to_string())));
379380
}
380381

381-
for param in generics.types.iter() {
382+
for param in generics.params.iter() {
383+
let value = match param.kind {
384+
GenericParamDefKind::Type(_) => {
385+
trait_ref.substs[param.index as usize].to_string()
386+
},
387+
GenericParamDefKind::Lifetime => continue,
388+
};
382389
let name = param.name.to_string();
383-
let ty = trait_ref.substs.type_for_def(param);
384-
let ty_str = ty.to_string();
385-
flags.push((name.clone(),
386-
Some(ty_str.clone())));
390+
flags.push((name, Some(value)));
387391
}
388392

389393
if let Some(true) = self_ty.ty_to_def_id().map(|def_id| def_id.is_local()) {

src/librustc/traits/mod.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use infer::outlives::env::OutlivesEnvironment;
2323
use middle::region;
2424
use middle::const_val::ConstEvalErr;
2525
use ty::subst::Substs;
26-
use ty::{self, AdtKind, Slice, Ty, TyCtxt, TypeFoldable, ToPredicate};
26+
use ty::{self, AdtKind, Slice, Ty, TyCtxt, GenericParamDefKind, TypeFoldable, ToPredicate};
2727
use ty::error::{ExpectedFound, TypeError};
2828
use infer::{InferCtxt};
2929

@@ -841,10 +841,14 @@ fn vtable_methods<'a, 'tcx>(
841841
// the method may have some early-bound lifetimes, add
842842
// regions for those
843843
let substs = trait_ref.map_bound(|trait_ref| {
844-
Substs::for_item(
845-
tcx, def_id,
846-
|_, _| tcx.types.re_erased,
847-
|def, _| trait_ref.substs.type_for_def(def))
844+
Substs::for_item(tcx, def_id, |param, _| {
845+
match param.kind {
846+
GenericParamDefKind::Lifetime => tcx.types.re_erased.into(),
847+
GenericParamDefKind::Type(_) => {
848+
trait_ref.substs[param.index as usize]
849+
}
850+
}
851+
})
848852
});
849853

850854
// the trait type may have higher-ranked lifetimes in it;

src/librustc/traits/object_safety.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
284284
}
285285

286286
// We can't monomorphize things like `fn foo<A>(...)`.
287-
if !self.generics_of(method.def_id).types.is_empty() {
287+
if self.generics_of(method.def_id).own_counts().types != 0 {
288288
return Some(MethodViolationCode::Generic);
289289
}
290290

@@ -387,7 +387,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
387387
}
388388

389389
pub(super) fn is_object_safe_provider<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
390-
trait_def_id: DefId)
391-
-> bool {
390+
trait_def_id: DefId) -> bool {
392391
tcx.object_safety_violations(trait_def_id).is_empty()
393392
}

0 commit comments

Comments
 (0)