Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup shebang handling in the lexer #59687

Merged
merged 1 commit into from
Apr 5, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions src/libsyntax/parse/lexer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crate::ast::{self, Ident};
use crate::source_map::{SourceMap, FilePathMapping};
use crate::parse::{token, ParseSess};
use crate::symbol::Symbol;

use errors::{Applicability, FatalError, Diagnostic, DiagnosticBuilder};
use syntax_pos::{BytePos, CharPos, Pos, Span, NO_EXPANSION};
use syntax_pos::{BytePos, Pos, Span, NO_EXPANSION};
use core::unicode::property::Pattern_White_Space;

use std::borrow::Cow;
Expand Down Expand Up @@ -667,14 +666,9 @@ impl<'a> StringReader<'a> {
return None;
}

// I guess this is the only way to figure out if
// we're at the beginning of the file...
let smap = SourceMap::new(FilePathMapping::empty());
smap.files.borrow_mut().source_files.push(self.source_file.clone());
let loc = smap.lookup_char_pos_adj(self.pos);
debug!("Skipping a shebang");
if loc.line == 1 && loc.col == CharPos(0) {
// FIXME: Add shebang "token", return it
let is_beginning_of_file = self.pos == self.source_file.start_pos;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, the new code accepts shebang at the start of anything that compiler treats as a source file - out-of-line mod, perhaps command line or macro?
And I'm not even sure what the old code does.
I rather wish this thing never existed in the language.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My reading is that the old code did the same, but in a more elaborate way, by binary searching in a single-element slice. I’ve manually checked that shebangs are accepted in the outofline modules today.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the referrnce, here’s the original commit that added the code: #2430

Accidentally, it also added this _adj function, which I would like to get rid of next

if is_beginning_of_file {
debug!("Skipping a shebang");
let start = self.pos;
while !self.ch_is('\n') && !self.is_eof() {
self.bump();
Expand Down Expand Up @@ -1911,7 +1905,7 @@ mod tests {

use crate::ast::{Ident, CrateConfig};
use crate::symbol::Symbol;
use crate::source_map::SourceMap;
use crate::source_map::{SourceMap, FilePathMapping};
use crate::feature_gate::UnstableFeatures;
use crate::parse::token;
use crate::diagnostics::plugin::ErrorMap;
Expand Down