Skip to content

Commit 26395e4

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 e2504cf commit 26395e4

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
@@ -1544,15 +1544,15 @@ impl<'a> LoweringContext<'a> {
15441544
}
15451545
TraitItemKind::Method(ref sig, None) => {
15461546
let names = this.lower_fn_args_to_names(&sig.decl);
1547-
hir::TraitItemKind::Method(this.lower_method_sig(sig),
1547+
hir::TraitItemKind::Method(this.lower_method_sig(&i.generics, sig),
15481548
hir::TraitMethod::Required(names))
15491549
}
15501550
TraitItemKind::Method(ref sig, Some(ref body)) => {
15511551
let body_id = this.lower_body(Some(&sig.decl), |this| {
15521552
let body = this.lower_block(body, false);
15531553
this.expr_block(body, ThinVec::new())
15541554
});
1555-
hir::TraitItemKind::Method(this.lower_method_sig(sig),
1555+
hir::TraitItemKind::Method(this.lower_method_sig(&i.generics, sig),
15561556
hir::TraitMethod::Provided(body_id))
15571557
}
15581558
TraitItemKind::Type(ref bounds, ref default) => {
@@ -1611,7 +1611,7 @@ impl<'a> LoweringContext<'a> {
16111611
let body = this.lower_block(body, false);
16121612
this.expr_block(body, ThinVec::new())
16131613
});
1614-
hir::ImplItemKind::Method(this.lower_method_sig(sig), body_id)
1614+
hir::ImplItemKind::Method(this.lower_method_sig(&i.generics, sig), body_id)
16151615
}
16161616
ImplItemKind::Type(ref ty) => hir::ImplItemKind::Type(this.lower_ty(ty)),
16171617
ImplItemKind::Macro(..) => panic!("Shouldn't exist any more"),
@@ -1723,9 +1723,9 @@ impl<'a> LoweringContext<'a> {
17231723
})
17241724
}
17251725

1726-
fn lower_method_sig(&mut self, sig: &MethodSig) -> hir::MethodSig {
1726+
fn lower_method_sig(&mut self, generics: &Generics, sig: &MethodSig) -> hir::MethodSig {
17271727
hir::MethodSig {
1728-
generics: self.lower_generics(&sig.generics),
1728+
generics: self.lower_generics(generics),
17291729
abi: sig.abi,
17301730
unsafety: self.lower_unsafety(sig.unsafety),
17311731
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
@@ -708,7 +708,6 @@ impl<'a, 'tcx> Visitor<'tcx> for Resolver<'a> {
708708
ItemRibKind
709709
}
710710
FnKind::Method(_, sig, _, _) => {
711-
self.visit_generics(&sig.generics);
712711
MethodRibKind(!sig.decl.has_self())
713712
}
714713
FnKind::Closure(_) => ClosureRibKind(node_id),
@@ -1845,6 +1844,7 @@ impl<'a> Resolver<'a> {
18451844

18461845
for trait_item in trait_items {
18471846
this.check_proc_macro_attrs(&trait_item.attrs);
1847+
this.visit_generics(&trait_item.generics);
18481848

18491849
match trait_item.node {
18501850
TraitItemKind::Const(ref ty, ref default) => {
@@ -1861,7 +1861,7 @@ impl<'a> Resolver<'a> {
18611861
}
18621862
TraitItemKind::Method(ref sig, _) => {
18631863
let type_parameters =
1864-
HasTypeParameters(&sig.generics,
1864+
HasTypeParameters(&trait_item.generics,
18651865
MethodRibKind(!sig.decl.has_self()));
18661866
this.with_type_parameter_rib(type_parameters, |this| {
18671867
visit::walk_trait_item(this, trait_item)
@@ -2056,6 +2056,7 @@ impl<'a> Resolver<'a> {
20562056
this.with_current_self_type(self_type, |this| {
20572057
for impl_item in impl_items {
20582058
this.check_proc_macro_attrs(&impl_item.attrs);
2059+
this.visit_generics(&impl_item.generics);
20592060
this.resolve_visibility(&impl_item.vis);
20602061
match impl_item.node {
20612062
ImplItemKind::Const(..) => {
@@ -2078,7 +2079,7 @@ impl<'a> Resolver<'a> {
20782079
// We also need a new scope for the method-
20792080
// specific type parameters.
20802081
let type_parameters =
2081-
HasTypeParameters(&sig.generics,
2082+
HasTypeParameters(&impl_item.generics,
20822083
MethodRibKind(!sig.decl.has_self()));
20832084
this.with_type_parameter_rib(type_parameters, |this| {
20842085
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
@@ -1174,7 +1174,6 @@ pub struct MethodSig {
11741174
pub constness: Spanned<Constness>,
11751175
pub abi: Abi,
11761176
pub decl: P<FnDecl>,
1177-
pub generics: Generics,
11781177
}
11791178

11801179
/// Represents an item declaration within a trait declaration,
@@ -1186,6 +1185,7 @@ pub struct TraitItem {
11861185
pub id: NodeId,
11871186
pub ident: Ident,
11881187
pub attrs: Vec<Attribute>,
1188+
pub generics: Generics,
11891189
pub node: TraitItemKind,
11901190
pub span: Span,
11911191
/// See `Item::tokens` for what this is
@@ -1207,6 +1207,7 @@ pub struct ImplItem {
12071207
pub vis: Visibility,
12081208
pub defaultness: Defaultness,
12091209
pub attrs: Vec<Attribute>,
1210+
pub generics: Generics,
12101211
pub node: ImplItemKind,
12111212
pub span: Span,
12121213
/// 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
@@ -1280,10 +1280,10 @@ impl<'a> Parser<'a> {
12801280
mut attrs: Vec<Attribute>) -> PResult<'a, TraitItem> {
12811281
let lo = self.span;
12821282

1283-
let (name, node) = if self.eat_keyword(keywords::Type) {
1283+
let (name, node, generics) = if self.eat_keyword(keywords::Type) {
12841284
let TyParam {ident, bounds, default, ..} = self.parse_ty_param(vec![])?;
12851285
self.expect(&token::Semi)?;
1286-
(ident, TraitItemKind::Type(bounds, default))
1286+
(ident, TraitItemKind::Type(bounds, default), ast::Generics::default())
12871287
} else if self.is_const_item() {
12881288
self.expect_keyword(keywords::Const)?;
12891289
let ident = self.parse_ident()?;
@@ -1298,7 +1298,7 @@ impl<'a> Parser<'a> {
12981298
self.expect(&token::Semi)?;
12991299
None
13001300
};
1301-
(ident, TraitItemKind::Const(ty, default))
1301+
(ident, TraitItemKind::Const(ty, default), ast::Generics::default())
13021302
} else if self.token.is_path_start() {
13031303
// trait item macro.
13041304
// code copied from parse_macro_use_or_failure... abstraction!
@@ -1321,7 +1321,7 @@ impl<'a> Parser<'a> {
13211321
}
13221322

13231323
let mac = respan(lo.to(self.prev_span), Mac_ { path: pth, tts: tts });
1324-
(keywords::Invalid.ident(), ast::TraitItemKind::Macro(mac))
1324+
(keywords::Invalid.ident(), ast::TraitItemKind::Macro(mac), ast::Generics::default())
13251325
} else {
13261326
let (constness, unsafety, abi) = self.parse_fn_front_matter()?;
13271327

@@ -1334,13 +1334,12 @@ impl<'a> Parser<'a> {
13341334
// definition...
13351335
p.parse_arg_general(false)
13361336
})?;
1337-
13381337
generics.where_clause = self.parse_where_clause()?;
1338+
13391339
let sig = ast::MethodSig {
13401340
unsafety,
13411341
constness,
13421342
decl: d,
1343-
generics,
13441343
abi,
13451344
};
13461345

@@ -1363,13 +1362,14 @@ impl<'a> Parser<'a> {
13631362
return Err(self.fatal(&format!("expected `;` or `{{`, found `{}`", token_str)));
13641363
}
13651364
};
1366-
(ident, ast::TraitItemKind::Method(sig, body))
1365+
(ident, ast::TraitItemKind::Method(sig, body), generics)
13671366
};
13681367

13691368
Ok(TraitItem {
13701369
id: ast::DUMMY_NODE_ID,
13711370
ident: name,
13721371
attrs,
1372+
generics,
13731373
node,
13741374
span: lo.to(self.prev_span),
13751375
tokens: None,
@@ -4834,12 +4834,12 @@ impl<'a> Parser<'a> {
48344834
let lo = self.span;
48354835
let vis = self.parse_visibility(false)?;
48364836
let defaultness = self.parse_defaultness()?;
4837-
let (name, node) = if self.eat_keyword(keywords::Type) {
4837+
let (name, node, generics) = if self.eat_keyword(keywords::Type) {
48384838
let name = self.parse_ident()?;
48394839
self.expect(&token::Eq)?;
48404840
let typ = self.parse_ty()?;
48414841
self.expect(&token::Semi)?;
4842-
(name, ast::ImplItemKind::Type(typ))
4842+
(name, ast::ImplItemKind::Type(typ), ast::Generics::default())
48434843
} else if self.is_const_item() {
48444844
self.expect_keyword(keywords::Const)?;
48454845
let name = self.parse_ident()?;
@@ -4848,11 +4848,11 @@ impl<'a> Parser<'a> {
48484848
self.expect(&token::Eq)?;
48494849
let expr = self.parse_expr()?;
48504850
self.expect(&token::Semi)?;
4851-
(name, ast::ImplItemKind::Const(typ, expr))
4851+
(name, ast::ImplItemKind::Const(typ, expr), ast::Generics::default())
48524852
} else {
4853-
let (name, inner_attrs, node) = self.parse_impl_method(&vis, at_end)?;
4853+
let (name, inner_attrs, generics, node) = self.parse_impl_method(&vis, at_end)?;
48544854
attrs.extend(inner_attrs);
4855-
(name, node)
4855+
(name, node, generics)
48564856
};
48574857

48584858
Ok(ImplItem {
@@ -4862,6 +4862,7 @@ impl<'a> Parser<'a> {
48624862
vis,
48634863
defaultness,
48644864
attrs,
4865+
generics,
48654866
node,
48664867
tokens: None,
48674868
})
@@ -4919,7 +4920,7 @@ impl<'a> Parser<'a> {
49194920

49204921
/// Parse a method or a macro invocation in a trait impl.
49214922
fn parse_impl_method(&mut self, vis: &Visibility, at_end: &mut bool)
4922-
-> PResult<'a, (Ident, Vec<ast::Attribute>, ast::ImplItemKind)> {
4923+
-> PResult<'a, (Ident, Vec<ast::Attribute>, ast::Generics, ast::ImplItemKind)> {
49234924
// code copied from parse_macro_use_or_failure... abstraction!
49244925
if self.token.is_path_start() {
49254926
// Method macro.
@@ -4946,7 +4947,7 @@ impl<'a> Parser<'a> {
49464947
}
49474948

49484949
let mac = respan(lo.to(self.prev_span), Mac_ { path: pth, tts: tts });
4949-
Ok((keywords::Invalid.ident(), vec![], ast::ImplItemKind::Macro(mac)))
4950+
Ok((keywords::Invalid.ident(), vec![], ast::Generics::default(), ast::ImplItemKind::Macro(mac)))
49504951
} else {
49514952
let (constness, unsafety, abi) = self.parse_fn_front_matter()?;
49524953
let ident = self.parse_ident()?;
@@ -4955,8 +4956,7 @@ impl<'a> Parser<'a> {
49554956
generics.where_clause = self.parse_where_clause()?;
49564957
*at_end = true;
49574958
let (inner_attrs, body) = self.parse_inner_attrs_and_block()?;
4958-
Ok((ident, inner_attrs, ast::ImplItemKind::Method(ast::MethodSig {
4959-
generics,
4959+
Ok((ident, inner_attrs, generics, ast::ImplItemKind::Method(ast::MethodSig {
49604960
abi,
49614961
unsafety,
49624962
constness,

src/libsyntax/print/pprust.rs

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

15231523
pub fn print_method_sig(&mut self,
15241524
ident: ast::Ident,
1525+
generics: &ast::Generics,
15251526
m: &ast::MethodSig,
15261527
vis: &ast::Visibility)
15271528
-> io::Result<()> {
@@ -1530,7 +1531,7 @@ impl<'a> State<'a> {
15301531
m.constness.node,
15311532
m.abi,
15321533
Some(ident),
1533-
&m.generics,
1534+
&generics,
15341535
vis)
15351536
}
15361537

@@ -1550,7 +1551,7 @@ impl<'a> State<'a> {
15501551
if body.is_some() {
15511552
self.head("")?;
15521553
}
1553-
self.print_method_sig(ti.ident, sig, &ast::Visibility::Inherited)?;
1554+
self.print_method_sig(ti.ident, &ti.generics, sig, &ast::Visibility::Inherited)?;
15541555
if let Some(ref body) = *body {
15551556
self.nbsp()?;
15561557
self.print_block_with_attrs(body, &ti.attrs)?;
@@ -1589,7 +1590,7 @@ impl<'a> State<'a> {
15891590
}
15901591
ast::ImplItemKind::Method(ref sig, ref body) => {
15911592
self.head("")?;
1592-
self.print_method_sig(ii.ident, sig, &ii.vis)?;
1593+
self.print_method_sig(ii.ident, &ii.generics, sig, &ii.vis)?;
15931594
self.nbsp()?;
15941595
self.print_block_with_attrs(body, &ii.attrs)?;
15951596
}

0 commit comments

Comments
 (0)