Skip to content

Commit c9b03c2

Browse files
committed
Auto merge of #23265 - eddyb:meth-ast-refactor, r=nikomatsakis
The end result is that common fields (id, name, attributes, etc.) are stored in now-structures `ImplItem` and `TraitItem`. The signature of a method is no longer duplicated between methods with a body (default/impl) and those without, they now share `MethodSig`. This is also a [breaking-change] because of minor bugfixes and changes to syntax extensions: * `pub fn` methods in a trait no longer parse - remove the `pub`, it has no meaning anymore * `MacResult::make_methods` is now `make_impl_items` and the return type has changed accordingly * `quote_method` is gone, because `P<ast::Method>` doesn't exist and it couldn't represent a full method anyways - could be replaced by `quote_impl_item`/`quote_trait_item` in the future, but I do hope we realize how silly that combinatorial macro expansion is and settle on a single `quote` macro + some type hints - or just no types at all (only token-trees) r? @nikomatsakis This is necessary (hopefully also sufficient) for associated constants.
2 parents 538840b + 9da9185 commit c9b03c2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1516
-2575
lines changed

src/librustc/lint/context.rs

+16-25
Original file line numberDiff line numberDiff line change
@@ -519,28 +519,8 @@ impl<'a, 'tcx, 'v> Visitor<'v> for Context<'a, 'tcx> {
519519

520520
fn visit_fn(&mut self, fk: FnKind<'v>, decl: &'v ast::FnDecl,
521521
body: &'v ast::Block, span: Span, id: ast::NodeId) {
522-
match fk {
523-
visit::FkMethod(_, _, m) => {
524-
self.with_lint_attrs(&m.attrs, |cx| {
525-
run_lints!(cx, check_fn, fk, decl, body, span, id);
526-
cx.visit_ids(|v| {
527-
v.visit_fn(fk, decl, body, span, id);
528-
});
529-
visit::walk_fn(cx, fk, decl, body, span);
530-
})
531-
},
532-
_ => {
533-
run_lints!(self, check_fn, fk, decl, body, span, id);
534-
visit::walk_fn(self, fk, decl, body, span);
535-
}
536-
}
537-
}
538-
539-
fn visit_ty_method(&mut self, t: &ast::TypeMethod) {
540-
self.with_lint_attrs(&t.attrs, |cx| {
541-
run_lints!(cx, check_ty_method, t);
542-
visit::walk_ty_method(cx, t);
543-
})
522+
run_lints!(self, check_fn, fk, decl, body, span, id);
523+
visit::walk_fn(self, fk, decl, body, span);
544524
}
545525

546526
fn visit_struct_def(&mut self,
@@ -611,9 +591,20 @@ impl<'a, 'tcx, 'v> Visitor<'v> for Context<'a, 'tcx> {
611591
visit::walk_generics(self, g);
612592
}
613593

614-
fn visit_trait_item(&mut self, m: &ast::TraitItem) {
615-
run_lints!(self, check_trait_item, m);
616-
visit::walk_trait_item(self, m);
594+
fn visit_trait_item(&mut self, trait_item: &ast::TraitItem) {
595+
self.with_lint_attrs(&trait_item.attrs, |cx| {
596+
run_lints!(cx, check_trait_item, trait_item);
597+
cx.visit_ids(|v| v.visit_trait_item(trait_item));
598+
visit::walk_trait_item(cx, trait_item);
599+
});
600+
}
601+
602+
fn visit_impl_item(&mut self, impl_item: &ast::ImplItem) {
603+
self.with_lint_attrs(&impl_item.attrs, |cx| {
604+
run_lints!(cx, check_impl_item, impl_item);
605+
cx.visit_ids(|v| v.visit_impl_item(impl_item));
606+
visit::walk_impl_item(cx, impl_item);
607+
});
617608
}
618609

619610
fn visit_opt_lifetime_ref(&mut self, sp: Span, lt: &Option<ast::Lifetime>) {

src/librustc/lint/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ pub trait LintPass {
143143
fn check_generics(&mut self, _: &Context, _: &ast::Generics) { }
144144
fn check_fn(&mut self, _: &Context,
145145
_: FnKind, _: &ast::FnDecl, _: &ast::Block, _: Span, _: ast::NodeId) { }
146-
fn check_ty_method(&mut self, _: &Context, _: &ast::TypeMethod) { }
147146
fn check_trait_item(&mut self, _: &Context, _: &ast::TraitItem) { }
147+
fn check_impl_item(&mut self, _: &Context, _: &ast::ImplItem) { }
148148
fn check_struct_def(&mut self, _: &Context,
149149
_: &ast::StructDef, _: ast::Ident, _: &ast::Generics, _: ast::NodeId) { }
150150
fn check_struct_def_post(&mut self, _: &Context,

src/librustc/metadata/encoder.rs

+36-68
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ fn encode_info_for_method<'a, 'tcx>(ecx: &EncodeContext<'a, 'tcx>,
808808
impl_path: PathElems,
809809
is_default_impl: bool,
810810
parent_id: NodeId,
811-
ast_item_opt: Option<&ast::ImplItem>) {
811+
impl_item_opt: Option<&ast::ImplItem>) {
812812

813813
debug!("encode_info_for_method: {:?} {:?}", m.def_id,
814814
token::get_name(m.name));
@@ -826,21 +826,20 @@ fn encode_info_for_method<'a, 'tcx>(ecx: &EncodeContext<'a, 'tcx>,
826826

827827
let elem = ast_map::PathName(m.name);
828828
encode_path(rbml_w, impl_path.chain(Some(elem).into_iter()));
829-
match ast_item_opt {
830-
Some(&ast::MethodImplItem(ref ast_method)) => {
831-
encode_attributes(rbml_w, &ast_method.attrs);
829+
if let Some(impl_item) = impl_item_opt {
830+
if let ast::MethodImplItem(ref sig, _) = impl_item.node {
831+
encode_attributes(rbml_w, &impl_item.attrs);
832832
let scheme = ty::lookup_item_type(ecx.tcx, m.def_id);
833833
let any_types = !scheme.generics.types.is_empty();
834-
if any_types || is_default_impl || attr::requests_inline(&ast_method.attrs) {
834+
if any_types || is_default_impl || attr::requests_inline(&impl_item.attrs) {
835835
encode_inlined_item(ecx, rbml_w, IIImplItemRef(local_def(parent_id),
836-
ast_item_opt.unwrap()));
836+
impl_item));
837837
}
838838
if !any_types {
839839
encode_symbol(ecx, rbml_w, m.def_id.node);
840840
}
841-
encode_method_argument_names(rbml_w, ast_method.pe_fn_decl());
841+
encode_method_argument_names(rbml_w, &sig.decl);
842842
}
843-
Some(_) | None => {}
844843
}
845844

846845
rbml_w.end_tag();
@@ -851,7 +850,7 @@ fn encode_info_for_associated_type(ecx: &EncodeContext,
851850
associated_type: &ty::AssociatedType,
852851
impl_path: PathElems,
853852
parent_id: NodeId,
854-
typedef_opt: Option<P<ast::Typedef>>) {
853+
impl_item_opt: Option<&ast::ImplItem>) {
855854
debug!("encode_info_for_associated_type({:?},{:?})",
856855
associated_type.def_id,
857856
token::get_name(associated_type.name));
@@ -873,13 +872,9 @@ fn encode_info_for_associated_type(ecx: &EncodeContext,
873872
let elem = ast_map::PathName(associated_type.name);
874873
encode_path(rbml_w, impl_path.chain(Some(elem).into_iter()));
875874

876-
match typedef_opt {
877-
None => {}
878-
Some(typedef) => {
879-
encode_attributes(rbml_w, &typedef.attrs);
880-
encode_type(ecx, rbml_w, ty::node_id_to_type(ecx.tcx,
881-
typedef.id));
882-
}
875+
if let Some(ii) = impl_item_opt {
876+
encode_attributes(rbml_w, &ii.attrs);
877+
encode_type(ecx, rbml_w, ty::node_id_to_type(ecx.tcx, ii.id));
883878
}
884879

885880
rbml_w.end_tag();
@@ -1226,7 +1221,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
12261221
let num_implemented_methods = ast_items.len();
12271222
for (i, &trait_item_def_id) in items.iter().enumerate() {
12281223
let ast_item = if i < num_implemented_methods {
1229-
Some(&ast_items[i])
1224+
Some(&*ast_items[i])
12301225
} else {
12311226
None
12321227
};
@@ -1236,11 +1231,8 @@ fn encode_info_for_item(ecx: &EncodeContext,
12361231
pos: rbml_w.mark_stable_position(),
12371232
});
12381233

1239-
let trait_item_type =
1240-
ty::impl_or_trait_item(tcx, trait_item_def_id.def_id());
1241-
match (trait_item_type, ast_item) {
1242-
(ty::MethodTraitItem(ref method_type),
1243-
Some(&ast::MethodImplItem(_))) => {
1234+
match ty::impl_or_trait_item(tcx, trait_item_def_id.def_id()) {
1235+
ty::MethodTraitItem(ref method_type) => {
12441236
encode_info_for_method(ecx,
12451237
rbml_w,
12461238
&**method_type,
@@ -1249,31 +1241,13 @@ fn encode_info_for_item(ecx: &EncodeContext,
12491241
item.id,
12501242
ast_item)
12511243
}
1252-
(ty::MethodTraitItem(ref method_type), _) => {
1253-
encode_info_for_method(ecx,
1254-
rbml_w,
1255-
&**method_type,
1256-
path.clone(),
1257-
false,
1258-
item.id,
1259-
None)
1260-
}
1261-
(ty::TypeTraitItem(ref associated_type),
1262-
Some(&ast::TypeImplItem(ref typedef))) => {
1263-
encode_info_for_associated_type(ecx,
1264-
rbml_w,
1265-
&**associated_type,
1266-
path.clone(),
1267-
item.id,
1268-
Some((*typedef).clone()))
1269-
}
1270-
(ty::TypeTraitItem(ref associated_type), _) => {
1244+
ty::TypeTraitItem(ref associated_type) => {
12711245
encode_info_for_associated_type(ecx,
12721246
rbml_w,
12731247
&**associated_type,
12741248
path.clone(),
12751249
item.id,
1276-
None)
1250+
ast_item)
12771251
}
12781252
}
12791253
}
@@ -1387,35 +1361,29 @@ fn encode_info_for_item(ecx: &EncodeContext,
13871361

13881362
encode_parent_sort(rbml_w, 't');
13891363

1390-
let trait_item = &ms[i];
1391-
let encode_trait_item = |rbml_w: &mut Encoder| {
1392-
// If this is a static method, we've already
1393-
// encoded this.
1394-
if is_nonstatic_method {
1395-
// FIXME: I feel like there is something funny
1396-
// going on.
1397-
encode_bounds_and_type_for_item(rbml_w, ecx, item_def_id.def_id().local_id());
1398-
}
1399-
};
1400-
match trait_item {
1401-
&ast::RequiredMethod(ref m) => {
1402-
encode_attributes(rbml_w, &m.attrs);
1403-
encode_trait_item(rbml_w);
1404-
encode_item_sort(rbml_w, 'r');
1405-
encode_method_argument_names(rbml_w, &*m.decl);
1406-
}
1364+
let trait_item = &*ms[i];
1365+
encode_attributes(rbml_w, &trait_item.attrs);
1366+
match trait_item.node {
1367+
ast::MethodTraitItem(ref sig, ref body) => {
1368+
// If this is a static method, we've already
1369+
// encoded this.
1370+
if is_nonstatic_method {
1371+
// FIXME: I feel like there is something funny
1372+
// going on.
1373+
encode_bounds_and_type_for_item(rbml_w, ecx,
1374+
item_def_id.def_id().local_id());
1375+
}
14071376

1408-
&ast::ProvidedMethod(ref m) => {
1409-
encode_attributes(rbml_w, &m.attrs);
1410-
encode_trait_item(rbml_w);
1411-
encode_item_sort(rbml_w, 'p');
1412-
encode_inlined_item(ecx, rbml_w, IITraitItemRef(def_id, trait_item));
1413-
encode_method_argument_names(rbml_w, &*m.pe_fn_decl());
1377+
if body.is_some() {
1378+
encode_item_sort(rbml_w, 'p');
1379+
encode_inlined_item(ecx, rbml_w, IITraitItemRef(def_id, trait_item));
1380+
} else {
1381+
encode_item_sort(rbml_w, 'r');
1382+
}
1383+
encode_method_argument_names(rbml_w, &sig.decl);
14141384
}
14151385

1416-
&ast::TypeTraitItem(ref associated_type) => {
1417-
encode_attributes(rbml_w,
1418-
&associated_type.attrs);
1386+
ast::TypeTraitItem(..) => {
14191387
encode_item_sort(rbml_w, 't');
14201388
}
14211389
}

src/librustc/middle/astencode.rs

+13-50
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ use middle::ty::{self, Ty, MethodCall, MethodCallee, MethodOrigin};
3232
use util::ppaux::ty_to_string;
3333

3434
use syntax::{ast, ast_map, ast_util, codemap, fold};
35-
use syntax::ast_util::PostExpansionMethod;
3635
use syntax::codemap::Span;
3736
use syntax::fold::Folder;
3837
use syntax::parse::token;
@@ -81,11 +80,8 @@ pub fn encode_inlined_item(ecx: &e::EncodeContext,
8180
let id = match ii {
8281
e::IIItemRef(i) => i.id,
8382
e::IIForeignRef(i) => i.id,
84-
e::IITraitItemRef(_, &ast::ProvidedMethod(ref m)) => m.id,
85-
e::IITraitItemRef(_, &ast::RequiredMethod(ref m)) => m.id,
86-
e::IITraitItemRef(_, &ast::TypeTraitItem(ref ti)) => ti.ty_param.id,
87-
e::IIImplItemRef(_, &ast::MethodImplItem(ref m)) => m.id,
88-
e::IIImplItemRef(_, &ast::TypeImplItem(ref ti)) => ti.id,
83+
e::IITraitItemRef(_, ti) => ti.id,
84+
e::IIImplItemRef(_, ii) => ii.id,
8985
};
9086
debug!("> Encoding inlined item: {} ({:?})",
9187
ecx.tcx.map.path_to_string(id),
@@ -157,19 +153,8 @@ pub fn decode_inlined_item<'tcx>(cdata: &cstore::crate_metadata,
157153
let ident = match *ii {
158154
ast::IIItem(ref i) => i.ident,
159155
ast::IIForeign(ref i) => i.ident,
160-
ast::IITraitItem(_, ref ti) => {
161-
match *ti {
162-
ast::ProvidedMethod(ref m) => m.pe_ident(),
163-
ast::RequiredMethod(ref ty_m) => ty_m.ident,
164-
ast::TypeTraitItem(ref ti) => ti.ty_param.ident,
165-
}
166-
},
167-
ast::IIImplItem(_, ref m) => {
168-
match *m {
169-
ast::MethodImplItem(ref m) => m.pe_ident(),
170-
ast::TypeImplItem(ref ti) => ti.ident,
171-
}
172-
}
156+
ast::IITraitItem(_, ref ti) => ti.ident,
157+
ast::IIImplItem(_, ref ii) => ii.ident
173158
};
174159
debug!("Fn named: {}", token::get_ident(ident));
175160
debug!("< Decoded inlined fn: {}::{}",
@@ -412,38 +397,16 @@ fn simplify_ast(ii: e::InlinedItemRef) -> ast::InlinedItem {
412397
.expect_one("expected one item"))
413398
}
414399
e::IITraitItemRef(d, ti) => {
415-
ast::IITraitItem(d, match *ti {
416-
ast::ProvidedMethod(ref m) => {
417-
ast::ProvidedMethod(
418-
fold::noop_fold_method(m.clone(), &mut fld)
419-
.expect_one("noop_fold_method must produce \
420-
exactly one method"))
421-
}
422-
ast::RequiredMethod(ref ty_m) => {
423-
ast::RequiredMethod(
424-
fold::noop_fold_type_method(ty_m.clone(), &mut fld))
425-
}
426-
ast::TypeTraitItem(ref associated_type) => {
427-
ast::TypeTraitItem(
428-
P(fold::noop_fold_associated_type(
429-
(**associated_type).clone(),
430-
&mut fld)))
431-
}
432-
})
400+
ast::IITraitItem(d,
401+
fold::noop_fold_trait_item(P(ti.clone()), &mut fld)
402+
.expect_one("noop_fold_trait_item must produce \
403+
exactly one trait item"))
433404
}
434-
e::IIImplItemRef(d, m) => {
435-
ast::IIImplItem(d, match *m {
436-
ast::MethodImplItem(ref m) => {
437-
ast::MethodImplItem(
438-
fold::noop_fold_method(m.clone(), &mut fld)
439-
.expect_one("noop_fold_method must produce \
440-
exactly one method"))
441-
}
442-
ast::TypeImplItem(ref td) => {
443-
ast::TypeImplItem(
444-
P(fold::noop_fold_typedef((**td).clone(), &mut fld)))
445-
}
446-
})
405+
e::IIImplItemRef(d, ii) => {
406+
ast::IIImplItem(d,
407+
fold::noop_fold_impl_item(P(ii.clone()), &mut fld)
408+
.expect_one("noop_fold_impl_item must produce \
409+
exactly one impl item"))
447410
}
448411
e::IIForeignRef(i) => {
449412
ast::IIForeign(fold::noop_fold_foreign_item(P(i.clone()), &mut fld))

0 commit comments

Comments
 (0)