Skip to content

Commit 07e946c

Browse files
committed
lowering: connect to parser via function pointer instead
1 parent 1899432 commit 07e946c

File tree

2 files changed

+12
-26
lines changed

2 files changed

+12
-26
lines changed

src/librustc/hir/lowering.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ pub struct LoweringContext<'a> {
8787

8888
resolver: &'a mut dyn Resolver,
8989

90-
parser: &'static dyn Parser,
90+
/// HACK(Centril): there is a cyclic dependency between the parser and lowering
91+
/// if we don't have this function pointer. To avoid that dependency so that
92+
/// librustc is independent of the parser, we use dynamic dispatch here.
93+
nt_to_tokenstream: NtToTokenstream,
9194

9295
/// The items being lowered are collected here.
9396
items: BTreeMap<hir::HirId, hir::Item>,
@@ -183,12 +186,7 @@ pub trait Resolver {
183186
fn has_derives(&self, node_id: NodeId, derives: SpecialDerives) -> bool;
184187
}
185188

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-
}
189+
type NtToTokenstream = fn(&Nonterminal, &ParseSess, Span) -> TokenStream;
192190

193191
/// Context of `impl Trait` in code, which determines whether it is allowed in an HIR subtree,
194192
/// and if so, what meaning it has.
@@ -246,7 +244,7 @@ pub fn lower_crate(
246244
dep_graph: &DepGraph,
247245
krate: &Crate,
248246
resolver: &mut dyn Resolver,
249-
parser: &'static dyn Parser,
247+
nt_to_tokenstream: NtToTokenstream,
250248
) -> hir::Crate {
251249
// We're constructing the HIR here; we don't care what we will
252250
// read, since we haven't even constructed the *input* to
@@ -260,7 +258,7 @@ pub fn lower_crate(
260258
sess,
261259
cstore,
262260
resolver,
263-
parser,
261+
nt_to_tokenstream,
264262
items: BTreeMap::new(),
265263
trait_items: BTreeMap::new(),
266264
impl_items: BTreeMap::new(),
@@ -1034,7 +1032,7 @@ impl<'a> LoweringContext<'a> {
10341032
fn lower_token(&mut self, token: Token) -> TokenStream {
10351033
match token.kind {
10361034
token::Interpolated(nt) => {
1037-
let tts = self.parser.nt_to_tokenstream(&nt, &self.sess.parse_sess, token.span);
1035+
let tts = (self.nt_to_tokenstream)(&nt, &self.sess.parse_sess, token.span);
10381036
self.lower_token_stream(tts)
10391037
}
10401038
_ => TokenTree::Token(token).into(),

src/librustc_interface/passes.rs

+4-16
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, Parser};
8+
use rustc::hir::lowering::lower_crate;
99
use rustc::hir::def_id::{CrateNum, LOCAL_CRATE};
1010
use rustc::lint;
1111
use rustc::middle::{self, reachable, resolve_lifetime, stability};
@@ -38,12 +38,9 @@ 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;
4441
use syntax::util::node_count::NodeCounter;
4542
use syntax::symbol::Symbol;
46-
use syntax_pos::{FileName, Span};
43+
use syntax_pos::FileName;
4744
use syntax_ext;
4845

4946
use rustc_serialize::json;
@@ -535,16 +532,6 @@ fn configure_and_expand_inner<'a>(
535532
Ok((krate, resolver))
536533
}
537534

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-
548535
pub fn lower_to_hir(
549536
sess: &Session,
550537
cstore: &CStore,
@@ -554,7 +541,8 @@ pub fn lower_to_hir(
554541
) -> Result<hir::map::Forest> {
555542
// Lower AST to HIR.
556543
let hir_forest = time(sess, "lowering AST -> HIR", || {
557-
let hir_crate = lower_crate(sess, cstore, &dep_graph, &krate, resolver, parser());
544+
let nt_to_tokenstream = syntax::parse::nt_to_tokenstream;
545+
let hir_crate = lower_crate(sess, cstore, &dep_graph, &krate, resolver, nt_to_tokenstream);
558546

559547
if sess.opts.debugging_opts.hir_stats {
560548
hir_stats::print_hir_stats(&hir_crate);

0 commit comments

Comments
 (0)