Skip to content

Commit 3a8f125

Browse files
authored
Rollup merge of rust-lang#68718 - Aaron1011:move-def-hir-span, r=petrochenkov
Move `rustc_hir::def_id` to `rustc_span::def_id` This will allow `HygieneData` to refer to `DefId` and `DefIndex`, which will enable proper serialization of Span hygiene information. This also reduces the number of things imported from `rustc_hir`, which might make it easier to remove dependencies on it.
2 parents 57b72b8 + 619051e commit 3a8f125

File tree

6 files changed

+27
-25
lines changed

6 files changed

+27
-25
lines changed

src/librustc/ich/hcx.rs

+6
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,12 @@ impl<'a> rustc_span::HashStableContext for StableHashingContext<'a> {
249249
self.hash_spans
250250
}
251251

252+
#[inline]
253+
fn hash_def_id(&mut self, def_id: DefId, hasher: &mut StableHasher) {
254+
let hcx = self;
255+
hcx.def_path_hash(def_id).hash_stable(hcx, hasher);
256+
}
257+
252258
fn byte_pos_to_line_and_col(
253259
&mut self,
254260
byte: BytePos,

src/librustc/ich/impls_hir.rs

-6
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@ use smallvec::SmallVec;
1111
use std::mem;
1212

1313
impl<'ctx> rustc_hir::HashStableContext for StableHashingContext<'ctx> {
14-
#[inline]
15-
fn hash_def_id(&mut self, def_id: DefId, hasher: &mut StableHasher) {
16-
let hcx = self;
17-
hcx.def_path_hash(def_id).hash_stable(hcx, hasher);
18-
}
19-
2014
#[inline]
2115
fn hash_hir_id(&mut self, hir_id: hir::HirId, hasher: &mut StableHasher) {
2216
let hcx = self;

src/librustc_hir/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
extern crate rustc_data_structures;
1313

1414
pub mod def;
15-
pub mod def_id;
15+
pub use rustc_span::def_id;
1616
mod hir;
1717
pub mod hir_id;
1818
pub mod intravisit;

src/librustc_hir/stable_hash_impls.rs

-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
22

3-
use crate::def_id::DefId;
43
use crate::hir::{BodyId, Expr, ImplItemId, ItemId, Mod, TraitItemId, Ty, VisibilityKind};
54
use crate::hir_id::HirId;
65

76
/// Requirements for a `StableHashingContext` to be used in this crate.
87
/// This is a hack to allow using the `HashStable_Generic` derive macro
98
/// instead of implementing everything in librustc.
109
pub trait HashStableContext: syntax::HashStableContext + rustc_target::HashStableContext {
11-
fn hash_def_id(&mut self, _: DefId, hasher: &mut StableHasher);
1210
fn hash_hir_id(&mut self, _: HirId, hasher: &mut StableHasher);
1311
fn hash_body_id(&mut self, _: BodyId, hasher: &mut StableHasher);
1412
fn hash_reference_to_item(&mut self, _: HirId, hasher: &mut StableHasher);
@@ -24,12 +22,6 @@ impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for HirId {
2422
}
2523
}
2624

27-
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for DefId {
28-
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
29-
hcx.hash_def_id(*self, hasher)
30-
}
31-
}
32-
3325
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for BodyId {
3426
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
3527
hcx.hash_body_id(*self, hasher)

src/librustc_hir/def_id.rs src/librustc_span/def_id.rs

+17-9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use crate::HashStableContext;
2+
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
13
use rustc_data_structures::AtomicRef;
24
use rustc_index::vec::Idx;
35
use rustc_serialize::{Decoder, Encoder};
@@ -18,15 +20,6 @@ pub enum CrateNum {
1820
Index(CrateId),
1921
}
2022

21-
impl ::std::fmt::Debug for CrateNum {
22-
fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
23-
match self {
24-
CrateNum::Index(id) => write!(fmt, "crate{}", id.private),
25-
CrateNum::ReservedForIncrCompCache => write!(fmt, "crate for decoding incr comp cache"),
26-
}
27-
}
28-
}
29-
3023
/// Item definitions in the currently-compiled crate would have the `CrateNum`
3124
/// `LOCAL_CRATE` in their `DefId`.
3225
pub const LOCAL_CRATE: CrateNum = CrateNum::Index(CrateId::from_u32_const(0));
@@ -100,6 +93,15 @@ impl rustc_serialize::UseSpecializedDecodable for CrateNum {
10093
}
10194
}
10295

96+
impl ::std::fmt::Debug for CrateNum {
97+
fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
98+
match self {
99+
CrateNum::Index(id) => write!(fmt, "crate{}", id.private),
100+
CrateNum::ReservedForIncrCompCache => write!(fmt, "crate for decoding incr comp cache"),
101+
}
102+
}
103+
}
104+
103105
rustc_index::newtype_index! {
104106
/// A DefIndex is an index into the hir-map for a crate, identifying a
105107
/// particular definition. It should really be considered an interned
@@ -207,3 +209,9 @@ impl fmt::Debug for LocalDefId {
207209

208210
impl rustc_serialize::UseSpecializedEncodable for LocalDefId {}
209211
impl rustc_serialize::UseSpecializedDecodable for LocalDefId {}
212+
213+
impl<CTX: HashStableContext> HashStable<CTX> for DefId {
214+
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
215+
hcx.hash_def_id(*self, hasher)
216+
}
217+
}

src/librustc_span/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ use edition::Edition;
2222
pub mod hygiene;
2323
use hygiene::Transparency;
2424
pub use hygiene::{DesugaringKind, ExpnData, ExpnId, ExpnKind, MacroKind, SyntaxContext};
25-
25+
pub mod def_id;
26+
use def_id::DefId;
2627
mod span_encoding;
2728
pub use span_encoding::{Span, DUMMY_SP};
2829

@@ -1558,6 +1559,7 @@ fn lookup_line(lines: &[BytePos], pos: BytePos) -> isize {
15581559
/// instead of implementing everything in librustc.
15591560
pub trait HashStableContext {
15601561
fn hash_spans(&self) -> bool;
1562+
fn hash_def_id(&mut self, _: DefId, hasher: &mut StableHasher);
15611563
fn byte_pos_to_line_and_col(
15621564
&mut self,
15631565
byte: BytePos,

0 commit comments

Comments
 (0)