Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 881d237

Browse files
authoredMar 14, 2025
Rollup merge of #137619 - Pyr0de:issue_137249, r=fmease
Provide helpful diagnostics for shebang lookalikes When `[` is not found after a `#!`, a note will be added to the exisiting error ``` error: expected `[`, found `/` --> src/main.rs:2:3 | 2 | #!/usr/bin/env -S cargo +nightly -Zscript | ^ expected `[` | = note: the token sequence `#!` here looks like the start of a shebang interpreter directive but it is not = help: if you meant this to be a shebang interpreter directive, move it to the very start of the file ``` Fixes #137249 r? `@fmease`
2 parents f8842bd + a73e44b commit 881d237

File tree

5 files changed

+37
-1
lines changed

5 files changed

+37
-1
lines changed
 

‎compiler/rustc_parse/src/parser/attr.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,29 @@ impl<'a> Parser<'a> {
127127
let lo = self.token.span;
128128
// Attributes can't have attributes of their own [Editor's note: not with that attitude]
129129
self.collect_tokens_no_attrs(|this| {
130+
let pound_hi = this.token.span.hi();
130131
assert!(this.eat(exp!(Pound)), "parse_attribute called in non-attribute position");
131132

133+
let not_lo = this.token.span.lo();
132134
let style =
133135
if this.eat(exp!(Bang)) { ast::AttrStyle::Inner } else { ast::AttrStyle::Outer };
134136

135-
this.expect(exp!(OpenBracket))?;
137+
let mut bracket_res = this.expect(exp!(OpenBracket));
138+
// If `#!` is not followed by `[`
139+
if let Err(err) = &mut bracket_res
140+
&& style == ast::AttrStyle::Inner
141+
&& pound_hi == not_lo
142+
{
143+
err.note(
144+
"the token sequence `#!` here looks like the start of \
145+
a shebang interpreter directive but it is not",
146+
);
147+
err.help(
148+
"if you meant this to be a shebang interpreter directive, \
149+
move it to the very start of the file",
150+
);
151+
}
152+
bracket_res?;
136153
let item = this.parse_attr_item(ForceCollect::No)?;
137154
this.expect(exp!(CloseBracket))?;
138155
let attr_sp = lo.to(this.prev_token.span);

‎tests/ui/parser/shebang/issue-71471-ignore-tidy.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ error: expected `[`, found `B`
33
|
44
LL | #!B
55
| ^ 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
69

710
error: aborting due to 1 previous error
811

‎tests/ui/parser/shebang/shebang-must-start-file.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ error: expected `[`, found `/`
33
|
44
LL | #!/bin/bash
55
| ^ 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
69

710
error: aborting due to 1 previous error
811

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// empty line
2+
# !/bin/env
3+
4+
// checks that diagnostics for shebang lookalikes is not present
5+
//@ error-pattern: expected `[`\n\n
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: expected `[`, found `/`
2+
--> $DIR/shebang-split.rs:2:4
3+
|
4+
LL | # !/bin/env
5+
| ^ expected `[`
6+
7+
error: aborting due to 1 previous error
8+

0 commit comments

Comments
 (0)
Failed to load comments.