Skip to content

Commit a8d076d

Browse files
committed
syntax/rustc: Make some metadata-related functions take slices, kill bad copies
Too small to review.
1 parent 8869e72 commit a8d076d

File tree

5 files changed

+67
-69
lines changed

5 files changed

+67
-69
lines changed

src/librustc/metadata/encoder.rs

+59-61
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ type abbrev_map = map::HashMap<ty::t, tyencode::ty_abbrev>;
6868

6969
pub type encode_inlined_item = fn@(ecx: @encode_ctxt,
7070
ebml_w: writer::Encoder,
71-
path: ast_map::path,
71+
path: &[ast_map::path_elt],
7272
ii: ast::inlined_item);
7373

7474
type encode_parms = {
@@ -194,7 +194,7 @@ fn encode_ty_type_param_bounds(ebml_w: writer::Encoder, ecx: @encode_ctxt,
194194
}
195195

196196
fn encode_type_param_bounds(ebml_w: writer::Encoder, ecx: @encode_ctxt,
197-
params: ~[ty_param]) {
197+
params: &[ty_param]) {
198198
let ty_param_bounds =
199199
@params.map(|param| ecx.tcx.ty_param_bounds.get(param.id));
200200
encode_ty_type_param_bounds(ebml_w, ecx, ty_param_bounds);
@@ -268,9 +268,10 @@ fn encode_parent_item(ebml_w: writer::Encoder, id: def_id) {
268268
}
269269

270270
fn encode_enum_variant_info(ecx: @encode_ctxt, ebml_w: writer::Encoder,
271-
id: node_id, variants: ~[variant],
272-
path: ast_map::path, index: @mut ~[entry<int>],
273-
ty_params: ~[ty_param]) {
271+
id: node_id, variants: &[variant],
272+
path: &[ast_map::path_elt],
273+
index: @mut ~[entry<int>],
274+
ty_params: &[ty_param]) {
274275
let mut disr_val = 0;
275276
let mut i = 0;
276277
let vi = ty::enum_variants(ecx.tcx,
@@ -297,8 +298,8 @@ fn encode_enum_variant_info(ecx: @encode_ctxt, ebml_w: writer::Encoder,
297298
encode_disr_val(ecx, ebml_w, vi[i].disr_val);
298299
disr_val = vi[i].disr_val;
299300
}
300-
encode_type_param_bounds(ebml_w, ecx, /*bad*/copy ty_params);
301-
encode_path(ecx, ebml_w, &path,
301+
encode_type_param_bounds(ebml_w, ecx, ty_params);
302+
encode_path(ecx, ebml_w, path,
302303
ast_map::path_name(variant.node.name));
303304
ebml_w.end_tag();
304305
disr_val += 1;
@@ -307,7 +308,7 @@ fn encode_enum_variant_info(ecx: @encode_ctxt, ebml_w: writer::Encoder,
307308
}
308309

309310
fn encode_path(ecx: @encode_ctxt, ebml_w: writer::Encoder,
310-
path: &ast_map::path, name: ast_map::path_elt) {
311+
path: &[ast_map::path_elt], name: ast_map::path_elt) {
311312
fn encode_path_elt(ecx: @encode_ctxt, ebml_w: writer::Encoder,
312313
elt: ast_map::path_elt) {
313314
let (tag, name) = match elt {
@@ -328,7 +329,7 @@ fn encode_path(ecx: @encode_ctxt, ebml_w: writer::Encoder,
328329
}
329330

330331
fn encode_info_for_mod(ecx: @encode_ctxt, ebml_w: writer::Encoder,
331-
md: _mod, id: node_id, +path: ast_map::path,
332+
md: _mod, id: node_id, path: &[ast_map::path_elt],
332333
name: ident) {
333334
ebml_w.start_tag(tag_items_data_item);
334335
encode_def_id(ebml_w, local_def(id));
@@ -357,7 +358,7 @@ fn encode_info_for_mod(ecx: @encode_ctxt, ebml_w: writer::Encoder,
357358
}
358359
}
359360

360-
encode_path(ecx, ebml_w, &path, ast_map::path_mod(name));
361+
encode_path(ecx, ebml_w, path, ast_map::path_mod(name));
361362

362363
// Encode the reexports of this module.
363364
debug!("(encoding info for module) encoding reexports for %d", id);
@@ -434,8 +435,8 @@ fn encode_method_sort(ebml_w: writer::Encoder, sort: char) {
434435

435436
/* Returns an index of items in this class */
436437
fn encode_info_for_struct(ecx: @encode_ctxt, ebml_w: writer::Encoder,
437-
path: ast_map::path,
438-
fields: ~[@struct_field],
438+
path: &[ast_map::path_elt],
439+
fields: &[@struct_field],
439440
global_index: @mut~[entry<int>]) -> ~[entry<int>] {
440441
/* Each class has its own index, since different classes
441442
may have fields with the same name */
@@ -455,7 +456,7 @@ fn encode_info_for_struct(ecx: @encode_ctxt, ebml_w: writer::Encoder,
455456
tcx.sess.str_of(nm), id);
456457
encode_visibility(ebml_w, vis);
457458
encode_name(ecx, ebml_w, nm);
458-
encode_path(ecx, ebml_w, &path, ast_map::path_name(nm));
459+
encode_path(ecx, ebml_w, path, ast_map::path_name(nm));
459460
encode_type(ecx, ebml_w, node_id_to_type(tcx, id));
460461
encode_mutability(ebml_w, mt);
461462
encode_def_id(ebml_w, local_def(id));
@@ -469,8 +470,8 @@ fn encode_info_for_struct(ecx: @encode_ctxt, ebml_w: writer::Encoder,
469470

470471
// This is for encoding info for ctors and dtors
471472
fn encode_info_for_ctor(ecx: @encode_ctxt, ebml_w: writer::Encoder,
472-
id: node_id, ident: ident, +path: ast_map::path,
473-
item: Option<inlined_item>, +tps: ~[ty_param]) {
473+
id: node_id, ident: ident, path: &[ast_map::path_elt],
474+
item: Option<inlined_item>, tps: &[ty_param]) {
474475
ebml_w.start_tag(tag_items_data_item);
475476
encode_name(ecx, ebml_w, ident);
476477
encode_def_id(ebml_w, local_def(id));
@@ -481,7 +482,7 @@ fn encode_info_for_ctor(ecx: @encode_ctxt, ebml_w: writer::Encoder,
481482
ecx.tcx.sess.str_of(ident),
482483
ty_to_str(ecx.tcx, its_ty), id);
483484
encode_type(ecx, ebml_w, its_ty);
484-
encode_path(ecx, ebml_w, &path, ast_map::path_name(ident));
485+
encode_path(ecx, ebml_w, path, ast_map::path_name(ident));
485486
match item {
486487
Some(ref it) => {
487488
(ecx.encode_inlined_item)(ecx, ebml_w, path, (*it));
@@ -495,7 +496,7 @@ fn encode_info_for_ctor(ecx: @encode_ctxt, ebml_w: writer::Encoder,
495496

496497
fn encode_info_for_method(ecx: @encode_ctxt,
497498
ebml_w: writer::Encoder,
498-
+impl_path: ast_map::path,
499+
impl_path: &[ast_map::path_elt],
499500
should_inline: bool,
500501
parent_id: node_id,
501502
m: @method,
@@ -514,7 +515,7 @@ fn encode_info_for_method(ecx: @encode_ctxt,
514515
encode_type_param_bounds(ebml_w, ecx, all_tps);
515516
encode_type(ecx, ebml_w, node_id_to_type(ecx.tcx, m.id));
516517
encode_name(ecx, ebml_w, m.ident);
517-
encode_path(ecx, ebml_w, &impl_path, ast_map::path_name(m.ident));
518+
encode_path(ecx, ebml_w, impl_path, ast_map::path_name(m.ident));
518519
encode_self_type(ebml_w, m.self_ty.node);
519520
if len > 0u || should_inline {
520521
(ecx.encode_inlined_item)(
@@ -544,7 +545,7 @@ fn purity_static_method_family(p: purity) -> char {
544545
}
545546

546547

547-
fn should_inline(attrs: ~[attribute]) -> bool {
548+
fn should_inline(attrs: &[attribute]) -> bool {
548549
match attr::find_inline_attr(attrs) {
549550
attr::ia_none | attr::ia_never => false,
550551
attr::ia_hint | attr::ia_always => true
@@ -554,7 +555,7 @@ fn should_inline(attrs: ~[attribute]) -> bool {
554555

555556
fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: writer::Encoder,
556557
item: @item, index: @mut ~[entry<int>],
557-
+path: ast_map::path) {
558+
path: &[ast_map::path_elt]) {
558559

559560
let tcx = ecx.tcx;
560561
let must_write =
@@ -582,7 +583,7 @@ fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: writer::Encoder,
582583
encode_family(ebml_w, 'c');
583584
encode_type(ecx, ebml_w, node_id_to_type(tcx, item.id));
584585
encode_symbol(ecx, ebml_w, item.id);
585-
encode_path(ecx, ebml_w, &path, ast_map::path_name(item.ident));
586+
encode_path(ecx, ebml_w, path, ast_map::path_name(item.ident));
586587
ebml_w.end_tag();
587588
}
588589
item_fn(_, purity, tps, _) => {
@@ -593,8 +594,8 @@ fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: writer::Encoder,
593594
let tps_len = tps.len();
594595
encode_type_param_bounds(ebml_w, ecx, tps);
595596
encode_type(ecx, ebml_w, node_id_to_type(tcx, item.id));
596-
encode_path(ecx, ebml_w, &path, ast_map::path_name(item.ident));
597-
encode_attributes(ebml_w, /*bad*/copy item.attrs);
597+
encode_path(ecx, ebml_w, path, ast_map::path_name(item.ident));
598+
encode_attributes(ebml_w, item.attrs);
598599
if tps_len > 0u || should_inline(item.attrs) {
599600
(ecx.encode_inlined_item)(ecx, ebml_w, path, ii_item(item));
600601
} else {
@@ -612,7 +613,7 @@ fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: writer::Encoder,
612613
encode_def_id(ebml_w, local_def(item.id));
613614
encode_family(ebml_w, 'n');
614615
encode_name(ecx, ebml_w, item.ident);
615-
encode_path(ecx, ebml_w, &path, ast_map::path_name(item.ident));
616+
encode_path(ecx, ebml_w, path, ast_map::path_name(item.ident));
616617
ebml_w.end_tag();
617618
}
618619
item_ty(_, tps) => {
@@ -623,7 +624,7 @@ fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: writer::Encoder,
623624
encode_type_param_bounds(ebml_w, ecx, tps);
624625
encode_type(ecx, ebml_w, node_id_to_type(tcx, item.id));
625626
encode_name(ecx, ebml_w, item.ident);
626-
encode_path(ecx, ebml_w, &path, ast_map::path_name(item.ident));
627+
encode_path(ecx, ebml_w, path, ast_map::path_name(item.ident));
627628
encode_region_param(ecx, ebml_w, item);
628629
ebml_w.end_tag();
629630
}
@@ -632,44 +633,42 @@ fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: writer::Encoder,
632633
do ebml_w.wr_tag(tag_items_data_item) {
633634
encode_def_id(ebml_w, local_def(item.id));
634635
encode_family(ebml_w, 't');
635-
encode_type_param_bounds(ebml_w, ecx, /*bad*/copy *tps);
636+
encode_type_param_bounds(ebml_w, ecx, *tps);
636637
encode_type(ecx, ebml_w, node_id_to_type(tcx, item.id));
637638
encode_name(ecx, ebml_w, item.ident);
638639
for (*enum_definition).variants.each |v| {
639640
encode_variant_id(ebml_w, local_def(v.node.id));
640641
}
641-
(ecx.encode_inlined_item)(ecx, ebml_w, /*bad*/copy path,
642-
ii_item(item));
643-
encode_path(ecx, ebml_w, &path, ast_map::path_name(item.ident));
642+
(ecx.encode_inlined_item)(ecx, ebml_w, path, ii_item(item));
643+
encode_path(ecx, ebml_w, path, ast_map::path_name(item.ident));
644644
encode_region_param(ecx, ebml_w, item);
645645
}
646646
encode_enum_variant_info(ecx,
647647
ebml_w,
648648
item.id,
649-
/*bad*/copy (*enum_definition).variants,
649+
(*enum_definition).variants,
650650
path,
651651
index,
652-
/*bad*/copy *tps);
652+
*tps);
653653
}
654654
item_struct(struct_def, tps) => {
655655
/* First, encode the fields
656656
These come first because we need to write them to make
657657
the index, and the index needs to be in the item for the
658658
class itself */
659-
// XXX: Bad copy of `path`.
660-
let idx = encode_info_for_struct(ecx, ebml_w, copy path,
661-
/*bad*/copy struct_def.fields, index);
659+
let idx = encode_info_for_struct(ecx, ebml_w, path,
660+
struct_def.fields, index);
662661
/* Encode the dtor */
663662
do struct_def.dtor.iter |dtor| {
664663
index.push({val: dtor.node.id, pos: ebml_w.writer.tell()});
665664
encode_info_for_ctor(ecx, ebml_w, dtor.node.id,
666665
ecx.tcx.sess.ident_of(
667666
ecx.tcx.sess.str_of(item.ident) +
668667
~"_dtor"),
669-
/*bad*/copy path, if tps.len() > 0u {
668+
path, if tps.len() > 0u {
670669
Some(ii_dtor(*dtor, item.ident, tps,
671670
local_def(item.id))) }
672-
else { None }, /*bad*/copy tps);
671+
else { None }, tps);
673672
}
674673

675674
/* Index the class*/
@@ -681,7 +680,7 @@ fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: writer::Encoder,
681680
encode_type_param_bounds(ebml_w, ecx, tps);
682681
encode_type(ecx, ebml_w, node_id_to_type(tcx, item.id));
683682
encode_name(ecx, ebml_w, item.ident);
684-
encode_path(ecx, ebml_w, &path, ast_map::path_name(item.ident));
683+
encode_path(ecx, ebml_w, path, ast_map::path_name(item.ident));
685684
encode_region_param(ecx, ebml_w, item);
686685
/* Encode the dtor */
687686
/* Encode id for dtor */
@@ -718,11 +717,10 @@ fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: writer::Encoder,
718717
encode_def_id(ebml_w, local_def(item.id));
719718
encode_family(ebml_w, 'i');
720719
encode_region_param(ecx, ebml_w, item);
721-
// XXX: Bad copy.
722-
encode_type_param_bounds(ebml_w, ecx, copy tps);
720+
encode_type_param_bounds(ebml_w, ecx, tps);
723721
encode_type(ecx, ebml_w, node_id_to_type(tcx, item.id));
724722
encode_name(ecx, ebml_w, item.ident);
725-
encode_attributes(ebml_w, /*bad*/copy item.attrs);
723+
encode_attributes(ebml_w, item.attrs);
726724
match ty.node {
727725
ast::ty_path(path, _) if path.idents.len() == 1 => {
728726
encode_impl_type_basename(ecx, ebml_w,
@@ -739,15 +737,17 @@ fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: writer::Encoder,
739737
do opt_trait.iter() |associated_trait| {
740738
encode_trait_ref(ebml_w, ecx, *associated_trait);
741739
}
742-
encode_path(ecx, ebml_w, &path, ast_map::path_name(item.ident));
740+
encode_path(ecx, ebml_w, path, ast_map::path_name(item.ident));
743741
ebml_w.end_tag();
744742

745-
let impl_path = vec::append_one(path,
746-
ast_map::path_name(item.ident));
743+
// >:-<
744+
let mut impl_path = vec::append(~[], path);
745+
impl_path += ~[ast_map::path_name(item.ident)];
746+
747747
for methods.each |m| {
748748
index.push({val: m.id, pos: ebml_w.writer.tell()});
749-
encode_info_for_method(ecx, ebml_w, /*bad*/copy impl_path,
750-
should_inline(/*bad*/copy m.attrs),
749+
encode_info_for_method(ecx, ebml_w, impl_path,
750+
should_inline(m.attrs),
751751
item.id, *m,
752752
vec::append(/*bad*/copy tps, m.tps));
753753
}
@@ -760,10 +760,10 @@ fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: writer::Encoder,
760760
encode_def_id(ebml_w, local_def(item.id));
761761
encode_family(ebml_w, 'I');
762762
encode_region_param(ecx, ebml_w, item);
763-
encode_type_param_bounds(ebml_w, ecx, /*bad*/copy *tps);
763+
encode_type_param_bounds(ebml_w, ecx, *tps);
764764
encode_type(ecx, ebml_w, node_id_to_type(tcx, item.id));
765765
encode_name(ecx, ebml_w, item.ident);
766-
encode_attributes(ebml_w, /*bad*/copy item.attrs);
766+
encode_attributes(ebml_w, item.attrs);
767767
let mut i = 0u;
768768
for vec::each(*ty::trait_methods(tcx, local_def(item.id))) |mty| {
769769
match (*ms)[i] {
@@ -772,7 +772,7 @@ fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: writer::Encoder,
772772
encode_def_id(ebml_w, local_def((*ty_m).id));
773773
encode_name(ecx, ebml_w, mty.ident);
774774
encode_type_param_bounds(ebml_w, ecx,
775-
/*bad*/copy (*ty_m).tps);
775+
(*ty_m).tps);
776776
encode_type(ecx, ebml_w, ty::mk_fn(tcx, /*bad*/copy mty.fty));
777777
encode_family(ebml_w, purity_fn_family(mty.fty.meta.purity));
778778
encode_self_type(ebml_w, mty.self_ty);
@@ -785,17 +785,17 @@ fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: writer::Encoder,
785785
ebml_w.start_tag(tag_item_trait_method);
786786
encode_def_id(ebml_w, local_def(m.id));
787787
encode_name(ecx, ebml_w, mty.ident);
788-
encode_type_param_bounds(ebml_w, ecx, /*bad*/copy m.tps);
788+
encode_type_param_bounds(ebml_w, ecx, m.tps);
789789
encode_type(ecx, ebml_w, ty::mk_fn(tcx, /*bad*/copy mty.fty));
790790
encode_family(ebml_w, purity_fn_family(mty.fty.meta.purity));
791791
encode_self_type(ebml_w, mty.self_ty);
792792
encode_method_sort(ebml_w, 'p');
793793
ebml_w.end_tag();
794794
}
795795
}
796-
i += 1u;
796+
i += 1;
797797
}
798-
encode_path(ecx, ebml_w, &path, ast_map::path_name(item.ident));
798+
encode_path(ecx, ebml_w, path, ast_map::path_name(item.ident));
799799
for traits.each |associated_trait| {
800800
encode_trait_ref(ebml_w, ecx, *associated_trait)
801801
}
@@ -821,9 +821,9 @@ fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: writer::Encoder,
821821
let polyty = ecx.tcx.tcache.get(local_def(ty_m.id));
822822
encode_ty_type_param_bounds(ebml_w, ecx, polyty.bounds);
823823
encode_type(ecx, ebml_w, polyty.ty);
824-
let m_path = vec::append_one(/*bad*/copy path,
825-
ast_map::path_name(item.ident));
826-
encode_path(ecx, ebml_w, &m_path, ast_map::path_name(ty_m.ident));
824+
let mut m_path = vec::append(~[], path); // :-(
825+
m_path += [ast_map::path_name(item.ident)];
826+
encode_path(ecx, ebml_w, m_path, ast_map::path_name(ty_m.ident));
827827
ebml_w.end_tag();
828828
}
829829

@@ -855,20 +855,18 @@ fn encode_info_for_foreign_item(ecx: @encode_ctxt,
855855
encode_type_param_bounds(ebml_w, ecx, tps);
856856
encode_type(ecx, ebml_w, node_id_to_type(ecx.tcx, nitem.id));
857857
if abi == foreign_abi_rust_intrinsic {
858-
// XXX: Bad copy of `path`.
859-
(ecx.encode_inlined_item)(ecx, ebml_w, copy path,
860-
ii_foreign(nitem));
858+
(ecx.encode_inlined_item)(ecx, ebml_w, path, ii_foreign(nitem));
861859
} else {
862860
encode_symbol(ecx, ebml_w, nitem.id);
863861
}
864-
encode_path(ecx, ebml_w, &path, ast_map::path_name(nitem.ident));
862+
encode_path(ecx, ebml_w, path, ast_map::path_name(nitem.ident));
865863
}
866864
foreign_item_const(*) => {
867865
encode_def_id(ebml_w, local_def(nitem.id));
868866
encode_family(ebml_w, 'c');
869867
encode_type(ecx, ebml_w, node_id_to_type(ecx.tcx, nitem.id));
870868
encode_symbol(ecx, ebml_w, nitem.id);
871-
encode_path(ecx, ebml_w, &path, ast_map::path_name(nitem.ident));
869+
encode_path(ecx, ebml_w, path, ast_map::path_name(nitem.ident));
872870
}
873871
}
874872
ebml_w.end_tag();
@@ -888,7 +886,7 @@ fn encode_info_for_items(ecx: @encode_ctxt, ebml_w: writer::Encoder,
888886
visit::visit_item(i, cx, v);
889887
match ecx.tcx.items.get(i.id) {
890888
ast_map::node_item(_, pt) => {
891-
encode_info_for_item(ecx, ebml_w, i, index, /*bad*/copy *pt);
889+
encode_info_for_item(ecx, ebml_w, i, index, *pt);
892890
}
893891
_ => fail ~"bad item"
894892
}
@@ -1001,7 +999,7 @@ fn encode_meta_item(ebml_w: writer::Encoder, mi: meta_item) {
1001999
}
10021000
}
10031001

1004-
fn encode_attributes(ebml_w: writer::Encoder, attrs: ~[attribute]) {
1002+
fn encode_attributes(ebml_w: writer::Encoder, attrs: &[attribute]) {
10051003
ebml_w.start_tag(tag_attributes);
10061004
for attrs.each |attr| {
10071005
ebml_w.start_tag(tag_attribute);

src/librustc/middle/astencode.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ trait tr_intern {
9393

9494
fn encode_inlined_item(ecx: @e::encode_ctxt,
9595
ebml_w: writer::Encoder,
96-
path: ast_map::path,
96+
path: &[ast_map::path_elt],
9797
ii: ast::inlined_item,
9898
maps: maps) {
9999
debug!("> Encoding inlined item: %s::%s (%u)",

0 commit comments

Comments
 (0)