Skip to content

Commit e477732

Browse files
committed
Don't strip shebang in expr-ctxt include!(…)
1 parent 80d8f29 commit e477732

File tree

6 files changed

+60
-3
lines changed

6 files changed

+60
-3
lines changed

compiler/rustc_builtin_macros/src/source_util.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,7 @@ pub(crate) fn expand_include<'cx>(
147147
let mut p = unwrap_or_emit_fatal(new_parser_from_file(
148148
self.psess,
149149
&self.path,
150-
// Don't strip frontmatter for backward compatibility, `---` may be the start of a
151-
// manifold negation. FIXME: Ideally, we wouldn't strip shebangs here either.
152-
StripTokens::Shebang,
150+
StripTokens::Nothing,
153151
Some(self.span),
154152
));
155153
let expr = parse_expr(&mut p).ok()?;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env my-rust-expr-evaluator
2+
3+
2 * (1 + 3)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env my-rust-script-runner
2+
3+
fn main() {}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Check that we *don't* strip shebang in files that were `include`d in an expression or
2+
// expression statement context.
3+
// We do that to be consistent with frontmatter (see test `frontmatter/include-in-expr-ctxt.rs`).
4+
// While there could be niche use cases for such shebang, it seems more confusing than beneficial.
5+
6+
fn main() {
7+
// expr ctxt
8+
_ = include!("auxiliary/shebang-expr.rs");
9+
//~^ ERROR non-expression macro in expression position
10+
//~? ERROR expected `[`, found `/`
11+
12+
// stmt ctxt (reuses expr expander)
13+
include!("auxiliary/shebang-expr.rs");
14+
//~^ ERROR non-statement macro in statement position
15+
//~? ERROR expected `[`, found `/`
16+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
error: expected `[`, found `/`
2+
--> $DIR/auxiliary/shebang-expr.rs:1:3
3+
|
4+
LL | #!/usr/bin/env my-rust-expr-evaluator
5+
| ^ expected `[`
6+
|
7+
= note: the token sequence `#!` here looks like the start of a shebang interpreter directive but it is not
8+
= help: if you meant this to be a shebang interpreter directive, move it to the very start of the file
9+
10+
error: non-expression macro in expression position: include
11+
--> $DIR/shebang-in-expr-ctxt.rs:8:9
12+
|
13+
LL | _ = include!("auxiliary/shebang-expr.rs");
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15+
16+
error: expected `[`, found `/`
17+
--> $DIR/auxiliary/shebang-expr.rs:1:3
18+
|
19+
LL | #!/usr/bin/env my-rust-expr-evaluator
20+
| ^ expected `[`
21+
|
22+
= note: the token sequence `#!` here looks like the start of a shebang interpreter directive but it is not
23+
= help: if you meant this to be a shebang interpreter directive, move it to the very start of the file
24+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
25+
26+
error: non-statement macro in statement position: include
27+
--> $DIR/shebang-in-expr-ctxt.rs:13:5
28+
|
29+
LL | include!("auxiliary/shebang-expr.rs");
30+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
31+
32+
error: aborting due to 4 previous errors
33+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Ensure that we strip shebang in files `include`d in item contexts.
2+
//@ check-pass
3+
4+
include!("auxiliary/shebang-items.rs");

0 commit comments

Comments
 (0)