diff options
| author | Tyler Mandry <tmandry@gmail.com> | 2020-09-01 18:24:46 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-09-01 18:24:46 -0700 |
| commit | 56b5de2f2938db95060c7bcc8130d509c555c11f (patch) | |
| tree | a404029f91e0bd4e047c169861109756eb87ce5e | |
| parent | c22de44a80529ddee669b94b9589bd78d15a4e6a (diff) | |
| parent | b1491eacfc2f2ff05ff2a1d3b557c30e15c5f81f (diff) | |
| download | rust-56b5de2f2938db95060c7bcc8130d509c555c11f.tar.gz rust-56b5de2f2938db95060c7bcc8130d509c555c11f.zip | |
Rollup merge of #76218 - petrochenkov:shebang3, r=matklad
lexer: Tiny improvement to shebang detection Lexer now discerns between regular comments and doc comments, so use that. The change only affects the choice of reported errors.
| -rw-r--r-- | compiler/rustc_lexer/src/lib.rs | 14 | ||||
| -rw-r--r-- | src/test/ui/parser/shebang/shebang-doc-comment.rs | 5 | ||||
| -rw-r--r-- | src/test/ui/parser/shebang/shebang-doc-comment.stderr | 8 |
3 files changed, 14 insertions, 13 deletions
diff --git a/compiler/rustc_lexer/src/lib.rs b/compiler/rustc_lexer/src/lib.rs index b7d6194cd77..44999bbe857 100644 --- a/compiler/rustc_lexer/src/lib.rs +++ b/compiler/rustc_lexer/src/lib.rs @@ -191,12 +191,16 @@ pub fn strip_shebang(input: &str) -> Option<usize> { // For simplicity we consider any line starting with `#!` a shebang, // regardless of restrictions put on shebangs by specific platforms. if let Some(input_tail) = input.strip_prefix("#!") { - // Ok, this is a shebang but if the next non-whitespace token is `[` or maybe - // a doc comment (due to `TokenKind::(Line,Block)Comment` ambiguity at lexer level), + // Ok, this is a shebang but if the next non-whitespace token is `[`, // then it may be valid Rust code, so consider it Rust code. - let next_non_whitespace_token = tokenize(input_tail).map(|tok| tok.kind).find(|tok| - !matches!(tok, TokenKind::Whitespace | TokenKind::LineComment { .. } | TokenKind::BlockComment { .. }) - ); + let next_non_whitespace_token = tokenize(input_tail).map(|tok| tok.kind).find(|tok| { + !matches!( + tok, + TokenKind::Whitespace + | TokenKind::LineComment { doc_style: None } + | TokenKind::BlockComment { doc_style: None, .. } + ) + }); if next_non_whitespace_token != Some(TokenKind::OpenBracket) { // No other choice than to consider this a shebang. return Some(2 + input_tail.lines().next().unwrap_or_default().len()); diff --git a/src/test/ui/parser/shebang/shebang-doc-comment.rs b/src/test/ui/parser/shebang/shebang-doc-comment.rs index 7dbb9eebc75..72866753e0e 100644 --- a/src/test/ui/parser/shebang/shebang-doc-comment.rs +++ b/src/test/ui/parser/shebang/shebang-doc-comment.rs @@ -1,6 +1,3 @@ #!///bin/bash [allow(unused_variables)] -//~^^ ERROR expected `[`, found doc comment - -// Doc comment is misinterpreted as a whitespace (regular comment) during shebang detection. -// Even if it wasn't, it would still result in an error, just a different one. +//~^ ERROR expected item, found `[` diff --git a/src/test/ui/parser/shebang/shebang-doc-comment.stderr b/src/test/ui/parser/shebang/shebang-doc-comment.stderr index f524f556837..2227d45ec5a 100644 --- a/src/test/ui/parser/shebang/shebang-doc-comment.stderr +++ b/src/test/ui/parser/shebang/shebang-doc-comment.stderr @@ -1,8 +1,8 @@ -error: expected `[`, found doc comment `///bin/bash` - --> $DIR/shebang-doc-comment.rs:1:3 +error: expected item, found `[` + --> $DIR/shebang-doc-comment.rs:2:1 | -LL | #!///bin/bash - | ^^^^^^^^^^^ expected `[` +LL | [allow(unused_variables)] + | ^ expected item error: aborting due to previous error |
