Skip to content

Commit 1558ae7

Browse files
committed
Auto merge of #51880 - varkor:generics-hir-generalisation-followup, r=eddyb
The Great Generics Generalisation: HIR Followup Addresses the final comments in #48149. r? @eddyb, but there are a few things I have yet to clean up. Making the PR now to more easily see when things break. cc @yodaldevoid
2 parents bf1e461 + ee9bd0f commit 1558ae7

File tree

57 files changed

+973
-801
lines changed

Some content is hidden

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

57 files changed

+973
-801
lines changed

src/librustc/hir/mod.rs

+25-4
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,13 @@ impl GenericArg {
401401
GenericArg::Type(t) => t.span,
402402
}
403403
}
404+
405+
pub fn id(&self) -> NodeId {
406+
match self {
407+
GenericArg::Lifetime(l) => l.id,
408+
GenericArg::Type(t) => t.id,
409+
}
410+
}
404411
}
405412

406413
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
@@ -445,6 +452,22 @@ impl GenericArgs {
445452
}
446453
bug!("GenericArgs::inputs: not a `Fn(T) -> U`");
447454
}
455+
456+
pub fn own_counts(&self) -> GenericParamCount {
457+
// We could cache this as a property of `GenericParamCount`, but
458+
// the aim is to refactor this away entirely eventually and the
459+
// presence of this method will be a constant reminder.
460+
let mut own_counts: GenericParamCount = Default::default();
461+
462+
for arg in &self.args {
463+
match arg {
464+
GenericArg::Lifetime(_) => own_counts.lifetimes += 1,
465+
GenericArg::Type(_) => own_counts.types += 1,
466+
};
467+
}
468+
469+
own_counts
470+
}
448471
}
449472

450473
/// A modifier on a bound, currently this is only used for `?Sized`, where the
@@ -503,6 +526,7 @@ pub struct GenericParam {
503526
pub kind: GenericParamKind,
504527
}
505528

529+
#[derive(Default)]
506530
pub struct GenericParamCount {
507531
pub lifetimes: usize,
508532
pub types: usize,
@@ -533,10 +557,7 @@ impl Generics {
533557
// We could cache this as a property of `GenericParamCount`, but
534558
// the aim is to refactor this away entirely eventually and the
535559
// presence of this method will be a constant reminder.
536-
let mut own_counts = GenericParamCount {
537-
lifetimes: 0,
538-
types: 0,
539-
};
560+
let mut own_counts: GenericParamCount = Default::default();
540561

541562
for param in &self.params {
542563
match param.kind {

src/librustc/middle/reachable.rs

+5-18
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use hir::map as hir_map;
2020
use hir::def::Def;
2121
use hir::def_id::{DefId, CrateNum};
2222
use rustc_data_structures::sync::Lrc;
23-
use ty::{self, TyCtxt, GenericParamDefKind};
23+
use ty::{self, TyCtxt};
2424
use ty::query::Providers;
2525
use middle::privacy;
2626
use session::config;
@@ -34,18 +34,6 @@ use hir::intravisit::{Visitor, NestedVisitorMap};
3434
use hir::itemlikevisit::ItemLikeVisitor;
3535
use hir::intravisit;
3636

37-
// Returns true if the given set of generics implies that the item it's
38-
// associated with must be inlined.
39-
fn generics_require_inlining(generics: &ty::Generics) -> bool {
40-
for param in &generics.params {
41-
match param.kind {
42-
GenericParamDefKind::Lifetime { .. } => {}
43-
GenericParamDefKind::Type { .. } => return true,
44-
}
45-
}
46-
false
47-
}
48-
4937
// Returns true if the given item must be inlined because it may be
5038
// monomorphized or it was marked with `#[inline]`. This will only return
5139
// true for functions.
@@ -60,7 +48,7 @@ fn item_might_be_inlined(tcx: TyCtxt<'a, 'tcx, 'tcx>,
6048
hir::ItemKind::Impl(..) |
6149
hir::ItemKind::Fn(..) => {
6250
let generics = tcx.generics_of(tcx.hir.local_def_id(item.id));
63-
generics_require_inlining(generics)
51+
generics.requires_monomorphization(tcx)
6452
}
6553
_ => false,
6654
}
@@ -71,7 +59,7 @@ fn method_might_be_inlined<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
7159
impl_src: DefId) -> bool {
7260
let codegen_fn_attrs = tcx.codegen_fn_attrs(impl_item.hir_id.owner_def_id());
7361
let generics = tcx.generics_of(tcx.hir.local_def_id(impl_item.id));
74-
if codegen_fn_attrs.requests_inline() || generics_require_inlining(generics) {
62+
if codegen_fn_attrs.requests_inline() || generics.requires_monomorphization(tcx) {
7563
return true
7664
}
7765
if let Some(impl_node_id) = tcx.hir.as_local_node_id(impl_src) {
@@ -189,8 +177,7 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
189177
hir::ImplItemKind::Method(..) => {
190178
let attrs = self.tcx.codegen_fn_attrs(def_id);
191179
let generics = self.tcx.generics_of(def_id);
192-
if generics_require_inlining(&generics) ||
193-
attrs.requests_inline() {
180+
if generics.requires_monomorphization(self.tcx) || attrs.requests_inline() {
194181
true
195182
} else {
196183
let impl_did = self.tcx
@@ -203,7 +190,7 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
203190
match self.tcx.hir.expect_item(impl_node_id).node {
204191
hir::ItemKind::Impl(..) => {
205192
let generics = self.tcx.generics_of(impl_did);
206-
generics_require_inlining(&generics)
193+
generics.requires_monomorphization(self.tcx)
207194
}
208195
_ => false
209196
}

src/librustc/ty/mod.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,7 @@ impl GenericParamDef {
881881
}
882882
}
883883

884+
#[derive(Default)]
884885
pub struct GenericParamCount {
885886
pub lifetimes: usize,
886887
pub types: usize,
@@ -913,15 +914,12 @@ impl<'a, 'gcx, 'tcx> Generics {
913914
// We could cache this as a property of `GenericParamCount`, but
914915
// the aim is to refactor this away entirely eventually and the
915916
// presence of this method will be a constant reminder.
916-
let mut own_counts = GenericParamCount {
917-
lifetimes: 0,
918-
types: 0,
919-
};
917+
let mut own_counts: GenericParamCount = Default::default();
920918

921919
for param in &self.params {
922920
match param.kind {
923921
GenericParamDefKind::Lifetime => own_counts.lifetimes += 1,
924-
GenericParamDefKind::Type {..} => own_counts.types += 1,
922+
GenericParamDefKind::Type { .. } => own_counts.types += 1,
925923
};
926924
}
927925

@@ -931,7 +929,7 @@ impl<'a, 'gcx, 'tcx> Generics {
931929
pub fn requires_monomorphization(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> bool {
932930
for param in &self.params {
933931
match param.kind {
934-
GenericParamDefKind::Type {..} => return true,
932+
GenericParamDefKind::Type { .. } => return true,
935933
GenericParamDefKind::Lifetime => {}
936934
}
937935
}

src/librustc/ty/subst.rs

-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,6 @@ impl<'a, 'gcx, 'tcx> Substs<'tcx> {
231231
mk_kind: &mut F)
232232
where F: FnMut(&ty::GenericParamDef, &[Kind<'tcx>]) -> Kind<'tcx>
233233
{
234-
235234
if let Some(def_id) = defs.parent {
236235
let parent_defs = tcx.generics_of(def_id);
237236
Substs::fill_item(substs, tcx, parent_defs, mk_kind);

src/librustc/util/ppaux.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,7 @@ impl PrintContext {
262262
let verbose = self.is_verbose;
263263
let mut num_supplied_defaults = 0;
264264
let mut has_self = false;
265-
let mut own_counts = GenericParamCount {
266-
lifetimes: 0,
267-
types: 0,
268-
};
265+
let mut own_counts: GenericParamCount = Default::default();
269266
let mut is_value_path = false;
270267
let fn_trait_kind = ty::tls::with(|tcx| {
271268
// Unfortunately, some kinds of items (e.g., closures) don't have

src/librustc_lint/types.rs

-8
Original file line numberDiff line numberDiff line change
@@ -819,14 +819,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for VariantSizeDifferences {
819819
fn check_item(&mut self, cx: &LateContext, it: &hir::Item) {
820820
if let hir::ItemKind::Enum(ref enum_definition, _) = it.node {
821821
let item_def_id = cx.tcx.hir.local_def_id(it.id);
822-
let generics = cx.tcx.generics_of(item_def_id);
823-
for param in &generics.params {
824-
match param.kind {
825-
ty::GenericParamDefKind::Lifetime { .. } => {},
826-
ty::GenericParamDefKind::Type { .. } => return,
827-
}
828-
}
829-
// Sizes only make sense for non-generic types.
830822
let t = cx.tcx.type_of(item_def_id);
831823
let ty = cx.tcx.erase_regions(&t);
832824
match cx.layout_of(ty) {

src/librustc_metadata/encoder.rs

+12-13
Original file line numberDiff line numberDiff line change
@@ -1262,12 +1262,9 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
12621262
hir::ItemKind::Const(..) => self.encode_optimized_mir(def_id),
12631263
hir::ItemKind::Fn(_, header, ..) => {
12641264
let generics = tcx.generics_of(def_id);
1265-
let has_types = generics.params.iter().any(|param| match param.kind {
1266-
ty::GenericParamDefKind::Type { .. } => true,
1267-
_ => false,
1268-
});
12691265
let needs_inline =
1270-
(has_types || tcx.codegen_fn_attrs(def_id).requests_inline()) &&
1266+
(generics.requires_monomorphization(tcx) ||
1267+
tcx.codegen_fn_attrs(def_id).requests_inline()) &&
12711268
!self.metadata_output_only();
12721269
let always_encode_mir = self.tcx.sess.opts.debugging_opts.always_encode_mir;
12731270
if needs_inline
@@ -1683,15 +1680,17 @@ impl<'a, 'b, 'tcx> IndexBuilder<'a, 'b, 'tcx> {
16831680
}
16841681

16851682
fn encode_info_for_generics(&mut self, generics: &hir::Generics) {
1686-
generics.params.iter().for_each(|param| match param.kind {
1687-
hir::GenericParamKind::Lifetime { .. } => {}
1688-
hir::GenericParamKind::Type { ref default, .. } => {
1689-
let def_id = self.tcx.hir.local_def_id(param.id);
1690-
let has_default = Untracked(default.is_some());
1691-
let encode_info = IsolatedEncoder::encode_info_for_ty_param;
1692-
self.record(def_id, encode_info, (def_id, has_default));
1683+
for param in &generics.params {
1684+
match param.kind {
1685+
hir::GenericParamKind::Lifetime { .. } => {}
1686+
hir::GenericParamKind::Type { ref default, .. } => {
1687+
let def_id = self.tcx.hir.local_def_id(param.id);
1688+
let has_default = Untracked(default.is_some());
1689+
let encode_info = IsolatedEncoder::encode_info_for_ty_param;
1690+
self.record(def_id, encode_info, (def_id, has_default));
1691+
}
16931692
}
1694-
});
1693+
}
16951694
}
16961695

16971696
fn encode_info_for_ty(&mut self, ty: &hir::Ty) {

src/librustc_passes/ast_validation.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -539,10 +539,9 @@ impl<'a> Visitor<'a> for NestedImplTraitVisitor<'a> {
539539
fn visit_generic_args(&mut self, _: Span, generic_args: &'a GenericArgs) {
540540
match *generic_args {
541541
GenericArgs::AngleBracketed(ref data) => {
542-
data.args.iter().for_each(|arg| match arg {
543-
GenericArg::Type(ty) => self.visit_ty(ty),
544-
_ => {}
545-
});
542+
for arg in &data.args {
543+
self.visit_generic_arg(arg)
544+
}
546545
for type_binding in &data.bindings {
547546
// Type bindings such as `Item=impl Debug` in `Iterator<Item=Debug>`
548547
// are allowed to contain nested `impl Trait`.

src/librustc_privacy/lib.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ extern crate rustc_typeck;
2323
extern crate syntax_pos;
2424
extern crate rustc_data_structures;
2525

26-
use rustc::hir::{self, GenericParamKind, PatKind};
26+
use rustc::hir::{self, PatKind};
2727
use rustc::hir::def::Def;
2828
use rustc::hir::def_id::{CRATE_DEF_INDEX, LOCAL_CRATE, CrateNum, DefId};
2929
use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
@@ -1270,14 +1270,11 @@ impl<'a, 'tcx> Visitor<'tcx> for ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> {
12701270
}
12711271

12721272
fn visit_generics(&mut self, generics: &'tcx hir::Generics) {
1273-
generics.params.iter().for_each(|param| match param.kind {
1274-
GenericParamKind::Lifetime { .. } => {}
1275-
GenericParamKind::Type { .. } => {
1276-
for bound in &param.bounds {
1277-
self.check_generic_bound(bound);
1278-
}
1273+
for param in &generics.params {
1274+
for bound in &param.bounds {
1275+
self.check_generic_bound(bound);
12791276
}
1280-
});
1277+
}
12811278
for predicate in &generics.where_clause.predicates {
12821279
match predicate {
12831280
&hir::WherePredicate::BoundPredicate(ref bound_pred) => {

src/librustc_resolve/lib.rs

+27-24
Original file line numberDiff line numberDiff line change
@@ -822,11 +822,12 @@ impl<'a, 'tcx, 'cl> Visitor<'tcx> for Resolver<'a, 'cl> {
822822
.filter_map(|param| match param.kind {
823823
GenericParamKind::Lifetime { .. } => None,
824824
GenericParamKind::Type { ref default, .. } => {
825-
if found_default || default.is_some() {
826-
found_default = true;
827-
return Some((Ident::with_empty_ctxt(param.ident.name), Def::Err));
825+
found_default |= default.is_some();
826+
if found_default {
827+
Some((Ident::with_empty_ctxt(param.ident.name), Def::Err))
828+
} else {
829+
None
828830
}
829-
None
830831
}
831832
}));
832833

@@ -2339,28 +2340,30 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
23392340
HasTypeParameters(generics, rib_kind) => {
23402341
let mut function_type_rib = Rib::new(rib_kind);
23412342
let mut seen_bindings = FxHashMap();
2342-
generics.params.iter().for_each(|param| match param.kind {
2343-
GenericParamKind::Lifetime { .. } => {}
2344-
GenericParamKind::Type { .. } => {
2345-
let ident = param.ident.modern();
2346-
debug!("with_type_parameter_rib: {}", param.id);
2347-
2348-
if seen_bindings.contains_key(&ident) {
2349-
let span = seen_bindings.get(&ident).unwrap();
2350-
let err = ResolutionError::NameAlreadyUsedInTypeParameterList(
2351-
ident.name,
2352-
span,
2353-
);
2354-
resolve_error(self, param.ident.span, err);
2355-
}
2356-
seen_bindings.entry(ident).or_insert(param.ident.span);
2343+
for param in &generics.params {
2344+
match param.kind {
2345+
GenericParamKind::Lifetime { .. } => {}
2346+
GenericParamKind::Type { .. } => {
2347+
let ident = param.ident.modern();
2348+
debug!("with_type_parameter_rib: {}", param.id);
2349+
2350+
if seen_bindings.contains_key(&ident) {
2351+
let span = seen_bindings.get(&ident).unwrap();
2352+
let err = ResolutionError::NameAlreadyUsedInTypeParameterList(
2353+
ident.name,
2354+
span,
2355+
);
2356+
resolve_error(self, param.ident.span, err);
2357+
}
2358+
seen_bindings.entry(ident).or_insert(param.ident.span);
23572359

2358-
// Plain insert (no renaming).
2359-
let def = Def::TyParam(self.definitions.local_def_id(param.id));
2360-
function_type_rib.bindings.insert(ident, def);
2361-
self.record_def(param.id, PathResolution::new(def));
2360+
// Plain insert (no renaming).
2361+
let def = Def::TyParam(self.definitions.local_def_id(param.id));
2362+
function_type_rib.bindings.insert(ident, def);
2363+
self.record_def(param.id, PathResolution::new(def));
2364+
}
23622365
}
2363-
});
2366+
}
23642367
self.ribs[TypeNS].push(function_type_rib);
23652368
}
23662369

0 commit comments

Comments
 (0)