Skip to content

Commit 9640d1c

Browse files
committed
Move #! checking.
Currently does the "is this a `#!` at the start of the file?" check for every single token(!) This commit moves it so it only happens once.
1 parent 14281e6 commit 9640d1c

File tree

1 file changed

+8
-9
lines changed
  • compiler/rustc_parse/src/lexer

1 file changed

+8
-9
lines changed

compiler/rustc_parse/src/lexer/mod.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,16 @@ pub struct UnmatchedBrace {
3838

3939
pub(crate) fn parse_token_trees<'a>(
4040
sess: &'a ParseSess,
41-
src: &'a str,
42-
start_pos: BytePos,
41+
mut src: &'a str,
42+
mut start_pos: BytePos,
4343
override_span: Option<Span>,
4444
) -> (PResult<'a, TokenStream>, Vec<UnmatchedBrace>) {
45+
// Skip `#!`, if present.
46+
if let Some(shebang_len) = rustc_lexer::strip_shebang(src) {
47+
src = &src[shebang_len..];
48+
start_pos = start_pos + BytePos::from_usize(shebang_len);
49+
}
50+
4551
StringReader { sess, start_pos, pos: start_pos, src, override_span }.into_token_trees()
4652
}
4753

@@ -65,13 +71,6 @@ impl<'a> StringReader<'a> {
6571
fn next_token(&mut self) -> (Spacing, Token) {
6672
let mut spacing = Spacing::Joint;
6773

68-
// Skip `#!` at the start of the file
69-
if self.pos == self.start_pos
70-
&& let Some(shebang_len) = rustc_lexer::strip_shebang(self.src)
71-
{
72-
self.pos = self.pos + BytePos::from_usize(shebang_len);
73-
}
74-
7574
// Skip trivial (whitespace & comments) tokens
7675
loop {
7776
let start_src_index = self.src_index(self.pos);

0 commit comments

Comments
 (0)