Skip to content

Commit a4dc65b

Browse files
committed
syntax/rustc: Eliminate some bad copies
r=pcwalton
1 parent 0ca369e commit a4dc65b

File tree

13 files changed

+103
-101
lines changed

13 files changed

+103
-101
lines changed

src/librustc/back/link.rs

Lines changed: 62 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -457,32 +457,32 @@ mod write {
457457
*
458458
*/
459459

460-
fn build_link_meta(sess: Session, c: ast::crate, output: &Path,
460+
fn build_link_meta(sess: Session, c: &ast::crate, output: &Path,
461461
symbol_hasher: &hash::State) -> link_meta {
462462

463463
type provided_metas =
464-
{name: Option<~str>,
465-
vers: Option<~str>,
464+
{name: Option<@str>,
465+
vers: Option<@str>,
466466
cmh_items: ~[@ast::meta_item]};
467467

468-
fn provided_link_metas(sess: Session, c: ast::crate) ->
468+
fn provided_link_metas(sess: Session, c: &ast::crate) ->
469469
provided_metas {
470-
let mut name: Option<~str> = None;
471-
let mut vers: Option<~str> = None;
472-
let mut cmh_items: ~[@ast::meta_item] = ~[];
473-
let linkage_metas =
474-
attr::find_linkage_metas(/*bad*/copy c.node.attrs);
475-
// XXX: Bad copy.
476-
attr::require_unique_names(sess.diagnostic(), copy linkage_metas);
470+
let mut name = None;
471+
let mut vers = None;
472+
let mut cmh_items = ~[];
473+
let linkage_metas = attr::find_linkage_metas(c.node.attrs);
474+
attr::require_unique_names(sess.diagnostic(), linkage_metas);
477475
for linkage_metas.each |meta| {
478476
if attr::get_meta_item_name(*meta) == ~"name" {
479477
match attr::get_meta_item_value_str(*meta) {
480-
Some(ref v) => { name = Some((/*bad*/copy *v)); }
478+
// Changing attr would avoid the need for the copy
479+
// here
480+
Some(v) => { name = Some(v.to_managed()); }
481481
None => cmh_items.push(*meta)
482482
}
483483
} else if attr::get_meta_item_name(*meta) == ~"vers" {
484484
match attr::get_meta_item_value_str(*meta) {
485-
Some(ref v) => { vers = Some((/*bad*/copy *v)); }
485+
Some(v) => { vers = Some(v.to_managed()); }
486486
None => cmh_items.push(*meta)
487487
}
488488
} else { cmh_items.push(*meta); }
@@ -492,9 +492,8 @@ fn build_link_meta(sess: Session, c: ast::crate, output: &Path,
492492

493493
// This calculates CMH as defined above
494494
fn crate_meta_extras_hash(symbol_hasher: &hash::State,
495-
_crate: ast::crate,
496-
metas: provided_metas,
497-
dep_hashes: ~[~str]) -> ~str {
495+
-cmh_items: ~[@ast::meta_item],
496+
dep_hashes: ~[~str]) -> @str {
498497
fn len_and_str(s: ~str) -> ~str {
499498
return fmt!("%u_%s", str::len(s), s);
500499
}
@@ -503,7 +502,7 @@ fn build_link_meta(sess: Session, c: ast::crate, output: &Path,
503502
return len_and_str(pprust::lit_to_str(@l));
504503
}
505504

506-
let cmh_items = attr::sort_meta_items(/*bad*/copy metas.cmh_items);
505+
let cmh_items = attr::sort_meta_items(cmh_items);
507506

508507
symbol_hasher.reset();
509508
for cmh_items.each |m| {
@@ -526,52 +525,53 @@ fn build_link_meta(sess: Session, c: ast::crate, output: &Path,
526525
symbol_hasher.write_str(len_and_str(*dh));
527526
}
528527

529-
return truncated_hash_result(symbol_hasher);
528+
// tjc: allocation is unfortunate; need to change core::hash
529+
return truncated_hash_result(symbol_hasher).to_managed();
530530
}
531531

532-
fn warn_missing(sess: Session, name: ~str, default: ~str) {
532+
fn warn_missing(sess: Session, name: &str, default: &str) {
533533
if !sess.building_library { return; }
534534
sess.warn(fmt!("missing crate link meta `%s`, using `%s` as default",
535535
name, default));
536536
}
537537

538-
fn crate_meta_name(sess: Session, _crate: ast::crate,
539-
output: &Path, metas: provided_metas) -> ~str {
540-
return match metas.name {
541-
Some(ref v) => (/*bad*/copy *v),
538+
fn crate_meta_name(sess: Session, output: &Path, -opt_name: Option<@str>)
539+
-> @str {
540+
return match opt_name {
541+
Some(v) => v,
542542
None => {
543-
let name = match output.filestem() {
544-
None => sess.fatal(fmt!("output file name `%s` doesn't\
543+
// to_managed could go away if there was a version of
544+
// filestem that returned an @str
545+
let name = session::expect(sess,
546+
output.filestem(),
547+
|| fmt!("output file name `%s` doesn't\
545548
appear to have a stem",
546-
output.to_str())),
547-
Some(ref s) => (/*bad*/copy *s)
548-
};
549-
// XXX: Bad copy.
550-
warn_missing(sess, ~"name", copy name);
549+
output.to_str())).to_managed();
550+
warn_missing(sess, ~"name", name);
551551
name
552552
}
553553
};
554554
}
555555

556-
fn crate_meta_vers(sess: Session, _crate: ast::crate,
557-
metas: provided_metas) -> ~str {
558-
return match metas.vers {
559-
Some(ref v) => (/*bad*/copy *v),
556+
fn crate_meta_vers(sess: Session, opt_vers: Option<@str>) -> @str {
557+
return match opt_vers {
558+
Some(v) => v,
560559
None => {
561-
let vers = ~"0.0";
562-
// Bad copy.
563-
warn_missing(sess, ~"vers", copy vers);
560+
let vers = @"0.0";
561+
warn_missing(sess, ~"vers", vers);
564562
vers
565563
}
566564
};
567565
}
568566

569-
let provided_metas = provided_link_metas(sess, c);
570-
let name = crate_meta_name(sess, c, output, provided_metas);
571-
let vers = crate_meta_vers(sess, c, provided_metas);
567+
let {name: opt_name, vers: opt_vers,
568+
cmh_items: cmh_items} = provided_link_metas(sess, c);
569+
let name = crate_meta_name(sess, output, move opt_name);
570+
let vers = crate_meta_vers(sess, move opt_vers);
572571
let dep_hashes = cstore::get_dep_hashes(sess.cstore);
573572
let extras_hash =
574-
crate_meta_extras_hash(symbol_hasher, c, provided_metas, dep_hashes);
573+
crate_meta_extras_hash(symbol_hasher, move cmh_items,
574+
dep_hashes);
575575

576576
return {name: name, vers: vers, extras_hash: extras_hash};
577577
}
@@ -583,7 +583,7 @@ fn truncated_hash_result(symbol_hasher: &hash::State) -> ~str unsafe {
583583

584584
// This calculates STH for a symbol, as defined above
585585
fn symbol_hash(tcx: ty::ctxt, symbol_hasher: &hash::State, t: ty::t,
586-
link_meta: link_meta) -> ~str {
586+
link_meta: link_meta) -> @str {
587587
// NB: do *not* use abbrevs here as we want the symbol names
588588
// to be independent of one another in the crate.
589589

@@ -593,20 +593,20 @@ fn symbol_hash(tcx: ty::ctxt, symbol_hasher: &hash::State, t: ty::t,
593593
symbol_hasher.write_str(link_meta.extras_hash);
594594
symbol_hasher.write_str(~"-");
595595
symbol_hasher.write_str(encoder::encoded_ty(tcx, t));
596-
let hash = truncated_hash_result(symbol_hasher);
596+
let mut hash = truncated_hash_result(symbol_hasher);
597597
// Prefix with _ so that it never blends into adjacent digits
598-
599-
return ~"_" + hash;
598+
str::unshift_char(&mut hash, '_');
599+
// tjc: allocation is unfortunate; need to change core::hash
600+
hash.to_managed()
600601
}
601602
602-
fn get_symbol_hash(ccx: @crate_ctxt, t: ty::t) -> ~str {
603+
fn get_symbol_hash(ccx: @crate_ctxt, t: ty::t) -> @str {
603604
match ccx.type_hashcodes.find(t) {
604-
Some(ref h) => return (/*bad*/copy *h),
605+
Some(h) => h,
605606
None => {
606607
let hash = symbol_hash(ccx.tcx, ccx.symbol_hasher, t, ccx.link_meta);
607-
// XXX: Bad copy. Prefer `@str`?
608-
ccx.type_hashcodes.insert(t, copy hash);
609-
return hash;
608+
ccx.type_hashcodes.insert(t, hash);
609+
hash
610610
}
611611
}
612612
}
@@ -664,30 +664,30 @@ fn mangle(sess: Session, ss: path) -> ~str {
664664

665665
fn exported_name(sess: Session,
666666
+path: path,
667-
+hash: ~str,
668-
+vers: ~str) -> ~str {
667+
hash: &str,
668+
vers: &str) -> ~str {
669669
return mangle(sess,
670-
vec::append_one(
671-
vec::append_one(path, path_name(sess.ident_of(hash))),
672-
path_name(sess.ident_of(vers))));
670+
vec::append_one(
671+
vec::append_one(path, path_name(sess.ident_of(hash.to_owned()))),
672+
path_name(sess.ident_of(vers.to_owned()))));
673673
}
674674

675675
fn mangle_exported_name(ccx: @crate_ctxt, +path: path, t: ty::t) -> ~str {
676676
let hash = get_symbol_hash(ccx, t);
677677
return exported_name(ccx.sess, path,
678678
hash,
679-
/*bad*/copy ccx.link_meta.vers);
679+
ccx.link_meta.vers);
680680
}
681681

682682
fn mangle_internal_name_by_type_only(ccx: @crate_ctxt,
683683
t: ty::t,
684-
+name: ~str) -> ~str {
684+
name: &str) -> ~str {
685685
let s = ppaux::ty_to_short_str(ccx.tcx, t);
686686
let hash = get_symbol_hash(ccx, t);
687687
return mangle(ccx.sess,
688-
~[path_name(ccx.sess.ident_of(name)),
689-
path_name(ccx.sess.ident_of(s)),
690-
path_name(ccx.sess.ident_of(hash))]);
688+
~[path_name(ccx.sess.ident_of(name.to_owned())),
689+
path_name(ccx.sess.ident_of(s)),
690+
path_name(ccx.sess.ident_of(hash.to_owned()))]);
691691
}
692692

693693
fn mangle_internal_name_by_path_and_seq(ccx: @crate_ctxt,
@@ -706,7 +706,7 @@ fn mangle_internal_name_by_seq(ccx: @crate_ctxt, +flav: ~str) -> ~str {
706706
}
707707

708708

709-
fn output_dll_filename(os: session::os, lm: &link_meta) -> ~str {
709+
fn output_dll_filename(os: session::os, lm: link_meta) -> ~str {
710710
let libname = fmt!("%s-%s-%s", lm.name, lm.extras_hash, lm.vers);
711711
let (dll_prefix, dll_suffix) = match os {
712712
session::os_win32 => (win32::DLL_PREFIX, win32::DLL_SUFFIX),
@@ -736,7 +736,7 @@ fn link_binary(sess: Session,
736736
}
737737

738738
let output = if sess.building_library {
739-
let long_libname = output_dll_filename(sess.targ_cfg.os, &lm);
739+
let long_libname = output_dll_filename(sess.targ_cfg.os, lm);
740740
debug!("link_meta.name: %s", lm.name);
741741
debug!("long_libname: %s", long_libname);
742742
debug!("out_filename: %s", out_filename.to_str());

src/librustc/driver/driver.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ fn default_configuration(sess: Session, +argv0: ~str, input: input) ->
108108
}
109109

110110
fn append_configuration(+cfg: ast::crate_cfg, +name: ~str) -> ast::crate_cfg {
111-
// XXX: Bad copy.
112-
if attr::contains_name(copy cfg, copy name) {
111+
if attr::contains_name(cfg, name) {
113112
return cfg;
114113
} else {
115114
return vec::append_one(cfg, attr::mk_word_item(name));

src/librustc/front/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ fn mk_tests(cx: test_ctxt) -> @ast::item {
297297

298298
fn is_std(cx: test_ctxt) -> bool {
299299
let is_std = {
300-
let items = attr::find_linkage_metas(/*bad*/copy cx.crate.node.attrs);
300+
let items = attr::find_linkage_metas(cx.crate.node.attrs);
301301
match attr::last_meta_item_value_str_by_name(items, ~"name") {
302302
Some(~"std") => true,
303303
_ => false

src/librustc/metadata/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,5 +152,5 @@ const tag_lang_items_item: uint = 0x73;
152152
const tag_lang_items_item_id: uint = 0x74;
153153
const tag_lang_items_item_node_id: uint = 0x75;
154154

155-
type link_meta = {name: ~str, vers: ~str, extras_hash: ~str};
155+
type link_meta = {name: @str, vers: @str, extras_hash: @str};
156156

src/librustc/metadata/creader.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,7 @@ fn visit_item(e: env, i: @ast::item) {
191191

192192
fn metas_with(+ident: ~str, +key: ~str, +metas: ~[@ast::meta_item])
193193
-> ~[@ast::meta_item] {
194-
// XXX: Bad copies.
195-
let name_items = attr::find_meta_items_by_name(copy metas, copy key);
194+
let name_items = attr::find_meta_items_by_name(metas, key);
196195
if name_items.is_empty() {
197196
vec::append_one(metas, attr::mk_name_value_item_str(key, ident))
198197
} else {

src/librustc/metadata/encoder.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ fn encode_info_for_foreign_item(ecx: @encode_ctxt,
881881
}
882882

883883
fn encode_info_for_items(ecx: @encode_ctxt, ebml_w: writer::Encoder,
884-
crate: @crate) -> ~[entry<int>] {
884+
crate: &crate) -> ~[entry<int>] {
885885
let index = @mut ~[];
886886
ebml_w.start_tag(tag_items_data);
887887
index.push({val: crate_node_id, pos: ebml_w.writer.tell()});
@@ -1021,20 +1021,20 @@ fn encode_attributes(ebml_w: writer::Encoder, attrs: ~[attribute]) {
10211021
// metadata that Rust cares about for linking crates. This attribute requires
10221022
// 'name' and 'vers' items, so if the user didn't provide them we will throw
10231023
// them in anyway with default values.
1024-
fn synthesize_crate_attrs(ecx: @encode_ctxt, crate: @crate) -> ~[attribute] {
1024+
fn synthesize_crate_attrs(ecx: @encode_ctxt, crate: &crate) -> ~[attribute] {
10251025

10261026
fn synthesize_link_attr(ecx: @encode_ctxt, +items: ~[@meta_item]) ->
10271027
attribute {
10281028

1029-
assert (ecx.link_meta.name != ~"");
1030-
assert (ecx.link_meta.vers != ~"");
1029+
assert ecx.link_meta.name.is_not_empty();
1030+
assert ecx.link_meta.vers.is_not_empty();
10311031

10321032
let name_item =
10331033
attr::mk_name_value_item_str(~"name",
1034-
/*bad*/copy ecx.link_meta.name);
1034+
ecx.link_meta.name.to_owned());
10351035
let vers_item =
10361036
attr::mk_name_value_item_str(~"vers",
1037-
/*bad*/copy ecx.link_meta.vers);
1037+
ecx.link_meta.vers.to_owned());
10381038

10391039
let other_items =
10401040
{
@@ -1156,7 +1156,7 @@ fn encode_crate_dep(ecx: @encode_ctxt, ebml_w: writer::Encoder,
11561156
ebml_w.end_tag();
11571157
}
11581158

1159-
fn encode_hash(ebml_w: writer::Encoder, hash: ~str) {
1159+
fn encode_hash(ebml_w: writer::Encoder, hash: &str) {
11601160
ebml_w.start_tag(tag_crate_hash);
11611161
ebml_w.writer.write(str::to_bytes(hash));
11621162
ebml_w.end_tag();
@@ -1169,7 +1169,7 @@ const metadata_encoding_version : &[u8] = &[0x72, //'r' as u8,
11691169
0x74, //'t' as u8,
11701170
0, 0, 0, 1 ];
11711171

1172-
fn encode_metadata(parms: encode_parms, crate: @crate) -> ~[u8] {
1172+
fn encode_metadata(parms: encode_parms, crate: &crate) -> ~[u8] {
11731173
let wr = @io::BytesWriter();
11741174
let stats =
11751175
{mut inline_bytes: 0,
@@ -1197,7 +1197,7 @@ fn encode_metadata(parms: encode_parms, crate: @crate) -> ~[u8] {
11971197

11981198
let ebml_w = writer::Encoder(wr as io::Writer);
11991199

1200-
encode_hash(ebml_w, /*bad*/copy ecx.link_meta.extras_hash);
1200+
encode_hash(ebml_w, ecx.link_meta.extras_hash);
12011201

12021202
let mut i = wr.pos;
12031203
let crate_attrs = synthesize_crate_attrs(ecx, crate);

src/librustc/metadata/loader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ fn load_library_crate(cx: ctxt) -> {ident: ~str, data: @~[u8]} {
7777
}
7878

7979
fn find_library_crate(cx: ctxt) -> Option<{ident: ~str, data: @~[u8]}> {
80-
attr::require_unique_names(cx.diag, /*bad*/copy cx.metas);
80+
attr::require_unique_names(cx.diag, cx.metas);
8181
find_library_crate_aux(cx, libname(cx), cx.filesearch)
8282
}
8383

src/librustc/middle/borrowck/preserve.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ priv impl &preserve_ctxt {
106106
let scope_region = if self.root_ub == 0 {
107107
ty::re_static
108108
} else {
109+
// Maybe if we pass in the parent instead here,
110+
// we can prevent the "scope not found" error
111+
debug!("scope_region thing: %? ", cmt.id);
109112
ty::re_scope(self.tcx().region_map.get(cmt.id))
110113
};
111114

0 commit comments

Comments
 (0)