Skip to content

Commit f61394f

Browse files
committed
Lifting Generics from MethodSig to TraitItem and ImplItem since we want to support generics in each variant of TraitItem and ImplItem
1 parent f6d7514 commit f61394f

File tree

11 files changed

+51
-40
lines changed

11 files changed

+51
-40
lines changed

src/librustc/hir/lowering.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,15 +1548,15 @@ impl<'a> LoweringContext<'a> {
15481548
}
15491549
TraitItemKind::Method(ref sig, None) => {
15501550
let names = this.lower_fn_args_to_names(&sig.decl);
1551-
hir::TraitItemKind::Method(this.lower_method_sig(sig),
1551+
hir::TraitItemKind::Method(this.lower_method_sig(&i.generics, sig),
15521552
hir::TraitMethod::Required(names))
15531553
}
15541554
TraitItemKind::Method(ref sig, Some(ref body)) => {
15551555
let body_id = this.lower_body(Some(&sig.decl), |this| {
15561556
let body = this.lower_block(body, false);
15571557
this.expr_block(body, ThinVec::new())
15581558
});
1559-
hir::TraitItemKind::Method(this.lower_method_sig(sig),
1559+
hir::TraitItemKind::Method(this.lower_method_sig(&i.generics, sig),
15601560
hir::TraitMethod::Provided(body_id))
15611561
}
15621562
TraitItemKind::Type(ref bounds, ref default) => {
@@ -1615,7 +1615,7 @@ impl<'a> LoweringContext<'a> {
16151615
let body = this.lower_block(body, false);
16161616
this.expr_block(body, ThinVec::new())
16171617
});
1618-
hir::ImplItemKind::Method(this.lower_method_sig(sig), body_id)
1618+
hir::ImplItemKind::Method(this.lower_method_sig(&i.generics, sig), body_id)
16191619
}
16201620
ImplItemKind::Type(ref ty) => hir::ImplItemKind::Type(this.lower_ty(ty)),
16211621
ImplItemKind::Macro(..) => panic!("Shouldn't exist any more"),
@@ -1727,9 +1727,9 @@ impl<'a> LoweringContext<'a> {
17271727
})
17281728
}
17291729

1730-
fn lower_method_sig(&mut self, sig: &MethodSig) -> hir::MethodSig {
1730+
fn lower_method_sig(&mut self, generics: &Generics, sig: &MethodSig) -> hir::MethodSig {
17311731
hir::MethodSig {
1732-
generics: self.lower_generics(&sig.generics),
1732+
generics: self.lower_generics(generics),
17331733
abi: sig.abi,
17341734
unsafety: self.lower_unsafety(sig.unsafety),
17351735
constness: self.lower_constness(sig.constness),

src/librustc_resolve/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,6 @@ impl<'a, 'tcx> Visitor<'tcx> for Resolver<'a> {
723723
ItemRibKind
724724
}
725725
FnKind::Method(_, sig, _, _) => {
726-
self.visit_generics(&sig.generics);
727726
MethodRibKind(!sig.decl.has_self())
728727
}
729728
FnKind::Closure(_) => ClosureRibKind(node_id),
@@ -1864,6 +1863,7 @@ impl<'a> Resolver<'a> {
18641863

18651864
for trait_item in trait_items {
18661865
this.check_proc_macro_attrs(&trait_item.attrs);
1866+
this.visit_generics(&trait_item.generics);
18671867

18681868
match trait_item.node {
18691869
TraitItemKind::Const(ref ty, ref default) => {
@@ -1880,7 +1880,7 @@ impl<'a> Resolver<'a> {
18801880
}
18811881
TraitItemKind::Method(ref sig, _) => {
18821882
let type_parameters =
1883-
HasTypeParameters(&sig.generics,
1883+
HasTypeParameters(&trait_item.generics,
18841884
MethodRibKind(!sig.decl.has_self()));
18851885
this.with_type_parameter_rib(type_parameters, |this| {
18861886
visit::walk_trait_item(this, trait_item)
@@ -2075,6 +2075,7 @@ impl<'a> Resolver<'a> {
20752075
this.with_current_self_type(self_type, |this| {
20762076
for impl_item in impl_items {
20772077
this.check_proc_macro_attrs(&impl_item.attrs);
2078+
this.visit_generics(&impl_item.generics);
20782079
this.resolve_visibility(&impl_item.vis);
20792080
match impl_item.node {
20802081
ImplItemKind::Const(..) => {
@@ -2097,7 +2098,7 @@ impl<'a> Resolver<'a> {
20972098
// We also need a new scope for the method-
20982099
// specific type parameters.
20992100
let type_parameters =
2100-
HasTypeParameters(&sig.generics,
2101+
HasTypeParameters(&impl_item.generics,
21012102
MethodRibKind(!sig.decl.has_self()));
21022103
this.with_type_parameter_rib(type_parameters, |this| {
21032104
visit::walk_impl_item(this, impl_item);

src/librustc_save_analysis/dump_visitor.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,23 +354,24 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
354354
body: Option<&'l ast::Block>,
355355
id: ast::NodeId,
356356
name: ast::Ident,
357+
generics: &'l ast::Generics,
357358
vis: ast::Visibility,
358359
span: Span) {
359360
debug!("process_method: {}:{}", id, name);
360361

361362
if let Some(mut method_data) = self.save_ctxt.get_method_data(id, name.name, span) {
362363

363-
let sig_str = ::make_signature(&sig.decl, &sig.generics);
364+
let sig_str = ::make_signature(&sig.decl, &generics);
364365
if body.is_some() {
365366
self.nest_tables(id, |v| {
366367
v.process_formals(&sig.decl.inputs, &method_data.qualname)
367368
});
368369
}
369370

370-
self.process_generic_params(&sig.generics, span, &method_data.qualname, id);
371+
self.process_generic_params(&generics, span, &method_data.qualname, id);
371372

372373
method_data.value = sig_str;
373-
method_data.sig = sig::method_signature(id, name, sig, &self.save_ctxt);
374+
method_data.sig = sig::method_signature(id, name, generics, sig, &self.save_ctxt);
374375
self.dumper.dump_def(vis == ast::Visibility::Public, method_data);
375376
}
376377

@@ -1007,6 +1008,7 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
10071008
body.as_ref().map(|x| &**x),
10081009
trait_item.id,
10091010
trait_item.ident,
1011+
&trait_item.generics,
10101012
ast::Visibility::Public,
10111013
trait_item.span);
10121014
}
@@ -1066,6 +1068,7 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
10661068
Some(body),
10671069
impl_item.id,
10681070
impl_item.ident,
1071+
&impl_item.generics,
10691072
impl_item.vis.clone(),
10701073
impl_item.span);
10711074
}

src/librustc_save_analysis/sig.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,14 @@ pub fn variant_signature(variant: &ast::Variant, scx: &SaveContext) -> Option<Si
7777

7878
pub fn method_signature(id: NodeId,
7979
ident: ast::Ident,
80+
generics: &ast::Generics,
8081
m: &ast::MethodSig,
8182
scx: &SaveContext)
8283
-> Option<Signature> {
8384
if !scx.config.signatures {
8485
return None;
8586
}
86-
make_method_signature(id, ident, m, scx).ok()
87+
make_method_signature(id, ident, generics, m, scx).ok()
8788
}
8889

8990
pub fn assoc_const_signature(id: NodeId,
@@ -895,6 +896,7 @@ fn make_assoc_const_signature(id: NodeId,
895896

896897
fn make_method_signature(id: NodeId,
897898
ident: ast::Ident,
899+
generics: &ast::Generics,
898900
m: &ast::MethodSig,
899901
scx: &SaveContext)
900902
-> Result {
@@ -915,7 +917,7 @@ fn make_method_signature(id: NodeId,
915917

916918
let mut sig = name_and_generics(text,
917919
0,
918-
&m.generics,
920+
generics,
919921
id,
920922
ident,
921923
scx)?;

src/libsyntax/ast.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1178,7 +1178,6 @@ pub struct MethodSig {
11781178
pub constness: Spanned<Constness>,
11791179
pub abi: Abi,
11801180
pub decl: P<FnDecl>,
1181-
pub generics: Generics,
11821181
}
11831182

11841183
/// Represents an item declaration within a trait declaration,
@@ -1190,6 +1189,7 @@ pub struct TraitItem {
11901189
pub id: NodeId,
11911190
pub ident: Ident,
11921191
pub attrs: Vec<Attribute>,
1192+
pub generics: Generics,
11931193
pub node: TraitItemKind,
11941194
pub span: Span,
11951195
/// See `Item::tokens` for what this is
@@ -1211,6 +1211,7 @@ pub struct ImplItem {
12111211
pub vis: Visibility,
12121212
pub defaultness: Defaultness,
12131213
pub attrs: Vec<Attribute>,
1214+
pub generics: Generics,
12141215
pub node: ImplItemKind,
12151216
pub span: Span,
12161217
/// See `Item::tokens` for what this is

src/libsyntax/ext/placeholders.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub fn placeholder(kind: ExpansionKind, id: ast::NodeId) -> Expansion {
3232

3333
let ident = keywords::Invalid.ident();
3434
let attrs = Vec::new();
35+
let generics = ast::Generics::default();
3536
let vis = ast::Visibility::Inherited;
3637
let span = DUMMY_SP;
3738
let expr_placeholder = || P(ast::Expr {
@@ -49,12 +50,12 @@ pub fn placeholder(kind: ExpansionKind, id: ast::NodeId) -> Expansion {
4950
tokens: None,
5051
}))),
5152
ExpansionKind::TraitItems => Expansion::TraitItems(SmallVector::one(ast::TraitItem {
52-
id, span, ident, attrs,
53+
id, span, ident, attrs, generics,
5354
node: ast::TraitItemKind::Macro(mac_placeholder()),
5455
tokens: None,
5556
})),
5657
ExpansionKind::ImplItems => Expansion::ImplItems(SmallVector::one(ast::ImplItem {
57-
id, span, ident, vis, attrs,
58+
id, span, ident, vis, attrs, generics,
5859
node: ast::ImplItemKind::Macro(mac_placeholder()),
5960
defaultness: ast::Defaultness::Final,
6061
tokens: None,

src/libsyntax/fold.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,7 @@ pub fn noop_fold_trait_item<T: Folder>(i: TraitItem, folder: &mut T)
943943
id: folder.new_id(i.id),
944944
ident: folder.fold_ident(i.ident),
945945
attrs: fold_attrs(i.attrs, folder),
946+
generics: folder.fold_generics(i.generics),
946947
node: match i.node {
947948
TraitItemKind::Const(ty, default) => {
948949
TraitItemKind::Const(folder.fold_ty(ty),
@@ -972,6 +973,7 @@ pub fn noop_fold_impl_item<T: Folder>(i: ImplItem, folder: &mut T)
972973
vis: folder.fold_vis(i.vis),
973974
ident: folder.fold_ident(i.ident),
974975
attrs: fold_attrs(i.attrs, folder),
976+
generics: folder.fold_generics(i.generics),
975977
defaultness: i.defaultness,
976978
node: match i.node {
977979
ast::ImplItemKind::Const(ty, expr) => {
@@ -1074,7 +1076,6 @@ pub fn noop_fold_foreign_item<T: Folder>(ni: ForeignItem, folder: &mut T) -> For
10741076

10751077
pub fn noop_fold_method_sig<T: Folder>(sig: MethodSig, folder: &mut T) -> MethodSig {
10761078
MethodSig {
1077-
generics: folder.fold_generics(sig.generics),
10781079
abi: sig.abi,
10791080
unsafety: sig.unsafety,
10801081
constness: sig.constness,

src/libsyntax/parse/parser.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,10 +1287,10 @@ impl<'a> Parser<'a> {
12871287
mut attrs: Vec<Attribute>) -> PResult<'a, TraitItem> {
12881288
let lo = self.span;
12891289

1290-
let (name, node) = if self.eat_keyword(keywords::Type) {
1290+
let (name, node, generics) = if self.eat_keyword(keywords::Type) {
12911291
let TyParam {ident, bounds, default, ..} = self.parse_ty_param(vec![])?;
12921292
self.expect(&token::Semi)?;
1293-
(ident, TraitItemKind::Type(bounds, default))
1293+
(ident, TraitItemKind::Type(bounds, default), ast::Generics::default())
12941294
} else if self.is_const_item() {
12951295
self.expect_keyword(keywords::Const)?;
12961296
let ident = self.parse_ident()?;
@@ -1305,7 +1305,7 @@ impl<'a> Parser<'a> {
13051305
self.expect(&token::Semi)?;
13061306
None
13071307
};
1308-
(ident, TraitItemKind::Const(ty, default))
1308+
(ident, TraitItemKind::Const(ty, default), ast::Generics::default())
13091309
} else if self.token.is_path_start() {
13101310
// trait item macro.
13111311
// code copied from parse_macro_use_or_failure... abstraction!
@@ -1328,7 +1328,7 @@ impl<'a> Parser<'a> {
13281328
}
13291329

13301330
let mac = respan(lo.to(self.prev_span), Mac_ { path: pth, tts: tts });
1331-
(keywords::Invalid.ident(), ast::TraitItemKind::Macro(mac))
1331+
(keywords::Invalid.ident(), ast::TraitItemKind::Macro(mac), ast::Generics::default())
13321332
} else {
13331333
let (constness, unsafety, abi) = self.parse_fn_front_matter()?;
13341334

@@ -1341,13 +1341,12 @@ impl<'a> Parser<'a> {
13411341
// definition...
13421342
p.parse_arg_general(false)
13431343
})?;
1344-
13451344
generics.where_clause = self.parse_where_clause()?;
1345+
13461346
let sig = ast::MethodSig {
13471347
unsafety,
13481348
constness,
13491349
decl: d,
1350-
generics,
13511350
abi,
13521351
};
13531352

@@ -1370,13 +1369,14 @@ impl<'a> Parser<'a> {
13701369
return Err(self.fatal(&format!("expected `;` or `{{`, found `{}`", token_str)));
13711370
}
13721371
};
1373-
(ident, ast::TraitItemKind::Method(sig, body))
1372+
(ident, ast::TraitItemKind::Method(sig, body), generics)
13741373
};
13751374

13761375
Ok(TraitItem {
13771376
id: ast::DUMMY_NODE_ID,
13781377
ident: name,
13791378
attrs,
1379+
generics,
13801380
node,
13811381
span: lo.to(self.prev_span),
13821382
tokens: None,
@@ -4901,12 +4901,12 @@ impl<'a> Parser<'a> {
49014901
let lo = self.span;
49024902
let vis = self.parse_visibility(false)?;
49034903
let defaultness = self.parse_defaultness()?;
4904-
let (name, node) = if self.eat_keyword(keywords::Type) {
4904+
let (name, node, generics) = if self.eat_keyword(keywords::Type) {
49054905
let name = self.parse_ident()?;
49064906
self.expect(&token::Eq)?;
49074907
let typ = self.parse_ty()?;
49084908
self.expect(&token::Semi)?;
4909-
(name, ast::ImplItemKind::Type(typ))
4909+
(name, ast::ImplItemKind::Type(typ), ast::Generics::default())
49104910
} else if self.is_const_item() {
49114911
self.expect_keyword(keywords::Const)?;
49124912
let name = self.parse_ident()?;
@@ -4915,11 +4915,11 @@ impl<'a> Parser<'a> {
49154915
self.expect(&token::Eq)?;
49164916
let expr = self.parse_expr()?;
49174917
self.expect(&token::Semi)?;
4918-
(name, ast::ImplItemKind::Const(typ, expr))
4918+
(name, ast::ImplItemKind::Const(typ, expr), ast::Generics::default())
49194919
} else {
4920-
let (name, inner_attrs, node) = self.parse_impl_method(&vis, at_end)?;
4920+
let (name, inner_attrs, generics, node) = self.parse_impl_method(&vis, at_end)?;
49214921
attrs.extend(inner_attrs);
4922-
(name, node)
4922+
(name, node, generics)
49234923
};
49244924

49254925
Ok(ImplItem {
@@ -4929,6 +4929,7 @@ impl<'a> Parser<'a> {
49294929
vis,
49304930
defaultness,
49314931
attrs,
4932+
generics,
49324933
node,
49334934
tokens: None,
49344935
})
@@ -4986,7 +4987,7 @@ impl<'a> Parser<'a> {
49864987

49874988
/// Parse a method or a macro invocation in a trait impl.
49884989
fn parse_impl_method(&mut self, vis: &Visibility, at_end: &mut bool)
4989-
-> PResult<'a, (Ident, Vec<ast::Attribute>, ast::ImplItemKind)> {
4990+
-> PResult<'a, (Ident, Vec<ast::Attribute>, ast::Generics, ast::ImplItemKind)> {
49904991
// code copied from parse_macro_use_or_failure... abstraction!
49914992
if self.token.is_path_start() {
49924993
// Method macro.
@@ -5013,7 +5014,7 @@ impl<'a> Parser<'a> {
50135014
}
50145015

50155016
let mac = respan(lo.to(self.prev_span), Mac_ { path: pth, tts: tts });
5016-
Ok((keywords::Invalid.ident(), vec![], ast::ImplItemKind::Macro(mac)))
5017+
Ok((keywords::Invalid.ident(), vec![], ast::Generics::default(), ast::ImplItemKind::Macro(mac)))
50175018
} else {
50185019
let (constness, unsafety, abi) = self.parse_fn_front_matter()?;
50195020
let ident = self.parse_ident()?;
@@ -5022,8 +5023,7 @@ impl<'a> Parser<'a> {
50225023
generics.where_clause = self.parse_where_clause()?;
50235024
*at_end = true;
50245025
let (inner_attrs, body) = self.parse_inner_attrs_and_block()?;
5025-
Ok((ident, inner_attrs, ast::ImplItemKind::Method(ast::MethodSig {
5026-
generics,
5026+
Ok((ident, inner_attrs, generics, ast::ImplItemKind::Method(ast::MethodSig {
50275027
abi,
50285028
unsafety,
50295029
constness,

src/libsyntax/print/pprust.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,6 +1525,7 @@ impl<'a> State<'a> {
15251525

15261526
pub fn print_method_sig(&mut self,
15271527
ident: ast::Ident,
1528+
generics: &ast::Generics,
15281529
m: &ast::MethodSig,
15291530
vis: &ast::Visibility)
15301531
-> io::Result<()> {
@@ -1533,7 +1534,7 @@ impl<'a> State<'a> {
15331534
m.constness.node,
15341535
m.abi,
15351536
Some(ident),
1536-
&m.generics,
1537+
&generics,
15371538
vis)
15381539
}
15391540

@@ -1553,7 +1554,7 @@ impl<'a> State<'a> {
15531554
if body.is_some() {
15541555
self.head("")?;
15551556
}
1556-
self.print_method_sig(ti.ident, sig, &ast::Visibility::Inherited)?;
1557+
self.print_method_sig(ti.ident, &ti.generics, sig, &ast::Visibility::Inherited)?;
15571558
if let Some(ref body) = *body {
15581559
self.nbsp()?;
15591560
self.print_block_with_attrs(body, &ti.attrs)?;
@@ -1592,7 +1593,7 @@ impl<'a> State<'a> {
15921593
}
15931594
ast::ImplItemKind::Method(ref sig, ref body) => {
15941595
self.head("")?;
1595-
self.print_method_sig(ii.ident, sig, &ii.vis)?;
1596+
self.print_method_sig(ii.ident, &ii.generics, sig, &ii.vis)?;
15961597
self.nbsp()?;
15971598
self.print_block_with_attrs(body, &ii.attrs)?;
15981599
}

0 commit comments

Comments
 (0)