Skip to content

Commit 731c002

Browse files
committed
Only allow feeding a value to newly created definitions.
1 parent 9f2c6b0 commit 731c002

File tree

6 files changed

+47
-33
lines changed

6 files changed

+47
-33
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
497497
self.tcx.hir().def_key(self.local_def_id(node_id)),
498498
);
499499

500-
let def_id = self.tcx.create_def(parent, data);
500+
let def_id = self.tcx.create_def(parent, data).def_id;
501501

502502
debug!("create_def: def_id_to_node_id[{:?}] <-> {:?}", def_id, node_id);
503503
self.resolver.node_id_to_def_id.insert(node_id, def_id);

compiler/rustc_hir/src/definitions.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -368,10 +368,6 @@ impl Definitions {
368368
LocalDefId { local_def_index: self.table.allocate(key, def_path_hash) }
369369
}
370370

371-
pub fn iter_local_def_id(&self) -> impl Iterator<Item = LocalDefId> + '_ {
372-
self.table.def_path_hashes.indices().map(|local_def_index| LocalDefId { local_def_index })
373-
}
374-
375371
#[inline(always)]
376372
pub fn local_def_path_hash_to_def_id(
377373
&self,
@@ -389,6 +385,10 @@ impl Definitions {
389385
pub fn def_path_hash_to_def_index_map(&self) -> &DefPathHashMap {
390386
&self.table.def_path_hash_to_index
391387
}
388+
389+
pub fn num_definitions(&self) -> usize {
390+
self.table.def_path_hashes.len()
391+
}
392392
}
393393

394394
#[derive(Copy, Clone, PartialEq, Debug)]

compiler/rustc_middle/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@
3030
#![feature(core_intrinsics)]
3131
#![feature(discriminant_kind)]
3232
#![feature(exhaustive_patterns)]
33+
#![feature(generators)]
3334
#![feature(get_mut_unchecked)]
3435
#![feature(if_let_guard)]
36+
#![feature(iter_from_generator)]
3537
#![feature(negative_impls)]
3638
#![feature(never_type)]
3739
#![feature(extern_types)]

compiler/rustc_middle/src/query/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ rustc_queries! {
165165
}
166166
cache_on_disk_if { key.is_local() }
167167
separate_provide_extern
168-
feedable
169168
}
170169

171170
query collect_trait_impl_trait_tys(key: DefId)

compiler/rustc_middle/src/ty/context.rs

+37-10
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ use rustc_hir::{
5353
use rustc_index::vec::{Idx, IndexVec};
5454
use rustc_macros::HashStable;
5555
use rustc_middle::mir::FakeReadCause;
56+
use rustc_query_system::dep_graph::DepNodeIndex;
5657
use rustc_query_system::ich::StableHashingContext;
5758
use rustc_serialize::opaque::{FileEncodeResult, FileEncoder};
5859
use rustc_session::config::{CrateType, OutputFilenames};
@@ -1009,6 +1010,14 @@ pub struct FreeRegionInfo {
10091010
pub is_impl_item: bool,
10101011
}
10111012

1013+
#[derive(Copy, Clone)]
1014+
pub struct TyCtxtFeed<'tcx> {
1015+
pub tcx: TyCtxt<'tcx>,
1016+
pub def_id: LocalDefId,
1017+
/// This struct should only be created by `create_def`.
1018+
_priv: (),
1019+
}
1020+
10121021
/// The central data structure of the compiler. It stores references
10131022
/// to the various **arenas** and also houses the results of the
10141023
/// various **compiler queries** that have been performed. See the
@@ -1471,12 +1480,15 @@ impl<'tcx> TyCtxt<'tcx> {
14711480
}
14721481

14731482
/// Create a new definition within the incr. comp. engine.
1474-
pub fn create_def(self, parent: LocalDefId, data: hir::definitions::DefPathData) -> LocalDefId {
1483+
pub fn create_def(
1484+
self,
1485+
parent: LocalDefId,
1486+
data: hir::definitions::DefPathData,
1487+
) -> TyCtxtFeed<'tcx> {
14751488
// This function modifies `self.definitions` using a side-effect.
14761489
// We need to ensure that these side effects are re-run by the incr. comp. engine.
14771490
// Depending on the forever-red node will tell the graph that the calling query
14781491
// needs to be re-evaluated.
1479-
use rustc_query_system::dep_graph::DepNodeIndex;
14801492
self.dep_graph.read_index(DepNodeIndex::FOREVER_RED_NODE);
14811493

14821494
// The following call has the side effect of modifying the tables inside `definitions`.
@@ -1493,23 +1505,38 @@ impl<'tcx> TyCtxt<'tcx> {
14931505
// This is fine because:
14941506
// - those queries are `eval_always` so we won't miss their result changing;
14951507
// - this write will have happened before these queries are called.
1496-
self.definitions.write().create_def(parent, data)
1508+
let def_id = self.definitions.write().create_def(parent, data);
1509+
1510+
TyCtxtFeed { tcx: self, def_id, _priv: () }
14971511
}
14981512

14991513
pub fn iter_local_def_id(self) -> impl Iterator<Item = LocalDefId> + 'tcx {
1500-
// Create a dependency to the crate to be sure we re-execute this when the amount of
1514+
// Create a dependency to the red node to be sure we re-execute this when the amount of
15011515
// definitions change.
1502-
self.ensure().hir_crate(());
1503-
// Leak a read lock once we start iterating on definitions, to prevent adding new ones
1504-
// while iterating. If some query needs to add definitions, it should be `ensure`d above.
1505-
let definitions = self.definitions.leak();
1506-
definitions.iter_local_def_id()
1516+
self.dep_graph.read_index(DepNodeIndex::FOREVER_RED_NODE);
1517+
1518+
let definitions = &self.definitions;
1519+
std::iter::from_generator(|| {
1520+
let mut i = 0;
1521+
1522+
// Recompute the number of definitions each time, because our caller may be creating
1523+
// new ones.
1524+
while i < { definitions.read().num_definitions() } {
1525+
let local_def_index = rustc_span::def_id::DefIndex::from_usize(i);
1526+
yield LocalDefId { local_def_index };
1527+
i += 1;
1528+
}
1529+
1530+
// Leak a read lock once we finish iterating on definitions, to prevent adding new ones.
1531+
definitions.leak();
1532+
})
15071533
}
15081534

15091535
pub fn def_path_table(self) -> &'tcx rustc_hir::definitions::DefPathTable {
15101536
// Create a dependency to the crate to be sure we re-execute this when the amount of
15111537
// definitions change.
1512-
self.ensure().hir_crate(());
1538+
self.dep_graph.read_index(DepNodeIndex::FOREVER_RED_NODE);
1539+
15131540
// Leak a read lock once we start iterating on definitions, to prevent adding new ones
15141541
// while iterating. If some query needs to add definitions, it should be `ensure`d above.
15151542
let definitions = self.definitions.leak();

compiler/rustc_middle/src/ty/query.rs

+3-17
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use crate::traits::query::{
2828
};
2929
use crate::traits::specialization_graph;
3030
use crate::traits::{self, ImplSource};
31+
use crate::ty::context::TyCtxtFeed;
3132
use crate::ty::fast_reject::SimplifiedType;
3233
use crate::ty::layout::TyAndLayout;
3334
use crate::ty::subst::{GenericArg, SubstsRef};
@@ -85,11 +86,6 @@ pub struct TyCtxtEnsure<'tcx> {
8586
pub tcx: TyCtxt<'tcx>,
8687
}
8788

88-
#[derive(Copy, Clone)]
89-
pub struct TyCtxtFeed<'tcx> {
90-
pub tcx: TyCtxt<'tcx>,
91-
}
92-
9389
impl<'tcx> TyCtxt<'tcx> {
9490
/// Returns a transparent wrapper for `TyCtxt`, which ensures queries
9591
/// are executed instead of just returning their results.
@@ -98,12 +94,6 @@ impl<'tcx> TyCtxt<'tcx> {
9894
TyCtxtEnsure { tcx: self }
9995
}
10096

101-
/// Returns a transparent wrapper for `TyCtxt`, for setting a result into a query.
102-
#[inline(always)]
103-
pub fn feed(self) -> TyCtxtFeed<'tcx> {
104-
TyCtxtFeed { tcx: self }
105-
}
106-
10797
/// Returns a transparent wrapper for `TyCtxt` which uses
10898
/// `span` as the location of queries performed through it.
10999
#[inline(always)]
@@ -355,12 +345,8 @@ macro_rules! define_feedable {
355345
impl<'tcx> TyCtxtFeed<'tcx> {
356346
$($(#[$attr])*
357347
#[inline(always)]
358-
pub fn $name(
359-
self,
360-
key: query_helper_param_ty!($($K)*),
361-
value: $V,
362-
) -> query_stored::$name<'tcx> {
363-
let key = key.into_query_param();
348+
pub fn $name(self, value: $V) -> query_stored::$name<'tcx> {
349+
let key = self.def_id.into_query_param();
364350
opt_remap_env_constness!([$($modifiers)*][key]);
365351

366352
let tcx = self.tcx;

0 commit comments

Comments
 (0)