librustc: Stop using @str for source.

This commit is contained in:
Patrick Walton 2014-01-15 16:26:20 -08:00 committed by Huon Wilson
parent f152be7a42
commit e68108b3e8
10 changed files with 87 additions and 89 deletions

View file

@ -138,7 +138,7 @@ fn parse_cfgspecs(cfgspecs: ~[~str], demitter: @diagnostic::Emitter)
-> ast::CrateConfig { -> ast::CrateConfig {
cfgspecs.move_iter().map(|s| { cfgspecs.move_iter().map(|s| {
let sess = parse::new_parse_sess(Some(demitter)); let sess = parse::new_parse_sess(Some(demitter));
parse::parse_meta_from_source_str(@"cfgspec", s.to_managed(), ~[], sess) parse::parse_meta_from_source_str(@"cfgspec", s, ~[], sess)
}).collect::<ast::CrateConfig>() }).collect::<ast::CrateConfig>()
} }
@ -146,8 +146,7 @@ pub enum Input {
/// Load source from file /// Load source from file
FileInput(Path), FileInput(Path),
/// The string is the source /// The string is the source
// FIXME (#2319): Don't really want to box the source string StrInput(~str)
StrInput(@str)
} }
pub fn phase_1_parse_input(sess: Session, cfg: ast::CrateConfig, input: &Input) pub fn phase_1_parse_input(sess: Session, cfg: ast::CrateConfig, input: &Input)
@ -157,9 +156,11 @@ pub fn phase_1_parse_input(sess: Session, cfg: ast::CrateConfig, input: &Input)
FileInput(ref file) => { FileInput(ref file) => {
parse::parse_crate_from_file(&(*file), cfg.clone(), sess.parse_sess) parse::parse_crate_from_file(&(*file), cfg.clone(), sess.parse_sess)
} }
StrInput(src) => { StrInput(ref src) => {
parse::parse_crate_from_source_str( parse::parse_crate_from_source_str(anon_src(),
anon_src(), src, cfg.clone(), sess.parse_sess) (*src).clone(),
cfg.clone(),
sess.parse_sess)
} }
} }
}) })
@ -624,7 +625,7 @@ pub fn pretty_print_input(sess: Session,
_ => @pprust::NoAnn as @pprust::PpAnn, _ => @pprust::NoAnn as @pprust::PpAnn,
}; };
let src = sess.codemap.get_filemap(source_name(input)).src; let src = &sess.codemap.get_filemap(source_name(input)).src;
let mut rdr = MemReader::new(src.as_bytes().to_owned()); let mut rdr = MemReader::new(src.as_bytes().to_owned());
let stdout = io::stdout(); let stdout = io::stdout();
pprust::print_crate(sess.codemap, pprust::print_crate(sess.codemap,

View file

@ -236,8 +236,9 @@ pub fn run_compiler(args: &[~str], demitter: @diagnostic::Emitter) {
1u => { 1u => {
let ifile = matches.free[0].as_slice(); let ifile = matches.free[0].as_slice();
if "-" == ifile { if "-" == ifile {
let src = str::from_utf8_owned(io::stdin().read_to_end()).unwrap(); let src =
(d::StrInput(src.to_managed()), None) str::from_utf8_owned(io::stdin().read_to_end()).unwrap();
(d::StrInput(src), None)
} else { } else {
(d::FileInput(Path::new(ifile)), Some(Path::new(ifile))) (d::FileInput(Path::new(ifile)), Some(Path::new(ifile)))
} }
@ -319,9 +320,11 @@ fn parse_crate_attrs(sess: session::Session,
d::FileInput(ref ifile) => { d::FileInput(ref ifile) => {
parse::parse_crate_attrs_from_file(ifile, ~[], sess.parse_sess) parse::parse_crate_attrs_from_file(ifile, ~[], sess.parse_sess)
} }
d::StrInput(src) => { d::StrInput(ref src) => {
parse::parse_crate_attrs_from_source_str( parse::parse_crate_attrs_from_source_str(d::anon_src(),
d::anon_src(), src, ~[], sess.parse_sess) (*src).clone(),
~[],
sess.parse_sess)
} }
} }
} }

View file

@ -137,7 +137,7 @@ fn runtest(test: &str, cratename: &str, libs: HashSet<Path>) {
} }
} }
fn maketest(s: &str, cratename: &str) -> @str { fn maketest(s: &str, cratename: &str) -> ~str {
let mut prog = ~r" let mut prog = ~r"
#[deny(warnings)]; #[deny(warnings)];
#[allow(unused_variable, dead_assignment, unused_mut, attribute_usage, dead_code)]; #[allow(unused_variable, dead_assignment, unused_mut, attribute_usage, dead_code)];
@ -156,7 +156,7 @@ fn maketest(s: &str, cratename: &str) -> @str {
prog.push_str("\n}"); prog.push_str("\n}");
} }
return prog.to_managed(); return prog;
} }
pub struct Collector { pub struct Collector {

View file

@ -206,7 +206,7 @@ pub struct FileMap {
/// e.g. `<anon>` /// e.g. `<anon>`
name: FileName, name: FileName,
/// The complete source code /// The complete source code
src: @str, src: ~str,
/// The start position of this source in the CodeMap /// The start position of this source in the CodeMap
start_pos: BytePos, start_pos: BytePos,
/// Locations of lines beginnings in the source code /// Locations of lines beginnings in the source code
@ -267,7 +267,7 @@ impl CodeMap {
} }
} }
pub fn new_filemap(&self, filename: FileName, src: @str) -> @FileMap { pub fn new_filemap(&self, filename: FileName, src: ~str) -> @FileMap {
let mut files = self.files.borrow_mut(); let mut files = self.files.borrow_mut();
let start_pos = match files.get().last() { let start_pos = match files.get().last() {
None => 0, None => 0,

View file

@ -1047,7 +1047,7 @@ mod test {
~[], sess); ~[], sess);
// should fail: // should fail:
let mut loader = ErrLoader; let mut loader = ErrLoader;
expand_crate(sess,&mut loader,~[],crate_ast); expand_crate(sess, &mut loader, ~[], crate_ast);
} }
#[test] fn test_contains_flatten (){ #[test] fn test_contains_flatten (){

View file

@ -203,7 +203,7 @@ pub mod rt {
($t:ty) => ( ($t:ty) => (
impl ToTokens for $t { impl ToTokens for $t {
fn to_tokens(&self, cx: &ExtCtxt) -> ~[TokenTree] { fn to_tokens(&self, cx: &ExtCtxt) -> ~[TokenTree] {
cx.parse_tts(self.to_source().to_managed()) cx.parse_tts(self.to_source())
} }
} }
) )
@ -213,7 +213,7 @@ pub mod rt {
($t:ty) => ( ($t:ty) => (
impl<'a> ToTokens for $t { impl<'a> ToTokens for $t {
fn to_tokens(&self, cx: &ExtCtxt) -> ~[TokenTree] { fn to_tokens(&self, cx: &ExtCtxt) -> ~[TokenTree] {
cx.parse_tts(self.to_source().to_managed()) cx.parse_tts(self.to_source())
} }
} }
) )
@ -240,15 +240,15 @@ pub mod rt {
impl_to_tokens!(u64) impl_to_tokens!(u64)
pub trait ExtParseUtils { pub trait ExtParseUtils {
fn parse_item(&self, s: @str) -> @ast::Item; fn parse_item(&self, s: ~str) -> @ast::Item;
fn parse_expr(&self, s: @str) -> @ast::Expr; fn parse_expr(&self, s: ~str) -> @ast::Expr;
fn parse_stmt(&self, s: @str) -> @ast::Stmt; fn parse_stmt(&self, s: ~str) -> @ast::Stmt;
fn parse_tts(&self, s: @str) -> ~[ast::TokenTree]; fn parse_tts(&self, s: ~str) -> ~[ast::TokenTree];
} }
impl<'a> ExtParseUtils for ExtCtxt<'a> { impl<'a> ExtParseUtils for ExtCtxt<'a> {
fn parse_item(&self, s: @str) -> @ast::Item { fn parse_item(&self, s: ~str) -> @ast::Item {
let res = parse::parse_item_from_source_str( let res = parse::parse_item_from_source_str(
@"<quote expansion>", @"<quote expansion>",
s, s,
@ -257,13 +257,13 @@ pub mod rt {
match res { match res {
Some(ast) => ast, Some(ast) => ast,
None => { None => {
error!("Parse error with ```\n{}\n```", s); error!("Parse error");
fail!() fail!()
} }
} }
} }
fn parse_stmt(&self, s: @str) -> @ast::Stmt { fn parse_stmt(&self, s: ~str) -> @ast::Stmt {
parse::parse_stmt_from_source_str( parse::parse_stmt_from_source_str(
@"<quote expansion>", @"<quote expansion>",
s, s,
@ -272,7 +272,7 @@ pub mod rt {
self.parse_sess()) self.parse_sess())
} }
fn parse_expr(&self, s: @str) -> @ast::Expr { fn parse_expr(&self, s: ~str) -> @ast::Expr {
parse::parse_expr_from_source_str( parse::parse_expr_from_source_str(
@"<quote expansion>", @"<quote expansion>",
s, s,
@ -280,7 +280,7 @@ pub mod rt {
self.parse_sess()) self.parse_sess())
} }
fn parse_tts(&self, s: @str) -> ~[ast::TokenTree] { fn parse_tts(&self, s: ~str) -> ~[ast::TokenTree] {
parse::parse_tts_from_source_str( parse::parse_tts_from_source_str(
@"<quote expansion>", @"<quote expansion>",
s, s,

View file

@ -114,11 +114,11 @@ pub fn expand_include_str(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
Some(src) => { Some(src) => {
// Add this input file to the code map to make it available as // Add this input file to the code map to make it available as
// dependency information // dependency information
let src = src.to_managed();
let filename = file.display().to_str().to_managed(); let filename = file.display().to_str().to_managed();
let interned = token::intern_and_get_ident(src);
cx.parse_sess.cm.new_filemap(filename, src); cx.parse_sess.cm.new_filemap(filename, src);
base::MRExpr(cx.expr_str(sp, token::intern_and_get_ident(src))) base::MRExpr(cx.expr_str(sp, interned))
} }
None => { None => {
cx.span_err(sp, format!("{} wasn't a utf-8 file", file.display())); cx.span_err(sp, format!("{} wasn't a utf-8 file", file.display()));

View file

@ -350,7 +350,7 @@ pub fn gather_comments_and_literals(span_diagnostic:
path: @str, path: @str,
srdr: &mut io::Reader) srdr: &mut io::Reader)
-> (~[Comment], ~[Literal]) { -> (~[Comment], ~[Literal]) {
let src = str::from_utf8_owned(srdr.read_to_end()).unwrap().to_managed(); let src = str::from_utf8_owned(srdr.read_to_end()).unwrap();
let cm = CodeMap::new(); let cm = CodeMap::new();
let filemap = cm.new_filemap(path, src); let filemap = cm.new_filemap(path, src);
let rdr = lexer::new_low_level_string_reader(span_diagnostic, filemap); let rdr = lexer::new_low_level_string_reader(span_diagnostic, filemap);

View file

@ -89,12 +89,11 @@ pub fn parse_crate_attrs_from_file(
return inner; return inner;
} }
pub fn parse_crate_from_source_str( pub fn parse_crate_from_source_str(name: @str,
name: @str, source: ~str,
source: @str, cfg: ast::CrateConfig,
cfg: ast::CrateConfig, sess: @ParseSess)
sess: @ParseSess -> ast::Crate {
) -> ast::Crate {
let mut p = new_parser_from_source_str(sess, let mut p = new_parser_from_source_str(sess,
/*bad*/ cfg.clone(), /*bad*/ cfg.clone(),
name, name,
@ -102,12 +101,11 @@ pub fn parse_crate_from_source_str(
maybe_aborted(p.parse_crate_mod(),p) maybe_aborted(p.parse_crate_mod(),p)
} }
pub fn parse_crate_attrs_from_source_str( pub fn parse_crate_attrs_from_source_str(name: @str,
name: @str, source: ~str,
source: @str, cfg: ast::CrateConfig,
cfg: ast::CrateConfig, sess: @ParseSess)
sess: @ParseSess -> ~[ast::Attribute] {
) -> ~[ast::Attribute] {
let mut p = new_parser_from_source_str(sess, let mut p = new_parser_from_source_str(sess,
/*bad*/ cfg.clone(), /*bad*/ cfg.clone(),
name, name,
@ -116,44 +114,40 @@ pub fn parse_crate_attrs_from_source_str(
return inner; return inner;
} }
pub fn parse_expr_from_source_str( pub fn parse_expr_from_source_str(name: @str,
name: @str, source: ~str,
source: @str, cfg: ast::CrateConfig,
cfg: ast::CrateConfig, sess: @ParseSess)
sess: @ParseSess -> @ast::Expr {
) -> @ast::Expr {
let mut p = new_parser_from_source_str(sess, cfg, name, source); let mut p = new_parser_from_source_str(sess, cfg, name, source);
maybe_aborted(p.parse_expr(), p) maybe_aborted(p.parse_expr(), p)
} }
pub fn parse_item_from_source_str( pub fn parse_item_from_source_str(name: @str,
name: @str, source: ~str,
source: @str, cfg: ast::CrateConfig,
cfg: ast::CrateConfig, sess: @ParseSess)
sess: @ParseSess -> Option<@ast::Item> {
) -> Option<@ast::Item> {
let mut p = new_parser_from_source_str(sess, cfg, name, source); let mut p = new_parser_from_source_str(sess, cfg, name, source);
let attrs = p.parse_outer_attributes(); let attrs = p.parse_outer_attributes();
maybe_aborted(p.parse_item(attrs),p) maybe_aborted(p.parse_item(attrs),p)
} }
pub fn parse_meta_from_source_str( pub fn parse_meta_from_source_str(name: @str,
name: @str, source: ~str,
source: @str, cfg: ast::CrateConfig,
cfg: ast::CrateConfig, sess: @ParseSess)
sess: @ParseSess -> @ast::MetaItem {
) -> @ast::MetaItem {
let mut p = new_parser_from_source_str(sess, cfg, name, source); let mut p = new_parser_from_source_str(sess, cfg, name, source);
maybe_aborted(p.parse_meta_item(),p) maybe_aborted(p.parse_meta_item(),p)
} }
pub fn parse_stmt_from_source_str( pub fn parse_stmt_from_source_str(name: @str,
name: @str, source: ~str,
source: @str, cfg: ast::CrateConfig,
cfg: ast::CrateConfig, attrs: ~[ast::Attribute],
attrs: ~[ast::Attribute], sess: @ParseSess)
sess: @ParseSess -> @ast::Stmt {
) -> @ast::Stmt {
let mut p = new_parser_from_source_str( let mut p = new_parser_from_source_str(
sess, sess,
cfg, cfg,
@ -163,12 +157,11 @@ pub fn parse_stmt_from_source_str(
maybe_aborted(p.parse_stmt(attrs),p) maybe_aborted(p.parse_stmt(attrs),p)
} }
pub fn parse_tts_from_source_str( pub fn parse_tts_from_source_str(name: @str,
name: @str, source: ~str,
source: @str, cfg: ast::CrateConfig,
cfg: ast::CrateConfig, sess: @ParseSess)
sess: @ParseSess -> ~[ast::TokenTree] {
) -> ~[ast::TokenTree] {
let mut p = new_parser_from_source_str( let mut p = new_parser_from_source_str(
sess, sess,
cfg, cfg,
@ -184,8 +177,8 @@ pub fn parse_tts_from_source_str(
pub fn new_parser_from_source_str(sess: @ParseSess, pub fn new_parser_from_source_str(sess: @ParseSess,
cfg: ast::CrateConfig, cfg: ast::CrateConfig,
name: @str, name: @str,
source: @str) source: ~str)
-> Parser { -> Parser {
filemap_to_parser(sess,string_to_filemap(sess,source,name),cfg) filemap_to_parser(sess,string_to_filemap(sess,source,name),cfg)
} }
@ -248,7 +241,8 @@ pub fn file_to_filemap(sess: @ParseSess, path: &Path, spanopt: Option<Span>)
}; };
match str::from_utf8_owned(bytes) { match str::from_utf8_owned(bytes) {
Some(s) => { Some(s) => {
return string_to_filemap(sess, s.to_managed(), return string_to_filemap(sess,
s,
path.as_str().unwrap().to_managed()); path.as_str().unwrap().to_managed());
} }
None => { None => {
@ -260,7 +254,7 @@ pub fn file_to_filemap(sess: @ParseSess, path: &Path, spanopt: Option<Span>)
// given a session and a string, add the string to // given a session and a string, add the string to
// the session's codemap and return the new filemap // the session's codemap and return the new filemap
pub fn string_to_filemap(sess: @ParseSess, source: @str, path: @str) pub fn string_to_filemap(sess: @ParseSess, source: ~str, path: @str)
-> @FileMap { -> @FileMap {
sess.cm.new_filemap(path, source) sess.cm.new_filemap(path, source)
} }

View file

@ -17,29 +17,29 @@ use parse::token;
// map a string to tts, using a made-up filename: return both the TokenTree's // map a string to tts, using a made-up filename: return both the TokenTree's
// and the ParseSess // and the ParseSess
pub fn string_to_tts_and_sess (source_str : @str) -> (~[ast::TokenTree], @ParseSess) { pub fn string_to_tts_and_sess (source_str : ~str) -> (~[ast::TokenTree], @ParseSess) {
let ps = new_parse_sess(None); let ps = new_parse_sess(None);
(filemap_to_tts(ps,string_to_filemap(ps,source_str,@"bogofile")),ps) (filemap_to_tts(ps,string_to_filemap(ps,source_str,@"bogofile")),ps)
} }
// map a string to tts, using a made-up filename: // map a string to tts, using a made-up filename:
pub fn string_to_tts(source_str : @str) -> ~[ast::TokenTree] { pub fn string_to_tts(source_str : ~str) -> ~[ast::TokenTree] {
let (tts,_) = string_to_tts_and_sess(source_str); let (tts,_) = string_to_tts_and_sess(source_str);
tts tts
} }
pub fn string_to_parser_and_sess(source_str: @str) -> (Parser,@ParseSess) { pub fn string_to_parser_and_sess(source_str: ~str) -> (Parser,@ParseSess) {
let ps = new_parse_sess(None); let ps = new_parse_sess(None);
(new_parser_from_source_str(ps,~[],@"bogofile",source_str),ps) (new_parser_from_source_str(ps,~[],@"bogofile",source_str),ps)
} }
// map string to parser (via tts) // map string to parser (via tts)
pub fn string_to_parser(source_str: @str) -> Parser { pub fn string_to_parser(source_str: ~str) -> Parser {
let (p,_) = string_to_parser_and_sess(source_str); let (p,_) = string_to_parser_and_sess(source_str);
p p
} }
fn with_error_checking_parse<T>(s: @str, f: |&mut Parser| -> T) -> T { fn with_error_checking_parse<T>(s: ~str, f: |&mut Parser| -> T) -> T {
let mut p = string_to_parser(s); let mut p = string_to_parser(s);
let x = f(&mut p); let x = f(&mut p);
p.abort_if_errors(); p.abort_if_errors();
@ -47,34 +47,34 @@ fn with_error_checking_parse<T>(s: @str, f: |&mut Parser| -> T) -> T {
} }
// parse a string, return a crate. // parse a string, return a crate.
pub fn string_to_crate (source_str : @str) -> ast::Crate { pub fn string_to_crate (source_str : ~str) -> ast::Crate {
with_error_checking_parse(source_str, |p| { with_error_checking_parse(source_str, |p| {
p.parse_crate_mod() p.parse_crate_mod()
}) })
} }
// parse a string, return a crate and the ParseSess // parse a string, return a crate and the ParseSess
pub fn string_to_crate_and_sess (source_str : @str) -> (ast::Crate,@ParseSess) { pub fn string_to_crate_and_sess (source_str : ~str) -> (ast::Crate,@ParseSess) {
let (mut p,ps) = string_to_parser_and_sess(source_str); let (mut p,ps) = string_to_parser_and_sess(source_str);
(p.parse_crate_mod(),ps) (p.parse_crate_mod(),ps)
} }
// parse a string, return an expr // parse a string, return an expr
pub fn string_to_expr (source_str : @str) -> @ast::Expr { pub fn string_to_expr (source_str : ~str) -> @ast::Expr {
with_error_checking_parse(source_str, |p| { with_error_checking_parse(source_str, |p| {
p.parse_expr() p.parse_expr()
}) })
} }
// parse a string, return an item // parse a string, return an item
pub fn string_to_item (source_str : @str) -> Option<@ast::Item> { pub fn string_to_item (source_str : ~str) -> Option<@ast::Item> {
with_error_checking_parse(source_str, |p| { with_error_checking_parse(source_str, |p| {
p.parse_item(~[]) p.parse_item(~[])
}) })
} }
// parse a string, return a stmt // parse a string, return a stmt
pub fn string_to_stmt(source_str : @str) -> @ast::Stmt { pub fn string_to_stmt(source_str : ~str) -> @ast::Stmt {
with_error_checking_parse(source_str, |p| { with_error_checking_parse(source_str, |p| {
p.parse_stmt(~[]) p.parse_stmt(~[])
}) })
@ -82,7 +82,7 @@ pub fn string_to_stmt(source_str : @str) -> @ast::Stmt {
// parse a string, return a pat. Uses "irrefutable"... which doesn't // parse a string, return a pat. Uses "irrefutable"... which doesn't
// (currently) affect parsing. // (currently) affect parsing.
pub fn string_to_pat(source_str : @str) -> @ast::Pat { pub fn string_to_pat(source_str : ~str) -> @ast::Pat {
string_to_parser(source_str).parse_pat() string_to_parser(source_str).parse_pat()
} }