Skip to content

Commit 0d61852

Browse files
committed
hir: Do not introduce dummy type names for extern blocks in def paths
Use a separate nameless `DefPathData` variant instead
1 parent dde825d commit 0d61852

File tree

8 files changed

+33
-28
lines changed

8 files changed

+33
-28
lines changed

compiler/rustc_hir/src/definitions.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,8 @@ pub enum DefPathData {
267267
// Different kinds of items and item-like things:
268268
/// An impl.
269269
Impl,
270+
/// An `extern` block.
271+
ForeignMod,
270272
/// Something in the type namespace.
271273
TypeNs(Symbol),
272274
/// Something in the value namespace.
@@ -469,7 +471,9 @@ impl DefPathData {
469471
match *self {
470472
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) => Some(name),
471473

472-
Impl | CrateRoot | Misc | ClosureExpr | Ctor | AnonConst | ImplTrait => None,
474+
Impl | ForeignMod | CrateRoot | Misc | ClosureExpr | Ctor | AnonConst | ImplTrait => {
475+
None
476+
}
473477
}
474478
}
475479

@@ -482,6 +486,7 @@ impl DefPathData {
482486
// Note that this does not show up in user print-outs.
483487
CrateRoot => DefPathDataName::Anon { namespace: kw::Crate },
484488
Impl => DefPathDataName::Anon { namespace: kw::Impl },
489+
ForeignMod => DefPathDataName::Anon { namespace: kw::Extern },
485490
Misc => DefPathDataName::Anon { namespace: sym::misc },
486491
ClosureExpr => DefPathDataName::Anon { namespace: sym::closure },
487492
Ctor => DefPathDataName::Anon { namespace: sym::constructor },

compiler/rustc_lint/src/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1040,8 +1040,8 @@ impl<'tcx> LateContext<'tcx> {
10401040
) -> Result<Self::Path, Self::Error> {
10411041
let mut path = print_prefix(self)?;
10421042

1043-
// Skip `::{{constructor}}` on tuple/unit structs.
1044-
if let DefPathData::Ctor = disambiguated_data.data {
1043+
// Skip `::{{extern}}` blocks and `::{{constructor}}` on tuple/unit structs.
1044+
if let DefPathData::ForeignMod | DefPathData::Ctor = disambiguated_data.data {
10451045
return Ok(path);
10461046
}
10471047

compiler/rustc_middle/src/ty/print/pretty.rs

+12-16
Original file line numberDiff line numberDiff line change
@@ -1740,30 +1740,26 @@ impl<F: fmt::Write> Printer<'tcx> for FmtPrinter<'_, 'tcx, F> {
17401740
) -> Result<Self::Path, Self::Error> {
17411741
self = print_prefix(self)?;
17421742

1743-
// Skip `::{{constructor}}` on tuple/unit structs.
1744-
if let DefPathData::Ctor = disambiguated_data.data {
1743+
// Skip `::{{extern}}` blocks and `::{{constructor}}` on tuple/unit structs.
1744+
if let DefPathData::ForeignMod | DefPathData::Ctor = disambiguated_data.data {
17451745
return Ok(self);
17461746
}
17471747

1748-
// FIXME(eddyb) `name` should never be empty, but it
1749-
// currently is for `extern { ... }` "foreign modules".
17501748
let name = disambiguated_data.data.name();
1751-
if name != DefPathDataName::Named(kw::Empty) {
1752-
if !self.empty_path {
1753-
write!(self, "::")?;
1754-
}
1749+
if !self.empty_path {
1750+
write!(self, "::")?;
1751+
}
17551752

1756-
if let DefPathDataName::Named(name) = name {
1757-
if Ident::with_dummy_span(name).is_raw_guess() {
1758-
write!(self, "r#")?;
1759-
}
1753+
if let DefPathDataName::Named(name) = name {
1754+
if Ident::with_dummy_span(name).is_raw_guess() {
1755+
write!(self, "r#")?;
17601756
}
1757+
}
17611758

1762-
let verbose = self.tcx.sess.verbose();
1763-
disambiguated_data.fmt_maybe_verbose(&mut self, verbose)?;
1759+
let verbose = self.tcx.sess.verbose();
1760+
disambiguated_data.fmt_maybe_verbose(&mut self, verbose)?;
17641761

1765-
self.empty_path = false;
1766-
}
1762+
self.empty_path = false;
17671763

17681764
Ok(self)
17691765
}

compiler/rustc_resolve/src/def_collector.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,14 @@ impl<'a, 'b> visit::Visitor<'a> for DefCollector<'a, 'b> {
9292
// information we encapsulate into, the better
9393
let def_data = match &i.kind {
9494
ItemKind::Impl { .. } => DefPathData::Impl,
95+
ItemKind::ForeignMod(..) => DefPathData::ForeignMod,
9596
ItemKind::Mod(..)
9697
| ItemKind::Trait(..)
9798
| ItemKind::TraitAlias(..)
9899
| ItemKind::Enum(..)
99100
| ItemKind::Struct(..)
100101
| ItemKind::Union(..)
101102
| ItemKind::ExternCrate(..)
102-
| ItemKind::ForeignMod(..)
103103
| ItemKind::TyAlias(..) => DefPathData::TypeNs(i.ident.name),
104104
ItemKind::Static(..) | ItemKind::Const(..) | ItemKind::Fn(..) => {
105105
DefPathData::ValueNs(i.ident.name)

compiler/rustc_symbol_mangling/src/legacy.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,8 @@ impl<'tcx> Printer<'tcx> for &mut SymbolPrinter<'tcx> {
311311
) -> Result<Self::Path, Self::Error> {
312312
self = print_prefix(self)?;
313313

314-
// Skip `::{{constructor}}` on tuple/unit structs.
315-
if let DefPathData::Ctor = disambiguated_data.data {
314+
// Skip `::{{extern}}` blocks and `::{{constructor}}` on tuple/unit structs.
315+
if let DefPathData::ForeignMod | DefPathData::Ctor = disambiguated_data.data {
316316
return Ok(self);
317317
}
318318

compiler/rustc_symbol_mangling/src/v0.rs

+4
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,10 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> {
771771
disambiguated_data: &DisambiguatedDefPathData,
772772
) -> Result<Self::Path, Self::Error> {
773773
let ns = match disambiguated_data.data {
774+
// FIXME: It shouldn't be necessary to add anything for extern block segments,
775+
// but we add 't' for backward compatibility.
776+
DefPathData::ForeignMod => 't',
777+
774778
// Uppercase categories are more stable than lowercase ones.
775779
DefPathData::TypeNs(_) => 't',
776780
DefPathData::ValueNs(_) => 'v',

src/librustdoc/clean/inline.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_data_structures::fx::FxHashSet;
88
use rustc_hir as hir;
99
use rustc_hir::def::{DefKind, Res};
1010
use rustc_hir::def_id::DefId;
11+
use rustc_hir::definitions::DefPathData;
1112
use rustc_hir::Mutability;
1213
use rustc_metadata::creader::{CStore, LoadedMacro};
1314
use rustc_middle::ty::{self, TyCtxt};
@@ -165,9 +166,8 @@ crate fn record_extern_fqn(cx: &mut DocContext<'_>, did: DefId, kind: ItemType)
165166
let crate_name = cx.tcx.crate_name(did.krate).to_string();
166167

167168
let relative = cx.tcx.def_path(did).data.into_iter().filter_map(|elem| {
168-
// extern blocks have an empty name
169-
let s = elem.data.to_string();
170-
if !s.is_empty() { Some(s) } else { None }
169+
// Filter out extern blocks
170+
(elem.data != DefPathData::ForeignMod).then(|| elem.data.to_string())
171171
});
172172
let fqn = if let ItemType::Macro = kind {
173173
// Check to see if it is a macro 2.0 or built-in macro

src/librustdoc/visit_ast.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
55
use rustc_hir as hir;
66
use rustc_hir::def::{DefKind, Res};
77
use rustc_hir::def_id::DefId;
8+
use rustc_hir::definitions::DefPathData;
89
use rustc_hir::Node;
910
use rustc_hir::CRATE_HIR_ID;
1011
use rustc_middle::middle::privacy::AccessLevel;
@@ -45,9 +46,8 @@ impl Module<'hir> {
4546
fn def_id_to_path(tcx: TyCtxt<'_>, did: DefId) -> Vec<String> {
4647
let crate_name = tcx.crate_name(did.krate).to_string();
4748
let relative = tcx.def_path(did).data.into_iter().filter_map(|elem| {
48-
// extern blocks have an empty name
49-
let s = elem.data.to_string();
50-
if !s.is_empty() { Some(s) } else { None }
49+
// Filter out extern blocks
50+
(elem.data != DefPathData::ForeignMod).then(|| elem.data.to_string())
5151
});
5252
std::iter::once(crate_name).chain(relative).collect()
5353
}

0 commit comments

Comments
 (0)