Skip to content

Commit c933440

Browse files
committed
Auto merge of #48208 - michaelwoerister:track-features, r=petrochenkov
Turn feature-gate table into a query so it is covered by dependency tracking. Turn access to feature gates into a query so we handle them correctly during incremental compilation. Features are still available via `Session` through `features_untracked()`. I wish we had a better way of hiding untracked information. It would be great if we could remove the `sess` field from `TyCtxt`. Fixes #47003.
2 parents e2746d8 + 93625f1 commit c933440

File tree

51 files changed

+231
-141
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+231
-141
lines changed

src/librustc/dep_graph/dep_node.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,9 @@ impl DepKind {
436436
}
437437

438438
define_dep_nodes!( <'tcx>
439+
// We use this for most things when incr. comp. is turned off.
440+
[] Null,
441+
439442
// Represents the `Krate` as a whole (the `hir::Krate` value) (as
440443
// distinct from the krate module). This is basically a hash of
441444
// the entire krate, so if you read from `Krate` (e.g., by calling
@@ -605,8 +608,8 @@ define_dep_nodes!( <'tcx>
605608
[input] MissingExternCrateItem(CrateNum),
606609
[input] UsedCrateSource(CrateNum),
607610
[input] PostorderCnums,
608-
[input] HasCloneClosures(CrateNum),
609-
[input] HasCopyClosures(CrateNum),
611+
[] HasCloneClosures(CrateNum),
612+
[] HasCopyClosures(CrateNum),
610613

611614
// This query is not expected to have inputs -- as a result, it's
612615
// not a good candidate for "replay" because it's essentially a
@@ -630,8 +633,6 @@ define_dep_nodes!( <'tcx>
630633
[] CompileCodegenUnit(InternedString),
631634
[input] OutputFilenames,
632635
[anon] NormalizeTy,
633-
// We use this for most things when incr. comp. is turned off.
634-
[] Null,
635636

636637
[] SubstituteNormalizeAndTestPredicates { key: (DefId, &'tcx Substs<'tcx>) },
637638

@@ -642,6 +643,7 @@ define_dep_nodes!( <'tcx>
642643

643644
[] GetSymbolExportLevel(DefId),
644645

646+
[input] Features,
645647
);
646648

647649
trait DepNodeParams<'a, 'gcx: 'tcx + 'a, 'tcx: 'a> : fmt::Debug {

src/librustc/hir/lowering.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ impl<'a> LoweringContext<'a> {
550550
{
551551
assert!(!self.is_collecting_in_band_lifetimes);
552552
assert!(self.lifetimes_to_define.is_empty());
553-
self.is_collecting_in_band_lifetimes = self.sess.features.borrow().in_band_lifetimes;
553+
self.is_collecting_in_band_lifetimes = self.sess.features_untracked().in_band_lifetimes;
554554

555555
assert!(self.in_band_ty_params.is_empty());
556556

@@ -964,7 +964,7 @@ impl<'a> LoweringContext<'a> {
964964
let span = t.span;
965965
match itctx {
966966
ImplTraitContext::Existential => {
967-
let has_feature = self.sess.features.borrow().conservative_impl_trait;
967+
let has_feature = self.sess.features_untracked().conservative_impl_trait;
968968
if !t.span.allows_unstable() && !has_feature {
969969
emit_feature_err(&self.sess.parse_sess, "conservative_impl_trait",
970970
t.span, GateIssue::Language,
@@ -988,7 +988,7 @@ impl<'a> LoweringContext<'a> {
988988
}, lifetimes)
989989
},
990990
ImplTraitContext::Universal(def_id) => {
991-
let has_feature = self.sess.features.borrow().universal_impl_trait;
991+
let has_feature = self.sess.features_untracked().universal_impl_trait;
992992
if !t.span.allows_unstable() && !has_feature {
993993
emit_feature_err(&self.sess.parse_sess, "universal_impl_trait",
994994
t.span, GateIssue::Language,
@@ -3713,7 +3713,7 @@ impl<'a> LoweringContext<'a> {
37133713
}
37143714

37153715
fn maybe_lint_bare_trait(&self, span: Span, id: NodeId, is_global: bool) {
3716-
if self.sess.features.borrow().dyn_trait {
3716+
if self.sess.features_untracked().dyn_trait {
37173717
self.sess.buffer_lint_with_diagnostic(
37183718
builtin::BARE_TRAIT_OBJECT, id, span,
37193719
"trait objects without an explicit `dyn` are deprecated",

src/librustc/ich/impls_syntax.rs

+19
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use std::hash as std_hash;
1717
use std::mem;
1818

1919
use syntax::ast;
20+
use syntax::feature_gate;
2021
use syntax::parse::token;
2122
use syntax::symbol::InternedString;
2223
use syntax::tokenstream;
@@ -460,3 +461,21 @@ fn stable_non_narrow_char(swc: ::syntax_pos::NonNarrowChar,
460461

461462
(pos.0 - filemap_start.0, width as u32)
462463
}
464+
465+
466+
467+
impl<'gcx> HashStable<StableHashingContext<'gcx>> for feature_gate::Features {
468+
fn hash_stable<W: StableHasherResult>(&self,
469+
hcx: &mut StableHashingContext<'gcx>,
470+
hasher: &mut StableHasher<W>) {
471+
// Unfortunately we cannot exhaustively list fields here, since the
472+
// struct is macro generated.
473+
self.declared_stable_lang_features.hash_stable(hcx, hasher);
474+
self.declared_lib_features.hash_stable(hcx, hasher);
475+
476+
self.walk_feature_fields(|feature_name, value| {
477+
feature_name.hash_stable(hcx, hasher);
478+
value.hash_stable(hcx, hasher);
479+
});
480+
}
481+
}

src/librustc/infer/error_reporting/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,11 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
289289
) {
290290
debug!("report_region_errors(): {} errors to start", errors.len());
291291

292-
if will_later_be_reported_by_nll && self.tcx.sess.nll() {
292+
if will_later_be_reported_by_nll && self.tcx.nll() {
293293
// With `#![feature(nll)]`, we want to present a nice user
294294
// experience, so don't even mention the errors from the
295295
// AST checker.
296-
if self.tcx.sess.features.borrow().nll {
296+
if self.tcx.features().nll {
297297
return;
298298
}
299299

src/librustc/middle/stability.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl<'a, 'tcx: 'a> Annotator<'a, 'tcx> {
131131
item_sp: Span, kind: AnnotationKind, visit_children: F)
132132
where F: FnOnce(&mut Self)
133133
{
134-
if self.tcx.sess.features.borrow().staged_api {
134+
if self.tcx.features().staged_api {
135135
// This crate explicitly wants staged API.
136136
debug!("annotate(id = {:?}, attrs = {:?})", id, attrs);
137137
if let Some(..) = attr::find_deprecation(self.tcx.sess.diagnostic(), attrs, item_sp) {
@@ -398,7 +398,7 @@ impl<'a, 'tcx> Index<'tcx> {
398398
pub fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Index<'tcx> {
399399
let is_staged_api =
400400
tcx.sess.opts.debugging_opts.force_unstable_if_unmarked ||
401-
tcx.sess.features.borrow().staged_api;
401+
tcx.features().staged_api;
402402
let mut staged_api = FxHashMap();
403403
staged_api.insert(LOCAL_CRATE, is_staged_api);
404404
let mut index = Index {
@@ -408,7 +408,7 @@ impl<'a, 'tcx> Index<'tcx> {
408408
active_features: FxHashSet(),
409409
};
410410

411-
let ref active_lib_features = tcx.sess.features.borrow().declared_lib_features;
411+
let ref active_lib_features = tcx.features().declared_lib_features;
412412

413413
// Put the active features into a map for quick lookup
414414
index.active_features = active_lib_features.iter().map(|&(ref s, _)| s.clone()).collect();
@@ -677,7 +677,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Checker<'a, 'tcx> {
677677

678678
// There's no good place to insert stability check for non-Copy unions,
679679
// so semi-randomly perform it here in stability.rs
680-
hir::ItemUnion(..) if !self.tcx.sess.features.borrow().untagged_unions => {
680+
hir::ItemUnion(..) if !self.tcx.features().untagged_unions => {
681681
let def_id = self.tcx.hir.local_def_id(item.id);
682682
let adt_def = self.tcx.adt_def(def_id);
683683
let ty = self.tcx.type_of(def_id);
@@ -721,8 +721,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
721721
/// were expected to be library features), and the list of features used from
722722
/// libraries, identify activated features that don't exist and error about them.
723723
pub fn check_unused_or_stable_features<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
724-
let sess = &tcx.sess;
725-
726724
let access_levels = &tcx.privacy_access_levels(LOCAL_CRATE);
727725

728726
if tcx.stability().staged_api[&LOCAL_CRATE] {
@@ -736,12 +734,12 @@ pub fn check_unused_or_stable_features<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
736734
krate.visit_all_item_likes(&mut missing.as_deep_visitor());
737735
}
738736

739-
let ref declared_lib_features = sess.features.borrow().declared_lib_features;
737+
let ref declared_lib_features = tcx.features().declared_lib_features;
740738
let mut remaining_lib_features: FxHashMap<Symbol, Span>
741739
= declared_lib_features.clone().into_iter().collect();
742740
remaining_lib_features.remove(&Symbol::intern("proc_macro"));
743741

744-
for &(ref stable_lang_feature, span) in &sess.features.borrow().declared_stable_lang_features {
742+
for &(ref stable_lang_feature, span) in &tcx.features().declared_stable_lang_features {
745743
let version = find_lang_feature_accepted_version(&stable_lang_feature.as_str())
746744
.expect("unexpectedly couldn't find version feature was stabilized");
747745
tcx.lint_node(lint::builtin::STABLE_FEATURES,

src/librustc/session/mod.rs

+19-47
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use lint::builtin::BuiltinLintDiagnostics;
2020
use middle::allocator::AllocatorKind;
2121
use middle::dependency_format;
2222
use session::search_paths::PathKind;
23-
use session::config::{BorrowckMode, DebugInfoLevel, OutputType, Epoch};
23+
use session::config::{DebugInfoLevel, OutputType, Epoch};
2424
use ty::tls;
2525
use util::nodemap::{FxHashMap, FxHashSet};
2626
use util::common::{duration_to_secs_str, ErrorReported};
@@ -93,7 +93,8 @@ pub struct Session {
9393
/// multiple crates with the same name to coexist. See the
9494
/// trans::back::symbol_names module for more information.
9595
pub crate_disambiguator: RefCell<Option<CrateDisambiguator>>,
96-
pub features: RefCell<feature_gate::Features>,
96+
97+
features: RefCell<Option<feature_gate::Features>>,
9798

9899
/// The maximum recursion limit for potentially infinitely recursive
99100
/// operations such as auto-dereference and monomorphization.
@@ -194,6 +195,7 @@ impl Session {
194195
None => bug!("accessing disambiguator before initialization"),
195196
}
196197
}
198+
197199
pub fn struct_span_warn<'a, S: Into<MultiSpan>>(&'a self,
198200
sp: S,
199201
msg: &str)
@@ -456,16 +458,22 @@ impl Session {
456458
self.opts.debugging_opts.print_llvm_passes
457459
}
458460

459-
/// If true, we should use NLL-style region checking instead of
460-
/// lexical style.
461-
pub fn nll(&self) -> bool {
462-
self.features.borrow().nll || self.opts.debugging_opts.nll
461+
/// Get the features enabled for the current compilation session.
462+
/// DO NOT USE THIS METHOD if there is a TyCtxt available, as it circumvents
463+
/// dependency tracking. Use tcx.features() instead.
464+
#[inline]
465+
pub fn features_untracked(&self) -> cell::Ref<feature_gate::Features> {
466+
let features = self.features.borrow();
467+
468+
if features.is_none() {
469+
bug!("Access to Session::features before it is initialized");
470+
}
471+
472+
cell::Ref::map(features, |r| r.as_ref().unwrap())
463473
}
464474

465-
/// If true, we should use the MIR-based borrowck (we may *also* use
466-
/// the AST-based borrowck).
467-
pub fn use_mir(&self) -> bool {
468-
self.borrowck_mode().use_mir()
475+
pub fn init_features(&self, features: feature_gate::Features) {
476+
*(self.features.borrow_mut()) = Some(features);
469477
}
470478

471479
/// If true, we should gather causal information during NLL
@@ -475,42 +483,6 @@ impl Session {
475483
self.opts.debugging_opts.nll_dump_cause
476484
}
477485

478-
/// If true, we should enable two-phase borrows checks. This is
479-
/// done with either `-Ztwo-phase-borrows` or with
480-
/// `#![feature(nll)]`.
481-
pub fn two_phase_borrows(&self) -> bool {
482-
self.features.borrow().nll || self.opts.debugging_opts.two_phase_borrows
483-
}
484-
485-
/// What mode(s) of borrowck should we run? AST? MIR? both?
486-
/// (Also considers the `#![feature(nll)]` setting.)
487-
pub fn borrowck_mode(&self) -> BorrowckMode {
488-
match self.opts.borrowck_mode {
489-
mode @ BorrowckMode::Mir |
490-
mode @ BorrowckMode::Compare => mode,
491-
492-
mode @ BorrowckMode::Ast => {
493-
if self.nll() {
494-
BorrowckMode::Mir
495-
} else {
496-
mode
497-
}
498-
}
499-
500-
}
501-
}
502-
503-
/// Should we emit EndRegion MIR statements? These are consumed by
504-
/// MIR borrowck, but not when NLL is used. They are also consumed
505-
/// by the validation stuff.
506-
pub fn emit_end_regions(&self) -> bool {
507-
// FIXME(#46875) -- we should not emit end regions when NLL is enabled,
508-
// but for now we can't stop doing so because it causes false positives
509-
self.opts.debugging_opts.emit_end_regions ||
510-
self.opts.debugging_opts.mir_emit_validate > 0 ||
511-
self.use_mir()
512-
}
513-
514486
/// Calculates the flavor of LTO to use for this compilation.
515487
pub fn lto(&self) -> config::Lto {
516488
// If our target has codegen requirements ignore the command line
@@ -1029,7 +1001,7 @@ pub fn build_session_(sopts: config::Options,
10291001
crate_types: RefCell::new(Vec::new()),
10301002
dependency_formats: RefCell::new(FxHashMap()),
10311003
crate_disambiguator: RefCell::new(None),
1032-
features: RefCell::new(feature_gate::Features::new()),
1004+
features: RefCell::new(None),
10331005
recursion_limit: Cell::new(64),
10341006
type_length_limit: Cell::new(1048576),
10351007
next_node_id: Cell::new(NodeId::new(1)),

src/librustc/traits/specialize/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ pub(super) fn specializes<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
164164

165165
// The feature gate should prevent introducing new specializations, but not
166166
// taking advantage of upstream ones.
167-
if !tcx.sess.features.borrow().specialization &&
167+
if !tcx.features().specialization &&
168168
(impl1_def_id.is_local() || impl2_def_id.is_local()) {
169169
return false;
170170
}

0 commit comments

Comments
 (0)