diff options
| author | Pyrode <pyrode.01@gmail.com> | 2025-02-27 12:23:47 +0530 |
|---|---|---|
| committer | Pyrode <pyrode.01@gmail.com> | 2025-03-13 19:53:53 +0530 |
| commit | a73e44bce16c9610c5415db3037a69eef0df3c4e (patch) | |
| tree | a56601586144e9e51f1ba63b2082aafca926ee94 /compiler/rustc_parse/src/parser | |
| parent | 961351c76c812e3aeb65bfb542742500a6436aed (diff) | |
| download | rust-a73e44bce16c9610c5415db3037a69eef0df3c4e.tar.gz rust-a73e44bce16c9610c5415db3037a69eef0df3c4e.zip | |
Provide helpful diagnostics for shebang lookalikes
Diffstat (limited to 'compiler/rustc_parse/src/parser')
| -rw-r--r-- | compiler/rustc_parse/src/parser/attr.rs | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/compiler/rustc_parse/src/parser/attr.rs b/compiler/rustc_parse/src/parser/attr.rs index 066b570c23f..53614049f08 100644 --- a/compiler/rustc_parse/src/parser/attr.rs +++ b/compiler/rustc_parse/src/parser/attr.rs @@ -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); |
