Skip to content

Commit 0409f86

Browse files
committed
auto merge of #6938 : jbclements/rust/hygiene_fns_and_cleanup, r=jbclements
I'm close to flipping the switch on hygiene for let-bound identifiers. This commit adds a bunch of support functions for that change... but also a huge amount of cleanup in syntax.rc. The most interesting of these are - the use of TLS for the interners everywhere. We had already breached the "no-global-state" dam by using TLS for encoding, and it saves a lot of code just to use it everywhere. Also, there were many places where two or more interners were passed in attached to different structures, and the danger of having those diverge seemed greater that the danger of having a single one get big and heavy. If the interner size proves to be a problem, it should be quite simple to add a "parameterize"-like dynamic binding form--because we don't have interesting continuation operations (or tail calling, mostly) this should just be a case of a mutation followed by another later mutation. Again, this is only if the interner gets too big. - I renamed the "repr" field of the identifier to "name". I can see the case for "repr" when there's only one field in the structure, but that's no longer the case; there's now a name and a context (both are uints). - the interner now just maps between strings and uints, rather than between idents and uints. The former state made perfect sense when identifiers didn't have syntax contexts, but that's no longer the case. I've run this patch against a fairly recent incoming, and it appears to pass all tests. Let's see if it can be merged....
2 parents c5fea4a + 91b6526 commit 0409f86

Some content is hidden

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

50 files changed

+875
-694
lines changed

src/librustc/back/link.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ pub fn mangle_internal_name_by_path(ccx: @CrateContext, path: path) -> ~str {
734734
}
735735

736736
pub fn mangle_internal_name_by_seq(ccx: @CrateContext, flav: &str) -> ~str {
737-
return fmt!("%s_%u", flav, (ccx.names)(flav).repr);
737+
return fmt!("%s_%u", flav, (ccx.names)(flav).name);
738738
}
739739

740740

src/librustc/driver/driver.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ use syntax::attr;
3939
use syntax::codemap;
4040
use syntax::diagnostic;
4141
use syntax::parse;
42+
use syntax::parse::token;
4243
use syntax::print::{pp, pprust};
4344
use syntax;
4445

@@ -230,7 +231,7 @@ pub fn compile_rest(sess: Session,
230231
sess.filesearch,
231232
session::sess_os_to_meta_os(sess.targ_cfg.os),
232233
sess.opts.is_static,
233-
sess.parse_sess.interner));
234+
token::get_ident_interner()));
234235

235236
let lang_items = time(time_passes, ~"language item collection", ||
236237
middle::lang_items::collect_language_items(crate, sess));
@@ -455,7 +456,7 @@ pub fn pretty_print_input(sess: Session, cfg: ast::crate_cfg, input: &input,
455456
let is_expanded = upto != cu_parse;
456457
let src = sess.codemap.get_filemap(source_name(input)).src;
457458
do io::with_str_reader(*src) |rdr| {
458-
pprust::print_crate(sess.codemap, sess.parse_sess.interner,
459+
pprust::print_crate(sess.codemap, token::get_ident_interner(),
459460
sess.span_diagnostic, crate.unwrap(),
460461
source_name(input),
461462
rdr, io::stdout(), ann, is_expanded);
@@ -754,7 +755,7 @@ pub fn build_session_(sopts: @session::options,
754755
let target_cfg = build_target_config(sopts, demitter);
755756
let p_s = parse::new_parse_sess_special_handler(span_diagnostic_handler,
756757
cm);
757-
let cstore = @mut cstore::mk_cstore(p_s.interner);
758+
let cstore = @mut cstore::mk_cstore(token::get_ident_interner());
758759
let filesearch = filesearch::mk_filesearch(
759760
&sopts.maybe_sysroot,
760761
sopts.target_triple,
@@ -963,7 +964,7 @@ pub fn early_error(emitter: diagnostic::Emitter, msg: ~str) -> ! {
963964

964965
pub fn list_metadata(sess: Session, path: &Path, out: @io::Writer) {
965966
metadata::loader::list_file_metadata(
966-
sess.parse_sess.interner,
967+
token::get_ident_interner(),
967968
session::sess_os_to_meta_os(sess.targ_cfg.os), path, out);
968969
}
969970

src/librustc/driver/session.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use syntax::diagnostic;
2626
use syntax::parse::ParseSess;
2727
use syntax::{ast, codemap};
2828
use syntax::abi;
29+
use syntax::parse::token;
2930
use syntax;
3031

3132
use core::hashmap::HashMap;
@@ -293,14 +294,19 @@ impl Session_ {
293294
self.opts.optimize == No && !self.debugging_opt(no_debug_borrows)
294295
}
295296

297+
// pointless function, now...
296298
pub fn str_of(@self, id: ast::ident) -> @~str {
297-
self.parse_sess.interner.get(id)
299+
token::ident_to_str(&id)
298300
}
301+
302+
// pointless function, now...
299303
pub fn ident_of(@self, st: &str) -> ast::ident {
300-
self.parse_sess.interner.intern(st)
304+
token::str_to_ident(st)
301305
}
306+
307+
// pointless function, now...
302308
pub fn intr(@self) -> @syntax::parse::token::ident_interner {
303-
self.parse_sess.interner
309+
token::get_ident_interner()
304310
}
305311
}
306312

src/librustc/front/test.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use syntax::codemap::{dummy_sp, span, ExpandedFrom, CallInfo, NameAndSpan};
2222
use syntax::codemap;
2323
use syntax::ext::base::ExtCtxt;
2424
use syntax::fold;
25+
use syntax::parse::token;
2526
use syntax::print::pprust;
2627
use syntax::{ast, ast_util};
2728

@@ -143,7 +144,7 @@ fn fold_item(cx: @mut TestCtxt, i: @ast::item, fld: @fold::ast_fold)
143144
-> Option<@ast::item> {
144145
cx.path.push(i.ident);
145146
debug!("current path: %s",
146-
ast_util::path_name_i(copy cx.path, cx.sess.parse_sess.interner));
147+
ast_util::path_name_i(copy cx.path));
147148

148149
if is_test_fn(cx, i) || is_bench_fn(i) {
149150
match i.node {
@@ -411,13 +412,10 @@ fn mk_test_desc_and_fn_rec(cx: &TestCtxt, test: &Test) -> @ast::expr {
411412

412413
let ext_cx = cx.ext_cx;
413414

414-
debug!("encoding %s", ast_util::path_name_i(path,
415-
cx.sess.parse_sess.interner));
415+
debug!("encoding %s", ast_util::path_name_i(path));
416416

417417
let name_lit: ast::lit =
418-
nospan(ast::lit_str(@ast_util::path_name_i(
419-
path,
420-
cx.sess.parse_sess.interner)));
418+
nospan(ast::lit_str(@ast_util::path_name_i(path)));
421419

422420
let name_expr = @ast::expr {
423421
id: cx.sess.next_node_id(),

src/librustc/metadata/creader.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use core::vec;
2222
use syntax::attr;
2323
use syntax::codemap::{span, dummy_sp};
2424
use syntax::diagnostic::span_handler;
25+
use syntax::parse::token;
2526
use syntax::parse::token::ident_interner;
2627
use syntax::visit;
2728
use syntax::ast;
@@ -176,7 +177,7 @@ fn visit_item(e: @mut Env, i: @ast::item) {
176177
}
177178
nn
178179
}
179-
None => e.intr.get(i.ident)
180+
None => token::ident_to_str(&i.ident)
180181
};
181182
if attr::find_attrs_by_name(i.attrs, "nolink").is_empty() {
182183
already_added =
@@ -235,7 +236,7 @@ fn resolve_crate(e: @mut Env,
235236
hash: @~str,
236237
span: span)
237238
-> ast::crate_num {
238-
let metas = metas_with_ident(@/*bad*/copy *e.intr.get(ident), metas);
239+
let metas = metas_with_ident(token::ident_to_str(&ident), metas);
239240

240241
match existing_match(e, metas, hash) {
241242
None => {
@@ -276,7 +277,7 @@ fn resolve_crate(e: @mut Env,
276277
match attr::last_meta_item_value_str_by_name(load_ctxt.metas,
277278
"name") {
278279
Some(v) => v,
279-
None => e.intr.get(ident),
280+
None => token::ident_to_str(&ident),
280281
};
281282
let cmeta = @cstore::crate_metadata {
282283
name: cname,
@@ -305,10 +306,11 @@ fn resolve_crate_deps(e: @mut Env, cdata: @~[u8]) -> cstore::cnum_map {
305306
for decoder::get_crate_deps(e.intr, cdata).each |dep| {
306307
let extrn_cnum = dep.cnum;
307308
let cname = dep.name;
309+
let cname_str = token::ident_to_str(&dep.name);
308310
let cmetas = metas_with(dep.vers, @~"vers", ~[]);
309311
debug!("resolving dep crate %s ver: %s hash: %s",
310-
*e.intr.get(dep.name), *dep.vers, *dep.hash);
311-
match existing_match(e, metas_with_ident(e.intr.get(cname),
312+
*cname_str, *dep.vers, *dep.hash);
313+
match existing_match(e, metas_with_ident(cname_str,
312314
copy cmetas),
313315
dep.hash) {
314316
Some(local_cnum) => {

src/librustc/metadata/decoder.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ use syntax::parse::token::{StringRef, ident_interner, special_idents};
4040
use syntax::print::pprust;
4141
use syntax::{ast, ast_util};
4242
use syntax::codemap;
43+
use syntax::parse::token;
4344

4445
type cmd = @crate_metadata;
4546

@@ -297,10 +298,10 @@ fn item_path(intr: @ident_interner, item_doc: ebml::Doc) -> ast_map::path {
297298
for reader::docs(path_doc) |tag, elt_doc| {
298299
if tag == tag_path_elt_mod {
299300
let str = reader::doc_as_str(elt_doc);
300-
result.push(ast_map::path_mod(intr.intern(str)));
301+
result.push(ast_map::path_mod(token::str_to_ident(str)));
301302
} else if tag == tag_path_elt_name {
302303
let str = reader::doc_as_str(elt_doc);
303-
result.push(ast_map::path_name(intr.intern(str)));
304+
result.push(ast_map::path_name(token::str_to_ident(str)));
304305
} else {
305306
// ignore tag_path_len element
306307
}
@@ -314,8 +315,8 @@ fn item_name(intr: @ident_interner, item: ebml::Doc) -> ast::ident {
314315
do reader::with_doc_data(name) |data| {
315316
let string = str::from_bytes_slice(data);
316317
match intr.find_equiv(&StringRef(string)) {
317-
None => intr.intern(string),
318-
Some(val) => val,
318+
None => token::str_to_ident(string),
319+
Some(val) => ast::new_ident(val),
319320
}
320321
}
321322
}
@@ -843,7 +844,7 @@ pub fn get_type_name_if_impl(intr: @ident_interner,
843844
}
844845

845846
for reader::tagged_docs(item, tag_item_impl_type_basename) |doc| {
846-
return Some(intr.intern(str::from_bytes(reader::doc_data(doc))));
847+
return Some(token::str_to_ident(str::from_bytes(reader::doc_data(doc))));
847848
}
848849

849850
return None;
@@ -1095,7 +1096,7 @@ pub fn get_crate_deps(intr: @ident_interner, data: @~[u8]) -> ~[crate_dep] {
10951096
}
10961097
for reader::tagged_docs(depsdoc, tag_crate_dep) |depdoc| {
10971098
deps.push(crate_dep {cnum: crate_num,
1098-
name: intr.intern(docstr(depdoc, tag_crate_dep_name)),
1099+
name: token::str_to_ident(docstr(depdoc, tag_crate_dep_name)),
10991100
vers: @docstr(depdoc, tag_crate_dep_vers),
11001101
hash: @docstr(depdoc, tag_crate_dep_hash)});
11011102
crate_num += 1;
@@ -1109,7 +1110,7 @@ fn list_crate_deps(intr: @ident_interner, data: @~[u8], out: @io::Writer) {
11091110
for get_crate_deps(intr, data).each |dep| {
11101111
out.write_str(
11111112
fmt!("%d %s-%s-%s\n",
1112-
dep.cnum, *intr.get(dep.name), *dep.hash, *dep.vers));
1113+
dep.cnum, *token::ident_to_str(&dep.name), *dep.hash, *dep.vers));
11131114
}
11141115

11151116
out.write_str("\n");

src/librustc/metadata/encoder.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ use syntax::opt_vec::OptVec;
4343
use syntax::opt_vec;
4444
use syntax::parse::token::special_idents;
4545
use syntax::{ast_util, visit};
46+
use syntax::parse::token;
4647
use syntax;
4748
use writer = extra::ebml::writer;
4849

@@ -141,8 +142,7 @@ fn add_to_index(ecx: @EncodeContext,
141142
full_path.push(name);
142143
index.push(
143144
entry {
144-
val: ast_util::path_name_i(full_path,
145-
ecx.tcx.sess.parse_sess.interner),
145+
val: ast_util::path_name_i(full_path),
146146
pos: ebml_w.writer.tell()
147147
});
148148
}
@@ -485,8 +485,7 @@ fn encode_info_for_mod(ecx: @EncodeContext,
485485
(%?/%?)",
486486
*ecx.tcx.sess.str_of(ident),
487487
did,
488-
ast_map::node_id_to_str(ecx.tcx.items, did, ecx.tcx
489-
.sess.parse_sess.interner));
488+
ast_map::node_id_to_str(ecx.tcx.items, did, token::get_ident_interner()));
490489

491490
ebml_w.start_tag(tag_mod_impl);
492491
ebml_w.wr_str(def_to_str(local_def(did)));
@@ -1055,7 +1054,7 @@ fn encode_info_for_item(ecx: @EncodeContext,
10551054
tcx.sess.span_unimpl(
10561055
item.span,
10571056
fmt!("Method %s is both provided and static",
1058-
*tcx.sess.intr().get(method_ty.ident)));
1057+
*token::ident_to_str(&method_ty.ident)));
10591058
}
10601059
encode_type_param_bounds(ebml_w, ecx,
10611060
&m.generics.ty_params);

src/librustc/metadata/loader.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use metadata::filesearch::FileSearch;
1919
use metadata::filesearch;
2020
use syntax::codemap::span;
2121
use syntax::diagnostic::span_handler;
22+
use syntax::parse::token;
2223
use syntax::parse::token::ident_interner;
2324
use syntax::print::pprust;
2425
use syntax::{ast, attr};
@@ -59,7 +60,7 @@ pub fn load_library_crate(cx: &Context) -> (~str, @~[u8]) {
5960
None => {
6061
cx.diag.span_fatal(
6162
cx.span, fmt!("can't find crate for `%s`",
62-
*cx.intr.get(cx.ident)));
63+
*token::ident_to_str(&cx.ident)));
6364
}
6465
}
6566
}

src/librustc/middle/astencode.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ use syntax::codemap::span;
4141
use syntax::codemap;
4242
use syntax::fold::*;
4343
use syntax::fold;
44+
use syntax::parse::token;
4445
use syntax;
4546
use writer = extra::ebml::writer;
4647

@@ -86,7 +87,7 @@ pub fn encode_inlined_item(ecx: @e::EncodeContext,
8687
ii: ast::inlined_item,
8788
maps: Maps) {
8889
debug!("> Encoding inlined item: %s::%s (%u)",
89-
ast_map::path_to_str(path, ecx.tcx.sess.parse_sess.interner),
90+
ast_map::path_to_str(path, token::get_ident_interner()),
9091
*ecx.tcx.sess.str_of(ii.ident()),
9192
ebml_w.writer.tell());
9293

@@ -99,7 +100,7 @@ pub fn encode_inlined_item(ecx: @e::EncodeContext,
99100
ebml_w.end_tag();
100101

101102
debug!("< Encoded inlined fn: %s::%s (%u)",
102-
ast_map::path_to_str(path, ecx.tcx.sess.parse_sess.interner),
103+
ast_map::path_to_str(path, token::get_ident_interner()),
103104
*ecx.tcx.sess.str_of(ii.ident()),
104105
ebml_w.writer.tell());
105106
}
@@ -119,7 +120,7 @@ pub fn decode_inlined_item(cdata: @cstore::crate_metadata,
119120
None => None,
120121
Some(ast_doc) => {
121122
debug!("> Decoding inlined fn: %s::?",
122-
ast_map::path_to_str(path, tcx.sess.parse_sess.interner));
123+
ast_map::path_to_str(path, token::get_ident_interner()));
123124
let mut ast_dsr = reader::Decoder(ast_doc);
124125
let from_id_range = Decodable::decode(&mut ast_dsr);
125126
let to_id_range = reserve_id_range(dcx.tcx.sess, from_id_range);
@@ -132,7 +133,7 @@ pub fn decode_inlined_item(cdata: @cstore::crate_metadata,
132133
let ii = renumber_ast(xcx, raw_ii);
133134
debug!("Fn named: %s", *tcx.sess.str_of(ii.ident()));
134135
debug!("< Decoded inlined fn: %s::%s",
135-
ast_map::path_to_str(path, tcx.sess.parse_sess.interner),
136+
ast_map::path_to_str(path, token::get_ident_interner()),
136137
*tcx.sess.str_of(ii.ident()));
137138
ast_map::map_decoded_item(tcx.sess.diagnostic(),
138139
dcx.tcx.items, path, &ii);
@@ -1167,7 +1168,7 @@ impl fake_ext_ctxt for fake_session {
11671168
}
11681169
}
11691170
fn ident_of(&self, st: &str) -> ast::ident {
1170-
self.interner.intern(st)
1171+
token::str_to_ident(st)
11711172
}
11721173
}
11731174

@@ -1236,9 +1237,9 @@ fn test_simplification() {
12361237
match (item_out, item_exp) {
12371238
(ast::ii_item(item_out), ast::ii_item(item_exp)) => {
12381239
assert!(pprust::item_to_str(item_out,
1239-
ext_cx.parse_sess().interner)
1240+
token::get_ident_interner())
12401241
== pprust::item_to_str(item_exp,
1241-
ext_cx.parse_sess().interner));
1242+
token::get_ident_interner()));
12421243
}
12431244
_ => fail!()
12441245
}

src/librustc/middle/borrowck/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use syntax::ast;
3030
use syntax::ast_map;
3131
use syntax::visit;
3232
use syntax::codemap::span;
33+
use syntax::parse::token;
3334

3435
macro_rules! if_ok(
3536
($inp: expr) => (
@@ -710,8 +711,8 @@ impl BorrowckCtxt {
710711
match *loan_path {
711712
LpVar(id) => {
712713
match self.tcx.items.find(&id) {
713-
Some(&ast_map::node_local(ident)) => {
714-
str::push_str(out, *self.tcx.sess.intr().get(ident));
714+
Some(&ast_map::node_local(ref ident)) => {
715+
str::push_str(out, *token::ident_to_str(ident));
715716
}
716717
r => {
717718
self.tcx.sess.bug(
@@ -724,9 +725,9 @@ impl BorrowckCtxt {
724725
LpExtend(lp_base, _, LpInterior(mc::InteriorField(fname))) => {
725726
self.append_loan_path_to_str_from_interior(lp_base, out);
726727
match fname {
727-
mc::NamedField(fname) => {
728+
mc::NamedField(ref fname) => {
728729
str::push_char(out, '.');
729-
str::push_str(out, *self.tcx.sess.intr().get(fname));
730+
str::push_str(out, *token::ident_to_str(fname));
730731
}
731732
mc::PositionalField(idx) => {
732733
str::push_char(out, '#'); // invent a notation here

0 commit comments

Comments
 (0)