Skip to content

Commit 5e98189

Browse files
committed
Store Ident in DefPathData instead of Symbol
This allows us to recover span information when emitting cross crate errors. A number of test cases benefit from this change, since we were previously not emitting messages due to invalid spans. There are four main parts to this commit: 1. Adding `Ident` to `DefPathData`, and updating the affected code. This mostly consists of mechanical changes. 2. Updating how we determine the disambiguator for a `DefPath`. Since `DefPathData` now stores a `Span` (inside the `Ident`), we need to make sure we ignore this span we determining if a path needs to be disambiguated. This ensure that two paths with the same symbols but different spans are considered equal (this can occur when a macro is repeatedly expanded to a definition). 3. Ensuring that we are able to decode `Spans` when decoding the `DefPathTable`. Since the `DefPathTable` is stored in `CrateMetadata`, we must decode it before we have a `CrateMetadata` available. Since decoding a `Span` requires access to several fields from `CrateMetadata`, this commit adds a new struct `InitialCrateMetadata`, which implements `Metadata` and stores all of the necessary fields. 4. The specialized metadata encoder/decoder impls for `Ident` are removed. This causes us to fall back to the default encoder/decoder implementations for `Ident`, which simply serializes and deserializes the fields of `Ident`. This is strictly an improvement - we still don't have any hygiene information, but we now have a non-dummy Span. This should hopefully allow us to test PR rust-lang#68941, since we will now use cross-crate spans in more places.
1 parent 5e9ebf4 commit 5e98189

Some content is hidden

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

46 files changed

+517
-224
lines changed

src/librustc/hir/map/definitions.rs

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX, LOCAL_CRATE}
1313
use rustc_index::vec::IndexVec;
1414
use rustc_session::CrateDisambiguator;
1515
use rustc_span::hygiene::ExpnId;
16-
use rustc_span::symbol::{sym, Symbol};
16+
use rustc_span::symbol::{sym, Ident, Symbol};
1717
use rustc_span::Span;
1818

1919
use std::fmt::Write;
@@ -90,6 +90,11 @@ pub struct Definitions {
9090
parent_modules_of_macro_defs: FxHashMap<ExpnId, DefId>,
9191
/// Item with a given `DefIndex` was defined during macro expansion with ID `ExpnId`.
9292
expansions_that_defined: FxHashMap<DefIndex, ExpnId>,
93+
// Keeps track of the current disambiguator for a given (DefIndex, DefPathData)
94+
// Note that we replace any `Spans` (e.g. in `Ident`) with `DUMMY_SP` before
95+
// inserting a `DefPathData`. This ensures that we create a new disambiguator
96+
// for definitions that have the same name, but different spans (e.g. definitions
97+
// created by a macro invocation).
9398
next_disambiguator: FxHashMap<(DefIndex, DefPathData), u32>,
9499
def_index_to_span: FxHashMap<DefIndex, Span>,
95100
/// When collecting definitions from an AST fragment produced by a macro invocation `ExpnId`
@@ -206,7 +211,7 @@ impl DefPath {
206211
let mut s = String::with_capacity(self.data.len() * 16);
207212

208213
for component in &self.data {
209-
write!(s, "::{}[{}]", component.data.as_symbol(), component.disambiguator).unwrap();
214+
write!(s, "::{}[{}]", component.data.as_ident(), component.disambiguator).unwrap();
210215
}
211216

212217
s
@@ -225,9 +230,9 @@ impl DefPath {
225230

226231
for component in &self.data {
227232
if component.disambiguator == 0 {
228-
write!(s, "::{}", component.data.as_symbol()).unwrap();
233+
write!(s, "::{}", component.data.as_ident()).unwrap();
229234
} else {
230-
write!(s, "{}[{}]", component.data.as_symbol(), component.disambiguator).unwrap();
235+
write!(s, "{}[{}]", component.data.as_ident(), component.disambiguator).unwrap();
231236
}
232237
}
233238

@@ -245,9 +250,9 @@ impl DefPath {
245250
opt_delimiter.map(|d| s.push(d));
246251
opt_delimiter = Some('-');
247252
if component.disambiguator == 0 {
248-
write!(s, "{}", component.data.as_symbol()).unwrap();
253+
write!(s, "{}", component.data.as_ident()).unwrap();
249254
} else {
250-
write!(s, "{}[{}]", component.data.as_symbol(), component.disambiguator).unwrap();
255+
write!(s, "{}[{}]", component.data.as_ident(), component.disambiguator).unwrap();
251256
}
252257
}
253258
s
@@ -267,13 +272,13 @@ pub enum DefPathData {
267272
/// An impl.
268273
Impl,
269274
/// Something in the type namespace.
270-
TypeNs(Symbol),
275+
TypeNs(Ident),
271276
/// Something in the value namespace.
272-
ValueNs(Symbol),
277+
ValueNs(Ident),
273278
/// Something in the macro namespace.
274-
MacroNs(Symbol),
279+
MacroNs(Ident),
275280
/// Something in the lifetime namespace.
276-
LifetimeNs(Symbol),
281+
LifetimeNs(Ident),
277282
/// A closure expression.
278283
ClosureExpr,
279284

@@ -432,7 +437,10 @@ impl Definitions {
432437

433438
// Find the next free disambiguator for this key.
434439
let disambiguator = {
435-
let next_disamb = self.next_disambiguator.entry((parent, data)).or_insert(0);
440+
// Replace any span with `DUMMY_SP` - two definitions which differ
441+
// only in their spans still need to be disambiguated.
442+
let data_dummy_span = data.with_dummy_span();
443+
let next_disamb = self.next_disambiguator.entry((parent, data_dummy_span)).or_insert(0);
436444
let disambiguator = *next_disamb;
437445
*next_disamb = next_disamb.checked_add(1).expect("disambiguator overflow");
438446
disambiguator
@@ -522,7 +530,20 @@ impl Definitions {
522530
}
523531

524532
impl DefPathData {
525-
pub fn get_opt_name(&self) -> Option<Symbol> {
533+
/// Replaces any `Spans` contains in this `DefPathData` with `DUMMY_SP`.
534+
/// Useful when using a `DefPathData` as a cache key.
535+
pub fn with_dummy_span(&self) -> DefPathData {
536+
use self::DefPathData::*;
537+
match *self {
538+
TypeNs(name) => TypeNs(Ident::with_dummy_span(name.name)),
539+
ValueNs(name) => ValueNs(Ident::with_dummy_span(name.name)),
540+
MacroNs(name) => MacroNs(Ident::with_dummy_span(name.name)),
541+
LifetimeNs(name) => LifetimeNs(Ident::with_dummy_span(name.name)),
542+
_ => *self,
543+
}
544+
}
545+
546+
pub fn get_opt_name(&self) -> Option<Ident> {
526547
use self::DefPathData::*;
527548
match *self {
528549
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) => Some(name),
@@ -531,22 +552,22 @@ impl DefPathData {
531552
}
532553
}
533554

534-
pub fn as_symbol(&self) -> Symbol {
555+
pub fn as_ident(&self) -> Ident {
535556
use self::DefPathData::*;
536557
match *self {
537558
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) => name,
538559
// Note that this does not show up in user print-outs.
539-
CrateRoot => sym::double_braced_crate,
540-
Impl => sym::double_braced_impl,
541-
Misc => sym::double_braced_misc,
542-
ClosureExpr => sym::double_braced_closure,
543-
Ctor => sym::double_braced_constructor,
544-
AnonConst => sym::double_braced_constant,
545-
ImplTrait => sym::double_braced_opaque,
560+
CrateRoot => Ident::with_dummy_span(sym::double_braced_crate),
561+
Impl => Ident::with_dummy_span(sym::double_braced_impl),
562+
Misc => Ident::with_dummy_span(sym::double_braced_misc),
563+
ClosureExpr => Ident::with_dummy_span(sym::double_braced_closure),
564+
Ctor => Ident::with_dummy_span(sym::double_braced_constructor),
565+
AnonConst => Ident::with_dummy_span(sym::double_braced_constant),
566+
ImplTrait => Ident::with_dummy_span(sym::double_braced_opaque),
546567
}
547568
}
548569

549570
pub fn to_string(&self) -> String {
550-
self.as_symbol().to_string()
571+
self.as_ident().to_string()
551572
}
552573
}

src/librustc/ty/mod.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2996,9 +2996,16 @@ impl<'tcx> TyCtxt<'tcx> {
29962996
hir_map::DefPathData::Ctor => {
29972997
self.item_name(DefId { krate: id.krate, index: def_key.parent.unwrap() })
29982998
}
2999-
_ => def_key.disambiguated_data.data.get_opt_name().unwrap_or_else(|| {
3000-
bug!("item_name: no name for {:?}", self.def_path(id));
3001-
}),
2999+
_ => {
3000+
def_key
3001+
.disambiguated_data
3002+
.data
3003+
.get_opt_name()
3004+
.unwrap_or_else(|| {
3005+
bug!("item_name: no name for {:?}", self.def_path(id));
3006+
})
3007+
.name
3008+
}
30023009
}
30033010
}
30043011
}

src/librustc/ty/print/obsolete.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,9 @@ impl DefPathBasedNames<'tcx> {
186186
// foo::bar::ItemName::
187187
for part in self.tcx.def_path(def_id).data {
188188
if self.omit_disambiguators {
189-
write!(output, "{}::", part.data.as_symbol()).unwrap();
189+
write!(output, "{}::", part.data.as_ident()).unwrap();
190190
} else {
191-
write!(output, "{}[{}]::", part.data.as_symbol(), part.disambiguator).unwrap();
191+
write!(output, "{}[{}]::", part.data.as_ident(), part.disambiguator).unwrap();
192192
}
193193
}
194194

src/librustc/ty/print/pretty.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_apfloat::ieee::{Double, Single};
1313
use rustc_apfloat::Float;
1414
use rustc_ast::ast;
1515
use rustc_attr::{SignedInt, UnsignedInt};
16-
use rustc_span::symbol::{kw, Symbol};
16+
use rustc_span::symbol::{kw, Ident, Symbol};
1717
use rustc_target::spec::abi::Abi;
1818

1919
use std::cell::Cell;
@@ -402,12 +402,14 @@ pub trait PrettyPrinter<'tcx>:
402402
.find(|child| child.res.def_id() == def_id)
403403
.map(|child| child.ident.name);
404404
if let Some(reexport) = reexport {
405-
*name = reexport;
405+
*name = Ident::with_dummy_span(reexport);
406406
}
407407
}
408408
// Re-exported `extern crate` (#43189).
409409
DefPathData::CrateRoot => {
410-
data = DefPathData::TypeNs(self.tcx().original_crate_name(def_id.krate));
410+
data = DefPathData::TypeNs(Ident::with_dummy_span(
411+
self.tcx().original_crate_name(def_id.krate),
412+
));
411413
}
412414
_ => {}
413415
}
@@ -1401,7 +1403,7 @@ impl<F: fmt::Write> Printer<'tcx> for FmtPrinter<'_, 'tcx, F> {
14011403

14021404
// FIXME(eddyb) `name` should never be empty, but it
14031405
// currently is for `extern { ... }` "foreign modules".
1404-
let name = disambiguated_data.data.as_symbol().as_str();
1406+
let name = disambiguated_data.data.to_string();
14051407
if !name.is_empty() {
14061408
if !self.empty_path {
14071409
write!(self, "::")?;

src/librustc/ty/query/profiling_support.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ impl<'p, 'c, 'tcx> QueryKeyStringBuilder<'p, 'c, 'tcx> {
6060

6161
match def_key.disambiguated_data.data {
6262
DefPathData::CrateRoot => {
63-
name = self.tcx.original_crate_name(def_id.krate).as_str();
63+
name = self.tcx.original_crate_name(def_id.krate).as_str().to_string();
6464
dis = "";
6565
end_index = 3;
6666
}
6767
other => {
68-
name = other.as_symbol().as_str();
68+
name = other.to_string();
6969
if def_key.disambiguated_data.disambiguator == 0 {
7070
dis = "";
7171
end_index = 3;

src/librustc_ast_lowering/lib.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -764,17 +764,21 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
764764
// Get the name we'll use to make the def-path. Note
765765
// that collisions are ok here and this shouldn't
766766
// really show up for end-user.
767-
let (str_name, kind) = match hir_name {
768-
ParamName::Plain(ident) => (ident.name, hir::LifetimeParamKind::InBand),
769-
ParamName::Fresh(_) => (kw::UnderscoreLifetime, hir::LifetimeParamKind::Elided),
770-
ParamName::Error => (kw::UnderscoreLifetime, hir::LifetimeParamKind::Error),
767+
let (name, kind) = match hir_name {
768+
ParamName::Plain(ident) => (ident, hir::LifetimeParamKind::InBand),
769+
ParamName::Fresh(_) => {
770+
(Ident::with_dummy_span(kw::UnderscoreLifetime), hir::LifetimeParamKind::Elided)
771+
}
772+
ParamName::Error => {
773+
(Ident::with_dummy_span(kw::UnderscoreLifetime), hir::LifetimeParamKind::Error)
774+
}
771775
};
772776

773777
// Add a definition for the in-band lifetime def.
774778
self.resolver.definitions().create_def_with_parent(
775779
parent_index,
776780
node_id,
777-
DefPathData::LifetimeNs(str_name),
781+
DefPathData::LifetimeNs(name),
778782
ExpnId::root(),
779783
span,
780784
);
@@ -1560,7 +1564,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15601564
self.context.resolver.definitions().create_def_with_parent(
15611565
self.parent,
15621566
def_node_id,
1563-
DefPathData::LifetimeNs(name.ident().name),
1567+
DefPathData::LifetimeNs(name.ident()),
15641568
ExpnId::root(),
15651569
lifetime.span,
15661570
);

src/librustc_codegen_llvm/debuginfo/namespace.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub fn item_namespace(cx: &CodegenCx<'ll, '_>, def_id: DefId) -> &'ll DIScope {
2929

3030
let namespace_name = match def_key.disambiguated_data.data {
3131
DefPathData::CrateRoot => cx.tcx.crate_name(def_id.krate),
32-
data => data.as_symbol(),
32+
data => data.as_ident().name,
3333
};
3434
let namespace_name = namespace_name.as_str();
3535

src/librustc_codegen_ssa/debuginfo/type_names.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ pub fn push_debuginfo_type_name<'tcx>(
217217
output.push_str(&tcx.crate_name(def_id.krate).as_str());
218218
for path_element in tcx.def_path(def_id).data {
219219
output.push_str("::");
220-
output.push_str(&path_element.data.as_symbol().as_str());
220+
output.push_str(&path_element.data.to_string());
221221
}
222222
} else {
223223
output.push_str(&tcx.item_name(def_id).as_str());

src/librustc_codegen_utils/symbol_names/legacy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ impl Printer<'tcx> for SymbolPrinter<'tcx> {
315315
self.path.finalize_pending_component();
316316
}
317317

318-
self.write_str(&disambiguated_data.data.as_symbol().as_str())?;
318+
self.write_str(&disambiguated_data.data.to_string())?;
319319
Ok(self)
320320
}
321321
fn path_generic_args(

src/librustc_infer/infer/error_reporting/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
631631
disambiguated_data: &DisambiguatedDefPathData,
632632
) -> Result<Self::Path, Self::Error> {
633633
let mut path = print_prefix(self)?;
634-
path.push(disambiguated_data.data.as_symbol().to_string());
634+
path.push(disambiguated_data.data.to_string());
635635
Ok(path)
636636
}
637637
fn path_generic_args(

src/librustc_lint/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ impl<'a, 'tcx> LateContext<'a, 'tcx> {
793793
_ => {}
794794
}
795795

796-
path.push(disambiguated_data.data.as_symbol());
796+
path.push(disambiguated_data.data.as_ident().name);
797797
Ok(path)
798798
}
799799

src/librustc_metadata/creader.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_ast::ast;
1414
use rustc_ast::attr;
1515
use rustc_ast::expand::allocator::{global_allocator_spans, AllocatorKind};
1616
use rustc_data_structures::svh::Svh;
17-
use rustc_data_structures::sync::Lrc;
17+
use rustc_data_structures::sync::{Lrc, Once};
1818
use rustc_errors::struct_span_err;
1919
use rustc_expand::base::SyntaxExtension;
2020
use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
@@ -29,6 +29,19 @@ use proc_macro::bridge::client::ProcMacro;
2929
use std::path::Path;
3030
use std::{cmp, fs};
3131

32+
crate type SourceMapImportInfo = Once<Vec<ImportedSourceFile>>;
33+
34+
/// Holds information about a rustc_span::SourceFile imported from another crate.
35+
/// See `imported_source_files()` for more information.
36+
crate struct ImportedSourceFile {
37+
/// This SourceFile's byte-offset within the source_map of its original crate
38+
pub original_start_pos: rustc_span::BytePos,
39+
/// The end of this SourceFile within the source_map of its original crate
40+
pub original_end_pos: rustc_span::BytePos,
41+
/// The imported SourceFile's representation within the local source_map
42+
pub translated_source_file: Lrc<rustc_span::SourceFile>,
43+
}
44+
3245
#[derive(Clone)]
3346
pub struct CStore {
3447
metas: IndexVec<CrateNum, Option<Lrc<CrateMetadata>>>,

0 commit comments

Comments
 (0)