Skip to content

Commit dca0344

Browse files
committed
Make librustc compile.
1 parent 8e873c3 commit dca0344

File tree

8 files changed

+773
-7
lines changed

8 files changed

+773
-7
lines changed

src/librustc/ty/query/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
For more information about how the query system works, see the [rustc dev guide].
2+
3+
[rustc dev guide]: https://rustc-dev-guide.rust-lang.org/query.html

src/librustc/ty/query/job.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use crate::ty::tls;
2+
3+
use rustc_query_system::query::deadlock;
4+
use rustc_rayon_core as rayon_core;
5+
use std::thread;
6+
7+
/// Creates a new thread and forwards information in thread locals to it.
8+
/// The new thread runs the deadlock handler.
9+
/// Must only be called when a deadlock is about to happen.
10+
pub unsafe fn handle_deadlock() {
11+
let registry = rayon_core::Registry::current();
12+
13+
let gcx_ptr = tls::GCX_PTR.with(|gcx_ptr| gcx_ptr as *const _);
14+
let gcx_ptr = &*gcx_ptr;
15+
16+
let rustc_span_globals =
17+
rustc_span::GLOBALS.with(|rustc_span_globals| rustc_span_globals as *const _);
18+
let rustc_span_globals = &*rustc_span_globals;
19+
let syntax_globals = rustc_ast::attr::GLOBALS.with(|syntax_globals| syntax_globals as *const _);
20+
let syntax_globals = &*syntax_globals;
21+
thread::spawn(move || {
22+
tls::GCX_PTR.set(gcx_ptr, || {
23+
rustc_ast::attr::GLOBALS.set(syntax_globals, || {
24+
rustc_span::GLOBALS
25+
.set(rustc_span_globals, || tls::with_global(|tcx| deadlock(tcx, &registry)))
26+
});
27+
})
28+
});
29+
}

src/librustc/ty/query/keys.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ use crate::infer::canonical::Canonical;
44
use crate::mir;
55
use crate::traits;
66
use crate::ty::fast_reject::SimplifiedType;
7-
use crate::ty::query::caches::DefaultCacheSelector;
87
use crate::ty::subst::{GenericArg, SubstsRef};
98
use crate::ty::{self, Ty, TyCtxt};
109
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
10+
use rustc_query_system::query::DefaultCacheSelector;
1111
use rustc_span::symbol::Symbol;
1212
use rustc_span::{Span, DUMMY_SP};
1313

src/librustc/ty/query/mod.rs

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
use crate::dep_graph::{self, DepConstructor, DepNode, DepNodeParams};
2+
use crate::hir::exports::Export;
3+
use crate::hir::map;
4+
use crate::infer::canonical::{self, Canonical};
5+
use crate::lint::LintLevelMap;
6+
use crate::middle::codegen_fn_attrs::CodegenFnAttrs;
7+
use crate::middle::cstore::{CrateSource, DepKind, NativeLibraryKind};
8+
use crate::middle::cstore::{ExternCrate, ForeignModule, LinkagePreference, NativeLibrary};
9+
use crate::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel};
10+
use crate::middle::lang_items::{LangItem, LanguageItems};
11+
use crate::middle::lib_features::LibFeatures;
12+
use crate::middle::privacy::AccessLevels;
13+
use crate::middle::region;
14+
use crate::middle::resolve_lifetime::{ObjectLifetimeDefault, Region, ResolveLifetimes};
15+
use crate::middle::stability::{self, DeprecationEntry};
16+
use crate::mir;
17+
use crate::mir::interpret::GlobalId;
18+
use crate::mir::interpret::{ConstEvalRawResult, ConstEvalResult, ConstValue};
19+
use crate::mir::interpret::{LitToConstError, LitToConstInput};
20+
use crate::mir::mono::CodegenUnit;
21+
use crate::traits::query::{
22+
CanonicalPredicateGoal, CanonicalProjectionGoal, CanonicalTyGoal,
23+
CanonicalTypeOpAscribeUserTypeGoal, CanonicalTypeOpEqGoal, CanonicalTypeOpNormalizeGoal,
24+
CanonicalTypeOpProvePredicateGoal, CanonicalTypeOpSubtypeGoal, NoSolution,
25+
};
26+
use crate::traits::query::{
27+
DropckOutlivesResult, DtorckConstraint, MethodAutoderefStepsResult, NormalizationResult,
28+
OutlivesBound,
29+
};
30+
use crate::traits::specialization_graph;
31+
use crate::traits::Clauses;
32+
use crate::traits::{self, Vtable};
33+
use crate::ty::steal::Steal;
34+
use crate::ty::subst::{GenericArg, SubstsRef};
35+
use crate::ty::util::AlwaysRequiresDrop;
36+
use crate::ty::{self, AdtSizedConstraint, CrateInherentImpls, ParamEnvAnd, Ty, TyCtxt};
37+
use crate::util::common::ErrorReported;
38+
use rustc_data_structures::fingerprint::Fingerprint;
39+
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
40+
use rustc_data_structures::profiling::ProfileCategory::*;
41+
use rustc_data_structures::stable_hasher::StableVec;
42+
use rustc_data_structures::svh::Svh;
43+
use rustc_data_structures::sync::Lrc;
44+
use rustc_hir as hir;
45+
use rustc_hir::def::DefKind;
46+
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, DefIdSet, LocalDefId};
47+
use rustc_hir::{Crate, HirIdSet, ItemLocalId, TraitCandidate};
48+
use rustc_index::vec::IndexVec;
49+
use rustc_session::config::{EntryFnType, OptLevel, OutputFilenames, SymbolManglingVersion};
50+
use rustc_session::CrateDisambiguator;
51+
use rustc_target::spec::PanicStrategy;
52+
53+
use rustc_ast::ast;
54+
use rustc_attr as attr;
55+
use rustc_span::symbol::Symbol;
56+
use rustc_span::{Span, DUMMY_SP};
57+
use std::borrow::Cow;
58+
use std::collections::BTreeMap;
59+
use std::ops::Deref;
60+
use std::sync::Arc;
61+
62+
#[macro_use]
63+
mod plumbing;
64+
pub(crate) use rustc_query_system::query::CycleError;
65+
use rustc_query_system::query::*;
66+
67+
mod stats;
68+
pub use self::stats::print_stats;
69+
70+
#[cfg(parallel_compiler)]
71+
mod job;
72+
#[cfg(parallel_compiler)]
73+
pub use self::job::handle_deadlock;
74+
pub use rustc_query_system::query::{QueryInfo, QueryJob, QueryJobId};
75+
76+
mod keys;
77+
use self::keys::Key;
78+
79+
mod values;
80+
use self::values::Value;
81+
82+
use rustc_query_system::query::QueryAccessors;
83+
pub use rustc_query_system::query::QueryConfig;
84+
pub(crate) use rustc_query_system::query::QueryDescription;
85+
86+
mod on_disk_cache;
87+
pub use self::on_disk_cache::OnDiskCache;
88+
89+
mod profiling_support;
90+
pub use self::profiling_support::{IntoSelfProfilingString, QueryKeyStringBuilder};
91+
92+
// Each of these queries corresponds to a function pointer field in the
93+
// `Providers` struct for requesting a value of that type, and a method
94+
// on `tcx: TyCtxt` (and `tcx.at(span)`) for doing that request in a way
95+
// which memoizes and does dep-graph tracking, wrapping around the actual
96+
// `Providers` that the driver creates (using several `rustc_*` crates).
97+
//
98+
// The result type of each query must implement `Clone`, and additionally
99+
// `ty::query::values::Value`, which produces an appropriate placeholder
100+
// (error) value if the query resulted in a query cycle.
101+
// Queries marked with `fatal_cycle` do not need the latter implementation,
102+
// as they will raise an fatal error on query cycles instead.
103+
104+
rustc_query_append! { [define_queries!][<'tcx>] }
105+
106+
/// The red/green evaluation system will try to mark a specific DepNode in the
107+
/// dependency graph as green by recursively trying to mark the dependencies of
108+
/// that `DepNode` as green. While doing so, it will sometimes encounter a `DepNode`
109+
/// where we don't know if it is red or green and we therefore actually have
110+
/// to recompute its value in order to find out. Since the only piece of
111+
/// information that we have at that point is the `DepNode` we are trying to
112+
/// re-evaluate, we need some way to re-run a query from just that. This is what
113+
/// `force_from_dep_node()` implements.
114+
///
115+
/// In the general case, a `DepNode` consists of a `DepKind` and an opaque
116+
/// GUID/fingerprint that will uniquely identify the node. This GUID/fingerprint
117+
/// is usually constructed by computing a stable hash of the query-key that the
118+
/// `DepNode` corresponds to. Consequently, it is not in general possible to go
119+
/// back from hash to query-key (since hash functions are not reversible). For
120+
/// this reason `force_from_dep_node()` is expected to fail from time to time
121+
/// because we just cannot find out, from the `DepNode` alone, what the
122+
/// corresponding query-key is and therefore cannot re-run the query.
123+
///
124+
/// The system deals with this case letting `try_mark_green` fail which forces
125+
/// the root query to be re-evaluated.
126+
///
127+
/// Now, if `force_from_dep_node()` would always fail, it would be pretty useless.
128+
/// Fortunately, we can use some contextual information that will allow us to
129+
/// reconstruct query-keys for certain kinds of `DepNode`s. In particular, we
130+
/// enforce by construction that the GUID/fingerprint of certain `DepNode`s is a
131+
/// valid `DefPathHash`. Since we also always build a huge table that maps every
132+
/// `DefPathHash` in the current codebase to the corresponding `DefId`, we have
133+
/// everything we need to re-run the query.
134+
///
135+
/// Take the `mir_validated` query as an example. Like many other queries, it
136+
/// just has a single parameter: the `DefId` of the item it will compute the
137+
/// validated MIR for. Now, when we call `force_from_dep_node()` on a `DepNode`
138+
/// with kind `MirValidated`, we know that the GUID/fingerprint of the `DepNode`
139+
/// is actually a `DefPathHash`, and can therefore just look up the corresponding
140+
/// `DefId` in `tcx.def_path_hash_to_def_id`.
141+
///
142+
/// When you implement a new query, it will likely have a corresponding new
143+
/// `DepKind`, and you'll have to support it here in `force_from_dep_node()`. As
144+
/// a rule of thumb, if your query takes a `DefId` or `LocalDefId` as sole parameter,
145+
/// then `force_from_dep_node()` should not fail for it. Otherwise, you can just
146+
/// add it to the "We don't have enough information to reconstruct..." group in
147+
/// the match below.
148+
pub fn force_from_dep_node<'tcx>(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> bool {
149+
// We must avoid ever having to call `force_from_dep_node()` for a
150+
// `DepNode::codegen_unit`:
151+
// Since we cannot reconstruct the query key of a `DepNode::codegen_unit`, we
152+
// would always end up having to evaluate the first caller of the
153+
// `codegen_unit` query that *is* reconstructible. This might very well be
154+
// the `compile_codegen_unit` query, thus re-codegenning the whole CGU just
155+
// to re-trigger calling the `codegen_unit` query with the right key. At
156+
// that point we would already have re-done all the work we are trying to
157+
// avoid doing in the first place.
158+
// The solution is simple: Just explicitly call the `codegen_unit` query for
159+
// each CGU, right after partitioning. This way `try_mark_green` will always
160+
// hit the cache instead of having to go through `force_from_dep_node`.
161+
// This assertion makes sure, we actually keep applying the solution above.
162+
debug_assert!(
163+
dep_node.kind != crate::dep_graph::DepKind::codegen_unit,
164+
"calling force_from_dep_node() on DepKind::codegen_unit"
165+
);
166+
167+
if !dep_node.kind.can_reconstruct_query_key() {
168+
return false;
169+
}
170+
171+
rustc_dep_node_force!([dep_node, tcx]
172+
// These are inputs that are expected to be pre-allocated and that
173+
// should therefore always be red or green already.
174+
crate::dep_graph::DepKind::CrateMetadata |
175+
176+
// These are anonymous nodes.
177+
crate::dep_graph::DepKind::TraitSelect |
178+
179+
// We don't have enough information to reconstruct the query key of
180+
// these.
181+
crate::dep_graph::DepKind::CompileCodegenUnit => {
182+
bug!("force_from_dep_node: encountered {:?}", dep_node)
183+
}
184+
);
185+
186+
false
187+
}
188+
189+
pub(crate) fn try_load_from_on_disk_cache<'tcx>(tcx: TyCtxt<'tcx>, dep_node: &DepNode) {
190+
rustc_dep_node_try_load_from_on_disk_cache!(dep_node, tcx)
191+
}

src/librustc/ty/query/on_disk_cache.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -994,7 +994,7 @@ fn encode_query_results<'a, 'tcx, Q, E>(
994994
query_result_index: &mut EncodedQueryResultIndex,
995995
) -> Result<(), E::Error>
996996
where
997-
Q: super::config::QueryDescription<TyCtxt<'tcx>>,
997+
Q: super::QueryDescription<TyCtxt<'tcx>>,
998998
Q::Value: Encodable,
999999
E: 'a + TyEncoder,
10001000
{

0 commit comments

Comments
 (0)