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.
This commit is contained in:
commit
56b5de2f29
3 changed files with 14 additions and 13 deletions
|
@ -191,12 +191,16 @@ pub fn strip_shebang(input: &str) -> Option<usize> {
|
||||||
// For simplicity we consider any line starting with `#!` a shebang,
|
// For simplicity we consider any line starting with `#!` a shebang,
|
||||||
// regardless of restrictions put on shebangs by specific platforms.
|
// regardless of restrictions put on shebangs by specific platforms.
|
||||||
if let Some(input_tail) = input.strip_prefix("#!") {
|
if let Some(input_tail) = input.strip_prefix("#!") {
|
||||||
// Ok, this is a shebang but if the next non-whitespace token is `[` or maybe
|
// Ok, this is a shebang but if the next non-whitespace token is `[`,
|
||||||
// a doc comment (due to `TokenKind::(Line,Block)Comment` ambiguity at lexer level),
|
|
||||||
// then it may be valid Rust code, so consider it Rust code.
|
// 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|
|
let next_non_whitespace_token = tokenize(input_tail).map(|tok| tok.kind).find(|tok| {
|
||||||
!matches!(tok, TokenKind::Whitespace | TokenKind::LineComment { .. } | TokenKind::BlockComment { .. })
|
!matches!(
|
||||||
);
|
tok,
|
||||||
|
TokenKind::Whitespace
|
||||||
|
| TokenKind::LineComment { doc_style: None }
|
||||||
|
| TokenKind::BlockComment { doc_style: None, .. }
|
||||||
|
)
|
||||||
|
});
|
||||||
if next_non_whitespace_token != Some(TokenKind::OpenBracket) {
|
if next_non_whitespace_token != Some(TokenKind::OpenBracket) {
|
||||||
// No other choice than to consider this a shebang.
|
// No other choice than to consider this a shebang.
|
||||||
return Some(2 + input_tail.lines().next().unwrap_or_default().len());
|
return Some(2 + input_tail.lines().next().unwrap_or_default().len());
|
||||||
|
|
|
@ -1,6 +1,3 @@
|
||||||
#!///bin/bash
|
#!///bin/bash
|
||||||
[allow(unused_variables)]
|
[allow(unused_variables)]
|
||||||
//~^^ ERROR expected `[`, found doc comment
|
//~^ ERROR expected item, found `[`
|
||||||
|
|
||||||
// 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.
|
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
error: expected `[`, found doc comment `///bin/bash`
|
error: expected item, found `[`
|
||||||
--> $DIR/shebang-doc-comment.rs:1:3
|
--> $DIR/shebang-doc-comment.rs:2:1
|
||||||
|
|
|
|
||||||
LL | #!///bin/bash
|
LL | [allow(unused_variables)]
|
||||||
| ^^^^^^^^^^^ expected `[`
|
| ^ expected item
|
||||||
|
|
||||||
error: aborting due to previous error
|
error: aborting due to previous error
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue