diff options
| author | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2020-06-22 00:40:11 +0300 |
|---|---|---|
| committer | Mark Rousskov <mark.simulacrum@gmail.com> | 2020-07-10 10:52:10 -0400 |
| commit | 5ceb94699b6bf02b6eaff72a160db65636e52155 (patch) | |
| tree | edd45ca45737a8db7676a3bb828d34aa0cf3a787 /src | |
| parent | 13da8ab450635a66a451d74f7c3499216215b092 (diff) | |
| download | rust-5ceb94699b6bf02b6eaff72a160db65636e52155.tar.gz rust-5ceb94699b6bf02b6eaff72a160db65636e52155.zip | |
rustc_lexer: Simplify shebang parsing once more
Diffstat (limited to 'src')
| -rw-r--r-- | src/librustc_lexer/src/lib.rs | 27 | ||||
| -rw-r--r-- | src/test/ui/parser/shebang/shebang-empty.rs | 4 | ||||
| -rw-r--r-- | src/test/ui/parser/shebang/shebang-space.rs | 5 | ||||
| -rw-r--r-- | src/test/ui/shebang.rs | 5 |
4 files changed, 21 insertions, 20 deletions
diff --git a/src/librustc_lexer/src/lib.rs b/src/librustc_lexer/src/lib.rs index c2139d07f37..9256bd00865 100644 --- a/src/librustc_lexer/src/lib.rs +++ b/src/librustc_lexer/src/lib.rs @@ -239,21 +239,18 @@ pub enum Base { /// but shebang isn't a part of rust syntax. pub fn strip_shebang(input: &str) -> Option<usize> { // Shebang must start with `#!` literally, without any preceding whitespace. - if input.starts_with("#!") { - let input_tail = &input[2..]; - // Shebang must have something non-whitespace after `#!` on the first line. - let first_line_tail = input_tail.lines().next()?; - if first_line_tail.contains(|c| !is_whitespace(c)) { - // 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), - // then it may be valid Rust code, so consider it Rust code. - let next_non_whitespace_token = tokenize(input_tail).map(|tok| tok.kind).filter(|tok| - !matches!(tok, TokenKind::Whitespace | TokenKind::LineComment | TokenKind::BlockComment { .. }) - ).next(); - if next_non_whitespace_token != Some(TokenKind::OpenBracket) { - // No other choice than to consider this a shebang. - return Some(2 + first_line_tail.len()); - } + // 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), + // 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 { .. }) + ); + 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()); } } None diff --git a/src/test/ui/parser/shebang/shebang-empty.rs b/src/test/ui/parser/shebang/shebang-empty.rs new file mode 100644 index 00000000000..e38cc637e94 --- /dev/null +++ b/src/test/ui/parser/shebang/shebang-empty.rs @@ -0,0 +1,4 @@ +#! + +// check-pass +fn main() {} diff --git a/src/test/ui/parser/shebang/shebang-space.rs b/src/test/ui/parser/shebang/shebang-space.rs new file mode 100644 index 00000000000..0978b759d2a --- /dev/null +++ b/src/test/ui/parser/shebang/shebang-space.rs @@ -0,0 +1,5 @@ +#! + +// check-pass +// ignore-tidy-end-whitespace +fn main() {} diff --git a/src/test/ui/shebang.rs b/src/test/ui/shebang.rs deleted file mode 100644 index 3d3ba468be9..00000000000 --- a/src/test/ui/shebang.rs +++ /dev/null @@ -1,5 +0,0 @@ -#!/usr/bin/env rustx - -// run-pass - -pub fn main() { println!("Hello World"); } |
