Skip to content

Commit 5ceb946

Browse files
petrochenkovMark-Simulacrum
authored andcommitted
rustc_lexer: Simplify shebang parsing once more
1 parent 13da8ab commit 5ceb946

File tree

4 files changed

+21
-20
lines changed

4 files changed

+21
-20
lines changed

src/librustc_lexer/src/lib.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -239,21 +239,18 @@ pub enum Base {
239239
/// but shebang isn't a part of rust syntax.
240240
pub fn strip_shebang(input: &str) -> Option<usize> {
241241
// Shebang must start with `#!` literally, without any preceding whitespace.
242-
if input.starts_with("#!") {
243-
let input_tail = &input[2..];
244-
// Shebang must have something non-whitespace after `#!` on the first line.
245-
let first_line_tail = input_tail.lines().next()?;
246-
if first_line_tail.contains(|c| !is_whitespace(c)) {
247-
// Ok, this is a shebang but if the next non-whitespace token is `[` or maybe
248-
// a doc comment (due to `TokenKind::(Line,Block)Comment` ambiguity at lexer level),
249-
// then it may be valid Rust code, so consider it Rust code.
250-
let next_non_whitespace_token = tokenize(input_tail).map(|tok| tok.kind).filter(|tok|
251-
!matches!(tok, TokenKind::Whitespace | TokenKind::LineComment | TokenKind::BlockComment { .. })
252-
).next();
253-
if next_non_whitespace_token != Some(TokenKind::OpenBracket) {
254-
// No other choice than to consider this a shebang.
255-
return Some(2 + first_line_tail.len());
256-
}
242+
// For simplicity we consider any line starting with `#!` a shebang,
243+
// regardless of restrictions put on shebangs by specific platforms.
244+
if let Some(input_tail) = input.strip_prefix("#!") {
245+
// Ok, this is a shebang but if the next non-whitespace token is `[` or maybe
246+
// a doc comment (due to `TokenKind::(Line,Block)Comment` ambiguity at lexer level),
247+
// then it may be valid Rust code, so consider it Rust code.
248+
let next_non_whitespace_token = tokenize(input_tail).map(|tok| tok.kind).find(|tok|
249+
!matches!(tok, TokenKind::Whitespace | TokenKind::LineComment | TokenKind::BlockComment { .. })
250+
);
251+
if next_non_whitespace_token != Some(TokenKind::OpenBracket) {
252+
// No other choice than to consider this a shebang.
253+
return Some(2 + input_tail.lines().next().unwrap_or_default().len());
257254
}
258255
}
259256
None
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!
2+
3+
// check-pass
4+
fn main() {}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!
2+
3+
// check-pass
4+
// ignore-tidy-end-whitespace
5+
fn main() {}

src/test/ui/shebang.rs

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)