Skip to content

Treat include_str! like it produces a raw string. #143077

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/source_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ pub(crate) fn expand_include_str(
Ok((bytes, bsp)) => match std::str::from_utf8(&bytes) {
Ok(src) => {
let interned_src = Symbol::intern(src);
MacEager::expr(cx.expr_str(cx.with_def_site_ctxt(bsp), interned_src))
MacEager::expr(cx.expr_str_raw(cx.with_def_site_ctxt(bsp), interned_src))
}
Err(utf8err) => {
let mut err = cx.dcx().struct_span_err(sp, format!("`{path}` wasn't a utf-8 file"));
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_expand/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,11 @@ impl<'a> ExtCtxt<'a> {
self.expr(span, ast::ExprKind::Lit(lit))
}

pub fn expr_str_raw(&self, span: Span, s: Symbol) -> P<ast::Expr> {
let lit = token::Lit::new(token::StrRaw(0), s, None);
self.expr(span, ast::ExprKind::Lit(lit))
}
Comment on lines +448 to +451
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can only assume 0 means "with 0 #s". But that would produce an invalid token, right? I.e. if the file being included contains ", this would break. Moreover for any number of #s you can construct a file which would break it ("####...)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fn desugar_doc_comments has some logic that counts how many hashes need to be added to keep the literal in #[doc = r"my arbitrary string from a sugared doc comment"] well formed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, forgot to mention, for doc comments the hash counter can overflow too.


pub fn expr_byte_str(&self, span: Span, bytes: Vec<u8>) -> P<ast::Expr> {
let lit = token::Lit::new(token::ByteStr, literal::escape_byte_str_symbol(&bytes), None);
self.expr(span, ast::ExprKind::Lit(lit))
Expand Down
3 changes: 2 additions & 1 deletion tests/ui/proc-macro/expand-expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ expand_expr_is!("Hello, World!", concat!("Hello, ", "World", "!"));
expand_expr_is!("int10floats5.3booltrue", concat!("int", 10, "floats", 5.3, "bool", true));
expand_expr_is!("Hello", concat!(r##"Hello"##));

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

expand_expr_is!(
Expand Down
14 changes: 7 additions & 7 deletions tests/ui/proc-macro/expand-expr.stderr
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
error: expected one of `.`, `?`, or an operator, found `;`
--> $DIR/expand-expr.rs:108:27
--> $DIR/expand-expr.rs:109:27
|
LL | expand_expr_fail!("string"; hello);
| ^ expected one of `.`, `?`, or an operator

error: expected expression, found `$`
--> $DIR/expand-expr.rs:111:19
--> $DIR/expand-expr.rs:112:19
|
LL | expand_expr_fail!($);
| ^ expected expression

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

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

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

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

error: recursion limit reached while expanding `recursive_expand!`
--> $DIR/expand-expr.rs:126:16
--> $DIR/expand-expr.rs:127:16
|
LL | const _: u32 = recursive_expand!();
| ^^^^^^^^^^^^^^^^^^^
Expand Down
Loading