Skip to content
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

Provide helpful diagnostics for shebang lookalikes #137619

Merged
merged 1 commit into from
Mar 14, 2025
Merged
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
19 changes: 18 additions & 1 deletion compiler/rustc_parse/src/parser/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,29 @@ impl<'a> Parser<'a> {
let lo = self.token.span;
// Attributes can't have attributes of their own [Editor's note: not with that attitude]
self.collect_tokens_no_attrs(|this| {
let pound_hi = this.token.span.hi();
assert!(this.eat(exp!(Pound)), "parse_attribute called in non-attribute position");

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

this.expect(exp!(OpenBracket))?;
let mut bracket_res = this.expect(exp!(OpenBracket));
// If `#!` is not followed by `[`
if let Err(err) = &mut bracket_res
&& style == ast::AttrStyle::Inner
&& pound_hi == not_lo
{
err.note(
"the token sequence `#!` here looks like the start of \
a shebang interpreter directive but it is not",
);
err.help(
"if you meant this to be a shebang interpreter directive, \
move it to the very start of the file",
);
}
bracket_res?;
let item = this.parse_attr_item(ForceCollect::No)?;
this.expect(exp!(CloseBracket))?;
let attr_sp = lo.to(this.prev_token.span);
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/parser/shebang/issue-71471-ignore-tidy.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ error: expected `[`, found `B`
|
LL | #!B
| ^ 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

error: aborting due to 1 previous error

3 changes: 3 additions & 0 deletions tests/ui/parser/shebang/shebang-must-start-file.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ error: expected `[`, found `/`
|
LL | #!/bin/bash
| ^ 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

error: aborting due to 1 previous error

5 changes: 5 additions & 0 deletions tests/ui/parser/shebang/shebang-split.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// empty line
# !/bin/env

// checks that diagnostics for shebang lookalikes is not present
//@ error-pattern: expected `[`\n\n
8 changes: 8 additions & 0 deletions tests/ui/parser/shebang/shebang-split.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: expected `[`, found `/`
--> $DIR/shebang-split.rs:2:4
|
LL | # !/bin/env
| ^ expected `[`

error: aborting due to 1 previous error

Loading