Skip to content

Commit 9126582

Browse files
committed
Create the definition directly when outside a query.
1 parent 9539a34 commit 9126582

3 files changed

Lines changed: 19 additions & 20 deletions

File tree

compiler/rustc_middle/src/queries.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ rustc_queries! {
204204
query create_def_raw(key: (
205205
LocalDefId, // parent
206206
DefPathData, // def_path_data
207-
Option<DepNode>, // caller query
207+
DepNode, // caller query
208208
usize, // counter of calls to `create_def_raw` by the caller query
209209
)) -> LocalDefId {
210210
// Accesses untracked data

compiler/rustc_middle/src/query/keys.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ impl<'tcx> QueryKey for (ty::Instance<'tcx>, CollectionMode) {
363363
}
364364
}
365365

366-
impl QueryKey for (LocalDefId, DefPathData, Option<DepNode>, usize) {
366+
impl QueryKey for (LocalDefId, DefPathData, DepNode, usize) {
367367
type Cache<V> = DefaultCache<Self, V>;
368368

369369
fn default_span(&self, _: TyCtxt<'_>) -> Span {

compiler/rustc_middle/src/ty/context.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,7 +1431,7 @@ impl<'tcx> TyCtxt<'tcx> {
14311431
#[instrument(level = "trace", skip(tcx), ret)]
14321432
fn create_def_raw_provider<'tcx>(
14331433
tcx: TyCtxt<'tcx>,
1434-
(parent, data, query, index): (LocalDefId, DefPathData, Option<DepNode>, usize),
1434+
(parent, data, query, index): (LocalDefId, DefPathData, DepNode, usize),
14351435
) -> LocalDefId {
14361436
// `query` and `index` are guaranteed to change for each successive call to
14371437
// `create_def_raw`, but in a predictable manner.
@@ -1482,31 +1482,30 @@ impl<'tcx> TyCtxt<'tcx> {
14821482
) -> TyCtxtFeed<'tcx, LocalDefId> {
14831483
let data = override_def_path_data.unwrap_or_else(|| def_kind.def_path_data(name));
14841484

1485-
// `create_def_raw` is a query, so it can be replayed by the dep-graph engine. However, we
1486-
// may invoke it multiple times with the same `(parent, data)` pair, and we expect to
1487-
// create *different* definitions from them. In order to make this compatible with the
1488-
// general model of queries, we add additional information which must change at each call.
1489-
let (dep_node, query_local_index) = ty::tls::with_context(|icx| match icx.task_deps {
1490-
// If we are inside a query, we can only use local information, and no global
1491-
// mutable state. The current query's name and the number of calls to `create_def`
1492-
// are local to the current query, so are ok to use.
1485+
let def_id = ty::tls::with_context(|icx| match icx.task_deps {
1486+
// `create_def_raw` is a query, so it can be replayed by the dep-graph engine.
1487+
// However, we may invoke it multiple times with the same `(parent, data)` pair,
1488+
// and we expect to create *different* definitions from them.
1489+
//
1490+
// In order to make this compatible with the general model of queries, we add
1491+
// additional information which must change at each call.
14931492
TaskDepsRef::Allow(deps) => {
14941493
let opt_dep_node_and_index = deps.lock().next_query_local_index();
1495-
if let Some((dep_node, index)) = opt_dep_node_and_index {
1496-
(Some(dep_node), index)
1497-
} else {
1494+
let Some((dep_node, index)) = opt_dep_node_and_index else {
14981495
// No idea how to support this for now...
14991496
bug!("trying to create a definition from an anonymous query")
1500-
}
1497+
};
1498+
self.create_def_raw((parent, data, dep_node, index))
15011499
}
1502-
// If we are not tracking dependencies, we can use global mutable state,
1503-
// so we use the total number of definitions as a proxy.
1500+
1501+
// If we are not tracking dependencies, we can use global mutable state.
15041502
TaskDepsRef::EvalAlways | TaskDepsRef::Forbid | TaskDepsRef::Ignore => {
1505-
let global_count = self.untracked.definitions.read().def_index_count();
1506-
(None, global_count)
1503+
// This query is `eval_always`, so we can access untracked data.
1504+
let mut disambiguator_state = self.untracked_disambiguator_state.lock();
1505+
let disambiguator = disambiguator_state.get_or_create(parent);
1506+
self.untracked.definitions.write().create_def(parent, data, disambiguator)
15071507
}
15081508
});
1509-
let def_id = self.create_def_raw((parent, data, dep_node, query_local_index));
15101509

15111510
let feed = TyCtxtFeed { tcx: self, key: def_id };
15121511
feed.def_kind(def_kind);

0 commit comments

Comments
 (0)