Skip to content

Commit fc4ae1d

Browse files
committed
Add query for loading crates.
This commit adds a query that can be used to load crates in later phases of the compiler for the purpose of diagnostics.
1 parent c6f6468 commit fc4ae1d

File tree

8 files changed

+38
-4
lines changed

8 files changed

+38
-4
lines changed

src/librustc/dep_graph/dep_node.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ use crate::ich::{Fingerprint, StableHashingContext};
5858
use rustc_data_structures::stable_hasher::{StableHasher, HashStable};
5959
use std::fmt;
6060
use std::hash::Hash;
61-
use syntax_pos::symbol::InternedString;
61+
use syntax_pos::symbol::{InternedString, Symbol};
6262
use crate::traits;
6363
use crate::traits::query::{
6464
CanonicalProjectionGoal, CanonicalTyGoal, CanonicalTypeOpAscribeUserTypeGoal,
@@ -593,6 +593,7 @@ define_dep_nodes!( <'tcx>
593593
[input] CrateDisambiguator(CrateNum),
594594
[input] CrateHash(CrateNum),
595595
[input] OriginalCrateName(CrateNum),
596+
[input] MaybeLoadExternCrate(Symbol),
596597
[input] ExtraFileName(CrateNum),
597598

598599
[] ImplementationsOfTrait { krate: CrateNum, trait_id: DefId },

src/librustc/middle/cstore.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ pub trait CrateStore {
201201
fn extern_mod_stmt_cnum_untracked(&self, emod_id: ast::NodeId) -> Option<CrateNum>;
202202
fn item_generics_cloned_untracked(&self, def: DefId, sess: &Session) -> ty::Generics;
203203
fn postorder_cnums_untracked(&self) -> Vec<CrateNum>;
204+
fn maybe_load_extern_crate_untracked(
205+
&self, sess: &Session, name: Symbol
206+
) -> Option<CrateNum>;
204207

205208
// This is basically a 1-based range of ints, which is a little
206209
// silly - I may fix that.

src/librustc/ty/context.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2989,6 +2989,9 @@ pub fn provide(providers: &mut ty::query::Providers<'_>) {
29892989
assert_eq!(id, LOCAL_CRATE);
29902990
tcx.crate_name
29912991
};
2992+
providers.maybe_load_extern_crate = |tcx, name| {
2993+
tcx.cstore.maybe_load_extern_crate_untracked(tcx.sess, name)
2994+
};
29922995
providers.get_lib_features = |tcx, id| {
29932996
assert_eq!(id, LOCAL_CRATE);
29942997
Lrc::new(middle::lib_features::collect(tcx))

src/librustc/ty/query/config.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::util::profiling::ProfileCategory;
1818
use std::borrow::Cow;
1919
use std::hash::Hash;
2020
use std::fmt::Debug;
21-
use syntax_pos::symbol::InternedString;
21+
use syntax_pos::symbol::{Symbol, InternedString};
2222
use rustc_data_structures::sync::Lock;
2323
use rustc_data_structures::fingerprint::Fingerprint;
2424
use crate::ich::StableHashingContext;
@@ -706,6 +706,12 @@ impl<'tcx> QueryDescription<'tcx> for queries::original_crate_name<'tcx> {
706706
}
707707
}
708708

709+
impl<'tcx> QueryDescription<'tcx> for queries::maybe_load_extern_crate<'tcx> {
710+
fn describe(_tcx: TyCtxt<'_, '_, '_>, name: Symbol) -> Cow<'static, str> {
711+
format!("loading crate with name {}", name).into()
712+
}
713+
}
714+
709715
impl<'tcx> QueryDescription<'tcx> for queries::extra_filename<'tcx> {
710716
fn describe(_tcx: TyCtxt<'_, '_, '_>, _: CrateNum) -> Cow<'static, str> {
711717
"looking up the extra filename for a crate".into()

src/librustc/ty/query/keys.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::mir;
1111
use std::fmt::Debug;
1212
use std::hash::Hash;
1313
use syntax_pos::{Span, DUMMY_SP};
14-
use syntax_pos::symbol::InternedString;
14+
use syntax_pos::symbol::{Symbol, InternedString};
1515

1616
/// The `Key` trait controls what types can legally be used as the key
1717
/// for a query.
@@ -190,6 +190,15 @@ impl Key for InternedString {
190190
}
191191
}
192192

193+
impl Key for Symbol {
194+
fn query_crate(&self) -> CrateNum {
195+
LOCAL_CRATE
196+
}
197+
fn default_span(&self, _tcx: TyCtxt<'_, '_, '_>) -> Span {
198+
DUMMY_SP
199+
}
200+
}
201+
193202
/// Canonical query goals correspond to abstract trait operations that
194203
/// are not tied to any crate in particular.
195204
impl<'tcx, T> Key for Canonical<'tcx, T>

src/librustc/ty/query/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@ define_queries! { <'tcx>
493493
[] fn crate_disambiguator: CrateDisambiguator(CrateNum) -> CrateDisambiguator,
494494
[] fn crate_hash: CrateHash(CrateNum) -> Svh,
495495
[] fn original_crate_name: OriginalCrateName(CrateNum) -> Symbol,
496+
[] fn maybe_load_extern_crate: MaybeLoadExternCrate(Symbol) -> Option<CrateNum>,
496497
[] fn extra_filename: ExtraFileName(CrateNum) -> String,
497498
},
498499

src/librustc/ty/query/plumbing.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,6 +1242,7 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
12421242
DepKind::MethodAutoderefSteps |
12431243
DepKind::InstanceDefSizeEstimate |
12441244
DepKind::ProgramClausesForEnv |
1245+
DepKind::MaybeLoadExternCrate |
12451246

12461247
// This one should never occur in this context
12471248
DepKind::Null => {

src/librustc_metadata/cstore_impl.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::cstore::{self, LoadedMacro};
2+
use crate::creader::CrateLoader;
23
use crate::encoder;
34
use crate::link_args;
45
use crate::native_libs;
@@ -31,7 +32,7 @@ use syntax::edition::Edition;
3132
use syntax::parse::source_file_to_stream;
3233
use syntax::parse::parser::emit_unclosed_delims;
3334
use syntax::symbol::Symbol;
34-
use syntax_pos::{Span, NO_EXPANSION, FileName};
35+
use syntax_pos::{Span, DUMMY_SP, NO_EXPANSION, FileName};
3536
use rustc_data_structures::bit_set::BitSet;
3637

3738
macro_rules! provide {
@@ -541,6 +542,15 @@ impl CrateStore for cstore::CStore {
541542
self.do_postorder_cnums_untracked()
542543
}
543544

545+
fn maybe_load_extern_crate_untracked(
546+
&self,
547+
sess: &Session,
548+
name: Symbol
549+
) -> Option<CrateNum> {
550+
let mut crate_loader = CrateLoader::new(sess, self, "");
551+
crate_loader.maybe_process_path_extern(name, DUMMY_SP)
552+
}
553+
544554
fn encode_metadata<'a, 'tcx>(&self,
545555
tcx: TyCtxt<'a, 'tcx, 'tcx>)
546556
-> EncodedMetadata

0 commit comments

Comments
 (0)