Skip to content

Commit

Permalink
rustc: Move features from Session to GlobalCtxt
Browse files Browse the repository at this point in the history
Removes two pieces of mutable state.
Follow up to rust-lang#114622.
  • Loading branch information
petrochenkov committed Aug 11, 2023
1 parent a07bc13 commit 7353c96
Show file tree
Hide file tree
Showing 30 changed files with 130 additions and 93 deletions.
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3805,6 +3805,7 @@ dependencies = [
"rustc_data_structures",
"rustc_errors",
"rustc_expand",
"rustc_feature",
"rustc_fluent_macro",
"rustc_fs_util",
"rustc_hir",
Expand Down
12 changes: 10 additions & 2 deletions compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use rustc_ast::*;
use rustc_ast::{walk_list, StaticItem};
use rustc_ast_pretty::pprust::{self, State};
use rustc_data_structures::fx::FxIndexMap;
use rustc_feature::Features;
use rustc_macros::Subdiagnostic;
use rustc_parse::validate_attr;
use rustc_session::lint::builtin::{
Expand Down Expand Up @@ -45,6 +46,7 @@ enum DisallowTildeConstContext<'a> {

struct AstValidator<'a> {
session: &'a Session,
features: &'a Features,

/// The span of the `extern` in an `extern { ... }` block, if any.
extern_mod: Option<&'a Item>,
Expand Down Expand Up @@ -1023,7 +1025,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
}
self.check_type_no_bounds(bounds, "this context");

if self.session.features_untracked().lazy_type_alias {
if self.features.lazy_type_alias {
if let Err(err) = self.check_type_alias_where_clause_location(ty_alias) {
self.err_handler().emit_err(err);
}
Expand Down Expand Up @@ -1500,9 +1502,15 @@ fn deny_equality_constraints(
this.err_handler().emit_err(err);
}

pub fn check_crate(session: &Session, krate: &Crate, lints: &mut LintBuffer) -> bool {
pub fn check_crate(
session: &Session,
features: &Features,
krate: &Crate,
lints: &mut LintBuffer,
) -> bool {
let mut validator = AstValidator {
session,
features,
extern_mod: None,
in_trait_impl: false,
in_const_trait_impl: false,
Expand Down
16 changes: 7 additions & 9 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,10 +514,10 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
}
}

pub fn check_crate(krate: &ast::Crate, sess: &Session) {
maybe_stage_features(sess, krate);
check_incompatible_features(sess);
let mut visitor = PostExpansionVisitor { sess, features: &sess.features_untracked() };
pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) {
maybe_stage_features(sess, features, krate);
check_incompatible_features(sess, features);
let mut visitor = PostExpansionVisitor { sess, features };

let spans = sess.parse_sess.gated_spans.spans.borrow();
macro_rules! gate_all {
Expand Down Expand Up @@ -600,12 +600,12 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session) {
visit::walk_crate(&mut visitor, krate);
}

fn maybe_stage_features(sess: &Session, krate: &ast::Crate) {
fn maybe_stage_features(sess: &Session, features: &Features, krate: &ast::Crate) {
// checks if `#![feature]` has been used to enable any lang feature
// does not check the same for lib features unless there's at least one
// declared lang feature
if !sess.opts.unstable_features.is_nightly_build() {
let lang_features = &sess.features_untracked().declared_lang_features;
let lang_features = &features.declared_lang_features;
if lang_features.len() == 0 {
return;
}
Expand Down Expand Up @@ -640,9 +640,7 @@ fn maybe_stage_features(sess: &Session, krate: &ast::Crate) {
}
}

fn check_incompatible_features(sess: &Session) {
let features = sess.features_untracked();

fn check_incompatible_features(sess: &Session, features: &Features) {
let declared_features = features
.declared_lang_features
.iter()
Expand Down
21 changes: 9 additions & 12 deletions compiler/rustc_attr/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -800,18 +800,15 @@ pub struct Deprecation {
}

/// Finds the deprecation attribute. `None` if none exists.
pub fn find_deprecation(sess: &Session, attrs: &[Attribute]) -> Option<(Deprecation, Span)> {
find_deprecation_generic(sess, attrs.iter())
}

fn find_deprecation_generic<'a, I>(sess: &Session, attrs_iter: I) -> Option<(Deprecation, Span)>
where
I: Iterator<Item = &'a Attribute>,
{
pub fn find_deprecation(
sess: &Session,
features: &Features,
attrs: &[Attribute],
) -> Option<(Deprecation, Span)> {
let mut depr: Option<(Deprecation, Span)> = None;
let is_rustc = sess.features_untracked().staged_api;
let is_rustc = features.staged_api;

'outer: for attr in attrs_iter {
'outer: for attr in attrs {
if !attr.has_name(sym::deprecated) {
continue;
}
Expand Down Expand Up @@ -872,7 +869,7 @@ where
}
}
sym::suggestion => {
if !sess.features_untracked().deprecated_suggestion {
if !features.deprecated_suggestion {
sess.emit_err(session_diagnostics::DeprecatedItemSuggestion {
span: mi.span,
is_nightly: sess.is_nightly_build().then_some(()),
Expand All @@ -890,7 +887,7 @@ where
meta.span(),
AttrError::UnknownMetaItem(
pprust::path_to_string(&mi.path),
if sess.features_untracked().deprecated_suggestion {
if features.deprecated_suggestion {
&["since", "note", "suggestion"]
} else {
&["since", "note"]
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub fn expand_assert<'cx>(
// If `generic_assert` is enabled, generates rich captured outputs
//
// FIXME(c410-f3r) See https://github.com/rust-lang/rust/issues/96949
else if let Some(features) = cx.ecfg.features && features.generic_assert {
else if cx.ecfg.features.generic_assert {
context::Context::new(cx, call_site_span).build(cond_expr, panic_path())
}
// If `generic_assert` is not enabled, only outputs a literal "assertion failed: ..."
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn expand_cfg(
&cfg,
&cx.sess.parse_sess,
cx.current_expansion.lint_node_id,
cx.ecfg.features,
Some(cx.ecfg.features),
);
MacEager::expr(cx.expr_bool(sp, matches_cfg))
}
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_builtin_macros/src/cfg_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ pub(crate) fn expand(

pub(crate) fn cfg_eval(
sess: &Session,
features: Option<&Features>,
features: &Features,
annotatable: Annotatable,
lint_node_id: NodeId,
) -> Annotatable {
let features = Some(features);
CfgEval { cfg: &mut StripUnconfigured { sess, features, config_tokens: true, lint_node_id } }
.configure_annotatable(annotatable)
// Since the item itself has already been configured by the `InvocationCollector`,
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_builtin_macros/src/proc_macro_harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use rustc_ast::{self as ast, attr, NodeId};
use rustc_ast_pretty::pprust;
use rustc_expand::base::{parse_macro_name_and_helper_attrs, ExtCtxt, ResolverExpand};
use rustc_expand::expand::{AstFragment, ExpansionConfig};
use rustc_feature::Features;
use rustc_session::Session;
use rustc_span::hygiene::AstPass;
use rustc_span::source_map::SourceMap;
Expand Down Expand Up @@ -46,13 +47,14 @@ struct CollectProcMacros<'a> {
pub fn inject(
krate: &mut ast::Crate,
sess: &Session,
features: &Features,
resolver: &mut dyn ResolverExpand,
is_proc_macro_crate: bool,
has_proc_macro_decls: bool,
is_test_crate: bool,
handler: &rustc_errors::Handler,
) {
let ecfg = ExpansionConfig::default("proc_macro".to_string());
let ecfg = ExpansionConfig::default("proc_macro".to_string(), features);
let mut cx = ExtCtxt::new(sess, ecfg, resolver, None);

let mut collect = CollectProcMacros {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use rustc_ast::{self as ast, attr};
use rustc_expand::base::{ExtCtxt, ResolverExpand};
use rustc_expand::expand::ExpansionConfig;
use rustc_feature::Features;
use rustc_session::Session;
use rustc_span::edition::Edition::*;
use rustc_span::hygiene::AstPass;
Expand All @@ -13,6 +14,7 @@ pub fn inject(
pre_configured_attrs: &[ast::Attribute],
resolver: &mut dyn ResolverExpand,
sess: &Session,
features: &Features,
) -> usize {
let orig_num_items = krate.items.len();
let edition = sess.parse_sess.edition;
Expand All @@ -39,7 +41,7 @@ pub fn inject(
let span = DUMMY_SP.with_def_site_ctxt(expn_id.to_expn_id());
let call_site = DUMMY_SP.with_call_site_ctxt(expn_id.to_expn_id());

let ecfg = ExpansionConfig::default("std_lib_injection".to_string());
let ecfg = ExpansionConfig::default("std_lib_injection".to_string(), features);
let cx = ExtCtxt::new(sess, ecfg, resolver, None);

// .rev() to preserve ordering above in combination with insert(0, ...)
Expand Down
13 changes: 8 additions & 5 deletions compiler/rustc_builtin_macros/src/test_harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ struct TestCtxt<'a> {

/// Traverse the crate, collecting all the test functions, eliding any
/// existing main functions, and synthesizing a main test harness
pub fn inject(krate: &mut ast::Crate, sess: &Session, resolver: &mut dyn ResolverExpand) {
pub fn inject(
krate: &mut ast::Crate,
sess: &Session,
features: &Features,
resolver: &mut dyn ResolverExpand,
) {
let span_diagnostic = sess.diagnostic();
let panic_strategy = sess.panic_strategy();
let platform_panic_strategy = sess.target.panic_strategy;
Expand Down Expand Up @@ -76,7 +81,7 @@ pub fn inject(krate: &mut ast::Crate, sess: &Session, resolver: &mut dyn Resolve
resolver,
reexport_test_harness_main,
krate,
&sess.features_untracked(),
features,
panic_strategy,
test_runner,
)
Expand Down Expand Up @@ -243,9 +248,7 @@ fn generate_test_harness(
panic_strategy: PanicStrategy,
test_runner: Option<ast::Path>,
) {
let mut econfig = ExpansionConfig::default("test".to_string());
econfig.features = Some(features);

let econfig = ExpansionConfig::default("test".to_string(), features);
let ext_cx = ExtCtxt::new(sess, econfig, resolver, None);

let expn_id = ext_cx.resolver.expansion_for_ast_pass(
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_expand/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use rustc_errors::{
Applicability, DiagnosticBuilder, DiagnosticMessage, ErrorGuaranteed, IntoDiagnostic,
MultiSpan, PResult,
};
use rustc_feature::Features;
use rustc_lint_defs::builtin::PROC_MACRO_BACK_COMPAT;
use rustc_lint_defs::{BufferedEarlyLint, BuiltinLintDiagnostics, RegisteredTools};
use rustc_parse::{self, parser, MACRO_ARGUMENTS};
Expand Down Expand Up @@ -767,6 +768,7 @@ impl SyntaxExtension {
/// and other properties converted from attributes.
pub fn new(
sess: &Session,
features: &Features,
kind: SyntaxExtensionKind,
span: Span,
helper_attrs: Vec<Symbol>,
Expand Down Expand Up @@ -816,7 +818,7 @@ impl SyntaxExtension {
allow_internal_unstable: (!allow_internal_unstable.is_empty())
.then(|| allow_internal_unstable.into()),
stability: stability.map(|(s, _)| s),
deprecation: attr::find_deprecation(&sess, attrs).map(|(d, _)| d),
deprecation: attr::find_deprecation(&sess, features, attrs).map(|(d, _)| d),
helper_attrs,
edition,
builtin_name,
Expand Down Expand Up @@ -957,6 +959,7 @@ pub trait LintStoreExpand {
fn pre_expansion_lint(
&self,
sess: &Session,
features: &Features,
registered_tools: &RegisteredTools,
node_id: NodeId,
attrs: &[Attribute],
Expand Down
21 changes: 9 additions & 12 deletions compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
| Annotatable::FieldDef(..)
| Annotatable::Variant(..) => panic!("unexpected annotatable"),
};
if self.cx.ecfg.proc_macro_hygiene() {
if self.cx.ecfg.features.proc_macro_hygiene {
return;
}
feature_err(
Expand Down Expand Up @@ -834,7 +834,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
}
}

if !self.cx.ecfg.proc_macro_hygiene() {
if !self.cx.ecfg.features.proc_macro_hygiene {
annotatable
.visit_with(&mut GateProcMacroInput { parse_sess: &self.cx.sess.parse_sess });
}
Expand Down Expand Up @@ -1122,6 +1122,7 @@ impl InvocationCollectorNode for P<ast::Item> {
if let Some(lint_store) = ecx.lint_store {
lint_store.pre_expansion_lint(
ecx.sess,
ecx.ecfg.features,
ecx.resolver.registered_tools(),
ecx.current_expansion.lint_node_id,
&attrs,
Expand Down Expand Up @@ -1580,7 +1581,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
fn cfg(&self) -> StripUnconfigured<'_> {
StripUnconfigured {
sess: &self.cx.sess,
features: self.cx.ecfg.features,
features: Some(self.cx.ecfg.features),
config_tokens: false,
lint_node_id: self.cx.current_expansion.lint_node_id,
}
Expand Down Expand Up @@ -1676,7 +1677,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
// Detect use of feature-gated or invalid attributes on macro invocations
// since they will not be detected after macro expansion.
fn check_attributes(&self, attrs: &[ast::Attribute], call: &ast::MacCall) {
let features = self.cx.ecfg.features.unwrap();
let features = self.cx.ecfg.features;
let mut attrs = attrs.iter().peekable();
let mut span: Option<Span> = None;
while let Some(attr) = attrs.next() {
Expand Down Expand Up @@ -1976,7 +1977,7 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {

pub struct ExpansionConfig<'feat> {
pub crate_name: String,
pub features: Option<&'feat Features>,
pub features: &'feat Features,
pub recursion_limit: Limit,
pub trace_mac: bool,
/// If false, strip `#[test]` nodes
Expand All @@ -1987,20 +1988,16 @@ pub struct ExpansionConfig<'feat> {
pub proc_macro_backtrace: bool,
}

impl<'feat> ExpansionConfig<'feat> {
pub fn default(crate_name: String) -> ExpansionConfig<'static> {
impl ExpansionConfig<'_> {
pub fn default(crate_name: String, features: &Features) -> ExpansionConfig<'_> {
ExpansionConfig {
crate_name,
features: None,
features,
recursion_limit: Limit::new(1024),
trace_mac: false,
should_test: false,
span_debug: false,
proc_macro_backtrace: false,
}
}

fn proc_macro_hygiene(&self) -> bool {
self.features.is_some_and(|features| features.proc_macro_hygiene)
}
}
Loading

0 comments on commit 7353c96

Please sign in to comment.