Skip to content

Commit 9d7b130

Browse files
committed
add new enum ast::StrStyle as field to ast::lit_str
For the benefit of the pretty printer we want to keep track of how string literals in the ast were originally represented in the source code. This commit changes parser functions so they don't extract strings from the token stream without at least also returning what style of string literal it was. This is stored in the resulting ast node for string literals, obviously, for the package id in `extern mod = r"package id"` view items, for the inline asm in `asm!()` invocations. For `asm!()`'s other arguments or for `extern "Rust" fn()` items, I just the style of string, because it seemed disproportionally cumbersome to thread that information through the string processing that happens with those string literals, given the limited advantage raw string literals would provide in these positions. The other syntax extensions don't seem to store passed string literals in the ast, so they also discard the style of strings they parse.
1 parent 9787872 commit 9d7b130

File tree

25 files changed

+92
-73
lines changed

25 files changed

+92
-73
lines changed

src/librustc/front/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ fn mk_test_desc_and_fn_rec(cx: &TestCtxt, test: &Test) -> @ast::Expr {
407407
debug2!("encoding {}", ast_util::path_name_i(path));
408408

409409
let name_lit: ast::lit =
410-
nospan(ast::lit_str(ast_util::path_name_i(path).to_managed()));
410+
nospan(ast::lit_str(ast_util::path_name_i(path).to_managed(), ast::CookedStr));
411411

412412
let name_expr = @ast::Expr {
413413
id: ast::DUMMY_NODE_ID,

src/librustc/metadata/creader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ fn visit_view_item(e: @mut Env, i: &ast::view_item) {
142142
let ident = token::ident_to_str(&ident);
143143
let meta_items = match path_opt {
144144
None => meta_items.clone(),
145-
Some(p) => {
145+
Some((p, _path_str_style)) => {
146146
let p_path = Path(p);
147147
match p_path.filestem() {
148148
Some(s) =>

src/librustc/metadata/encoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1446,7 +1446,7 @@ fn encode_meta_item(ebml_w: &mut writer::Encoder, mi: @MetaItem) {
14461446
}
14471447
MetaNameValue(name, value) => {
14481448
match value.node {
1449-
lit_str(value) => {
1449+
lit_str(value, _) => {
14501450
ebml_w.start_tag(tag_meta_item_name_value);
14511451
ebml_w.start_tag(tag_meta_item_name);
14521452
ebml_w.writer.write(name.as_bytes());

src/librustc/middle/check_const.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pub fn check_pat(v: &mut CheckCrateVisitor, p: @Pat, _is_const: bool) {
8686
match e.node {
8787
ExprVstore(
8888
@Expr { node: ExprLit(@codemap::Spanned {
89-
node: lit_str(_),
89+
node: lit_str(*),
9090
_}),
9191
_ },
9292
ExprVstoreUniq
@@ -120,7 +120,7 @@ pub fn check_expr(v: &mut CheckCrateVisitor,
120120
"disallowed operator in constant expression");
121121
return;
122122
}
123-
ExprLit(@codemap::Spanned {node: lit_str(_), _}) => { }
123+
ExprLit(@codemap::Spanned {node: lit_str(*), _}) => { }
124124
ExprBinary(*) | ExprUnary(*) => {
125125
if method_map.contains_key(&e.id) {
126126
sess.span_err(e.span, "user-defined operators are not \

src/librustc/middle/const_eval.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ pub fn eval_const_expr_partial<T: ty::ExprTyProvider>(tcx: &T, e: &Expr)
475475

476476
pub fn lit_to_const(lit: &lit) -> const_val {
477477
match lit.node {
478-
lit_str(s) => const_str(s),
478+
lit_str(s, _) => const_str(s),
479479
lit_char(n) => const_uint(n as u64),
480480
lit_int(n, _) => const_int(n),
481481
lit_uint(n, _) => const_uint(n),

src/librustc/middle/trans/consts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub fn const_lit(cx: &mut CrateContext, e: &ast::Expr, lit: ast::lit)
7171
}
7272
ast::lit_bool(b) => C_bool(b),
7373
ast::lit_nil => C_nil(),
74-
ast::lit_str(s) => C_estr_slice(cx, s)
74+
ast::lit_str(s, _) => C_estr_slice(cx, s)
7575
}
7676
}
7777

src/librustc/middle/trans/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ fn trans_rvalue_dps_unadjusted(bcx: @mut Block, expr: &ast::Expr,
705705
args.iter().enumerate().map(|(i, arg)| (i, *arg)).collect();
706706
return trans_adt(bcx, repr, 0, numbered_fields, None, dest);
707707
}
708-
ast::ExprLit(@codemap::Spanned {node: ast::lit_str(s), _}) => {
708+
ast::ExprLit(@codemap::Spanned {node: ast::lit_str(s, _), _}) => {
709709
return tvec::trans_lit_str(bcx, expr, s, dest);
710710
}
711711
ast::ExprVstore(contents, ast::ExprVstoreSlice) |

src/librustc/middle/trans/tvec.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ pub fn trans_slice_vstore(bcx: @mut Block,
205205

206206
// Handle the &"..." case:
207207
match content_expr.node {
208-
ast::ExprLit(@codemap::Spanned {node: ast::lit_str(s), span: _}) => {
208+
ast::ExprLit(@codemap::Spanned {node: ast::lit_str(s, _), span: _}) => {
209209
return trans_lit_str(bcx, content_expr, s, dest);
210210
}
211211
_ => {}
@@ -296,7 +296,7 @@ pub fn trans_uniq_or_managed_vstore(bcx: @mut Block, heap: heap, vstore_expr: &a
296296
heap_exchange => {
297297
match content_expr.node {
298298
ast::ExprLit(@codemap::Spanned {
299-
node: ast::lit_str(s), span
299+
node: ast::lit_str(s, _), span
300300
}) => {
301301
let llptrval = C_cstr(bcx.ccx(), s);
302302
let llptrval = PointerCast(bcx, llptrval, Type::i8p());
@@ -357,7 +357,7 @@ pub fn write_content(bcx: @mut Block,
357357
let _indenter = indenter();
358358

359359
match content_expr.node {
360-
ast::ExprLit(@codemap::Spanned { node: ast::lit_str(s), _ }) => {
360+
ast::ExprLit(@codemap::Spanned { node: ast::lit_str(s, _), _ }) => {
361361
match dest {
362362
Ignore => {
363363
return bcx;
@@ -490,7 +490,7 @@ pub fn elements_required(bcx: @mut Block, content_expr: &ast::Expr) -> uint {
490490
//! Figure out the number of elements we need to store this content
491491
492492
match content_expr.node {
493-
ast::ExprLit(@codemap::Spanned { node: ast::lit_str(s), _ }) => {
493+
ast::ExprLit(@codemap::Spanned { node: ast::lit_str(s, _), _ }) => {
494494
s.len()
495495
},
496496
ast::ExprVec(ref es, _) => es.len(),

src/librustc/middle/ty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3266,7 +3266,7 @@ pub fn expr_kind(tcx: ctxt,
32663266
ast::ExprDoBody(*) |
32673267
ast::ExprBlock(*) |
32683268
ast::ExprRepeat(*) |
3269-
ast::ExprLit(@codemap::Spanned {node: lit_str(_), _}) |
3269+
ast::ExprLit(@codemap::Spanned {node: lit_str(*), _}) |
32703270
ast::ExprVstore(_, ast::ExprVstoreSlice) |
32713271
ast::ExprVstore(_, ast::ExprVstoreMutSlice) |
32723272
ast::ExprVec(*) => {

src/librustc/middle/typeck/check/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2259,7 +2259,7 @@ pub fn check_expr_with_unifier(fcx: @mut FnCtxt,
22592259
match expr.node {
22602260
ast::ExprVstore(ev, vst) => {
22612261
let typ = match ev.node {
2262-
ast::ExprLit(@codemap::Spanned { node: ast::lit_str(_), _ }) => {
2262+
ast::ExprLit(@codemap::Spanned { node: ast::lit_str(*), _ }) => {
22632263
let tt = ast_expr_vstore_to_vstore(fcx, ev, vst);
22642264
ty::mk_estr(tcx, tt)
22652265
}

src/librustdoc/clean.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,7 @@ impl Clean<ViewItemInner> for ast::view_item_ {
10081008
fn clean(&self) -> ViewItemInner {
10091009
match self {
10101010
&ast::view_item_extern_mod(ref i, ref p, ref mi, ref id) =>
1011-
ExternMod(i.clean(), p.map(|x| x.to_owned()), mi.clean(), *id),
1011+
ExternMod(i.clean(), p.map(|&(ref x, _)| x.to_owned()), mi.clean(), *id),
10121012
&ast::view_item_use(ref vp) => Import(vp.clean())
10131013
}
10141014
}
@@ -1114,7 +1114,7 @@ impl ToSource for syntax::codemap::Span {
11141114

11151115
fn lit_to_str(lit: &ast::lit) -> ~str {
11161116
match lit.node {
1117-
ast::lit_str(st) => st.to_owned(),
1117+
ast::lit_str(st, _) => st.to_owned(),
11181118
ast::lit_char(c) => ~"'" + std::char::from_u32(c).unwrap().to_str() + "'",
11191119
ast::lit_int(i, _t) => i.to_str(),
11201120
ast::lit_uint(u, _t) => u.to_str(),

src/librustpkg/util.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ impl<'self> Visitor<()> for ViewItemVisitor<'self> {
406406
// ignore metadata, I guess
407407
ast::view_item_extern_mod(lib_ident, path_opt, _, _) => {
408408
let lib_name = match path_opt {
409-
Some(p) => p,
409+
Some((p, _)) => p,
410410
None => self.sess.str_of(lib_ident)
411411
};
412412
debug2!("Finding and installing... {}", lib_name);
@@ -513,7 +513,7 @@ pub fn find_and_install_dependencies(context: &BuildContext,
513513

514514
pub fn mk_string_lit(s: @str) -> ast::lit {
515515
Spanned {
516-
node: ast::lit_str(s),
516+
node: ast::lit_str(s, ast::CookedStr),
517517
span: dummy_sp()
518518
}
519519
}

src/libsyntax/ast.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -680,11 +680,17 @@ pub enum mac_ {
680680
mac_invoc_tt(Path,~[token_tree],SyntaxContext), // new macro-invocation
681681
}
682682

683+
#[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
684+
pub enum StrStyle {
685+
CookedStr,
686+
RawStr(uint)
687+
}
688+
683689
pub type lit = Spanned<lit_>;
684690

685691
#[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
686692
pub enum lit_ {
687-
lit_str(@str),
693+
lit_str(@str, StrStyle),
688694
lit_char(u32),
689695
lit_int(i64, int_ty),
690696
lit_uint(u64, uint_ty),
@@ -862,6 +868,7 @@ pub enum asm_dialect {
862868
#[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
863869
pub struct inline_asm {
864870
asm: @str,
871+
asm_str_style: StrStyle,
865872
clobbers: @str,
866873
inputs: ~[(@str, @Expr)],
867874
outputs: ~[(@str, @Expr)],
@@ -1027,7 +1034,7 @@ pub enum view_item_ {
10271034
// optional @str: if present, this is a location (containing
10281035
// arbitrary characters) from which to fetch the crate sources
10291036
// For example, extern mod whatever = "github.com/mozilla/rust"
1030-
view_item_extern_mod(Ident, Option<@str>, ~[@MetaItem], NodeId),
1037+
view_item_extern_mod(Ident, Option<(@str, StrStyle)>, ~[@MetaItem], NodeId),
10311038
view_item_use(~[@view_path]),
10321039
}
10331040

src/libsyntax/attr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl AttrMetaMethods for MetaItem {
6767
match self.node {
6868
MetaNameValue(_, ref v) => {
6969
match v.node {
70-
ast::lit_str(s) => Some(s),
70+
ast::lit_str(s, _) => Some(s),
7171
_ => None,
7272
}
7373
},
@@ -127,7 +127,7 @@ impl AttributeMethods for Attribute {
127127
/* Constructors */
128128

129129
pub fn mk_name_value_item_str(name: @str, value: @str) -> @MetaItem {
130-
let value_lit = dummy_spanned(ast::lit_str(value));
130+
let value_lit = dummy_spanned(ast::lit_str(value, ast::CookedStr));
131131
mk_name_value_item(name, value_lit)
132132
}
133133

@@ -153,7 +153,7 @@ pub fn mk_attr(item: @MetaItem) -> Attribute {
153153

154154
pub fn mk_sugared_doc_attr(text: @str, lo: BytePos, hi: BytePos) -> Attribute {
155155
let style = doc_comment_style(text);
156-
let lit = spanned(lo, hi, ast::lit_str(text));
156+
let lit = spanned(lo, hi, ast::lit_str(text, ast::CookedStr));
157157
let attr = Attribute_ {
158158
style: style,
159159
value: @spanned(lo, hi, MetaNameValue(@"doc", lit)),

src/libsyntax/ext/asm.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pub fn expand_asm(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree])
4444
tts.to_owned());
4545

4646
let mut asm = @"";
47+
let mut asm_str_style = None;
4748
let mut outputs = ~[];
4849
let mut inputs = ~[];
4950
let mut cons = ~"";
@@ -58,8 +59,11 @@ pub fn expand_asm(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree])
5859
while continue_ {
5960
match state {
6061
Asm => {
61-
asm = expr_to_str(cx, p.parse_expr(),
62-
"inline assembly must be a string literal.");
62+
let (s, style) =
63+
expr_to_str(cx, p.parse_expr(),
64+
"inline assembly must be a string literal.");
65+
asm = s;
66+
asm_str_style = Some(style);
6367
}
6468
Outputs => {
6569
while *p.token != token::EOF &&
@@ -70,7 +74,7 @@ pub fn expand_asm(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree])
7074
p.eat(&token::COMMA);
7175
}
7276

73-
let constraint = p.parse_str();
77+
let (constraint, _str_style) = p.parse_str();
7478
p.expect(&token::LPAREN);
7579
let out = p.parse_expr();
7680
p.expect(&token::RPAREN);
@@ -93,7 +97,7 @@ pub fn expand_asm(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree])
9397
p.eat(&token::COMMA);
9498
}
9599

96-
let constraint = p.parse_str();
100+
let (constraint, _str_style) = p.parse_str();
97101
p.expect(&token::LPAREN);
98102
let input = p.parse_expr();
99103
p.expect(&token::RPAREN);
@@ -111,14 +115,15 @@ pub fn expand_asm(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree])
111115
p.eat(&token::COMMA);
112116
}
113117

114-
let clob = format!("~\\{{}\\}", p.parse_str());
118+
let (s, _str_style) = p.parse_str();
119+
let clob = format!("~\\{{}\\}", s);
115120
clobs.push(clob);
116121
}
117122

118123
cons = clobs.connect(",");
119124
}
120125
Options => {
121-
let option = p.parse_str();
126+
let (option, _str_style) = p.parse_str();
122127

123128
if "volatile" == option {
124129
volatile = true;
@@ -175,6 +180,7 @@ pub fn expand_asm(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree])
175180
id: ast::DUMMY_NODE_ID,
176181
node: ast::ExprInlineAsm(ast::inline_asm {
177182
asm: asm,
183+
asm_str_style: asm_str_style.unwrap(),
178184
clobbers: cons.to_managed(),
179185
inputs: inputs,
180186
outputs: outputs,

src/libsyntax/ext/base.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,10 +410,10 @@ impl ExtCtxt {
410410
}
411411
}
412412

413-
pub fn expr_to_str(cx: @ExtCtxt, expr: @ast::Expr, err_msg: &str) -> @str {
413+
pub fn expr_to_str(cx: @ExtCtxt, expr: @ast::Expr, err_msg: &str) -> (@str, ast::StrStyle) {
414414
match expr.node {
415415
ast::ExprLit(l) => match l.node {
416-
ast::lit_str(s) => s,
416+
ast::lit_str(s, style) => (s, style),
417417
_ => cx.span_fatal(l.span, err_msg)
418418
},
419419
_ => cx.span_fatal(expr.span, err_msg)

src/libsyntax/ext/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ impl AstBuilder for @ExtCtxt {
562562
self.expr_vstore(sp, self.expr_vec(sp, exprs), ast::ExprVstoreSlice)
563563
}
564564
fn expr_str(&self, sp: Span, s: @str) -> @ast::Expr {
565-
self.expr_lit(sp, ast::lit_str(s))
565+
self.expr_lit(sp, ast::lit_str(s, ast::CookedStr))
566566
}
567567
fn expr_str_uniq(&self, sp: Span, s: @str) -> @ast::Expr {
568568
self.expr_vstore(sp, self.expr_str(sp, s), ast::ExprVstoreUniq)

src/libsyntax/ext/bytes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub fn expand_syntax_ext(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree]) -> bas
2828
// expression is a literal
2929
ast::ExprLit(lit) => match lit.node {
3030
// string literal, push each byte to vector expression
31-
ast::lit_str(s) => {
31+
ast::lit_str(s, _) => {
3232
for byte in s.byte_iter() {
3333
bytes.push(cx.expr_u8(expr.span, byte));
3434
}

src/libsyntax/ext/deriving/generic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ impl<'self> TraitDef<'self> {
361361
span,
362362
cx.meta_name_value(span,
363363
@"doc",
364-
ast::lit_str(@"Automatically derived.")));
364+
ast::lit_str(@"Automatically derived.", ast::CookedStr)));
365365
cx.item(
366366
span,
367367
::parse::token::special_idents::clownshoes_extensions,

src/libsyntax/ext/env.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,13 @@ pub fn expand_env(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree])
4141
cx.span_fatal(sp, "env! takes 1 or 2 arguments");
4242
}
4343

44-
let var = expr_to_str(cx, exprs[0], "expected string literal");
44+
let (var, _var_str_style) = expr_to_str(cx, exprs[0], "expected string literal");
4545
let msg = match exprs.len() {
4646
1 => format!("Environment variable {} not defined", var).to_managed(),
47-
2 => expr_to_str(cx, exprs[1], "expected string literal"),
47+
2 => {
48+
let (s, _style) = expr_to_str(cx, exprs[1], "expected string literal");
49+
s
50+
}
4851
_ => cx.span_fatal(sp, "env! takes 1 or 2 arguments")
4952
};
5053

src/libsyntax/ext/fmt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub fn expand_syntax_ext(cx: @ExtCtxt, sp: Span, tts: &[ast::token_tree])
3030
if args.len() == 0 {
3131
cx.span_fatal(sp, "fmt! takes at least 1 argument.");
3232
}
33-
let fmt =
33+
let (fmt, _fmt_str_style) =
3434
expr_to_str(cx, args[0],
3535
"first argument to fmt! must be a string literal.");
3636
let fmtspan = args[0].span;

src/libsyntax/ext/format.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -722,8 +722,8 @@ pub fn expand_args(ecx: @ExtCtxt, sp: Span,
722722
(_, None) => { return MRExpr(ecx.expr_uint(sp, 2)); }
723723
};
724724
cx.fmtsp = efmt.span;
725-
let fmt = expr_to_str(ecx, efmt,
726-
"format argument must be a string literal.");
725+
let (fmt, _fmt_str_style) = expr_to_str(ecx, efmt,
726+
"format argument must be a string literal.");
727727

728728
let mut err = false;
729729
do parse::parse_error::cond.trap(|m| {

src/libsyntax/ext/quote.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ pub mod rt {
118118

119119
impl<'self> ToSource for &'self str {
120120
fn to_source(&self) -> @str {
121-
let lit = dummy_spanned(ast::lit_str(self.to_managed()));
121+
let lit = dummy_spanned(ast::lit_str(self.to_managed(), ast::CookedStr));
122122
pprust::lit_to_str(@lit).to_managed()
123123
}
124124
}

0 commit comments

Comments
 (0)