Skip to content

Commit eaac45a

Browse files
committed
Auto merge of #66565 - Mark-Simulacrum:syntax-cfg-mod, r=petrochenkov
Move process_configure_mod to rustc_parse This removes the hack in favor of perhaps a less principled, but less painful, approach. This also supports my work to decouple `Session` from librustc, as `ParseSess` currently has `Attribute` as "part" of it but after this PR will no longer do so.
2 parents 564f2d3 + 70805e6 commit eaac45a

File tree

19 files changed

+34
-58
lines changed

19 files changed

+34
-58
lines changed

src/librustc/session/mod.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use syntax::edition::Edition;
2424
use syntax::feature_gate;
2525
use errors::json::JsonEmitter;
2626
use syntax::source_map;
27-
use syntax::sess::{ParseSess, ProcessCfgMod};
27+
use syntax::sess::ParseSess;
2828
use syntax_pos::{MultiSpan, Span};
2929

3030
use rustc_target::spec::{PanicStrategy, RelroLevel, Target, TargetTriple};
@@ -925,7 +925,6 @@ pub fn build_session(
925925
sopts: config::Options,
926926
local_crate_source_file: Option<PathBuf>,
927927
registry: errors::registry::Registry,
928-
process_cfg_mod: ProcessCfgMod,
929928
) -> Session {
930929
let file_path_mapping = sopts.file_path_mapping();
931930

@@ -936,7 +935,6 @@ pub fn build_session(
936935
Lrc::new(source_map::SourceMap::new(file_path_mapping)),
937936
DiagnosticOutput::Default,
938937
Default::default(),
939-
process_cfg_mod,
940938
)
941939
}
942940

@@ -1015,7 +1013,6 @@ pub fn build_session_with_source_map(
10151013
source_map: Lrc<source_map::SourceMap>,
10161014
diagnostics_output: DiagnosticOutput,
10171015
lint_caps: FxHashMap<lint::LintId, lint::Level>,
1018-
process_cfg_mod: ProcessCfgMod,
10191016
) -> Session {
10201017
// FIXME: This is not general enough to make the warning lint completely override
10211018
// normal diagnostic warnings, since the warning lint can also be denied and changed
@@ -1061,7 +1058,6 @@ pub fn build_session_with_source_map(
10611058
diagnostic_handler,
10621059
source_map,
10631060
lint_caps,
1064-
process_cfg_mod,
10651061
)
10661062
}
10671063

@@ -1071,7 +1067,6 @@ fn build_session_(
10711067
span_diagnostic: errors::Handler,
10721068
source_map: Lrc<source_map::SourceMap>,
10731069
driver_lint_caps: FxHashMap<lint::LintId, lint::Level>,
1074-
process_cfg_mod: ProcessCfgMod,
10751070
) -> Session {
10761071
let self_profiler =
10771072
if let SwitchWithOptPath::Enabled(ref d) = sopts.debugging_opts.self_profile {
@@ -1109,7 +1104,6 @@ fn build_session_(
11091104
let parse_sess = ParseSess::with_span_handler(
11101105
span_diagnostic,
11111106
source_map,
1112-
process_cfg_mod,
11131107
);
11141108
let sysroot = match &sopts.maybe_sysroot {
11151109
Some(sysroot) => sysroot.clone(),

src/librustc_interface/interface.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use syntax::ast::{self, MetaItemKind};
2121
use syntax::token;
2222
use syntax::source_map::{FileName, FileLoader, SourceMap};
2323
use syntax::sess::ParseSess;
24-
use syntax_expand::config::process_configure_mod;
2524
use syntax_pos::edition;
2625

2726
pub type Result<T> = result::Result<T, ErrorReported>;
@@ -69,7 +68,7 @@ impl Compiler {
6968
pub fn parse_cfgspecs(cfgspecs: Vec<String>) -> FxHashSet<(String, Option<String>)> {
7069
syntax::with_default_globals(move || {
7170
let cfg = cfgspecs.into_iter().map(|s| {
72-
let sess = ParseSess::with_silent_emitter(process_configure_mod);
71+
let sess = ParseSess::with_silent_emitter();
7372
let filename = FileName::cfg_spec_source_code(&s);
7473
let mut parser = new_parser_from_source_str(&sess, filename, s.to_string());
7574

src/librustc_interface/tests.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use rustc_target::spec::{MergeFunctions, PanicStrategy, RelroLevel};
1717
use syntax::symbol::sym;
1818
use syntax::edition::{Edition, DEFAULT_EDITION};
1919
use syntax;
20-
use syntax_expand::config::process_configure_mod;
2120
use rustc_data_structures::fx::FxHashSet;
2221
use rustc_errors::{ColorConfig, emitter::HumanReadableErrorType, registry};
2322

@@ -32,7 +31,7 @@ fn build_session_options_and_crate_config(matches: getopts::Matches) -> (Options
3231
fn mk_session(matches: getopts::Matches) -> (Session, CfgSpecs) {
3332
let registry = registry::Registry::new(&[]);
3433
let (sessopts, cfg) = build_session_options_and_crate_config(matches);
35-
let sess = build_session(sessopts, None, registry, process_configure_mod);
34+
let sess = build_session(sessopts, None, registry);
3635
(sess, cfg)
3736
}
3837

src/librustc_interface/util.rs

-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ use syntax::util::lev_distance::find_best_match_for_name;
3131
use syntax::source_map::{FileLoader, RealFileLoader, SourceMap};
3232
use syntax::symbol::{Symbol, sym};
3333
use syntax::{self, ast, attr};
34-
use syntax_expand::config::process_configure_mod;
3534
use syntax_pos::edition::Edition;
3635
#[cfg(not(parallel_compiler))]
3736
use std::{thread, panic};
@@ -81,7 +80,6 @@ pub fn create_session(
8180
source_map.clone(),
8281
diagnostic_output,
8382
lint_caps,
84-
process_configure_mod,
8583
);
8684

8785
sess.prof.register_queries(|profiler| {

src/libsyntax_expand/config.rs renamed to src/librustc_parse/config.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
use rustc_parse::validate_attr;
1+
//! Process the potential `cfg` attributes on a module.
2+
//! Also determine if the module should be included in this configuration.
3+
//!
4+
//! This module properly belongs in syntax_expand, but for now it's tied into
5+
//! parsing, so we leave it here to avoid complicated out-of-line dependencies.
6+
//!
7+
//! A principled solution to this wrong location would be to implement [#64197].
8+
//!
9+
//! [#64197]: https://github.com/rust-lang/rust/issues/64197
10+
11+
use crate::validate_attr;
212
use syntax::attr::HasAttrs;
313
use syntax::feature_gate::{
414
feature_err,
@@ -113,7 +123,7 @@ impl<'a> StripUnconfigured<'a> {
113123
return vec![];
114124
}
115125

116-
let res = rustc_parse::parse_in_attr(self.sess, &attr, |p| p.parse_cfg_attr());
126+
let res = crate::parse_in_attr(self.sess, &attr, |p| p.parse_cfg_attr());
117127
let (cfg_predicate, expanded_attrs) = match res {
118128
Ok(result) => result,
119129
Err(mut e) => {

src/librustc_parse/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ pub mod parser;
2525
use parser::{Parser, emit_unclosed_delims, make_unclosed_delims_error};
2626
pub mod lexer;
2727
pub mod validate_attr;
28+
#[macro_use]
29+
pub mod config;
2830

2931
#[derive(Clone)]
3032
pub struct Directory<'a> {

src/librustc_parse/parser/module.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ impl<'a> Parser<'a> {
4040

4141
/// Parses a `mod <foo> { ... }` or `mod <foo>;` item.
4242
pub(super) fn parse_item_mod(&mut self, outer_attrs: &[Attribute]) -> PResult<'a, ItemInfo> {
43-
// HACK(Centril): See documentation on `ParseSess::process_cfg_mod`.
44-
let (in_cfg, outer_attrs) = (self.sess.process_cfg_mod)(
43+
let (in_cfg, outer_attrs) = crate::config::process_configure_mod(
4544
self.sess,
4645
self.cfg_mods,
4746
outer_attrs,

src/librustdoc/html/highlight.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use syntax::token::{self, Token};
1616
use syntax::sess::ParseSess;
1717
use syntax::source_map::SourceMap;
1818
use syntax::symbol::{kw, sym};
19-
use syntax_expand::config::process_configure_mod;
2019
use syntax_pos::{Span, FileName};
2120

2221
/// Highlights `src`, returning the HTML output.
@@ -34,7 +33,7 @@ pub fn render_with_highlighting(
3433
class, tooltip).unwrap();
3534
}
3635

37-
let sess = ParseSess::with_silent_emitter(process_configure_mod);
36+
let sess = ParseSess::with_silent_emitter();
3837
let fm = sess.source_map().new_source_file(
3938
FileName::Custom(String::from("rustdoc-highlighting")),
4039
src.to_owned(),

src/librustdoc/passes/check_code_block_syntax.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use rustc_parse::lexer::{StringReader as Lexer};
33
use syntax::token;
44
use syntax::sess::ParseSess;
55
use syntax::source_map::FilePathMapping;
6-
use syntax_expand::config::process_configure_mod;
76
use syntax_pos::{InnerSpan, FileName};
87

98
use crate::clean;
@@ -28,7 +27,7 @@ struct SyntaxChecker<'a, 'tcx> {
2827

2928
impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
3029
fn check_rust_syntax(&self, item: &clean::Item, dox: &str, code_block: RustCodeBlock) {
31-
let sess = ParseSess::new(FilePathMapping::empty(), process_configure_mod);
30+
let sess = ParseSess::new(FilePathMapping::empty());
3231
let source_file = sess.source_map().new_source_file(
3332
FileName::Custom(String::from("doctest")),
3433
dox[code_block.code].to_owned(),

src/librustdoc/test.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use std::path::PathBuf;
1717
use std::process::{self, Command, Stdio};
1818
use std::str;
1919
use syntax::symbol::sym;
20-
use syntax_expand::config::process_configure_mod;
2120
use syntax_pos::{BytePos, DUMMY_SP, Pos, Span, FileName};
2221
use tempfile::Builder as TempFileBuilder;
2322
use testing;
@@ -415,7 +414,7 @@ pub fn make_test(s: &str,
415414
let emitter = EmitterWriter::new(box io::sink(), None, false, false, false, None, false);
416415
// FIXME(misdreavus): pass `-Z treat-err-as-bug` to the doctest parser
417416
let handler = Handler::with_emitter(false, None, box emitter);
418-
let sess = ParseSess::with_span_handler(handler, cm, process_configure_mod);
417+
let sess = ParseSess::with_span_handler(handler, cm);
419418

420419
let mut found_main = false;
421420
let mut found_extern_crate = cratename.is_none();

src/libsyntax/sess.rs

+5-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Contains `ParseSess` which holds state living beyond what one `Parser` might.
22
//! It also serves as an input to the parser itself.
33
4-
use crate::ast::{CrateConfig, NodeId, Attribute};
4+
use crate::ast::{CrateConfig, NodeId};
55
use crate::early_buffered_lints::{BufferedEarlyLint, BufferedEarlyLintId};
66
use crate::source_map::{SourceMap, FilePathMapping};
77
use crate::feature_gate::UnstableFeatures;
@@ -89,40 +89,26 @@ pub struct ParseSess {
8989
pub gated_spans: GatedSpans,
9090
/// The parser has reached `Eof` due to an unclosed brace. Used to silence unnecessary errors.
9191
pub reached_eof: Lock<bool>,
92-
/// Process the potential `cfg` attributes on a module.
93-
/// Also determine if the module should be included in this configuration.
94-
///
95-
/// HACK(Centril): This is used to break a cyclic dependency between
96-
/// the parser and cfg-stripping as defined in `syntax_expand::config`.
97-
/// The dependency edge from the parser comes from `parse_item_mod`.
98-
/// A principled solution to this hack would be to implement [#64197].
99-
///
100-
/// [#64197]: https://github.com/rust-lang/rust/issues/64197
101-
pub process_cfg_mod: ProcessCfgMod,
10292
}
10393

104-
pub type ProcessCfgMod = fn(&ParseSess, bool, &[Attribute]) -> (bool, Vec<Attribute>);
105-
10694
impl ParseSess {
107-
pub fn new(file_path_mapping: FilePathMapping, process_cfg_mod: ProcessCfgMod) -> Self {
95+
pub fn new(file_path_mapping: FilePathMapping) -> Self {
10896
let cm = Lrc::new(SourceMap::new(file_path_mapping));
10997
let handler = Handler::with_tty_emitter(
11098
ColorConfig::Auto,
11199
true,
112100
None,
113101
Some(cm.clone()),
114102
);
115-
ParseSess::with_span_handler(handler, cm, process_cfg_mod)
103+
ParseSess::with_span_handler(handler, cm)
116104
}
117105

118106
pub fn with_span_handler(
119107
handler: Handler,
120108
source_map: Lrc<SourceMap>,
121-
process_cfg_mod: ProcessCfgMod,
122109
) -> Self {
123110
Self {
124111
span_diagnostic: handler,
125-
process_cfg_mod,
126112
unstable_features: UnstableFeatures::from_environment(),
127113
config: FxHashSet::default(),
128114
edition: ExpnId::root().expn_data().edition,
@@ -138,10 +124,10 @@ impl ParseSess {
138124
}
139125
}
140126

141-
pub fn with_silent_emitter(process_cfg_mod: ProcessCfgMod) -> Self {
127+
pub fn with_silent_emitter() -> Self {
142128
let cm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
143129
let handler = Handler::with_emitter(false, None, Box::new(SilentEmitter));
144-
ParseSess::with_span_handler(handler, cm, process_cfg_mod)
130+
ParseSess::with_span_handler(handler, cm)
145131
}
146132

147133
#[inline]

src/libsyntax_expand/expand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::hygiene::{ExpnId, SyntaxContext, ExpnData, ExpnKind};
44
use crate::mbe::macro_rules::annotate_err_with_kind;
55
use crate::placeholders::{placeholder, PlaceholderExpander};
66
use crate::config::StripUnconfigured;
7-
use crate::configure;
7+
use rustc_parse::configure;
88

99
use rustc_parse::DirectoryOwnership;
1010
use rustc_parse::parser::Parser;

src/libsyntax_expand/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub use mbe::macro_rules::compile_declarative_macro;
3333
pub mod base;
3434
pub mod build;
3535
pub mod expand;
36-
#[macro_use] pub mod config;
36+
pub use rustc_parse::config;
3737
pub mod proc_macro;
3838

3939
crate mod mbe;

src/libsyntax_expand/parse/lexer/tests.rs

-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use crate::config::process_configure_mod;
2-
31
use rustc_data_structures::sync::Lrc;
42
use rustc_parse::lexer::StringReader;
53
use syntax::token::{self, Token, TokenKind};
@@ -27,7 +25,6 @@ fn mk_sess(sm: Lrc<SourceMap>) -> ParseSess {
2725
ParseSess::with_span_handler(
2826
Handler::with_emitter(true, None, Box::new(emitter)),
2927
sm,
30-
process_configure_mod,
3128
)
3229
}
3330

src/libsyntax_expand/parse/tests.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::config::process_configure_mod;
21
use crate::tests::{matches_codepattern, string_to_stream, with_error_checking_parse};
32

43
use rustc_parse::new_parser_from_source_str;
@@ -19,7 +18,7 @@ use errors::PResult;
1918
use std::path::PathBuf;
2019

2120
fn sess() -> ParseSess {
22-
ParseSess::new(FilePathMapping::empty(), process_configure_mod)
21+
ParseSess::new(FilePathMapping::empty())
2322
}
2423

2524
/// Parses an item.

src/libsyntax_expand/tests.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::config::process_configure_mod;
21
use rustc_parse::{source_file_to_stream, new_parser_from_source_str, parser::Parser};
32
use syntax::ast;
43
use syntax::tokenstream::TokenStream;
@@ -34,7 +33,7 @@ crate fn with_error_checking_parse<'a, T, F>(s: String, ps: &'a ParseSess, f: F)
3433

3534
/// Maps a string to tts, using a made-up filename.
3635
crate fn string_to_stream(source_str: String) -> TokenStream {
37-
let ps = ParseSess::new(FilePathMapping::empty(), process_configure_mod);
36+
let ps = ParseSess::new(FilePathMapping::empty());
3837
source_file_to_stream(
3938
&ps,
4039
ps.source_map().new_source_file(PathBuf::from("bogofile").into(),
@@ -44,7 +43,7 @@ crate fn string_to_stream(source_str: String) -> TokenStream {
4443

4544
/// Parses a string, returns a crate.
4645
crate fn string_to_crate(source_str : String) -> ast::Crate {
47-
let ps = ParseSess::new(FilePathMapping::empty(), process_configure_mod);
46+
let ps = ParseSess::new(FilePathMapping::empty());
4847
with_error_checking_parse(source_str, &ps, |p| {
4948
p.parse_crate_mod()
5049
})

src/test/ui-fulldeps/ast_stmt_expr_attr.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use syntax::source_map::{FilePathMapping, FileName};
2222
use syntax::ptr::P;
2323
use syntax::print::pprust;
2424
use syntax::token;
25-
use syntax_expand::config::process_configure_mod;
2625
use std::fmt;
2726

2827
// Copied out of syntax::util::parser_testing
@@ -75,7 +74,7 @@ fn str_compare<T, F: Fn(&T) -> String>(e: &str, expected: &[T], actual: &[T], f:
7574
}
7675

7776
fn sess() -> ParseSess {
78-
ParseSess::new(FilePathMapping::empty(), process_configure_mod)
77+
ParseSess::new(FilePathMapping::empty())
7978
}
8079

8180
fn check_expr_attrs(es: &str, expected: &[&str]) {

src/test/ui-fulldeps/mod_dir_path_canonicalized.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use rustc_parse::new_parser_from_file;
1212
use std::path::Path;
1313
use syntax::sess::ParseSess;
1414
use syntax::source_map::FilePathMapping;
15-
use syntax_expand::config::process_configure_mod;
1615

1716
#[path = "mod_dir_simple/test.rs"]
1817
mod gravy;
@@ -24,7 +23,7 @@ pub fn main() {
2423
}
2524

2625
fn parse() {
27-
let parse_session = ParseSess::new(FilePathMapping::empty(), process_configure_mod);
26+
let parse_session = ParseSess::new(FilePathMapping::empty());
2827

2928
let path = Path::new(file!());
3029
let path = path.canonicalize().unwrap();

src/test/ui-fulldeps/pprust-expr-roundtrip.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ use syntax::source_map::FilePathMapping;
3333
use syntax::mut_visit::{self, MutVisitor, visit_clobber};
3434
use syntax::print::pprust;
3535
use syntax::ptr::P;
36-
use syntax_expand::config::process_configure_mod;
3736

3837
fn parse_expr(ps: &ParseSess, src: &str) -> Option<P<Expr>> {
3938
let src_as_string = src.to_string();
@@ -205,7 +204,7 @@ fn main() {
205204
}
206205

207206
fn run() {
208-
let ps = ParseSess::new(FilePathMapping::empty(), process_configure_mod);
207+
let ps = ParseSess::new(FilePathMapping::empty());
209208

210209
iter_exprs(2, &mut |mut e| {
211210
// If the pretty printer is correct, then `parse(print(e))` should be identical to `e`,

0 commit comments

Comments
 (0)