Skip to content

Commit ad16f9c

Browse files
committed
Lifted generics into TraitItem and ImplItem from MethodSig -- HIR now matches AST
1 parent ea6b18e commit ad16f9c

File tree

9 files changed

+42
-71
lines changed

9 files changed

+42
-71
lines changed

src/librustc/hir/intravisit.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -773,9 +773,7 @@ pub fn walk_fn_kind<'v, V: Visitor<'v>>(visitor: &mut V, function_kind: FnKind<'
773773
FnKind::ItemFn(_, generics, ..) => {
774774
visitor.visit_generics(generics);
775775
}
776-
FnKind::Method(_, sig, ..) => {
777-
visitor.visit_generics(&sig.generics);
778-
}
776+
FnKind::Method(..) |
779777
FnKind::Closure(_) => {}
780778
}
781779
}
@@ -795,6 +793,7 @@ pub fn walk_fn<'v, V: Visitor<'v>>(visitor: &mut V,
795793
pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v TraitItem) {
796794
visitor.visit_name(trait_item.span, trait_item.name);
797795
walk_list!(visitor, visit_attribute, &trait_item.attrs);
796+
visitor.visit_generics(&trait_item.generics);
798797
match trait_item.node {
799798
TraitItemKind::Const(ref ty, default) => {
800799
visitor.visit_id(trait_item.id);
@@ -803,7 +802,6 @@ pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v Trai
803802
}
804803
TraitItemKind::Method(ref sig, TraitMethod::Required(ref names)) => {
805804
visitor.visit_id(trait_item.id);
806-
visitor.visit_generics(&sig.generics);
807805
visitor.visit_fn_decl(&sig.decl);
808806
for name in names {
809807
visitor.visit_name(name.span, name.node);
@@ -845,6 +843,7 @@ pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplIt
845843
ref vis,
846844
ref defaultness,
847845
ref attrs,
846+
ref generics,
848847
ref node,
849848
span
850849
} = *impl_item;
@@ -853,6 +852,7 @@ pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplIt
853852
visitor.visit_vis(vis);
854853
visitor.visit_defaultness(defaultness);
855854
walk_list!(visitor, visit_attribute, attrs);
855+
visitor.visit_generics(generics);
856856
match *node {
857857
ImplItemKind::Const(ref ty, body) => {
858858
visitor.visit_id(impl_item.id);

src/librustc/hir/lowering.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,6 +1535,7 @@ impl<'a> LoweringContext<'a> {
15351535
hir_id,
15361536
name: this.lower_ident(i.ident),
15371537
attrs: this.lower_attrs(&i.attrs),
1538+
generics: this.lower_generics(&i.generics),
15381539
node: match i.node {
15391540
TraitItemKind::Const(ref ty, ref default) => {
15401541
hir::TraitItemKind::Const(this.lower_ty(ty),
@@ -1544,15 +1545,15 @@ impl<'a> LoweringContext<'a> {
15441545
}
15451546
TraitItemKind::Method(ref sig, None) => {
15461547
let names = this.lower_fn_args_to_names(&sig.decl);
1547-
hir::TraitItemKind::Method(this.lower_method_sig(&i.generics, sig),
1548+
hir::TraitItemKind::Method(this.lower_method_sig(sig),
15481549
hir::TraitMethod::Required(names))
15491550
}
15501551
TraitItemKind::Method(ref sig, Some(ref body)) => {
15511552
let body_id = this.lower_body(Some(&sig.decl), |this| {
15521553
let body = this.lower_block(body, false);
15531554
this.expr_block(body, ThinVec::new())
15541555
});
1555-
hir::TraitItemKind::Method(this.lower_method_sig(&i.generics, sig),
1556+
hir::TraitItemKind::Method(this.lower_method_sig(sig),
15561557
hir::TraitMethod::Provided(body_id))
15571558
}
15581559
TraitItemKind::Type(ref bounds, ref default) => {
@@ -1599,6 +1600,7 @@ impl<'a> LoweringContext<'a> {
15991600
hir_id,
16001601
name: this.lower_ident(i.ident),
16011602
attrs: this.lower_attrs(&i.attrs),
1603+
generics: this.lower_generics(&i.generics),
16021604
vis: this.lower_visibility(&i.vis, None),
16031605
defaultness: this.lower_defaultness(i.defaultness, true /* [1] */),
16041606
node: match i.node {
@@ -1611,7 +1613,7 @@ impl<'a> LoweringContext<'a> {
16111613
let body = this.lower_block(body, false);
16121614
this.expr_block(body, ThinVec::new())
16131615
});
1614-
hir::ImplItemKind::Method(this.lower_method_sig(&i.generics, sig), body_id)
1616+
hir::ImplItemKind::Method(this.lower_method_sig(sig), body_id)
16151617
}
16161618
ImplItemKind::Type(ref ty) => hir::ImplItemKind::Type(this.lower_ty(ty)),
16171619
ImplItemKind::Macro(..) => panic!("Shouldn't exist any more"),
@@ -1723,9 +1725,8 @@ impl<'a> LoweringContext<'a> {
17231725
})
17241726
}
17251727

1726-
fn lower_method_sig(&mut self, generics: &Generics, sig: &MethodSig) -> hir::MethodSig {
1728+
fn lower_method_sig(&mut self, sig: &MethodSig) -> hir::MethodSig {
17271729
hir::MethodSig {
1728-
generics: self.lower_generics(generics),
17291730
abi: sig.abi,
17301731
unsafety: self.lower_unsafety(sig.unsafety),
17311732
constness: self.lower_constness(sig.constness),

src/librustc/hir/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1234,7 +1234,6 @@ pub struct MethodSig {
12341234
pub constness: Constness,
12351235
pub abi: Abi,
12361236
pub decl: P<FnDecl>,
1237-
pub generics: Generics,
12381237
}
12391238

12401239
// The bodies for items are stored "out of line", in a separate
@@ -1255,6 +1254,7 @@ pub struct TraitItem {
12551254
pub name: Name,
12561255
pub hir_id: HirId,
12571256
pub attrs: HirVec<Attribute>,
1257+
pub generics: Generics,
12581258
pub node: TraitItemKind,
12591259
pub span: Span,
12601260
}
@@ -1299,6 +1299,7 @@ pub struct ImplItem {
12991299
pub vis: Visibility,
13001300
pub defaultness: Defaultness,
13011301
pub attrs: HirVec<Attribute>,
1302+
pub generics: Generics,
13021303
pub node: ImplItemKind,
13031304
pub span: Span,
13041305
}

src/librustc/hir/print.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,7 @@ impl<'a> State<'a> {
879879
pub fn print_method_sig(&mut self,
880880
name: ast::Name,
881881
m: &hir::MethodSig,
882+
generics: &hir::Generics,
882883
vis: &hir::Visibility,
883884
arg_names: &[Spanned<ast::Name>],
884885
body_id: Option<hir::BodyId>)
@@ -888,7 +889,7 @@ impl<'a> State<'a> {
888889
m.constness,
889890
m.abi,
890891
Some(name),
891-
&m.generics,
892+
generics,
892893
vis,
893894
arg_names,
894895
body_id)
@@ -904,12 +905,12 @@ impl<'a> State<'a> {
904905
self.print_associated_const(ti.name, &ty, default, &hir::Inherited)?;
905906
}
906907
hir::TraitItemKind::Method(ref sig, hir::TraitMethod::Required(ref arg_names)) => {
907-
self.print_method_sig(ti.name, sig, &hir::Inherited, arg_names, None)?;
908+
self.print_method_sig(ti.name, sig, &ti.generics, &hir::Inherited, arg_names, None)?;
908909
self.s.word(";")?;
909910
}
910911
hir::TraitItemKind::Method(ref sig, hir::TraitMethod::Provided(body)) => {
911912
self.head("")?;
912-
self.print_method_sig(ti.name, sig, &hir::Inherited, &[], Some(body))?;
913+
self.print_method_sig(ti.name, sig, &ti.generics, &hir::Inherited, &[], Some(body))?;
913914
self.nbsp()?;
914915
self.end()?; // need to close a box
915916
self.end()?; // need to close a box
@@ -937,7 +938,7 @@ impl<'a> State<'a> {
937938
}
938939
hir::ImplItemKind::Method(ref sig, body) => {
939940
self.head("")?;
940-
self.print_method_sig(ii.name, sig, &ii.vis, &[], Some(body))?;
941+
self.print_method_sig(ii.name, sig, &ii.generics, &ii.vis, &[], Some(body))?;
941942
self.nbsp()?;
942943
self.end()?; // need to close a box
943944
self.end()?; // need to close a box

src/librustc/ich/impls_hir.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,7 @@ impl_stable_hash_for!(struct hir::MethodSig {
220220
unsafety,
221221
constness,
222222
abi,
223-
decl,
224-
generics
223+
decl
225224
});
226225

227226
impl_stable_hash_for!(struct hir::TypeBinding {
@@ -695,6 +694,7 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for hir::TraitItem {
695694
hir_id: _,
696695
name,
697696
ref attrs,
697+
ref generics,
698698
ref node,
699699
span
700700
} = *self;
@@ -703,6 +703,7 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for hir::TraitItem {
703703
id.hash_stable(hcx, hasher);
704704
name.hash_stable(hcx, hasher);
705705
attrs.hash_stable(hcx, hasher);
706+
generics.hash_stable(hcx, hasher);
706707
node.hash_stable(hcx, hasher);
707708
span.hash_stable(hcx, hasher);
708709
});
@@ -731,6 +732,7 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for hir::ImplItem {
731732
ref vis,
732733
defaultness,
733734
ref attrs,
735+
ref generics,
734736
ref node,
735737
span
736738
} = *self;
@@ -741,6 +743,7 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for hir::ImplItem {
741743
vis.hash_stable(hcx, hasher);
742744
defaultness.hash_stable(hcx, hasher);
743745
attrs.hash_stable(hcx, hasher);
746+
generics.hash_stable(hcx, hasher);
744747
node.hash_stable(hcx, hasher);
745748
span.hash_stable(hcx, hasher);
746749
});

src/librustc/middle/reachable.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,10 @@ fn item_might_be_inlined(item: &hir::Item) -> bool {
5858
}
5959

6060
fn method_might_be_inlined<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
61-
sig: &hir::MethodSig,
6261
impl_item: &hir::ImplItem,
6362
impl_src: DefId) -> bool {
6463
if attr::requests_inline(&impl_item.attrs) ||
65-
generics_require_inlining(&sig.generics) {
64+
generics_require_inlining(&impl_item.generics) {
6665
return true
6766
}
6867
if let Some(impl_node_id) = tcx.hir.as_local_node_id(impl_src) {
@@ -176,8 +175,8 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
176175
Some(hir_map::NodeImplItem(impl_item)) => {
177176
match impl_item.node {
178177
hir::ImplItemKind::Const(..) => true,
179-
hir::ImplItemKind::Method(ref sig, _) => {
180-
if generics_require_inlining(&sig.generics) ||
178+
hir::ImplItemKind::Method(..) => {
179+
if generics_require_inlining(&impl_item.generics) ||
181180
attr::requests_inline(&impl_item.attrs) {
182181
true
183182
} else {
@@ -293,9 +292,9 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
293292
hir::ImplItemKind::Const(_, body) => {
294293
self.visit_nested_body(body);
295294
}
296-
hir::ImplItemKind::Method(ref sig, body) => {
295+
hir::ImplItemKind::Method(_, body) => {
297296
let did = self.tcx.hir.get_parent_did(search_item);
298-
if method_might_be_inlined(self.tcx, sig, impl_item, did) {
297+
if method_might_be_inlined(self.tcx, impl_item, did) {
299298
self.visit_nested_body(body)
300299
}
301300
}

src/librustc/middle/resolve_lifetime.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
410410
if let hir::TraitItemKind::Method(ref sig, _) = trait_item.node {
411411
self.visit_early_late(
412412
Some(self.hir_map.get_parent(trait_item.id)),
413-
&sig.decl, &sig.generics,
413+
&sig.decl, &trait_item.generics,
414414
|this| intravisit::walk_trait_item(this, trait_item))
415415
} else {
416416
intravisit::walk_trait_item(self, trait_item);
@@ -421,7 +421,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
421421
if let hir::ImplItemKind::Method(ref sig, _) = impl_item.node {
422422
self.visit_early_late(
423423
Some(self.hir_map.get_parent(impl_item.id)),
424-
&sig.decl, &sig.generics,
424+
&sig.decl, &impl_item.generics,
425425
|this| intravisit::walk_impl_item(this, impl_item))
426426
} else {
427427
intravisit::walk_impl_item(self, impl_item);

src/librustc_typeck/check/compare_method.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -568,15 +568,11 @@ fn compare_number_of_generics<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
568568
let num_trait_m_type_params = trait_m_generics.types.len();
569569
if num_impl_m_type_params != num_trait_m_type_params {
570570
let impl_m_node_id = tcx.hir.as_local_node_id(impl_m.def_id).unwrap();
571-
let span = match tcx.hir.expect_impl_item(impl_m_node_id).node {
572-
ImplItemKind::Method(ref impl_m_sig, _) => {
573-
if impl_m_sig.generics.is_parameterized() {
574-
impl_m_sig.generics.span
575-
} else {
576-
impl_m_span
577-
}
578-
}
579-
_ => bug!("{:?} is not a method", impl_m),
571+
let impl_m_item = tcx.hir.expect_impl_item(impl_m_node_id);
572+
let span = if impl_m_item.generics.is_parameterized() {
573+
impl_m_item.generics.span
574+
} else {
575+
impl_m_span
580576
};
581577

582578
let mut err = struct_span_err!(tcx.sess,

src/librustc_typeck/collect.rs

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -257,19 +257,9 @@ fn type_param_predicates<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
257257

258258
let item_node_id = tcx.hir.as_local_node_id(item_def_id).unwrap();
259259
let ast_generics = match tcx.hir.get(item_node_id) {
260-
NodeTraitItem(item) => {
261-
match item.node {
262-
TraitItemKind::Method(ref sig, _) => &sig.generics,
263-
_ => return result
264-
}
265-
}
260+
NodeTraitItem(item) => &item.generics,
266261

267-
NodeImplItem(item) => {
268-
match item.node {
269-
ImplItemKind::Method(ref sig, _) => &sig.generics,
270-
_ => return result
271-
}
272-
}
262+
NodeImplItem(item) => &item.generics,
273263

274264
NodeItem(item) => {
275265
match item.node {
@@ -814,12 +804,12 @@ fn has_late_bound_regions<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
814804
match node {
815805
hir_map::NodeTraitItem(item) => match item.node {
816806
hir::TraitItemKind::Method(ref sig, _) =>
817-
has_late_bound_regions(tcx, &sig.generics, &sig.decl),
807+
has_late_bound_regions(tcx, &item.generics, &sig.decl),
818808
_ => None,
819809
},
820810
hir_map::NodeImplItem(item) => match item.node {
821811
hir::ImplItemKind::Method(ref sig, _) =>
822-
has_late_bound_regions(tcx, &sig.generics, &sig.decl),
812+
has_late_bound_regions(tcx, &item.generics, &sig.decl),
823813
_ => None,
824814
},
825815
hir_map::NodeForeignItem(item) => match item.node {
@@ -877,19 +867,9 @@ fn generics_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
877867

878868
let no_generics = hir::Generics::empty();
879869
let ast_generics = match node {
880-
NodeTraitItem(item) => {
881-
match item.node {
882-
TraitItemKind::Method(ref sig, _) => &sig.generics,
883-
_ => &no_generics
884-
}
885-
}
870+
NodeTraitItem(item) => &item.generics,
886871

887-
NodeImplItem(item) => {
888-
match item.node {
889-
ImplItemKind::Method(ref sig, _) => &sig.generics,
890-
_ => &no_generics
891-
}
892-
}
872+
NodeImplItem(item) => &item.generics,
893873

894874
NodeItem(item) => {
895875
match item.node {
@@ -1336,19 +1316,9 @@ fn predicates_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
13361316
let icx = ItemCtxt::new(tcx, def_id);
13371317
let no_generics = hir::Generics::empty();
13381318
let ast_generics = match node {
1339-
NodeTraitItem(item) => {
1340-
match item.node {
1341-
TraitItemKind::Method(ref sig, _) => &sig.generics,
1342-
_ => &no_generics
1343-
}
1344-
}
1319+
NodeTraitItem(item) => &item.generics,
13451320

1346-
NodeImplItem(item) => {
1347-
match item.node {
1348-
ImplItemKind::Method(ref sig, _) => &sig.generics,
1349-
_ => &no_generics
1350-
}
1351-
}
1321+
NodeImplItem(item) => &item.generics,
13521322

13531323
NodeItem(item) => {
13541324
match item.node {

0 commit comments

Comments
 (0)