From 07e946caf7a4b9e5781b15f75b927b9e695b4f7c Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sun, 13 Oct 2019 23:27:18 +0200 Subject: [PATCH] lowering: connect to parser via function pointer instead --- src/librustc/hir/lowering.rs | 18 ++++++++---------- src/librustc_interface/passes.rs | 20 ++++---------------- 2 files changed, 12 insertions(+), 26 deletions(-) diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs index 062533f3d0972..747848edc7a8d 100644 --- a/src/librustc/hir/lowering.rs +++ b/src/librustc/hir/lowering.rs @@ -87,7 +87,10 @@ pub struct LoweringContext<'a> { resolver: &'a mut dyn Resolver, - parser: &'static dyn Parser, + /// HACK(Centril): there is a cyclic dependency between the parser and lowering + /// if we don't have this function pointer. To avoid that dependency so that + /// librustc is independent of the parser, we use dynamic dispatch here. + nt_to_tokenstream: NtToTokenstream, /// The items being lowered are collected here. items: BTreeMap, @@ -183,12 +186,7 @@ pub trait Resolver { fn has_derives(&self, node_id: NodeId, derives: SpecialDerives) -> bool; } -/// HACK(Centril): there is a cyclic dependency between the parser and lowering -/// if we don't have this trait. To avoid that dependency so that librustc is -/// independent of the parser, we use type erasure here. -pub trait Parser { - fn nt_to_tokenstream(&self, nt: &Nonterminal, sess: &ParseSess, span: Span) -> TokenStream; -} +type NtToTokenstream = fn(&Nonterminal, &ParseSess, Span) -> TokenStream; /// Context of `impl Trait` in code, which determines whether it is allowed in an HIR subtree, /// and if so, what meaning it has. @@ -246,7 +244,7 @@ pub fn lower_crate( dep_graph: &DepGraph, krate: &Crate, resolver: &mut dyn Resolver, - parser: &'static dyn Parser, + nt_to_tokenstream: NtToTokenstream, ) -> hir::Crate { // We're constructing the HIR here; we don't care what we will // read, since we haven't even constructed the *input* to @@ -260,7 +258,7 @@ pub fn lower_crate( sess, cstore, resolver, - parser, + nt_to_tokenstream, items: BTreeMap::new(), trait_items: BTreeMap::new(), impl_items: BTreeMap::new(), @@ -1034,7 +1032,7 @@ impl<'a> LoweringContext<'a> { fn lower_token(&mut self, token: Token) -> TokenStream { match token.kind { token::Interpolated(nt) => { - let tts = self.parser.nt_to_tokenstream(&nt, &self.sess.parse_sess, token.span); + let tts = (self.nt_to_tokenstream)(&nt, &self.sess.parse_sess, token.span); self.lower_token_stream(tts) } _ => TokenTree::Token(token).into(), diff --git a/src/librustc_interface/passes.rs b/src/librustc_interface/passes.rs index 5078379e92053..328798862b864 100644 --- a/src/librustc_interface/passes.rs +++ b/src/librustc_interface/passes.rs @@ -5,7 +5,7 @@ use crate::proc_macro_decls; use log::{info, warn, log_enabled}; use rustc::dep_graph::DepGraph; use rustc::hir; -use rustc::hir::lowering::{lower_crate, Parser}; +use rustc::hir::lowering::lower_crate; use rustc::hir::def_id::{CrateNum, LOCAL_CRATE}; use rustc::lint; use rustc::middle::{self, reachable, resolve_lifetime, stability}; @@ -38,12 +38,9 @@ use syntax::early_buffered_lints::BufferedEarlyLint; use syntax::ext::base::{NamedSyntaxExtension, ExtCtxt}; use syntax::mut_visit::MutVisitor; use syntax::parse::{self, PResult}; -use syntax::parse::token::Nonterminal; -use syntax::parse::ParseSess; -use syntax::tokenstream::TokenStream; use syntax::util::node_count::NodeCounter; use syntax::symbol::Symbol; -use syntax_pos::{FileName, Span}; +use syntax_pos::FileName; use syntax_ext; use rustc_serialize::json; @@ -535,16 +532,6 @@ fn configure_and_expand_inner<'a>( Ok((krate, resolver)) } -fn parser() -> &'static dyn Parser { - struct Parse; - impl Parser for Parse { - fn nt_to_tokenstream(&self, nt: &Nonterminal, sess: &ParseSess, span: Span) -> TokenStream { - syntax::parse::nt_to_tokenstream(nt, sess, span) - } - } - &Parse -} - pub fn lower_to_hir( sess: &Session, cstore: &CStore, @@ -554,7 +541,8 @@ pub fn lower_to_hir( ) -> Result { // Lower AST to HIR. let hir_forest = time(sess, "lowering AST -> HIR", || { - let hir_crate = lower_crate(sess, cstore, &dep_graph, &krate, resolver, parser()); + let nt_to_tokenstream = syntax::parse::nt_to_tokenstream; + let hir_crate = lower_crate(sess, cstore, &dep_graph, &krate, resolver, nt_to_tokenstream); if sess.opts.debugging_opts.hir_stats { hir_stats::print_hir_stats(&hir_crate);