Skip to content

Commit 1899432

Browse files
committed
lowering: don't rely on parser directly.
1 parent 42f32f0 commit 1899432

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

src/librustc/hir/lowering.rs

+14-6
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ use syntax::print::pprust;
7070
use syntax::source_map::{respan, ExpnData, ExpnKind, DesugaringKind, Spanned};
7171
use syntax::symbol::{kw, sym, Symbol};
7272
use syntax::tokenstream::{TokenStream, TokenTree};
73-
use syntax::parse;
74-
use syntax::parse::token::{self, Token};
73+
use syntax::parse::token::{self, Nonterminal, Token};
74+
use syntax::parse::ParseSess;
7575
use syntax::visit::{self, Visitor};
7676
use syntax_pos::Span;
7777

@@ -87,6 +87,8 @@ pub struct LoweringContext<'a> {
8787

8888
resolver: &'a mut dyn Resolver,
8989

90+
parser: &'static dyn Parser,
91+
9092
/// The items being lowered are collected here.
9193
items: BTreeMap<hir::HirId, hir::Item>,
9294

@@ -181,6 +183,13 @@ pub trait Resolver {
181183
fn has_derives(&self, node_id: NodeId, derives: SpecialDerives) -> bool;
182184
}
183185

186+
/// HACK(Centril): there is a cyclic dependency between the parser and lowering
187+
/// if we don't have this trait. To avoid that dependency so that librustc is
188+
/// independent of the parser, we use type erasure here.
189+
pub trait Parser {
190+
fn nt_to_tokenstream(&self, nt: &Nonterminal, sess: &ParseSess, span: Span) -> TokenStream;
191+
}
192+
184193
/// Context of `impl Trait` in code, which determines whether it is allowed in an HIR subtree,
185194
/// and if so, what meaning it has.
186195
#[derive(Debug)]
@@ -237,6 +246,7 @@ pub fn lower_crate(
237246
dep_graph: &DepGraph,
238247
krate: &Crate,
239248
resolver: &mut dyn Resolver,
249+
parser: &'static dyn Parser,
240250
) -> hir::Crate {
241251
// We're constructing the HIR here; we don't care what we will
242252
// read, since we haven't even constructed the *input* to
@@ -250,6 +260,7 @@ pub fn lower_crate(
250260
sess,
251261
cstore,
252262
resolver,
263+
parser,
253264
items: BTreeMap::new(),
254265
trait_items: BTreeMap::new(),
255266
impl_items: BTreeMap::new(),
@@ -1023,10 +1034,7 @@ impl<'a> LoweringContext<'a> {
10231034
fn lower_token(&mut self, token: Token) -> TokenStream {
10241035
match token.kind {
10251036
token::Interpolated(nt) => {
1026-
// FIXME(Centril): Consider indirection `(parse_sess.nt_to_tokenstream)(...)`
1027-
// to hack around the current hack that requires `nt_to_tokenstream` to live
1028-
// in the parser.
1029-
let tts = parse::nt_to_tokenstream(&nt, &self.sess.parse_sess, token.span);
1037+
let tts = self.parser.nt_to_tokenstream(&nt, &self.sess.parse_sess, token.span);
10301038
self.lower_token_stream(tts)
10311039
}
10321040
_ => TokenTree::Token(token).into(),

src/librustc_interface/passes.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::proc_macro_decls;
55
use log::{info, warn, log_enabled};
66
use rustc::dep_graph::DepGraph;
77
use rustc::hir;
8-
use rustc::hir::lowering::lower_crate;
8+
use rustc::hir::lowering::{lower_crate, Parser};
99
use rustc::hir::def_id::{CrateNum, LOCAL_CRATE};
1010
use rustc::lint;
1111
use rustc::middle::{self, reachable, resolve_lifetime, stability};
@@ -38,9 +38,12 @@ use syntax::early_buffered_lints::BufferedEarlyLint;
3838
use syntax::ext::base::{NamedSyntaxExtension, ExtCtxt};
3939
use syntax::mut_visit::MutVisitor;
4040
use syntax::parse::{self, PResult};
41+
use syntax::parse::token::Nonterminal;
42+
use syntax::parse::ParseSess;
43+
use syntax::tokenstream::TokenStream;
4144
use syntax::util::node_count::NodeCounter;
4245
use syntax::symbol::Symbol;
43-
use syntax_pos::FileName;
46+
use syntax_pos::{FileName, Span};
4447
use syntax_ext;
4548

4649
use rustc_serialize::json;
@@ -532,6 +535,16 @@ fn configure_and_expand_inner<'a>(
532535
Ok((krate, resolver))
533536
}
534537

538+
fn parser() -> &'static dyn Parser {
539+
struct Parse;
540+
impl Parser for Parse {
541+
fn nt_to_tokenstream(&self, nt: &Nonterminal, sess: &ParseSess, span: Span) -> TokenStream {
542+
syntax::parse::nt_to_tokenstream(nt, sess, span)
543+
}
544+
}
545+
&Parse
546+
}
547+
535548
pub fn lower_to_hir(
536549
sess: &Session,
537550
cstore: &CStore,
@@ -541,7 +554,7 @@ pub fn lower_to_hir(
541554
) -> Result<hir::map::Forest> {
542555
// Lower AST to HIR.
543556
let hir_forest = time(sess, "lowering AST -> HIR", || {
544-
let hir_crate = lower_crate(sess, cstore, &dep_graph, &krate, resolver);
557+
let hir_crate = lower_crate(sess, cstore, &dep_graph, &krate, resolver, parser());
545558

546559
if sess.opts.debugging_opts.hir_stats {
547560
hir_stats::print_hir_stats(&hir_crate);

0 commit comments

Comments
 (0)