Skip to content

Commit 4343c20

Browse files
committed
Use ty::Generics instead of hir::Generics for various checks
1 parent dde942b commit 4343c20

File tree

4 files changed

+40
-29
lines changed

4 files changed

+40
-29
lines changed

src/librustc/middle/reachable.rs

Lines changed: 23 additions & 16 deletions
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};
23+
use ty::{self, TyCtxt, GenericParamDefKind};
2424
use ty::query::Providers;
2525
use middle::privacy;
2626
use session::config;
@@ -29,19 +29,19 @@ use util::nodemap::{NodeSet, FxHashSet};
2929
use rustc_target::spec::abi::Abi;
3030
use syntax::ast;
3131
use syntax::attr;
32-
use hir::{self, GenericParamKind};
32+
use hir;
3333
use hir::def_id::LOCAL_CRATE;
3434
use hir::intravisit::{Visitor, NestedVisitorMap};
3535
use hir::itemlikevisit::ItemLikeVisitor;
3636
use hir::intravisit;
3737

3838
// Returns true if the given set of generics implies that the item it's
3939
// associated with must be inlined.
40-
fn generics_require_inlining(generics: &hir::Generics) -> bool {
40+
fn generics_require_inlining(generics: &ty::Generics) -> bool {
4141
for param in &generics.params {
4242
match param.kind {
43-
GenericParamKind::Lifetime { .. } => {}
44-
GenericParamKind::Type { .. } => return true,
43+
GenericParamDefKind::Lifetime { .. } => {}
44+
GenericParamDefKind::Type { .. } => return true,
4545
}
4646
}
4747
false
@@ -50,14 +50,17 @@ fn generics_require_inlining(generics: &hir::Generics) -> bool {
5050
// Returns true if the given item must be inlined because it may be
5151
// monomorphized or it was marked with `#[inline]`. This will only return
5252
// true for functions.
53-
fn item_might_be_inlined(item: &hir::Item, attrs: CodegenFnAttrs) -> bool {
53+
fn item_might_be_inlined(tcx: TyCtxt<'a, 'tcx, 'tcx>,
54+
item: &hir::Item,
55+
attrs: CodegenFnAttrs) -> bool {
5456
if attrs.requests_inline() {
5557
return true
5658
}
5759

5860
match item.node {
59-
hir::ItemImpl(_, _, _, ref generics, ..) |
60-
hir::ItemFn(.., ref generics, _) => {
61+
hir::ItemImpl(..) |
62+
hir::ItemFn(..) => {
63+
let generics = tcx.generics_of(tcx.hir.local_def_id(item.id));
6164
generics_require_inlining(generics)
6265
}
6366
_ => false,
@@ -68,14 +71,14 @@ fn method_might_be_inlined<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
6871
impl_item: &hir::ImplItem,
6972
impl_src: DefId) -> bool {
7073
let codegen_fn_attrs = tcx.codegen_fn_attrs(impl_item.hir_id.owner_def_id());
71-
if codegen_fn_attrs.requests_inline() ||
72-
generics_require_inlining(&impl_item.generics) {
74+
let generics = tcx.generics_of(tcx.hir.local_def_id(impl_item.id));
75+
if codegen_fn_attrs.requests_inline() || generics_require_inlining(generics) {
7376
return true
7477
}
7578
if let Some(impl_node_id) = tcx.hir.as_local_node_id(impl_src) {
7679
match tcx.hir.find(impl_node_id) {
7780
Some(hir_map::NodeItem(item)) =>
78-
item_might_be_inlined(&item, codegen_fn_attrs),
81+
item_might_be_inlined(tcx, &item, codegen_fn_attrs),
7982
Some(..) | None =>
8083
span_bug!(impl_item.span, "impl did is not an item")
8184
}
@@ -169,7 +172,7 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
169172
Some(hir_map::NodeItem(item)) => {
170173
match item.node {
171174
hir::ItemFn(..) =>
172-
item_might_be_inlined(&item, self.tcx.codegen_fn_attrs(def_id)),
175+
item_might_be_inlined(self.tcx, &item, self.tcx.codegen_fn_attrs(def_id)),
173176
_ => false,
174177
}
175178
}
@@ -186,7 +189,8 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
186189
hir::ImplItemKind::Const(..) => true,
187190
hir::ImplItemKind::Method(..) => {
188191
let attrs = self.tcx.codegen_fn_attrs(def_id);
189-
if generics_require_inlining(&impl_item.generics) ||
192+
let generics = self.tcx.generics_of(def_id);
193+
if generics_require_inlining(&generics) ||
190194
attrs.requests_inline() {
191195
true
192196
} else {
@@ -198,8 +202,9 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
198202
// does too.
199203
let impl_node_id = self.tcx.hir.as_local_node_id(impl_did).unwrap();
200204
match self.tcx.hir.expect_item(impl_node_id).node {
201-
hir::ItemImpl(_, _, _, ref generics, ..) => {
202-
generics_require_inlining(generics)
205+
hir::ItemImpl(..) => {
206+
let generics = self.tcx.generics_of(impl_did);
207+
generics_require_inlining(&generics)
203208
}
204209
_ => false
205210
}
@@ -257,7 +262,9 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
257262
match item.node {
258263
hir::ItemFn(.., body) => {
259264
let def_id = self.tcx.hir.local_def_id(item.id);
260-
if item_might_be_inlined(&item, self.tcx.codegen_fn_attrs(def_id)) {
265+
if item_might_be_inlined(self.tcx,
266+
&item,
267+
self.tcx.codegen_fn_attrs(def_id)) {
261268
self.visit_nested_body(body);
262269
}
263270
}

src/librustc_lint/types.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -810,15 +810,16 @@ impl LintPass for VariantSizeDifferences {
810810

811811
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for VariantSizeDifferences {
812812
fn check_item(&mut self, cx: &LateContext, it: &hir::Item) {
813-
if let hir::ItemEnum(ref enum_definition, ref generics) = it.node {
813+
if let hir::ItemEnum(ref enum_definition, _) = it.node {
814+
let item_def_id = cx.tcx.hir.local_def_id(it.id);
815+
let generics = cx.tcx.generics_of(item_def_id);
814816
for param in &generics.params {
815817
match param.kind {
816-
hir::GenericParamKind::Lifetime { .. } => {},
817-
hir::GenericParamKind::Type { .. } => return,
818+
ty::GenericParamDefKind::Lifetime { .. } => {},
819+
ty::GenericParamDefKind::Type { .. } => return,
818820
}
819821
}
820822
// Sizes only make sense for non-generic types.
821-
let item_def_id = cx.tcx.hir.local_def_id(it.id);
822823
let t = cx.tcx.type_of(item_def_id);
823824
let ty = cx.tcx.erase_regions(&t);
824825
match cx.layout_of(ty) {

src/librustc_metadata/encoder.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,9 +1235,10 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
12351235
self.encode_optimized_mir(def_id)
12361236
}
12371237
hir::ItemConst(..) => self.encode_optimized_mir(def_id),
1238-
hir::ItemFn(_, _, constness, _, ref generics, _) => {
1238+
hir::ItemFn(_, _, constness, ..) => {
1239+
let generics = tcx.generics_of(def_id);
12391240
let has_types = generics.params.iter().any(|param| match param.kind {
1240-
hir::GenericParamKind::Type { .. } => true,
1241+
ty::GenericParamDefKind::Type { .. } => true,
12411242
_ => false,
12421243
});
12431244
let needs_inline =

src/librustc_typeck/check/mod.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,10 +1261,11 @@ pub fn check_item_type<'a,'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, it: &'tcx hir::Item
12611261
hir::ItemUnion(..) => {
12621262
check_union(tcx, it.id, it.span);
12631263
}
1264-
hir::ItemTy(_, ref generics) => {
1264+
hir::ItemTy(..) => {
12651265
let def_id = tcx.hir.local_def_id(it.id);
12661266
let pty_ty = tcx.type_of(def_id);
1267-
check_bounds_are_used(tcx, generics, pty_ty);
1267+
let generics = tcx.generics_of(def_id);
1268+
check_bounds_are_used(tcx, &generics, pty_ty);
12681269
}
12691270
hir::ItemForeignMod(ref m) => {
12701271
check_abi(tcx, it.span, m.abi);
@@ -5178,7 +5179,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
51785179
}
51795180

51805181
pub fn check_bounds_are_used<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
5181-
generics: &hir::Generics,
5182+
generics: &ty::Generics,
51825183
ty: Ty<'tcx>) {
51835184
let own_counts = generics.own_counts();
51845185
debug!("check_bounds_are_used(n_tps={}, ty={:?})", own_counts.types, ty);
@@ -5202,14 +5203,15 @@ pub fn check_bounds_are_used<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
52025203
}
52035204

52045205
let types = generics.params.iter().filter(|param| match param.kind {
5205-
hir::GenericParamKind::Type { .. } => true,
5206+
ty::GenericParamDefKind::Type { .. } => true,
52065207
_ => false,
52075208
});
52085209
for (&used, param) in types_used.iter().zip(types) {
52095210
if !used {
5210-
struct_span_err!(tcx.sess, param.span, E0091, "type parameter `{}` is unused",
5211-
param.name.name())
5212-
.span_label(param.span, "unused type parameter")
5211+
let id = tcx.hir.as_local_node_id(param.def_id).unwrap();
5212+
let span = tcx.hir.span(id);
5213+
struct_span_err!(tcx.sess, span, E0091, "type parameter `{}` is unused", param.name)
5214+
.span_label(span, "unused type parameter")
52135215
.emit();
52145216
}
52155217
}

0 commit comments

Comments
 (0)