Skip to content

Commit 253fc0e

Browse files
committed
Auto merge of #65698 - msizanoen1:dual-proc-macro-hash, r=petrochenkov
Dual proc macro hash This PR changes current `-Z dual-proc-macro` mechanism from resolving only by name to including the hash of the host crate inside the transistive dependency information to prevent name conflicts. Fix partially #62558
2 parents 472e678 + 8a0d233 commit 253fc0e

File tree

11 files changed

+35
-8
lines changed

11 files changed

+35
-8
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3393,6 +3393,7 @@ dependencies = [
33933393
"serde",
33943394
"serde_json",
33953395
"smallvec",
3396+
"syn 0.15.35",
33963397
"url 2.1.0",
33973398
"winapi 0.3.6",
33983399
]

src/librustc/middle/cstore.rs

+1
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ pub trait CrateStore {
216216
fn crate_is_private_dep_untracked(&self, cnum: CrateNum) -> bool;
217217
fn crate_disambiguator_untracked(&self, cnum: CrateNum) -> CrateDisambiguator;
218218
fn crate_hash_untracked(&self, cnum: CrateNum) -> Svh;
219+
fn crate_host_hash_untracked(&self, cnum: CrateNum) -> Option<Svh>;
219220
fn item_generics_cloned_untracked(&self, def: DefId, sess: &Session) -> ty::Generics;
220221
fn postorder_cnums_untracked(&self) -> Vec<CrateNum>;
221222

src/librustc/query/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,10 @@ rustc_queries! {
754754
eval_always
755755
desc { "looking up the hash a crate" }
756756
}
757+
query crate_host_hash(_: CrateNum) -> Option<Svh> {
758+
eval_always
759+
desc { "looking up the hash of a host version of a crate" }
760+
}
757761
query original_crate_name(_: CrateNum) -> Symbol {
758762
eval_always
759763
desc { "looking up the original name a crate" }

src/librustc/ty/context.rs

+4
Original file line numberDiff line numberDiff line change
@@ -3015,6 +3015,10 @@ pub fn provide(providers: &mut ty::query::Providers<'_>) {
30153015
assert_eq!(cnum, LOCAL_CRATE);
30163016
tcx.arena.alloc_slice(&tcx.cstore.crates_untracked())
30173017
};
3018+
providers.crate_host_hash = |tcx, cnum| {
3019+
assert_ne!(cnum, LOCAL_CRATE);
3020+
tcx.cstore.crate_host_hash_untracked(cnum)
3021+
};
30183022
providers.postorder_cnums = |tcx, cnum| {
30193023
assert_eq!(cnum, LOCAL_CRATE);
30203024
tcx.arena.alloc_slice(&tcx.cstore.postorder_cnums_untracked())

src/librustc_metadata/creader.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ impl<'a> CrateLoader<'a> {
191191

192192
let Library { source, metadata } = lib;
193193
let crate_root = metadata.get_root();
194+
let host_hash = host_lib.as_ref().map(|lib| lib.metadata.get_root().hash);
194195
self.verify_no_symbol_conflicts(span, &crate_root);
195196

196197
let private_dep = self.sess.opts.externs.get(&name.as_str())
@@ -245,6 +246,7 @@ impl<'a> CrateLoader<'a> {
245246
def_path_table,
246247
trait_impls,
247248
root: crate_root,
249+
host_hash,
248250
blob: metadata,
249251
cnum_map,
250252
cnum,
@@ -283,9 +285,7 @@ impl<'a> CrateLoader<'a> {
283285
LoadResult::Previous(cnum) => return Some((LoadResult::Previous(cnum), None)),
284286
LoadResult::Loaded(library) => Some(LoadResult::Loaded(library))
285287
};
286-
// Don't look for a matching hash when looking for the host crate.
287-
// It won't be the same as the target crate hash
288-
locate_ctxt.hash = None;
288+
locate_ctxt.hash = locate_ctxt.host_hash;
289289
// Use the locate_ctxt when looking for the host proc macro crate, as that is required
290290
// so we want it to affect the error message
291291
(locate_ctxt, result)
@@ -334,10 +334,15 @@ impl<'a> CrateLoader<'a> {
334334
dep: Option<(&'b CratePaths, &'b CrateDep)>,
335335
) -> Result<CrateNum, LoadError<'b>> {
336336
info!("resolving crate `{}`", name);
337-
let (root, hash, extra_filename, path_kind) = match dep {
338-
Some((root, dep)) =>
339-
(Some(root), Some(&dep.hash), Some(&dep.extra_filename[..]), PathKind::Dependency),
340-
None => (None, None, None, PathKind::Crate),
337+
let (root, hash, host_hash, extra_filename, path_kind) = match dep {
338+
Some((root, dep)) => (
339+
Some(root),
340+
Some(&dep.hash),
341+
dep.host_hash.as_ref(),
342+
Some(&dep.extra_filename[..]),
343+
PathKind::Dependency
344+
),
345+
None => (None, None, None, None, PathKind::Crate),
341346
};
342347
let result = if let Some(cnum) = self.existing_match(name, hash, path_kind) {
343348
(LoadResult::Previous(cnum), None)
@@ -348,6 +353,7 @@ impl<'a> CrateLoader<'a> {
348353
span,
349354
crate_name: name,
350355
hash,
356+
host_hash,
351357
extra_filename,
352358
filesearch: self.sess.target_filesearch(path_kind),
353359
target: &self.sess.target.target,

src/librustc_metadata/cstore.rs

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use rustc::mir::interpret::AllocDecodingState;
1010
use rustc_index::vec::IndexVec;
1111
use rustc::util::nodemap::FxHashMap;
1212
use rustc_data_structures::sync::{Lrc, Lock, MetadataRef, Once, AtomicCell};
13+
use rustc_data_structures::svh::Svh;
1314
use syntax::ast;
1415
use syntax::edition::Edition;
1516
use syntax_expand::base::SyntaxExtension;
@@ -87,6 +88,8 @@ crate struct CrateMetadata {
8788
/// Whether or not this crate should be consider a private dependency
8889
/// for purposes of the 'exported_private_dependencies' lint
8990
crate private_dep: bool,
91+
/// The hash for the host proc macro. Used to support `-Z dual-proc-macro`.
92+
crate host_hash: Option<Svh>,
9093

9194
// --- Data used only for improving diagnostics ---
9295

src/librustc_metadata/cstore_impl.rs

+4
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,10 @@ impl CrateStore for cstore::CStore {
505505
self.get_crate_data(cnum).root.hash
506506
}
507507

508+
fn crate_host_hash_untracked(&self, cnum: CrateNum) -> Option<Svh> {
509+
self.get_crate_data(cnum).host_hash
510+
}
511+
508512
/// Returns the `DefKey` for a given `DefId`. This indicates the
509513
/// parent `DefId` as well as some idea of what kind of data the
510514
/// `DefId` refers to.

src/librustc_metadata/encoder.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1418,6 +1418,7 @@ impl EncodeContext<'tcx> {
14181418
let dep = CrateDep {
14191419
name: self.tcx.original_crate_name(cnum),
14201420
hash: self.tcx.crate_hash(cnum),
1421+
host_hash: self.tcx.crate_host_hash(cnum),
14211422
kind: self.tcx.dep_kind(cnum),
14221423
extra_filename: self.tcx.extra_filename(cnum),
14231424
};

src/librustc_metadata/locator.rs

+2
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ crate struct Context<'a> {
258258
pub span: Span,
259259
pub crate_name: Symbol,
260260
pub hash: Option<&'a Svh>,
261+
pub host_hash: Option<&'a Svh>,
261262
pub extra_filename: Option<&'a str>,
262263
// points to either self.sess.target.target or self.sess.host, must match triple
263264
pub target: &'a Target,
@@ -929,6 +930,7 @@ pub fn find_plugin_registrar(
929930
span,
930931
crate_name: name,
931932
hash: None,
933+
host_hash: None,
932934
extra_filename: None,
933935
filesearch: sess.host_filesearch(PathKind::Crate),
934936
target: &sess.host,

src/librustc_metadata/schema.rs

+1
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ crate struct CrateRoot<'tcx> {
217217
crate struct CrateDep {
218218
pub name: ast::Name,
219219
pub hash: Svh,
220+
pub host_hash: Option<Svh>,
220221
pub kind: DepKind,
221222
pub extra_filename: String,
222223
}

src/tools/rustc-workspace-hack/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ serde = { version = "1.0.82", features = ['derive'] }
6464
serde_json = { version = "1.0.31", features = ["raw_value"] }
6565
smallvec = { version = "0.6", features = ['union', 'may_dangle'] }
6666
url = { version = "2.0", features = ['serde'] }
67-
67+
syn = { version = "0.15", features = ['full'] }
6868

6969
[target.'cfg(not(windows))'.dependencies]
7070
openssl = { version = "0.10.12", optional = true }

0 commit comments

Comments
 (0)