Skip to content

Commit

Permalink
parse: nix new_sub_parser_from_file
Browse files Browse the repository at this point in the history
  • Loading branch information
Centril committed Mar 21, 2020
1 parent d18ed20 commit eaa0ae5
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/librustc_builtin_macros/source_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rustc_ast_pretty::pprust;
use rustc_expand::base::{self, *};
use rustc_expand::module::DirectoryOwnership;
use rustc_expand::panictry;
use rustc_parse::{self, new_sub_parser_from_file, parser::Parser};
use rustc_parse::{self, new_parser_from_file, parser::Parser};
use rustc_session::lint::builtin::INCOMPLETE_INCLUDE;
use rustc_span::symbol::Symbol;
use rustc_span::{self, Pos, Span};
Expand Down Expand Up @@ -110,7 +110,7 @@ pub fn expand_include<'cx>(
return DummyResult::any(sp);
}
};
let p = new_sub_parser_from_file(cx.parse_sess(), &file, sp);
let p = new_parser_from_file(cx.parse_sess(), &file, Some(sp));

// If in the included file we have e.g., `mod bar;`,
// then the path of `bar.rs` should be relative to the directory of `file`.
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_expand/module.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rustc_ast::ast::{self, Attribute, Ident, Mod};
use rustc_ast::{attr, token};
use rustc_errors::{struct_span_err, PResult};
use rustc_parse::new_sub_parser_from_file;
use rustc_parse::new_parser_from_file;
use rustc_session::parse::ParseSess;
use rustc_span::source_map::{FileName, Span};
use rustc_span::symbol::sym;
Expand Down Expand Up @@ -60,7 +60,7 @@ crate fn parse_external_mod(
drop(included_mod_stack);

// Actually parse the external file as a module.
let mut module = new_sub_parser_from_file(sess, &mp.path, span).parse_mod(&token::Eof)?;
let mut module = new_parser_from_file(sess, &mp.path, Some(span)).parse_mod(&token::Eof)?;
module.0.inline = false;
module
};
Expand Down
16 changes: 5 additions & 11 deletions src/librustc_parse/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ macro_rules! panictry_buffer {
}

pub fn parse_crate_from_file<'a>(input: &Path, sess: &'a ParseSess) -> PResult<'a, ast::Crate> {
let mut parser = new_parser_from_file(sess, input);
let mut parser = new_parser_from_file(sess, input, None);
parser.parse_crate_mod()
}

pub fn parse_crate_attrs_from_file<'a>(
input: &Path,
sess: &'a ParseSess,
) -> PResult<'a, Vec<ast::Attribute>> {
let mut parser = new_parser_from_file(sess, input);
let mut parser = new_parser_from_file(sess, input, None);
parser.parse_inner_attributes()
}

Expand Down Expand Up @@ -106,8 +106,9 @@ pub fn maybe_new_parser_from_source_str(
}

/// Creates a new parser, handling errors as appropriate if the file doesn't exist.
pub fn new_parser_from_file<'a>(sess: &'a ParseSess, path: &Path) -> Parser<'a> {
source_file_to_parser(sess, file_to_source_file(sess, path, None))
/// If a span is given, that is used on an error as the as the source of the problem.
pub fn new_parser_from_file<'a>(sess: &'a ParseSess, path: &Path, sp: Option<Span>) -> Parser<'a> {
source_file_to_parser(sess, file_to_source_file(sess, path, sp))
}

/// Creates a new parser, returning buffered diagnostics if the file doesn't exist,
Expand All @@ -120,13 +121,6 @@ pub fn maybe_new_parser_from_file<'a>(
maybe_source_file_to_parser(sess, file)
}

/// Given a session, a path, and a span,
/// add the file at the given path to the `source_map`, and returns a parser.
/// On an error, uses the given span as the source of the problem.
pub fn new_sub_parser_from_file<'a>(sess: &'a ParseSess, path: &Path, sp: Span) -> Parser<'a> {
source_file_to_parser(sess, file_to_source_file(sess, path, Some(sp)))
}

/// Given a `source_file` and config, returns a parser.
fn source_file_to_parser(sess: &ParseSess, source_file: Lrc<SourceFile>) -> Parser<'_> {
panictry_buffer!(&sess.span_diagnostic, maybe_source_file_to_parser(sess, source_file))
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui-fulldeps/mod_dir_path_canonicalized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ fn parse() {

let path = Path::new(file!());
let path = path.canonicalize().unwrap();
let mut parser = new_parser_from_file(&parse_session, &path);
let mut parser = new_parser_from_file(&parse_session, &path, None);
let _ = parser.parse_crate_mod();
}

0 comments on commit eaa0ae5

Please sign in to comment.