Skip to content

Commit bb30144

Browse files
committed
Lifted generics into TraitItem and ImplItem from MethodSig -- HIR now matches AST
1 parent 892e468 commit bb30144

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
@@ -780,9 +780,7 @@ pub fn walk_fn_kind<'v, V: Visitor<'v>>(visitor: &mut V, function_kind: FnKind<'
780780
FnKind::ItemFn(_, generics, ..) => {
781781
visitor.visit_generics(generics);
782782
}
783-
FnKind::Method(_, sig, ..) => {
784-
visitor.visit_generics(&sig.generics);
785-
}
783+
FnKind::Method(..) |
786784
FnKind::Closure(_) => {}
787785
}
788786
}
@@ -802,6 +800,7 @@ pub fn walk_fn<'v, V: Visitor<'v>>(visitor: &mut V,
802800
pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v TraitItem) {
803801
visitor.visit_name(trait_item.span, trait_item.name);
804802
walk_list!(visitor, visit_attribute, &trait_item.attrs);
803+
visitor.visit_generics(&trait_item.generics);
805804
match trait_item.node {
806805
TraitItemKind::Const(ref ty, default) => {
807806
visitor.visit_id(trait_item.id);
@@ -810,7 +809,6 @@ pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v Trai
810809
}
811810
TraitItemKind::Method(ref sig, TraitMethod::Required(ref names)) => {
812811
visitor.visit_id(trait_item.id);
813-
visitor.visit_generics(&sig.generics);
814812
visitor.visit_fn_decl(&sig.decl);
815813
for name in names {
816814
visitor.visit_name(name.span, name.node);
@@ -852,6 +850,7 @@ pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplIt
852850
ref vis,
853851
ref defaultness,
854852
ref attrs,
853+
ref generics,
855854
ref node,
856855
span
857856
} = *impl_item;
@@ -860,6 +859,7 @@ pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplIt
860859
visitor.visit_vis(vis);
861860
visitor.visit_defaultness(defaultness);
862861
walk_list!(visitor, visit_attribute, attrs);
862+
visitor.visit_generics(generics);
863863
match *node {
864864
ImplItemKind::Const(ref ty, body) => {
865865
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
@@ -1539,6 +1539,7 @@ impl<'a> LoweringContext<'a> {
15391539
hir_id,
15401540
name: this.lower_ident(i.ident),
15411541
attrs: this.lower_attrs(&i.attrs),
1542+
generics: this.lower_generics(&i.generics),
15421543
node: match i.node {
15431544
TraitItemKind::Const(ref ty, ref default) => {
15441545
hir::TraitItemKind::Const(this.lower_ty(ty),
@@ -1548,15 +1549,15 @@ impl<'a> LoweringContext<'a> {
15481549
}
15491550
TraitItemKind::Method(ref sig, None) => {
15501551
let names = this.lower_fn_args_to_names(&sig.decl);
1551-
hir::TraitItemKind::Method(this.lower_method_sig(&i.generics, sig),
1552+
hir::TraitItemKind::Method(this.lower_method_sig(sig),
15521553
hir::TraitMethod::Required(names))
15531554
}
15541555
TraitItemKind::Method(ref sig, Some(ref body)) => {
15551556
let body_id = this.lower_body(Some(&sig.decl), |this| {
15561557
let body = this.lower_block(body, false);
15571558
this.expr_block(body, ThinVec::new())
15581559
});
1559-
hir::TraitItemKind::Method(this.lower_method_sig(&i.generics, sig),
1560+
hir::TraitItemKind::Method(this.lower_method_sig(sig),
15601561
hir::TraitMethod::Provided(body_id))
15611562
}
15621563
TraitItemKind::Type(ref bounds, ref default) => {
@@ -1603,6 +1604,7 @@ impl<'a> LoweringContext<'a> {
16031604
hir_id,
16041605
name: this.lower_ident(i.ident),
16051606
attrs: this.lower_attrs(&i.attrs),
1607+
generics: this.lower_generics(&i.generics),
16061608
vis: this.lower_visibility(&i.vis, None),
16071609
defaultness: this.lower_defaultness(i.defaultness, true /* [1] */),
16081610
node: match i.node {
@@ -1615,7 +1617,7 @@ impl<'a> LoweringContext<'a> {
16151617
let body = this.lower_block(body, false);
16161618
this.expr_block(body, ThinVec::new())
16171619
});
1618-
hir::ImplItemKind::Method(this.lower_method_sig(&i.generics, sig), body_id)
1620+
hir::ImplItemKind::Method(this.lower_method_sig(sig), body_id)
16191621
}
16201622
ImplItemKind::Type(ref ty) => hir::ImplItemKind::Type(this.lower_ty(ty)),
16211623
ImplItemKind::Macro(..) => panic!("Shouldn't exist any more"),
@@ -1727,9 +1729,8 @@ impl<'a> LoweringContext<'a> {
17271729
})
17281730
}
17291731

1730-
fn lower_method_sig(&mut self, generics: &Generics, sig: &MethodSig) -> hir::MethodSig {
1732+
fn lower_method_sig(&mut self, sig: &MethodSig) -> hir::MethodSig {
17311733
hir::MethodSig {
1732-
generics: self.lower_generics(generics),
17331734
abi: sig.abi,
17341735
unsafety: self.lower_unsafety(sig.unsafety),
17351736
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
@@ -1295,7 +1295,6 @@ pub struct MethodSig {
12951295
pub constness: Constness,
12961296
pub abi: Abi,
12971297
pub decl: P<FnDecl>,
1298-
pub generics: Generics,
12991298
}
13001299

13011300
// The bodies for items are stored "out of line", in a separate
@@ -1316,6 +1315,7 @@ pub struct TraitItem {
13161315
pub name: Name,
13171316
pub hir_id: HirId,
13181317
pub attrs: HirVec<Attribute>,
1318+
pub generics: Generics,
13191319
pub node: TraitItemKind,
13201320
pub span: Span,
13211321
}
@@ -1360,6 +1360,7 @@ pub struct ImplItem {
13601360
pub vis: Visibility,
13611361
pub defaultness: Defaultness,
13621362
pub attrs: HirVec<Attribute>,
1363+
pub generics: Generics,
13631364
pub node: ImplItemKind,
13641365
pub span: Span,
13651366
}

src/librustc/hir/print.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,7 @@ impl<'a> State<'a> {
880880
pub fn print_method_sig(&mut self,
881881
name: ast::Name,
882882
m: &hir::MethodSig,
883+
generics: &hir::Generics,
883884
vis: &hir::Visibility,
884885
arg_names: &[Spanned<ast::Name>],
885886
body_id: Option<hir::BodyId>)
@@ -889,7 +890,7 @@ impl<'a> State<'a> {
889890
m.constness,
890891
m.abi,
891892
Some(name),
892-
&m.generics,
893+
generics,
893894
vis,
894895
arg_names,
895896
body_id)
@@ -905,12 +906,12 @@ impl<'a> State<'a> {
905906
self.print_associated_const(ti.name, &ty, default, &hir::Inherited)?;
906907
}
907908
hir::TraitItemKind::Method(ref sig, hir::TraitMethod::Required(ref arg_names)) => {
908-
self.print_method_sig(ti.name, sig, &hir::Inherited, arg_names, None)?;
909+
self.print_method_sig(ti.name, sig, &ti.generics, &hir::Inherited, arg_names, None)?;
909910
self.s.word(";")?;
910911
}
911912
hir::TraitItemKind::Method(ref sig, hir::TraitMethod::Provided(body)) => {
912913
self.head("")?;
913-
self.print_method_sig(ti.name, sig, &hir::Inherited, &[], Some(body))?;
914+
self.print_method_sig(ti.name, sig, &ti.generics, &hir::Inherited, &[], Some(body))?;
914915
self.nbsp()?;
915916
self.end()?; // need to close a box
916917
self.end()?; // need to close a box
@@ -938,7 +939,7 @@ impl<'a> State<'a> {
938939
}
939940
hir::ImplItemKind::Method(ref sig, body) => {
940941
self.head("")?;
941-
self.print_method_sig(ii.name, sig, &ii.vis, &[], Some(body))?;
942+
self.print_method_sig(ii.name, sig, &ii.generics, &ii.vis, &[], Some(body))?;
942943
self.nbsp()?;
943944
self.end()?; // need to close a box
944945
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
@@ -232,8 +232,7 @@ impl_stable_hash_for!(struct hir::MethodSig {
232232
unsafety,
233233
constness,
234234
abi,
235-
decl,
236-
generics
235+
decl
237236
});
238237

239238
impl_stable_hash_for!(struct hir::TypeBinding {
@@ -709,13 +708,15 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for hir::TraitItem {
709708
hir_id: _,
710709
name,
711710
ref attrs,
711+
ref generics,
712712
ref node,
713713
span
714714
} = *self;
715715

716716
hcx.hash_hir_item_like(attrs, |hcx| {
717717
name.hash_stable(hcx, hasher);
718718
attrs.hash_stable(hcx, hasher);
719+
generics.hash_stable(hcx, hasher);
719720
node.hash_stable(hcx, hasher);
720721
span.hash_stable(hcx, hasher);
721722
});
@@ -744,6 +745,7 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for hir::ImplItem {
744745
ref vis,
745746
defaultness,
746747
ref attrs,
748+
ref generics,
747749
ref node,
748750
span
749751
} = *self;
@@ -753,6 +755,7 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for hir::ImplItem {
753755
vis.hash_stable(hcx, hasher);
754756
defaultness.hash_stable(hcx, hasher);
755757
attrs.hash_stable(hcx, hasher);
758+
generics.hash_stable(hcx, hasher);
756759
node.hash_stable(hcx, hasher);
757760
span.hash_stable(hcx, hasher);
758761
});

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
@@ -412,7 +412,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
412412
if let hir::TraitItemKind::Method(ref sig, _) = trait_item.node {
413413
self.visit_early_late(
414414
Some(self.hir_map.get_parent(trait_item.id)),
415-
&sig.decl, &sig.generics,
415+
&sig.decl, &trait_item.generics,
416416
|this| intravisit::walk_trait_item(this, trait_item))
417417
} else {
418418
intravisit::walk_trait_item(self, trait_item);
@@ -423,7 +423,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
423423
if let hir::ImplItemKind::Method(ref sig, _) = impl_item.node {
424424
self.visit_early_late(
425425
Some(self.hir_map.get_parent(impl_item.id)),
426-
&sig.decl, &sig.generics,
426+
&sig.decl, &impl_item.generics,
427427
|this| intravisit::walk_impl_item(this, impl_item))
428428
} else {
429429
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
@@ -261,19 +261,9 @@ fn type_param_predicates<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
261261

262262
let item_node_id = tcx.hir.as_local_node_id(item_def_id).unwrap();
263263
let ast_generics = match tcx.hir.get(item_node_id) {
264-
NodeTraitItem(item) => {
265-
match item.node {
266-
TraitItemKind::Method(ref sig, _) => &sig.generics,
267-
_ => return result
268-
}
269-
}
264+
NodeTraitItem(item) => &item.generics,
270265

271-
NodeImplItem(item) => {
272-
match item.node {
273-
ImplItemKind::Method(ref sig, _) => &sig.generics,
274-
_ => return result
275-
}
276-
}
266+
NodeImplItem(item) => &item.generics,
277267

278268
NodeItem(item) => {
279269
match item.node {
@@ -818,12 +808,12 @@ fn has_late_bound_regions<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
818808
match node {
819809
hir_map::NodeTraitItem(item) => match item.node {
820810
hir::TraitItemKind::Method(ref sig, _) =>
821-
has_late_bound_regions(tcx, &sig.generics, &sig.decl),
811+
has_late_bound_regions(tcx, &item.generics, &sig.decl),
822812
_ => None,
823813
},
824814
hir_map::NodeImplItem(item) => match item.node {
825815
hir::ImplItemKind::Method(ref sig, _) =>
826-
has_late_bound_regions(tcx, &sig.generics, &sig.decl),
816+
has_late_bound_regions(tcx, &item.generics, &sig.decl),
827817
_ => None,
828818
},
829819
hir_map::NodeForeignItem(item) => match item.node {
@@ -881,19 +871,9 @@ fn generics_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
881871

882872
let no_generics = hir::Generics::empty();
883873
let ast_generics = match node {
884-
NodeTraitItem(item) => {
885-
match item.node {
886-
TraitItemKind::Method(ref sig, _) => &sig.generics,
887-
_ => &no_generics
888-
}
889-
}
874+
NodeTraitItem(item) => &item.generics,
890875

891-
NodeImplItem(item) => {
892-
match item.node {
893-
ImplItemKind::Method(ref sig, _) => &sig.generics,
894-
_ => &no_generics
895-
}
896-
}
876+
NodeImplItem(item) => &item.generics,
897877

898878
NodeItem(item) => {
899879
match item.node {
@@ -1353,19 +1333,9 @@ fn explicit_predicates_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
13531333
let icx = ItemCtxt::new(tcx, def_id);
13541334
let no_generics = hir::Generics::empty();
13551335
let ast_generics = match node {
1356-
NodeTraitItem(item) => {
1357-
match item.node {
1358-
TraitItemKind::Method(ref sig, _) => &sig.generics,
1359-
_ => &no_generics
1360-
}
1361-
}
1336+
NodeTraitItem(item) => &item.generics,
13621337

1363-
NodeImplItem(item) => {
1364-
match item.node {
1365-
ImplItemKind::Method(ref sig, _) => &sig.generics,
1366-
_ => &no_generics
1367-
}
1368-
}
1338+
NodeImplItem(item) => &item.generics,
13691339

13701340
NodeItem(item) => {
13711341
match item.node {

0 commit comments

Comments
 (0)