Skip to content

Commit 914a9f4

Browse files
authored
Rollup merge of rust-lang#88677 - petrochenkov:exportid, r=davidtwco
rustc: Remove local variable IDs from `Export`s Local variables can never be exported.
2 parents 666df67 + 294510e commit 914a9f4

File tree

19 files changed

+71
-68
lines changed

19 files changed

+71
-68
lines changed

compiler/rustc_data_structures/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#![feature(iter_map_while)]
2222
#![feature(maybe_uninit_uninit_array)]
2323
#![feature(min_specialization)]
24+
#![feature(never_type)]
2425
#![feature(type_alias_impl_trait)]
2526
#![feature(new_uninit)]
2627
#![feature(nll)]

compiler/rustc_data_structures/src/stable_hasher.rs

+6
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,12 @@ impl_stable_hash_via_hash!(i128);
209209
impl_stable_hash_via_hash!(char);
210210
impl_stable_hash_via_hash!(());
211211

212+
impl<CTX> HashStable<CTX> for ! {
213+
fn hash_stable(&self, _ctx: &mut CTX, _hasher: &mut StableHasher) {
214+
unreachable!()
215+
}
216+
}
217+
212218
impl<CTX> HashStable<CTX> for ::std::num::NonZeroU32 {
213219
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
214220
self.get().hash_stable(ctx, hasher)

compiler/rustc_hir/src/def.rs

+5
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,11 @@ impl<Id> Res<Id> {
598598
}
599599
}
600600

601+
#[track_caller]
602+
pub fn expect_non_local<OtherId>(self) -> Res<OtherId> {
603+
self.map_id(|_| panic!("unexpected `Res::Local`"))
604+
}
605+
601606
pub fn macro_kind(self) -> Option<MacroKind> {
602607
match self {
603608
Res::Def(DefKind::Macro(kind), _) => Some(kind),

compiler/rustc_metadata/src/rmeta/decoder.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1020,10 +1020,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
10201020
}
10211021

10221022
/// Iterates over each child of the given item.
1023-
fn each_child_of_item<F>(&self, id: DefIndex, mut callback: F, sess: &Session)
1024-
where
1025-
F: FnMut(Export<hir::HirId>),
1026-
{
1023+
fn each_child_of_item(&self, id: DefIndex, mut callback: impl FnMut(Export), sess: &Session) {
10271024
if let Some(data) = &self.root.proc_macro_data {
10281025
/* If we are loading as a proc macro, we want to return the view of this crate
10291026
* as a proc macro crate.

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+17-23
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use crate::rmeta::encoder;
55

66
use rustc_ast as ast;
77
use rustc_data_structures::stable_map::FxHashMap;
8-
use rustc_hir as hir;
98
use rustc_hir::def::{CtorKind, DefKind};
109
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, CRATE_DEF_INDEX, LOCAL_CRATE};
1110
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
@@ -326,28 +325,27 @@ pub fn provide(providers: &mut Providers) {
326325
// (restrict scope of mutable-borrow of `visible_parent_map`)
327326
{
328327
let visible_parent_map = &mut visible_parent_map;
329-
let mut add_child =
330-
|bfs_queue: &mut VecDeque<_>, child: &Export<hir::HirId>, parent: DefId| {
331-
if child.vis != ty::Visibility::Public {
332-
return;
333-
}
328+
let mut add_child = |bfs_queue: &mut VecDeque<_>, child: &Export, parent: DefId| {
329+
if child.vis != ty::Visibility::Public {
330+
return;
331+
}
334332

335-
if let Some(child) = child.res.opt_def_id() {
336-
match visible_parent_map.entry(child) {
337-
Entry::Occupied(mut entry) => {
338-
// If `child` is defined in crate `cnum`, ensure
339-
// that it is mapped to a parent in `cnum`.
340-
if child.is_local() && entry.get().is_local() {
341-
entry.insert(parent);
342-
}
343-
}
344-
Entry::Vacant(entry) => {
333+
if let Some(child) = child.res.opt_def_id() {
334+
match visible_parent_map.entry(child) {
335+
Entry::Occupied(mut entry) => {
336+
// If `child` is defined in crate `cnum`, ensure
337+
// that it is mapped to a parent in `cnum`.
338+
if child.is_local() && entry.get().is_local() {
345339
entry.insert(parent);
346-
bfs_queue.push_back(child);
347340
}
348341
}
342+
Entry::Vacant(entry) => {
343+
entry.insert(parent);
344+
bfs_queue.push_back(child);
345+
}
349346
}
350-
};
347+
}
348+
};
351349

352350
while let Some(def) = bfs_queue.pop_front() {
353351
for child in tcx.item_children(def).iter() {
@@ -393,11 +391,7 @@ impl CStore {
393391
self.get_crate_data(def.krate).get_visibility(def.index)
394392
}
395393

396-
pub fn item_children_untracked(
397-
&self,
398-
def_id: DefId,
399-
sess: &Session,
400-
) -> Vec<Export<hir::HirId>> {
394+
pub fn item_children_untracked(&self, def_id: DefId, sess: &Session) -> Vec<Export> {
401395
let mut result = vec![];
402396
self.get_crate_data(def_id.krate).each_child_of_item(
403397
def_id.index,

compiler/rustc_metadata/src/rmeta/encoder.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -1065,14 +1065,7 @@ impl EncodeContext<'a, 'tcx> {
10651065
// items - we encode information about proc-macros later on.
10661066
let reexports = if !self.is_proc_macro {
10671067
match tcx.module_exports(local_def_id) {
1068-
Some(exports) => {
1069-
let hir = self.tcx.hir();
1070-
self.lazy(
1071-
exports
1072-
.iter()
1073-
.map(|export| export.map_id(|id| hir.local_def_id_to_hir_id(id))),
1074-
)
1075-
}
1068+
Some(exports) => self.lazy(exports),
10761069
_ => Lazy::empty(),
10771070
}
10781071
} else {

compiler/rustc_metadata/src/rmeta/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ struct RenderedConst(String);
359359

360360
#[derive(MetadataEncodable, MetadataDecodable)]
361361
struct ModData {
362-
reexports: Lazy<[Export<hir::HirId>]>,
362+
reexports: Lazy<[Export]>,
363363
expansion: ExpnId,
364364
}
365365

compiler/rustc_middle/src/hir/exports.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,18 @@ use std::fmt::Debug;
1111

1212
/// This is the replacement export map. It maps a module to all of the exports
1313
/// within.
14-
pub type ExportMap<Id> = FxHashMap<LocalDefId, Vec<Export<Id>>>;
14+
pub type ExportMap = FxHashMap<LocalDefId, Vec<Export>>;
1515

1616
#[derive(Copy, Clone, Debug, TyEncodable, TyDecodable, HashStable)]
17-
pub struct Export<Id> {
17+
pub struct Export {
1818
/// The name of the target.
1919
pub ident: Ident,
2020
/// The resolution of the target.
21-
pub res: Res<Id>,
21+
/// Local variables cannot be exported, so this `Res` doesn't need the ID parameter.
22+
pub res: Res<!>,
2223
/// The span of the target.
2324
pub span: Span,
2425
/// The visibility of the export.
2526
/// We include non-`pub` exports for hygienic macros that get used from extern crates.
2627
pub vis: ty::Visibility,
2728
}
28-
29-
impl<Id> Export<Id> {
30-
pub fn map_id<R>(self, map: impl FnMut(Id) -> R) -> Export<R> {
31-
Export { ident: self.ident, res: self.res.map_id(map), span: self.span, vis: self.vis }
32-
}
33-
}

compiler/rustc_middle/src/query/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,7 @@ rustc_queries! {
11891189
desc { "traits in scope at a block" }
11901190
}
11911191

1192-
query module_exports(def_id: LocalDefId) -> Option<&'tcx [Export<LocalDefId>]> {
1192+
query module_exports(def_id: LocalDefId) -> Option<&'tcx [Export]> {
11931193
desc { |tcx| "looking up items exported by `{}`", tcx.def_path_str(def_id.to_def_id()) }
11941194
}
11951195

@@ -1401,7 +1401,7 @@ rustc_queries! {
14011401
eval_always
14021402
desc { "fetching what a crate is named" }
14031403
}
1404-
query item_children(def_id: DefId) -> &'tcx [Export<hir::HirId>] {
1404+
query item_children(def_id: DefId) -> &'tcx [Export] {
14051405
desc { |tcx| "collecting child items of `{}`", tcx.def_path_str(def_id) }
14061406
}
14071407
query extern_mod_stmt_cnum(def_id: LocalDefId) -> Option<CrateNum> {

compiler/rustc_middle/src/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ pub struct ResolverOutputs {
127127
pub extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
128128
pub maybe_unused_trait_imports: FxHashSet<LocalDefId>,
129129
pub maybe_unused_extern_crates: Vec<(LocalDefId, Span)>,
130-
pub export_map: ExportMap<LocalDefId>,
130+
pub export_map: ExportMap,
131131
pub glob_map: FxHashMap<LocalDefId, FxHashSet<Symbol>>,
132132
/// Extern prelude entries. The value is `true` if the entry was introduced
133133
/// via `extern crate` item and not `--extern` option or compiler built-in.

compiler/rustc_resolve/src/build_reduced_graph.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,6 @@ impl<'a> Resolver<'a> {
228228
crate fn build_reduced_graph_external(&mut self, module: Module<'a>) {
229229
let def_id = module.def_id().expect("unpopulated module without a def-id");
230230
for child in self.cstore().item_children_untracked(def_id, self.session) {
231-
let child = child.map_id(|_| panic!("unexpected id"));
232231
let parent_scope = ParentScope::module(module, self);
233232
BuildReducedGraphVisitor { r: self, parent_scope }
234233
.build_reduced_graph_for_external_crate_res(child);
@@ -946,9 +945,10 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
946945
}
947946

948947
/// Builds the reduced graph for a single item in an external crate.
949-
fn build_reduced_graph_for_external_crate_res(&mut self, child: Export<NodeId>) {
948+
fn build_reduced_graph_for_external_crate_res(&mut self, child: Export) {
950949
let parent = self.parent_scope.module;
951950
let Export { ident, res, vis, span } = child;
951+
let res = res.expect_non_local();
952952
let expansion = self.parent_scope.expansion;
953953
// Record primary definitions.
954954
match res {

compiler/rustc_resolve/src/imports.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use crate::{NameBinding, NameBindingKind, PathResult, PrivacyError, ToNameBindin
1111

1212
use rustc_ast::unwrap_or;
1313
use rustc_ast::NodeId;
14-
use rustc_ast_lowering::ResolverAstLowering;
1514
use rustc_data_structures::fx::FxHashSet;
1615
use rustc_data_structures::ptr_key::PtrKey;
1716
use rustc_errors::{pluralize, struct_span_err, Applicability};
@@ -1387,13 +1386,13 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
13871386

13881387
let mut reexports = Vec::new();
13891388

1390-
module.for_each_child(self.r, |this, ident, _, binding| {
1389+
module.for_each_child(self.r, |_, ident, _, binding| {
13911390
// Filter away ambiguous imports and anything that has def-site hygiene.
13921391
// FIXME: Implement actual cross-crate hygiene.
13931392
let is_good_import =
13941393
binding.is_import() && !binding.is_ambiguity() && !ident.span.from_expansion();
13951394
if is_good_import || binding.is_macro_def() {
1396-
let res = binding.res().map_id(|id| this.local_def_id(id));
1395+
let res = binding.res().expect_non_local();
13971396
if res != def::Res::Err {
13981397
reexports.push(Export { ident, res, span: binding.span, vis: binding.vis });
13991398
}

compiler/rustc_resolve/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#![feature(crate_visibility_modifier)]
1515
#![feature(format_args_capture)]
1616
#![feature(iter_zip)]
17+
#![feature(never_type)]
1718
#![feature(nll)]
1819
#![recursion_limit = "256"]
1920
#![allow(rustdoc::private_intra_doc_links)]
@@ -911,7 +912,7 @@ pub struct Resolver<'a> {
911912

912913
/// `CrateNum` resolutions of `extern crate` items.
913914
extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
914-
export_map: ExportMap<LocalDefId>,
915+
export_map: ExportMap,
915916
trait_map: Option<NodeMap<Vec<TraitCandidate>>>,
916917

917918
/// A map from nodes to anonymous modules.

compiler/rustc_serialize/src/serialize.rs

+12
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,18 @@ direct_serialize_impls! {
366366
char emit_char read_char
367367
}
368368

369+
impl<S: Encoder> Encodable<S> for ! {
370+
fn encode(&self, _s: &mut S) -> Result<(), S::Error> {
371+
unreachable!()
372+
}
373+
}
374+
375+
impl<D: Decoder> Decodable<D> for ! {
376+
fn decode(_d: &mut D) -> Result<!, D::Error> {
377+
unreachable!()
378+
}
379+
}
380+
369381
impl<S: Encoder> Encodable<S> for ::std::num::NonZeroU32 {
370382
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
371383
s.emit_u32(self.get())

compiler/rustc_typeck/src/check/method/suggest.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1655,7 +1655,7 @@ fn compute_all_traits(tcx: TyCtxt<'_>, (): ()) -> &[DefId] {
16551655
tcx: TyCtxt<'_>,
16561656
traits: &mut Vec<DefId>,
16571657
external_mods: &mut FxHashSet<DefId>,
1658-
res: Res,
1658+
res: Res<!>,
16591659
) {
16601660
match res {
16611661
Res::Def(DefKind::Trait | DefKind::TraitAlias, def_id) => {

src/librustdoc/clean/inline.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -482,12 +482,13 @@ fn build_module(
482482
// visit each node at most once.
483483
for &item in cx.tcx.item_children(did).iter() {
484484
if item.vis == ty::Visibility::Public {
485-
if let Some(def_id) = item.res.mod_def_id() {
485+
let res = item.res.expect_non_local();
486+
if let Some(def_id) = res.mod_def_id() {
486487
if did == def_id || !visited.insert(def_id) {
487488
continue;
488489
}
489490
}
490-
if let Res::PrimTy(p) = item.res {
491+
if let Res::PrimTy(p) = res {
491492
// Primitive types can't be inlined so generate an import instead.
492493
let prim_ty = clean::PrimitiveType::from(p);
493494
items.push(clean::Item {
@@ -500,7 +501,7 @@ fn build_module(
500501
clean::ImportSource {
501502
path: clean::Path {
502503
global: false,
503-
res: item.res,
504+
res,
504505
segments: vec![clean::PathSegment {
505506
name: prim_ty.as_sym(),
506507
args: clean::GenericArgs::AngleBracketed {
@@ -515,9 +516,7 @@ fn build_module(
515516
))),
516517
cfg: None,
517518
});
518-
} else if let Some(i) =
519-
try_inline(cx, did, None, item.res, item.ident.name, None, visited)
520-
{
519+
} else if let Some(i) = try_inline(cx, did, None, res, item.ident.name, None, visited) {
521520
items.extend(i)
522521
}
523522
}

src/librustdoc/clean/types.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ impl ExternalCrate {
212212
crate fn keywords(&self, tcx: TyCtxt<'_>) -> ThinVec<(DefId, Symbol)> {
213213
let root = self.def_id();
214214

215-
let as_keyword = |res: Res| {
215+
let as_keyword = |res: Res<!>| {
216216
if let Res::Def(DefKind::Mod, def_id) = res {
217217
let attrs = tcx.get_attrs(def_id);
218218
let mut keyword = None;
@@ -243,7 +243,8 @@ impl ExternalCrate {
243243
hir::ItemKind::Use(ref path, hir::UseKind::Single)
244244
if item.vis.node.is_pub() =>
245245
{
246-
as_keyword(path.res).map(|(_, prim)| (id.def_id.to_def_id(), prim))
246+
as_keyword(path.res.expect_non_local())
247+
.map(|(_, prim)| (id.def_id.to_def_id(), prim))
247248
}
248249
_ => None,
249250
}
@@ -274,7 +275,7 @@ impl ExternalCrate {
274275
// Also note that this does not attempt to deal with modules tagged
275276
// duplicately for the same primitive. This is handled later on when
276277
// rendering by delegating everything to a hash map.
277-
let as_primitive = |res: Res| {
278+
let as_primitive = |res: Res<!>| {
278279
if let Res::Def(DefKind::Mod, def_id) = res {
279280
let attrs = tcx.get_attrs(def_id);
280281
let mut prim = None;
@@ -309,7 +310,7 @@ impl ExternalCrate {
309310
hir::ItemKind::Use(ref path, hir::UseKind::Single)
310311
if item.vis.node.is_pub() =>
311312
{
312-
as_primitive(path.res).map(|(_, prim)| {
313+
as_primitive(path.res.expect_non_local()).map(|(_, prim)| {
313314
// Pretend the primitive is local.
314315
(id.def_id.to_def_id(), prim)
315316
})

src/librustdoc/visit_lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl<'a, 'tcx> LibEmbargoVisitor<'a, 'tcx> {
6767
}
6868
}
6969

70-
fn visit_item(&mut self, res: Res) {
70+
fn visit_item(&mut self, res: Res<!>) {
7171
let def_id = res.def_id();
7272
let vis = self.tcx.visibility(def_id);
7373
let inherited_item_level = if vis == Visibility::Public { self.prev_level } else { None };

src/tools/clippy/clippy_utils/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ pub fn path_to_res(cx: &LateContext<'_>, path: &[&str]) -> Res {
520520
}
521521
};
522522
}
523-
fn item_child_by_name<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, name: &str) -> Option<&'tcx Export<HirId>> {
523+
fn item_child_by_name<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, name: &str) -> Option<&'tcx Export> {
524524
tcx.item_children(def_id)
525525
.iter()
526526
.find(|item| item.ident.name.as_str() == name)
@@ -557,7 +557,7 @@ pub fn path_to_res(cx: &LateContext<'_>, path: &[&str]) -> Res {
557557
None
558558
}
559559
});
560-
try_res!(last).res
560+
try_res!(last).res.expect_non_local()
561561
}
562562

563563
/// Convenience function to get the `DefId` of a trait by path.

0 commit comments

Comments
 (0)