diff options
| author | Ben Kimock <kimockb@gmail.com> | 2025-03-16 06:01:32 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-03-16 06:01:32 +0000 |
| commit | ea2a2bd61adb62fe47300011ce0c743a98368f79 (patch) | |
| tree | 001d780c6d9145424e81b3488d472beb488fa237 /compiler/rustc_parse/src/parser | |
| parent | b724fabb5f86585cd7cc2b14daa2c5a34ed779ef (diff) | |
| parent | 17ae00d1228ced4eb5a19fa4371b080da87b6b2a (diff) | |
| download | rust-ea2a2bd61adb62fe47300011ce0c743a98368f79.tar.gz rust-ea2a2bd61adb62fe47300011ce0c743a98368f79.zip | |
Merge pull request #4229 from rust-lang/rustup-2025-03-16
Automatic Rustup
Diffstat (limited to 'compiler/rustc_parse/src/parser')
| -rw-r--r-- | compiler/rustc_parse/src/parser/attr.rs | 19 | ||||
| -rw-r--r-- | compiler/rustc_parse/src/parser/expr.rs | 3 |
2 files changed, 20 insertions, 2 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); diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index fc9a511d56a..41720e99e3b 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -2598,7 +2598,8 @@ impl<'a> Parser<'a> { } /// Parses the condition of a `if` or `while` expression. - fn parse_expr_cond(&mut self) -> PResult<'a, P<Expr>> { + // Public because it is used in rustfmt forks such as https://github.com/tucant/rustfmt/blob/30c83df9e1db10007bdd16dafce8a86b404329b2/src/parse/macros/html.rs#L57 for custom if expressions. + pub fn parse_expr_cond(&mut self) -> PResult<'a, P<Expr>> { let attrs = self.parse_outer_attributes()?; let (mut cond, _) = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL | Restrictions::ALLOW_LET, attrs)?; |
