Skip to content

Commit dfe7c65

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

File tree

1 file changed

+8
-1
lines changed
  • compiler/rustc_parse/src/parser

1 file changed

+8
-1
lines changed

compiler/rustc_parse/src/parser/attr.rs

Lines changed: 8 additions & 1 deletion
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);

0 commit comments

Comments
 (0)