Skip to content

Commit d626f5b

Browse files
authored
Rollup merge of #70077 - Aaron1011:feature/new-def-path-ident, r=petrochenkov
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 a side table, which gets encoded into 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.
2 parents 3d8b961 + 86b8dea commit d626f5b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+345
-75
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
@@ -12,7 +12,7 @@ use rustc::traits::specialization_graph;
1212
use rustc::ty::codec::{self as ty_codec, TyEncoder};
1313
use rustc::ty::layout::VariantIdx;
1414
use rustc::ty::{self, SymbolName, Ty, TyCtxt};
15-
use rustc_ast::ast;
15+
use rustc_ast::ast::{self, Ident};
1616
use rustc_ast::attr;
1717
use rustc_data_structures::fingerprint::Fingerprint;
1818
use rustc_data_structures::fx::FxHashMap;
@@ -30,7 +30,7 @@ use rustc_index::vec::Idx;
3030
use rustc_serialize::{opaque, Encodable, Encoder, SpecializedEncoder};
3131
use rustc_session::config::{self, CrateType};
3232
use rustc_span::source_map::Spanned;
33-
use rustc_span::symbol::{kw, sym, Ident, Symbol};
33+
use rustc_span::symbol::{kw, sym, Symbol};
3434
use rustc_span::{self, ExternalSource, FileName, SourceFile, Span};
3535
use std::hash::Hash;
3636
use std::num::NonZeroUsize;
@@ -220,13 +220,6 @@ impl<'tcx> SpecializedEncoder<Span> for EncodeContext<'tcx> {
220220
}
221221
}
222222

223-
impl SpecializedEncoder<Ident> for EncodeContext<'tcx> {
224-
fn specialized_encode(&mut self, ident: &Ident) -> Result<(), Self::Error> {
225-
// FIXME(jseyfried): intercrate hygiene
226-
ident.name.encode(self)
227-
}
228-
}
229-
230223
impl<'tcx> SpecializedEncoder<LocalDefId> for EncodeContext<'tcx> {
231224
#[inline]
232225
fn specialized_encode(&mut self, def_id: &LocalDefId) -> Result<(), Self::Error> {
@@ -633,6 +626,7 @@ impl EncodeContext<'tcx> {
633626
assert!(f.did.is_local());
634627
f.did.index
635628
}));
629+
self.encode_ident_span(def_id, variant.ident);
636630
self.encode_stability(def_id);
637631
self.encode_deprecation(def_id);
638632
self.encode_item_type(def_id);
@@ -735,6 +729,7 @@ impl EncodeContext<'tcx> {
735729
record!(self.per_def.visibility[def_id] <- field.vis);
736730
record!(self.per_def.span[def_id] <- self.tcx.def_span(def_id));
737731
record!(self.per_def.attributes[def_id] <- variant_data.fields()[field_index].attrs);
732+
self.encode_ident_span(def_id, field.ident);
738733
self.encode_stability(def_id);
739734
self.encode_deprecation(def_id);
740735
self.encode_item_type(def_id);
@@ -869,6 +864,7 @@ impl EncodeContext<'tcx> {
869864
record!(self.per_def.visibility[def_id] <- trait_item.vis);
870865
record!(self.per_def.span[def_id] <- ast_item.span);
871866
record!(self.per_def.attributes[def_id] <- ast_item.attrs);
867+
self.encode_ident_span(def_id, ast_item.ident);
872868
self.encode_stability(def_id);
873869
self.encode_const_stability(def_id);
874870
self.encode_deprecation(def_id);
@@ -952,6 +948,7 @@ impl EncodeContext<'tcx> {
952948
record!(self.per_def.visibility[def_id] <- impl_item.vis);
953949
record!(self.per_def.span[def_id] <- ast_item.span);
954950
record!(self.per_def.attributes[def_id] <- ast_item.attrs);
951+
self.encode_ident_span(def_id, impl_item.ident);
955952
self.encode_stability(def_id);
956953
self.encode_const_stability(def_id);
957954
self.encode_deprecation(def_id);
@@ -1058,6 +1055,8 @@ impl EncodeContext<'tcx> {
10581055

10591056
debug!("EncodeContext::encode_info_for_item({:?})", def_id);
10601057

1058+
self.encode_ident_span(def_id, item.ident);
1059+
10611060
record!(self.per_def.kind[def_id] <- match item.kind {
10621061
hir::ItemKind::Static(_, hir::Mutability::Mut, _) => EntryKind::MutStatic,
10631062
hir::ItemKind::Static(_, hir::Mutability::Not, _) => EntryKind::ImmStatic,
@@ -1284,6 +1283,7 @@ impl EncodeContext<'tcx> {
12841283
record!(self.per_def.visibility[def_id] <- ty::Visibility::Public);
12851284
record!(self.per_def.span[def_id] <- macro_def.span);
12861285
record!(self.per_def.attributes[def_id] <- macro_def.attrs);
1286+
self.encode_ident_span(def_id, macro_def.ident);
12871287
self.encode_stability(def_id);
12881288
self.encode_deprecation(def_id);
12891289
}
@@ -1528,6 +1528,7 @@ impl EncodeContext<'tcx> {
15281528
ty::Visibility::from_hir(&nitem.vis, nitem.hir_id, self.tcx));
15291529
record!(self.per_def.span[def_id] <- nitem.span);
15301530
record!(self.per_def.attributes[def_id] <- nitem.attrs);
1531+
self.encode_ident_span(def_id, nitem.ident);
15311532
self.encode_stability(def_id);
15321533
self.encode_const_stability(def_id);
15331534
self.encode_deprecation(def_id);
@@ -1622,6 +1623,10 @@ impl EncodeContext<'tcx> {
16221623
}
16231624
}
16241625

1626+
fn encode_ident_span(&mut self, def_id: DefId, ident: Ident) {
1627+
record!(self.per_def.ident_span[def_id] <- ident.span);
1628+
}
1629+
16251630
/// In some cases, along with the item itself, we also
16261631
/// encode some sub-items. Usually we want some info from the item
16271632
/// 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
@@ -256,6 +256,7 @@ define_per_def_tables! {
256256
kind: Table<DefIndex, Lazy<EntryKind>>,
257257
visibility: Table<DefIndex, Lazy<ty::Visibility>>,
258258
span: Table<DefIndex, Lazy<Span>>,
259+
ident_span: Table<DefIndex, Lazy<Span>>,
259260
attributes: Table<DefIndex, Lazy<[ast::Attribute]>>,
260261
children: Table<DefIndex, Lazy<[DefIndex]>>,
261262
stability: Table<DefIndex, Lazy<attr::Stability>>,

src/librustc_resolve/build_reduced_graph.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,10 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
904904
self.insert_field_names(def_id, field_names);
905905
}
906906
Res::Def(DefKind::AssocFn, def_id) => {
907-
if cstore.associated_item_cloned_untracked(def_id).method_has_self_argument {
907+
if cstore
908+
.associated_item_cloned_untracked(def_id, self.r.session)
909+
.method_has_self_argument
910+
{
908911
self.r.has_self.insert(def_id);
909912
}
910913
}

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

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
// FIXME: missing sysroot spans (#53081)
2+
// ignore-i586-unknown-linux-gnu
3+
// ignore-i586-unknown-linux-musl
4+
// ignore-i686-unknown-linux-musl
5+
16
#[derive(Debug)]
27
struct Foo {
38
i: isize,

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
error[E0599]: no method named `clone` found for struct `Foo` in the current scope
2-
--> $DIR/copy-a-resource.rs:18:16
2+
--> $DIR/copy-a-resource.rs:23:16
33
|
44
LL | struct Foo {
55
| ---------- method `clone` not found for this
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.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
// FIXME: missing sysroot spans (#53081)
2+
// ignore-i586-unknown-linux-gnu
3+
// ignore-i586-unknown-linux-musl
4+
// ignore-i686-unknown-linux-musl
5+
16
trait Foo {
27
type X;
38
fn method(&self) {}

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0599]: no method named `clone` found for struct `Bar<NotClone>` in the current scope
2-
--> $DIR/derive-assoc-type-not-impl.rs:18:30
2+
--> $DIR/derive-assoc-type-not-impl.rs:23:30
33
|
44
LL | struct Bar<T: Foo> {
55
| ------------------
@@ -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.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
// FIXME: missing sysroot spans (#53081)
2+
// ignore-i586-unknown-linux-gnu
3+
// ignore-i586-unknown-linux-musl
4+
// ignore-i686-unknown-linux-musl
5+
16
fn main() {
27
let x = Some(1);
38

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
error[E0004]: non-exhaustive patterns: `None` and `Some(_)` not covered
2-
--> $DIR/E0004-2.rs:4:11
2+
--> $DIR/E0004-2.rs:9:11
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.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
// FIXME: missing sysroot spans (#53081)
2+
// ignore-i586-unknown-linux-gnu
3+
// ignore-i586-unknown-linux-musl
4+
// ignore-i686-unknown-linux-musl
5+
16
fn main() {
27
let x = Some(1);
38
let Some(y) = x; //~ ERROR E0005

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
error[E0005]: refutable pattern in local binding: `None` not covered
2-
--> $DIR/E0005.rs:3:9
2+
--> $DIR/E0005.rs:8:9
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

0 commit comments

Comments
 (0)