Skip to content

Commit 624fb95

Browse files
committed
Move four fields from ParseSess to Session.
`ParseSess` is separate from, but sits within, `Session`. The separation is because there are some places (e.g. `Parser` methods) where `ParseSess` is available but `Session` is not. However, `ParseSess` has four fields that are only accessed from places where `Session` is also available. This commit moves those fields to `Session`. This means that `ParseSess` only contains the fields it genuinely needs, and various `sess.psess.foo` occurrences are reduced to `sess.foo`.
1 parent ef76c63 commit 624fb95

11 files changed

Lines changed: 59 additions & 62 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/cfg.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ pub(crate) fn parse_name_value<S: Stage>(
222222
}
223223
};
224224

225-
match cx.sess.psess.check_config.expecteds.get(&name) {
225+
match cx.sess.check_config.expecteds.get(&name) {
226226
Some(ExpectedValues::Some(values)) if !values.contains(&value.map(|(v, _)| v)) => cx
227227
.emit_dyn_lint_with_sess(
228228
UNEXPECTED_CFGS,
@@ -232,7 +232,7 @@ pub(crate) fn parse_name_value<S: Stage>(
232232
},
233233
span,
234234
),
235-
None if cx.sess.psess.check_config.exhaustive_names => cx.emit_dyn_lint_with_sess(
235+
None if cx.sess.check_config.exhaustive_names => cx.emit_dyn_lint_with_sess(
236236
UNEXPECTED_CFGS,
237237
move |dcx, level, sess| {
238238
check_cfg::unexpected_cfg_name(sess, (name, name_span), value).into_diag(dcx, level)
@@ -280,7 +280,7 @@ pub fn eval_config_entry(sess: &Session, cfg_entry: &CfgEntry) -> EvalConfigResu
280280
}
281281
}
282282
CfgEntry::NameValue { name, value, span } => {
283-
if sess.psess.config.contains(&(*name, *value)) {
283+
if sess.config.contains(&(*name, *value)) {
284284
EvalConfigResult::True
285285
} else {
286286
EvalConfigResult::False { reason: cfg_entry.clone(), reason_span: *span }

compiler/rustc_attr_parsing/src/attributes/diagnostic/check_cfg.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ fn sort_and_truncate_possibilities(
2525
} else {
2626
match filter_well_known_names {
2727
FilterWellKnownNames::Yes => {
28-
possibilities.retain(|cfg_name| {
29-
!sess.psess.check_config.well_known_names.contains(cfg_name)
30-
});
28+
possibilities
29+
.retain(|cfg_name| !sess.check_config.well_known_names.contains(cfg_name));
3130
}
3231
FilterWellKnownNames::No => {}
3332
};
@@ -105,13 +104,12 @@ pub(crate) fn unexpected_cfg_name(
105104
value: Option<(Symbol, Span)>,
106105
) -> errors::UnexpectedCfgName {
107106
#[allow(rustc::potential_query_instability)]
108-
let possibilities: Vec<Symbol> = sess.psess.check_config.expecteds.keys().copied().collect();
107+
let possibilities: Vec<Symbol> = sess.check_config.expecteds.keys().copied().collect();
109108

110109
let mut names_possibilities: Vec<_> = if value.is_none() {
111110
// We later sort and display all the possibilities, so the order here does not matter.
112111
#[allow(rustc::potential_query_instability)]
113-
sess.psess
114-
.check_config
112+
sess.check_config
115113
.expecteds
116114
.iter()
117115
.filter_map(|(k, v)| match v {
@@ -167,7 +165,7 @@ pub(crate) fn unexpected_cfg_name(
167165
is_feature_cfg |= best_match == sym::feature;
168166

169167
if let Some(ExpectedValues::Some(best_match_values)) =
170-
sess.psess.check_config.expecteds.get(&best_match)
168+
sess.check_config.expecteds.get(&best_match)
171169
{
172170
// We will soon sort, so the initial order does not matter.
173171
#[allow(rustc::potential_query_instability)]
@@ -285,7 +283,7 @@ pub(crate) fn unexpected_cfg_value(
285283
(name, name_span): (Symbol, Span),
286284
value: Option<(Symbol, Span)>,
287285
) -> errors::UnexpectedCfgValue {
288-
let Some(ExpectedValues::Some(values)) = &sess.psess.check_config.expecteds.get(&name) else {
286+
let Some(ExpectedValues::Some(values)) = &sess.check_config.expecteds.get(&name) else {
289287
panic!(
290288
"it shouldn't be possible to have a diagnostic on a value whose name is not in values"
291289
);
@@ -305,7 +303,7 @@ pub(crate) fn unexpected_cfg_value(
305303
let is_from_external_macro = name_span.in_external_macro(sess.source_map());
306304

307305
let code_sugg = if let Some((value, _)) = value
308-
&& sess.psess.check_config.well_known_names.contains(&name)
306+
&& sess.check_config.well_known_names.contains(&name)
309307
&& let valid_names = possible_well_known_names_for_cfg_value(sess, value)
310308
&& !valid_names.is_empty()
311309
{
@@ -378,12 +376,12 @@ pub(crate) fn unexpected_cfg_value(
378376
// We don't want to encourage people to add values to a well-known names, as these are
379377
// defined by rustc/Rust itself. Users can still do this if they wish, but should not be
380378
// encouraged to do so.
381-
let can_suggest_adding_value = !sess.psess.check_config.well_known_names.contains(&name)
379+
let can_suggest_adding_value = !sess.check_config.well_known_names.contains(&name)
382380
// Except when working on rustc or the standard library itself, in which case we want to
383381
// suggest adding these cfgs to the "normal" place because of bootstrapping reasons. As a
384382
// basic heuristic, we use the "cheat" unstable feature enable method and the
385383
// non-ui-testing enabled option.
386-
|| (matches!(sess.psess.unstable_features, rustc_feature::UnstableFeatures::Cheat)
384+
|| (matches!(sess.unstable_features, rustc_feature::UnstableFeatures::Cheat)
387385
&& !sess.opts.unstable_opts.ui_testing);
388386

389387
let inst = |escape_quotes| {
@@ -429,13 +427,11 @@ pub(crate) fn unexpected_cfg_value(
429427
fn possible_well_known_names_for_cfg_value(sess: &Session, value: Symbol) -> Vec<Symbol> {
430428
#[allow(rustc::potential_query_instability)]
431429
let mut names = sess
432-
.psess
433430
.check_config
434431
.well_known_names
435432
.iter()
436433
.filter(|name| {
437-
sess.psess
438-
.check_config
434+
sess.check_config
439435
.expecteds
440436
.get(*name)
441437
.map(|expected_values| expected_values.contains(&Some(value)))

compiler/rustc_codegen_ssa/src/assert_module_sources.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl<'tcx> AssertModuleSource<'tcx> {
171171
/// Scan for a `cfg="foo"` attribute and check whether we have a
172172
/// cfg flag called `foo`.
173173
fn check_config(&self, value: Symbol) -> bool {
174-
let config = &self.tcx.sess.psess.config;
174+
let config = &self.tcx.sess.config;
175175
debug!("check_config(config={:?}, value={:?})", config, value);
176176
if config.iter().any(|&(name, _)| name == value) {
177177
debug!("check_config: matched");

compiler/rustc_driver_impl/src/lib.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,6 @@ fn print_crate_info(
734734
}
735735
Cfg => {
736736
let mut cfgs = sess
737-
.psess
738737
.config
739738
.iter()
740739
.filter_map(|&(name, value)| {
@@ -763,7 +762,7 @@ fn print_crate_info(
763762

764763
// INSTABILITY: We are sorting the output below.
765764
#[allow(rustc::potential_query_instability)]
766-
for (name, expected_values) in &sess.psess.check_config.expecteds {
765+
for (name, expected_values) in &sess.check_config.expecteds {
767766
use crate::config::ExpectedValues;
768767
match expected_values {
769768
ExpectedValues::Any => {
@@ -791,9 +790,7 @@ fn print_crate_info(
791790
}
792791

793792
check_cfgs.sort_unstable();
794-
if !sess.psess.check_config.exhaustive_names
795-
&& sess.psess.check_config.exhaustive_values
796-
{
793+
if !sess.check_config.exhaustive_names && sess.check_config.exhaustive_values {
797794
println_info!("cfg(any())");
798795
}
799796
for check_cfg in check_cfgs {

compiler/rustc_expand/src/proc_macro_server.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc_proc_macro::bridge::{
1515
DelimSpan, Diagnostic, ExpnGlobals, Group, Ident, LitKind, Literal, Punct, TokenTree, server,
1616
};
1717
use rustc_proc_macro::{Delimiter, Level};
18+
use rustc_session::Session;
1819
use rustc_session::parse::ParseSess;
1920
use rustc_span::def_id::CrateNum;
2021
use rustc_span::{BytePos, FileName, Pos, Span, Symbol, sym};
@@ -440,6 +441,10 @@ impl<'a, 'b> Rustc<'a, 'b> {
440441
}
441442
}
442443

444+
fn sess(&self) -> &Session {
445+
&self.ecx.sess
446+
}
447+
443448
fn psess(&self) -> &ParseSess {
444449
self.ecx.psess()
445450
}
@@ -825,7 +830,7 @@ impl server::Server for Rustc<'_, '_> {
825830
/// since we've loaded `my_proc_macro` from disk in order to execute it).
826831
/// In this way, we have obtained a span pointing into `my_proc_macro`
827832
fn span_save_span(&mut self, span: Self::Span) -> usize {
828-
self.psess().save_proc_macro_span(span)
833+
self.sess().save_proc_macro_span(span)
829834
}
830835

831836
fn span_recover_proc_macro_span(&mut self, id: usize) -> Self::Span {

compiler/rustc_incremental/src/persist/clean.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,7 @@ impl<'tcx> CleanVisitor<'tcx> {
183183
item_id: LocalDefId,
184184
attr: &RustcCleanAttribute,
185185
) -> Option<Assertion> {
186-
self.tcx
187-
.sess
188-
.psess
189-
.config
190-
.contains(&(attr.cfg, None))
191-
.then(|| self.assertion_auto(item_id, attr))
186+
self.tcx.sess.config.contains(&(attr.cfg, None)).then(|| self.assertion_auto(item_id, attr))
192187
}
193188

194189
/// Gets the "auto" assertion on pre-validated attr, along with the `except` labels.
@@ -406,7 +401,7 @@ struct FindAllAttrs<'tcx> {
406401

407402
impl<'tcx> FindAllAttrs<'tcx> {
408403
fn is_active_attr(&self, attr: &RustcCleanAttribute) -> bool {
409-
self.tcx.sess.psess.config.contains(&(attr.cfg, None))
404+
self.tcx.sess.config.contains(&(attr.cfg, None))
410405
}
411406

412407
fn report_unchecked_attrs(&self, mut checked_attrs: FxHashSet<Span>) {

compiler/rustc_interface/src/interface.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,11 +452,11 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
452452
let cfg = parse_cfg(sess.dcx(), config.crate_cfg);
453453
let mut cfg = config::build_configuration(&sess, cfg);
454454
util::add_configuration(&mut cfg, &mut sess, &*codegen_backend);
455-
sess.psess.config = cfg;
455+
sess.config = cfg;
456456

457457
let mut check_cfg = parse_check_cfg(sess.dcx(), config.crate_check_cfg);
458458
check_cfg.fill_well_known(&sess.target);
459-
sess.psess.check_config = check_cfg;
459+
sess.check_config = check_cfg;
460460

461461
if let Some(psess_created) = config.psess_created {
462462
psess_created(&mut sess.psess);

compiler/rustc_metadata/src/dependency_format.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ fn add_library(
309309
// This error is probably a little obscure, but I imagine that it
310310
// can be refined over time.
311311
if link2 != link || link == RequireStatic {
312-
let linking_to_rustc_driver = tcx.sess.psess.unstable_features.is_nightly_build()
312+
let linking_to_rustc_driver = tcx.sess.unstable_features.is_nightly_build()
313313
&& tcx.crates(()).iter().any(|&cnum| tcx.crate_name(cnum) == sym::rustc_driver);
314314
tcx.dcx().emit_err(CrateDepMultiple {
315315
crate_name: tcx.crate_name(cnum),

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1984,7 +1984,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
19841984
let stability = tcx.lookup_stability(CRATE_DEF_ID);
19851985
let macros =
19861986
self.lazy_array(tcx.resolutions(()).proc_macros.iter().map(|p| p.local_def_index));
1987-
for (i, span) in self.tcx.sess.psess.proc_macro_quoted_spans() {
1987+
for (i, span) in self.tcx.sess.proc_macro_quoted_spans() {
19881988
let span = self.lazy(span);
19891989
self.tables.proc_macro_quoted_spans.set_some(i, span);
19901990
}

compiler/rustc_session/src/parse.rs

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@ use rustc_errors::{
1414
BufferedEarlyLint, ColorConfig, DecorateDiagCompat, Diag, DiagCtxt, DiagCtxtHandle,
1515
DiagMessage, EmissionGuarantee, Level, MultiSpan, StashKey,
1616
};
17-
use rustc_feature::{GateIssue, UnstableFeatures, find_feature_issue};
17+
use rustc_feature::{GateIssue, find_feature_issue};
1818
use rustc_span::edition::Edition;
1919
use rustc_span::hygiene::ExpnId;
2020
use rustc_span::source_map::{FilePathMapping, SourceMap};
2121
use rustc_span::{Span, Symbol, sym};
2222

2323
use crate::Session;
24-
use crate::config::{Cfg, CheckCfg};
2524
use crate::errors::{
2625
CliFeatureDiagnosticHelp, FeatureDiagnosticForIssue, FeatureDiagnosticHelp,
2726
FeatureDiagnosticSuggestion, FeatureGateError, SuggestUpgradeCompiler,
@@ -182,7 +181,7 @@ pub fn add_feature_diagnostics_for_issue<G: EmissionGuarantee>(
182181
}
183182

184183
// #23973: do not suggest `#![feature(...)]` if we are in beta/stable
185-
if sess.psess.unstable_features.is_nightly_build() {
184+
if sess.unstable_features.is_nightly_build() {
186185
if feature_from_cli {
187186
err.subdiagnostic(CliFeatureDiagnosticHelp { feature });
188187
} else if let Some(span) = inject_span {
@@ -226,7 +225,7 @@ pub fn feature_err_unstable_feature_bound(
226225
let mut err = sess.dcx().create_err(FeatureGateError { span, explain: explain.into() });
227226

228227
// #23973: do not suggest `#![feature(...)]` if we are in beta/stable
229-
if sess.psess.unstable_features.is_nightly_build() {
228+
if sess.unstable_features.is_nightly_build() {
230229
err.subdiagnostic(FeatureDiagnosticHelp { feature });
231230

232231
if feature == sym::rustc_attrs {
@@ -245,9 +244,6 @@ pub fn feature_err_unstable_feature_bound(
245244
/// Info about a parsing session.
246245
pub struct ParseSess {
247246
dcx: DiagCtxt,
248-
pub unstable_features: UnstableFeatures,
249-
pub config: Cfg,
250-
pub check_config: CheckCfg,
251247
pub edition: Edition,
252248
/// Places where raw identifiers were used. This is used to avoid complaining about idents
253249
/// clashing with keywords in new editions.
@@ -264,9 +260,6 @@ pub struct ParseSess {
264260
pub ambiguous_block_expr_parse: Lock<FxIndexMap<Span, Span>>,
265261
pub gated_spans: GatedSpans,
266262
pub symbol_gallery: SymbolGallery,
267-
/// Spans passed to `proc_macro::quote_span`. Each span has a numerical
268-
/// identifier represented by its position in the vector.
269-
proc_macro_quoted_spans: AppendOnlyVec<Span>,
270263
/// Used to generate new `AttrId`s. Every `AttrId` is unique.
271264
pub attr_id_generator: AttrIdGenerator,
272265
}
@@ -286,9 +279,6 @@ impl ParseSess {
286279
pub fn with_dcx(dcx: DiagCtxt, source_map: Arc<SourceMap>) -> Self {
287280
Self {
288281
dcx,
289-
unstable_features: UnstableFeatures::from_environment(None),
290-
config: Cfg::default(),
291-
check_config: CheckCfg::default(),
292282
edition: ExpnId::root().expn_data().edition,
293283
raw_identifier_spans: Default::default(),
294284
bad_unicode_identifiers: Lock::new(Default::default()),
@@ -297,7 +287,6 @@ impl ParseSess {
297287
ambiguous_block_expr_parse: Lock::new(Default::default()),
298288
gated_spans: GatedSpans::default(),
299289
symbol_gallery: SymbolGallery::default(),
300-
proc_macro_quoted_spans: Default::default(),
301290
attr_id_generator: AttrIdGenerator::new(),
302291
}
303292
}
@@ -385,16 +374,6 @@ impl ParseSess {
385374
});
386375
}
387376

388-
pub fn save_proc_macro_span(&self, span: Span) -> usize {
389-
self.proc_macro_quoted_spans.push(span)
390-
}
391-
392-
pub fn proc_macro_quoted_spans(&self) -> impl Iterator<Item = (usize, Span)> {
393-
// This is equivalent to `.iter().copied().enumerate()`, but that isn't possible for
394-
// AppendOnlyVec, so we resort to this scheme.
395-
self.proc_macro_quoted_spans.iter_enumerated()
396-
}
397-
398377
pub fn dcx(&self) -> DiagCtxtHandle<'_> {
399378
self.dcx.handle()
400379
}

0 commit comments

Comments
 (0)