Skip to content

Commit a5be7fa

Browse files
committed
Provide helpful diagnostics for shebang lookalikes
1 parent a18bd8a commit a5be7fa

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

compiler/rustc_parse/src/parser/attr.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,14 @@ impl<'a> Parser<'a> {
130130
let style =
131131
if this.eat(exp!(Not)) { ast::AttrStyle::Inner } else { ast::AttrStyle::Outer };
132132

133-
this.expect(exp!(OpenBracket))?;
133+
let bracket_res = this.expect(exp!(OpenBracket));
134+
// If `#!` is not followed by `[`
135+
if bracket_res.is_err() && style == ast::AttrStyle::Inner {
136+
return Err(bracket_res
137+
.unwrap_err()
138+
.with_note("shebangs `#!` must be at the start of the file"));
139+
}
140+
bracket_res?;
134141
let item = this.parse_attr_item(ForceCollect::No)?;
135142
this.expect(exp!(CloseBracket))?;
136143
let attr_sp = lo.to(this.prev_token.span);

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

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error: expected `[`, found `B`
33
|
44
LL | #!B
55
| ^ expected `[`
6+
|
7+
= note: shebangs `#!` must be at the start of the file
68

79
error: aborting due to 1 previous error
810

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

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error: expected `[`, found `/`
33
|
44
LL | #!/bin/bash
55
| ^ expected `[`
6+
|
7+
= note: shebangs `#!` must be at the start of the file
68

79
error: aborting due to 1 previous error
810

0 commit comments

Comments
 (0)