Skip to content

Commit 9d437fd

Browse files
committed
Treat include_str! like it produces a raw string.
There are two reasons to do this: - It just makes sense. The contents of an included file is just like the contents of a raw string literal, because string escapes like `\"` and `\x61` don't get special treatment. - We can avoid escaping it when putting it into `token::Lit::StrRaw`, unlike `token::Lit::Str`. On a tiny test program that included an 80 MiB file, this reduced compile time from 2.2s to 1.0s. The change is detectable from proc macros that use `to_string` on tokens, as the change to the `expand-expr.rs` test indicates. But this kind of change is allowable, and it seems very unlikely to cause problems in practice.
1 parent 111e9bc commit 9d437fd

File tree

4 files changed

+15
-9
lines changed

4 files changed

+15
-9
lines changed

compiler/rustc_builtin_macros/src/source_util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ pub(crate) fn expand_include_str(
207207
Ok((bytes, bsp)) => match std::str::from_utf8(&bytes) {
208208
Ok(src) => {
209209
let interned_src = Symbol::intern(src);
210-
MacEager::expr(cx.expr_str(cx.with_def_site_ctxt(bsp), interned_src))
210+
MacEager::expr(cx.expr_str_raw(cx.with_def_site_ctxt(bsp), interned_src))
211211
}
212212
Err(utf8err) => {
213213
let mut err = cx.dcx().struct_span_err(sp, format!("`{path}` wasn't a utf-8 file"));

compiler/rustc_expand/src/build.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,11 @@ impl<'a> ExtCtxt<'a> {
445445
self.expr(span, ast::ExprKind::Lit(lit))
446446
}
447447

448+
pub fn expr_str_raw(&self, span: Span, s: Symbol) -> P<ast::Expr> {
449+
let lit = token::Lit::new(token::StrRaw(0), s, None);
450+
self.expr(span, ast::ExprKind::Lit(lit))
451+
}
452+
448453
pub fn expr_byte_str(&self, span: Span, bytes: Vec<u8>) -> P<ast::Expr> {
449454
let lit = token::Lit::new(token::ByteStr, literal::escape_byte_str_symbol(&bytes), None);
450455
self.expr(span, ast::ExprKind::Lit(lit))

tests/ui/proc-macro/expand-expr.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ expand_expr_is!("Hello, World!", concat!("Hello, ", "World", "!"));
1717
expand_expr_is!("int10floats5.3booltrue", concat!("int", 10, "floats", 5.3, "bool", true));
1818
expand_expr_is!("Hello", concat!(r##"Hello"##));
1919

20-
expand_expr_is!("Included file contents\n", include_str!("auxiliary/included-file.txt"));
20+
expand_expr_is!(r"Included file contents
21+
", include_str!("auxiliary/included-file.txt"));
2122
expand_expr_is!(b"Included file contents\n", include_bytes!("auxiliary/included-file.txt"));
2223

2324
expand_expr_is!(

tests/ui/proc-macro/expand-expr.stderr

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
error: expected one of `.`, `?`, or an operator, found `;`
2-
--> $DIR/expand-expr.rs:108:27
2+
--> $DIR/expand-expr.rs:109:27
33
|
44
LL | expand_expr_fail!("string"; hello);
55
| ^ expected one of `.`, `?`, or an operator
66

77
error: expected expression, found `$`
8-
--> $DIR/expand-expr.rs:111:19
8+
--> $DIR/expand-expr.rs:112:19
99
|
1010
LL | expand_expr_fail!($);
1111
| ^ expected expression
1212

1313
error: expected expression, found `$`
14-
--> $DIR/expand-expr.rs:112:29
14+
--> $DIR/expand-expr.rs:113:29
1515
|
1616
LL | expand_expr_fail!(echo_tts!($));
1717
| ^ expected expression
1818

1919
error: expected expression, found `$`
20-
--> $DIR/expand-expr.rs:113:28
20+
--> $DIR/expand-expr.rs:114:28
2121
|
2222
LL | expand_expr_fail!(echo_pm!($));
2323
| ^ expected expression
2424

2525
error: macro expansion ignores `hello` and any tokens following
26-
--> $DIR/expand-expr.rs:117:47
26+
--> $DIR/expand-expr.rs:118:47
2727
|
2828
LL | expand_expr_is!("string", echo_tts!("string"; hello));
2929
| --------------------^^^^^- caused by the macro expansion here
@@ -35,7 +35,7 @@ LL | expand_expr_is!("string", echo_tts!("string"; hello););
3535
| +
3636

3737
error: macro expansion ignores `;` and any tokens following
38-
--> $DIR/expand-expr.rs:118:44
38+
--> $DIR/expand-expr.rs:119:44
3939
|
4040
LL | expand_expr_is!("string", echo_pm!("string"; hello));
4141
| -----------------^------- caused by the macro expansion here
@@ -47,7 +47,7 @@ LL | expand_expr_is!("string", echo_pm!("string"; hello););
4747
| +
4848

4949
error: recursion limit reached while expanding `recursive_expand!`
50-
--> $DIR/expand-expr.rs:126:16
50+
--> $DIR/expand-expr.rs:127:16
5151
|
5252
LL | const _: u32 = recursive_expand!();
5353
| ^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)