Skip to content

Commit 1f13a4f

Browse files
committed
Store idents for DefPathData into crate metadata
Previously, we threw away the `Span` associated with a definition's identifier when we encoded crate metadata, causing us to lose location and hygiene information. We now store the identifier's `Span` in the crate metadata. When we decode items from the metadata, we combine the name and span back into an `Ident`. This improves the output of several tests, which previously had messages suppressed due to dummy spans. This is a prerequisite for #68686, since throwing away a `Span` means that we lose hygiene information.
1 parent f4c675c commit 1f13a4f

28 files changed

+224
-39
lines changed

src/librustc_metadata/rmeta/decoder.rs

+31-25
Original file line numberDiff line numberDiff line change
@@ -509,14 +509,6 @@ impl<'a, 'tcx> SpecializedDecoder<Span> for DecodeContext<'a, 'tcx> {
509509
}
510510
}
511511

512-
impl SpecializedDecoder<Ident> for DecodeContext<'_, '_> {
513-
fn specialized_decode(&mut self) -> Result<Ident, Self::Error> {
514-
// FIXME(jseyfried): intercrate hygiene
515-
516-
Ok(Ident::with_dummy_span(Symbol::decode(self)?))
517-
}
518-
}
519-
520512
impl<'a, 'tcx> SpecializedDecoder<Fingerprint> for DecodeContext<'a, 'tcx> {
521513
fn specialized_decode(&mut self) -> Result<Fingerprint, Self::Error> {
522514
Fingerprint::decode_opaque(&mut self.opaque)
@@ -663,15 +655,27 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
663655
&self.raw_proc_macros.unwrap()[pos]
664656
}
665657

666-
fn item_name(&self, item_index: DefIndex) -> Symbol {
658+
fn item_ident(&self, item_index: DefIndex, sess: &Session) -> Ident {
667659
if !self.is_proc_macro(item_index) {
668-
self.def_key(item_index)
660+
let name = self
661+
.def_key(item_index)
669662
.disambiguated_data
670663
.data
671664
.get_opt_name()
672-
.expect("no name in item_name")
665+
.expect("no name in item_ident");
666+
let span = self
667+
.root
668+
.per_def
669+
.ident_span
670+
.get(self, item_index)
671+
.map(|data| data.decode((self, sess)))
672+
.unwrap_or_else(|| panic!("Missing ident span for {:?} ({:?})", name, item_index));
673+
Ident::new(name, span)
673674
} else {
674-
Symbol::intern(self.raw_proc_macro(item_index).name())
675+
Ident::new(
676+
Symbol::intern(self.raw_proc_macro(item_index).name()),
677+
self.get_span(item_index, sess),
678+
)
675679
}
676680
}
677681

@@ -750,6 +754,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
750754
kind: &EntryKind,
751755
index: DefIndex,
752756
parent_did: DefId,
757+
sess: &Session,
753758
) -> ty::VariantDef {
754759
let data = match kind {
755760
EntryKind::Variant(data) | EntryKind::Struct(data, _) | EntryKind::Union(data, _) => {
@@ -771,7 +776,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
771776

772777
ty::VariantDef::new(
773778
tcx,
774-
Ident::with_dummy_span(self.item_name(index)),
779+
self.item_ident(index, sess),
775780
variant_did,
776781
ctor_did,
777782
data.discr,
@@ -783,7 +788,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
783788
.decode(self)
784789
.map(|index| ty::FieldDef {
785790
did: self.local_def_id(index),
786-
ident: Ident::with_dummy_span(self.item_name(index)),
791+
ident: self.item_ident(index, sess),
787792
vis: self.get_visibility(index),
788793
})
789794
.collect(),
@@ -812,10 +817,10 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
812817
.get(self, item_id)
813818
.unwrap_or(Lazy::empty())
814819
.decode(self)
815-
.map(|index| self.get_variant(tcx, &self.kind(index), index, did))
820+
.map(|index| self.get_variant(tcx, &self.kind(index), index, did, tcx.sess))
816821
.collect()
817822
} else {
818-
std::iter::once(self.get_variant(tcx, &kind, item_id, did)).collect()
823+
std::iter::once(self.get_variant(tcx, &kind, item_id, did, tcx.sess)).collect()
819824
};
820825

821826
tcx.alloc_adt_def(did, adt_kind, variants, repr)
@@ -1007,7 +1012,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
10071012
if let Some(kind) = self.def_kind(child_index) {
10081013
callback(Export {
10091014
res: Res::Def(kind, self.local_def_id(child_index)),
1010-
ident: Ident::with_dummy_span(self.item_name(child_index)),
1015+
ident: self.item_ident(child_index, sess),
10111016
vis: self.get_visibility(child_index),
10121017
span: self
10131018
.root
@@ -1028,10 +1033,11 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
10281033

10291034
let def_key = self.def_key(child_index);
10301035
let span = self.get_span(child_index, sess);
1031-
if let (Some(kind), Some(name)) =
1032-
(self.def_kind(child_index), def_key.disambiguated_data.data.get_opt_name())
1033-
{
1034-
let ident = Ident::with_dummy_span(name);
1036+
if let (Some(kind), true) = (
1037+
self.def_kind(child_index),
1038+
def_key.disambiguated_data.data.get_opt_name().is_some(),
1039+
) {
1040+
let ident = self.item_ident(child_index, sess);
10351041
let vis = self.get_visibility(child_index);
10361042
let def_id = self.local_def_id(child_index);
10371043
let res = Res::Def(kind, def_id);
@@ -1138,10 +1144,10 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11381144
}
11391145
}
11401146

1141-
fn get_associated_item(&self, id: DefIndex) -> ty::AssocItem {
1147+
fn get_associated_item(&self, id: DefIndex, sess: &Session) -> ty::AssocItem {
11421148
let def_key = self.def_key(id);
11431149
let parent = self.local_def_id(def_key.parent.unwrap());
1144-
let name = def_key.disambiguated_data.data.get_opt_name().unwrap();
1150+
let ident = self.item_ident(id, sess);
11451151

11461152
let (kind, container, has_self) = match self.kind(id) {
11471153
EntryKind::AssocConst(container, _, _) => (ty::AssocKind::Const, container, false),
@@ -1155,7 +1161,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11551161
};
11561162

11571163
ty::AssocItem {
1158-
ident: Ident::with_dummy_span(name),
1164+
ident,
11591165
kind,
11601166
vis: self.get_visibility(id),
11611167
defaultness: container.defaultness(),
@@ -1219,7 +1225,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
12191225
.get(self, id)
12201226
.unwrap_or(Lazy::empty())
12211227
.decode(self)
1222-
.map(|index| respan(self.get_span(index, sess), self.item_name(index)))
1228+
.map(|index| respan(self.get_span(index, sess), self.item_ident(index, sess).name))
12231229
.collect()
12241230
}
12251231

src/librustc_metadata/rmeta/decoder/cstore_impl.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
110110
|child| result.push(child.res.def_id()), tcx.sess);
111111
tcx.arena.alloc_slice(&result)
112112
}
113-
associated_item => { cdata.get_associated_item(def_id.index) }
113+
associated_item => { cdata.get_associated_item(def_id.index, tcx.sess) }
114114
impl_trait_ref => { cdata.get_impl_trait(def_id.index, tcx) }
115115
impl_polarity => { cdata.get_impl_polarity(def_id.index) }
116116
coerce_unsized_info => {
@@ -442,8 +442,8 @@ impl CStore {
442442
)
443443
}
444444

445-
pub fn associated_item_cloned_untracked(&self, def: DefId) -> ty::AssocItem {
446-
self.get_crate_data(def.krate).get_associated_item(def.index)
445+
pub fn associated_item_cloned_untracked(&self, def: DefId, sess: &Session) -> ty::AssocItem {
446+
self.get_crate_data(def.krate).get_associated_item(def.index, sess)
447447
}
448448

449449
pub fn crate_source_untracked(&self, cnum: CrateNum) -> CrateSource {

src/librustc_metadata/rmeta/encoder.rs

+14-9
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc::traits::specialization_graph;
1313
use rustc::ty::codec::{self as ty_codec, TyEncoder};
1414
use rustc::ty::layout::VariantIdx;
1515
use rustc::ty::{self, SymbolName, Ty, TyCtxt};
16-
use rustc_ast::ast;
16+
use rustc_ast::ast::{self, Ident};
1717
use rustc_ast::attr;
1818
use rustc_data_structures::fingerprint::Fingerprint;
1919
use rustc_data_structures::fx::FxHashMap;
@@ -29,7 +29,7 @@ use rustc_index::vec::Idx;
2929
use rustc_serialize::{opaque, Encodable, Encoder, SpecializedEncoder};
3030
use rustc_session::config::{self, CrateType};
3131
use rustc_span::source_map::Spanned;
32-
use rustc_span::symbol::{kw, sym, Ident, Symbol};
32+
use rustc_span::symbol::{kw, sym, Symbol};
3333
use rustc_span::{self, ExternalSource, FileName, SourceFile, Span};
3434
use std::hash::Hash;
3535
use std::num::NonZeroUsize;
@@ -219,13 +219,6 @@ impl<'tcx> SpecializedEncoder<Span> for EncodeContext<'tcx> {
219219
}
220220
}
221221

222-
impl SpecializedEncoder<Ident> for EncodeContext<'tcx> {
223-
fn specialized_encode(&mut self, ident: &Ident) -> Result<(), Self::Error> {
224-
// FIXME(jseyfried): intercrate hygiene
225-
ident.name.encode(self)
226-
}
227-
}
228-
229222
impl<'tcx> SpecializedEncoder<LocalDefId> for EncodeContext<'tcx> {
230223
#[inline]
231224
fn specialized_encode(&mut self, def_id: &LocalDefId) -> Result<(), Self::Error> {
@@ -631,6 +624,7 @@ impl EncodeContext<'tcx> {
631624
assert!(f.did.is_local());
632625
f.did.index
633626
}));
627+
self.encode_ident_span(def_id, variant.ident);
634628
self.encode_stability(def_id);
635629
self.encode_deprecation(def_id);
636630
self.encode_item_type(def_id);
@@ -733,6 +727,7 @@ impl EncodeContext<'tcx> {
733727
record!(self.per_def.visibility[def_id] <- field.vis);
734728
record!(self.per_def.span[def_id] <- self.tcx.def_span(def_id));
735729
record!(self.per_def.attributes[def_id] <- variant_data.fields()[field_index].attrs);
730+
self.encode_ident_span(def_id, field.ident);
736731
self.encode_stability(def_id);
737732
self.encode_deprecation(def_id);
738733
self.encode_item_type(def_id);
@@ -867,6 +862,7 @@ impl EncodeContext<'tcx> {
867862
record!(self.per_def.visibility[def_id] <- trait_item.vis);
868863
record!(self.per_def.span[def_id] <- ast_item.span);
869864
record!(self.per_def.attributes[def_id] <- ast_item.attrs);
865+
self.encode_ident_span(def_id, ast_item.ident);
870866
self.encode_stability(def_id);
871867
self.encode_const_stability(def_id);
872868
self.encode_deprecation(def_id);
@@ -948,6 +944,7 @@ impl EncodeContext<'tcx> {
948944
record!(self.per_def.visibility[def_id] <- impl_item.vis);
949945
record!(self.per_def.span[def_id] <- ast_item.span);
950946
record!(self.per_def.attributes[def_id] <- ast_item.attrs);
947+
self.encode_ident_span(def_id, impl_item.ident);
951948
self.encode_stability(def_id);
952949
self.encode_const_stability(def_id);
953950
self.encode_deprecation(def_id);
@@ -1051,6 +1048,8 @@ impl EncodeContext<'tcx> {
10511048

10521049
debug!("EncodeContext::encode_info_for_item({:?})", def_id);
10531050

1051+
self.encode_ident_span(def_id, item.ident);
1052+
10541053
record!(self.per_def.kind[def_id] <- match item.kind {
10551054
hir::ItemKind::Static(_, hir::Mutability::Mut, _) => EntryKind::MutStatic,
10561055
hir::ItemKind::Static(_, hir::Mutability::Not, _) => EntryKind::ImmStatic,
@@ -1275,6 +1274,7 @@ impl EncodeContext<'tcx> {
12751274
record!(self.per_def.visibility[def_id] <- ty::Visibility::Public);
12761275
record!(self.per_def.span[def_id] <- macro_def.span);
12771276
record!(self.per_def.attributes[def_id] <- macro_def.attrs);
1277+
self.encode_ident_span(def_id, macro_def.ident);
12781278
self.encode_stability(def_id);
12791279
self.encode_deprecation(def_id);
12801280
}
@@ -1519,6 +1519,7 @@ impl EncodeContext<'tcx> {
15191519
ty::Visibility::from_hir(&nitem.vis, nitem.hir_id, self.tcx));
15201520
record!(self.per_def.span[def_id] <- nitem.span);
15211521
record!(self.per_def.attributes[def_id] <- nitem.attrs);
1522+
self.encode_ident_span(def_id, nitem.ident);
15221523
self.encode_stability(def_id);
15231524
self.encode_const_stability(def_id);
15241525
self.encode_deprecation(def_id);
@@ -1613,6 +1614,10 @@ impl EncodeContext<'tcx> {
16131614
}
16141615
}
16151616

1617+
fn encode_ident_span(&mut self, def_id: DefId, ident: Ident) {
1618+
record!(self.per_def.ident_span[def_id] <- ident.span);
1619+
}
1620+
16161621
/// In some cases, along with the item itself, we also
16171622
/// encode some sub-items. Usually we want some info from the item
16181623
/// so it's easier to do that here then to wait until we would encounter

src/librustc_metadata/rmeta/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ define_per_def_tables! {
255255
kind: Table<DefIndex, Lazy<EntryKind>>,
256256
visibility: Table<DefIndex, Lazy<ty::Visibility>>,
257257
span: Table<DefIndex, Lazy<Span>>,
258+
ident_span: Table<DefIndex, Lazy<Span>>,
258259
attributes: Table<DefIndex, Lazy<[ast::Attribute]>>,
259260
children: Table<DefIndex, Lazy<[DefIndex]>>,
260261
stability: Table<DefIndex, Lazy<attr::Stability>>,

src/librustc_resolve/build_reduced_graph.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,10 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
902902
self.insert_field_names(def_id, field_names);
903903
}
904904
Res::Def(DefKind::AssocFn, def_id) => {
905-
if cstore.associated_item_cloned_untracked(def_id).method_has_self_argument {
905+
if cstore
906+
.associated_item_cloned_untracked(def_id, self.r.session)
907+
.method_has_self_argument
908+
{
906909
self.r.has_self.insert(def_id);
907910
}
908911
}

src/test/ui/copy-a-resource.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ LL | struct Foo {
66
...
77
LL | let _y = x.clone();
88
| ^^^^^ method not found in `Foo`
9+
|
10+
::: $SRC_DIR/libcore/clone.rs:LL:COL
11+
|
12+
LL | fn clone(&self) -> Self;
13+
| -----
14+
| |
15+
| the method is available for `std::sync::Arc<Foo>` here
16+
| the method is available for `std::rc::Rc<Foo>` here
917
|
1018
= help: items from traits can only be used if the trait is implemented and in scope
1119
= note: the following trait defines an item `clone`, perhaps you need to implement it:

src/test/ui/derives/derive-assoc-type-not-impl.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ LL | struct NotClone;
1212
...
1313
LL | Bar::<NotClone> { x: 1 }.clone();
1414
| ^^^^^ method not found in `Bar<NotClone>`
15+
|
16+
::: $SRC_DIR/libcore/clone.rs:LL:COL
17+
|
18+
LL | fn clone(&self) -> Self;
19+
| -----
20+
| |
21+
| the method is available for `std::sync::Arc<Bar<NotClone>>` here
22+
| the method is available for `std::rc::Rc<Bar<NotClone>>` here
1523
|
1624
= note: the method `clone` exists but the following trait bounds were not satisfied:
1725
`NotClone: std::clone::Clone`

src/test/ui/error-codes/E0004-2.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ error[E0004]: non-exhaustive patterns: `None` and `Some(_)` not covered
33
|
44
LL | match x { }
55
| ^ patterns `None` and `Some(_)` not covered
6+
|
7+
::: $SRC_DIR/libcore/option.rs:LL:COL
8+
|
9+
LL | None,
10+
| ---- not covered
11+
...
12+
LL | Some(#[stable(feature = "rust1", since = "1.0.0")] T),
13+
| ---- not covered
614
|
715
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
816

src/test/ui/error-codes/E0005.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ error[E0005]: refutable pattern in local binding: `None` not covered
33
|
44
LL | let Some(y) = x;
55
| ^^^^^^^ pattern `None` not covered
6+
|
7+
::: $SRC_DIR/libcore/option.rs:LL:COL
8+
|
9+
LL | None,
10+
| ---- not covered
611
|
712
= note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
813
= note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html

src/test/ui/error-codes/E0297.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ error[E0005]: refutable pattern in `for` loop binding: `None` not covered
33
|
44
LL | for Some(x) in xs {}
55
| ^^^^^^^ pattern `None` not covered
6+
|
7+
::: $SRC_DIR/libcore/option.rs:LL:COL
8+
|
9+
LL | None,
10+
| ---- not covered
611

712
error: aborting due to previous error
813

src/test/ui/feature-gates/feature-gate-exhaustive-patterns.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ error[E0005]: refutable pattern in local binding: `Err(_)` not covered
33
|
44
LL | let Ok(_x) = foo();
55
| ^^^^^^ pattern `Err(_)` not covered
6+
|
7+
::: $SRC_DIR/libcore/result.rs:LL:COL
8+
|
9+
LL | Err(#[stable(feature = "rust1", since = "1.0.0")] E),
10+
| --- not covered
611
|
712
= note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
813
= note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html

src/test/ui/generic-associated-types/iterable.stderr

+10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ LL | impl<T> Iterable for Vec<T> {
55
| --------------------------- in this `impl` item
66
LL | type Item<'a> where T: 'a = <std::slice::Iter<'a, T> as Iterator>::Item;
77
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected reference, found associated type
8+
|
9+
::: $SRC_DIR/libcore/iter/traits/iterator.rs:LL:COL
10+
|
11+
LL | type Item;
12+
| ---- associated type defined here
813
|
914
= note: expected reference `&T`
1015
found associated type `<std::vec::Vec<T> as Iterable>::Item<'_>`
@@ -18,6 +23,11 @@ LL | impl<T> Iterable for [T] {
1823
| ------------------------ in this `impl` item
1924
LL | type Item<'a> where T: 'a = <std::slice::Iter<'a, T> as Iterator>::Item;
2025
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected reference, found associated type
26+
|
27+
::: $SRC_DIR/libcore/iter/traits/iterator.rs:LL:COL
28+
|
29+
LL | type Item;
30+
| ---- associated type defined here
2131
|
2232
= note: expected reference `&T`
2333
found associated type `<[T] as Iterable>::Item<'_>`

0 commit comments

Comments
 (0)