Skip to content

Commit f284eb3

Browse files
committed
decouple eval_src_mod from Parser
1 parent 81b3500 commit f284eb3

File tree

1 file changed

+29
-40
lines changed

1 file changed

+29
-40
lines changed

src/librustc_parse/parser/module.rs

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl<'a> Parser<'a> {
5252
self.directory.ownership,
5353
&self.directory.path,
5454
)?;
55-
self.eval_src_mod(path, directory_ownership, id.to_string(), id.span)?
55+
eval_src_mod(self.sess, self.cfg_mods, path, directory_ownership, id)?
5656
} else {
5757
(ast::Mod { inner: DUMMY_SP, items: Vec::new(), inline: false }, Vec::new())
5858
}
@@ -100,48 +100,37 @@ impl<'a> Parser<'a> {
100100

101101
Ok(Mod { inner: inner_lo.to(hi), items, inline: true })
102102
}
103+
}
103104

104-
/// Reads a module from a source file.
105-
fn eval_src_mod(
106-
&mut self,
107-
path: PathBuf,
108-
directory_ownership: DirectoryOwnership,
109-
name: String,
110-
id_sp: Span,
111-
) -> PResult<'a, (Mod, Vec<Attribute>)> {
112-
let mut included_mod_stack = self.sess.included_mod_stack.borrow_mut();
113-
self.error_on_circular_module(id_sp, &path, &included_mod_stack)?;
114-
included_mod_stack.push(path.clone());
115-
drop(included_mod_stack);
116-
117-
let mut p0 =
118-
new_sub_parser_from_file(self.sess, &path, directory_ownership, Some(name), id_sp);
119-
p0.cfg_mods = self.cfg_mods;
120-
let mut module = p0.parse_mod(&token::Eof)?;
121-
module.0.inline = false;
122-
123-
self.sess.included_mod_stack.borrow_mut().pop();
124-
Ok(module)
125-
}
126-
127-
fn error_on_circular_module(
128-
&self,
129-
span: Span,
130-
path: &Path,
131-
included_mod_stack: &[PathBuf],
132-
) -> PResult<'a, ()> {
133-
if let Some(i) = included_mod_stack.iter().position(|p| *p == path) {
134-
let mut err = String::from("circular modules: ");
135-
let len = included_mod_stack.len();
136-
for p in &included_mod_stack[i..len] {
137-
err.push_str(&p.to_string_lossy());
138-
err.push_str(" -> ");
139-
}
140-
err.push_str(&path.to_string_lossy());
141-
return Err(self.struct_span_err(span, &err[..]));
105+
/// Reads a module from a source file.
106+
fn eval_src_mod<'a>(
107+
sess: &'a ParseSess,
108+
cfg_mods: bool,
109+
path: PathBuf,
110+
dir_ownership: DirectoryOwnership,
111+
id: ast::Ident,
112+
) -> PResult<'a, (Mod, Vec<Attribute>)> {
113+
let mut included_mod_stack = sess.included_mod_stack.borrow_mut();
114+
if let Some(i) = included_mod_stack.iter().position(|p| *p == path) {
115+
let mut err = String::from("circular modules: ");
116+
for p in &included_mod_stack[i..] {
117+
err.push_str(&p.to_string_lossy());
118+
err.push_str(" -> ");
142119
}
143-
Ok(())
120+
err.push_str(&path.to_string_lossy());
121+
return Err(sess.span_diagnostic.struct_span_err(id.span, &err[..]));
144122
}
123+
included_mod_stack.push(path.clone());
124+
drop(included_mod_stack);
125+
126+
let mut p0 =
127+
new_sub_parser_from_file(sess, &path, dir_ownership, Some(id.to_string()), id.span);
128+
p0.cfg_mods = cfg_mods;
129+
let mut module = p0.parse_mod(&token::Eof)?;
130+
module.0.inline = false;
131+
132+
sess.included_mod_stack.borrow_mut().pop();
133+
Ok(module)
145134
}
146135

147136
fn push_directory(

0 commit comments

Comments
 (0)