rustc_passes/
check_export.rs

1use std::iter;
2use std::ops::ControlFlow;
3
4use rustc_abi::ExternAbi;
5use rustc_attr_data_structures::{AttributeKind, find_attr};
6use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
7use rustc_hir as hir;
8use rustc_hir::def::DefKind;
9use rustc_hir::def_id::{DefId, LocalDefId};
10use rustc_hir::intravisit::{self, Visitor};
11use rustc_middle::hir::nested_filter;
12use rustc_middle::middle::privacy::{EffectiveVisibility, Level};
13use rustc_middle::query::{LocalCrate, Providers};
14use rustc_middle::ty::{
15    self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitor, Visibility,
16};
17use rustc_session::config::CrateType;
18use rustc_span::Span;
19
20use crate::errors::UnexportableItem;
21
22struct ExportableItemCollector<'tcx> {
23    tcx: TyCtxt<'tcx>,
24    exportable_items: FxIndexSet<DefId>,
25    in_exportable_mod: bool,
26    seen_exportable_in_mod: bool,
27}
28
29impl<'tcx> ExportableItemCollector<'tcx> {
30    fn new(tcx: TyCtxt<'tcx>) -> ExportableItemCollector<'tcx> {
31        ExportableItemCollector {
32            tcx,
33            exportable_items: Default::default(),
34            in_exportable_mod: false,
35            seen_exportable_in_mod: false,
36        }
37    }
38
39    fn report_wrong_site(&self, def_id: LocalDefId) {
40        let def_descr = self.tcx.def_descr(def_id.to_def_id());
41        self.tcx.dcx().emit_err(UnexportableItem::Item {
42            descr: &format!("{}", def_descr),
43            span: self.tcx.def_span(def_id),
44        });
45    }
46
47    fn item_is_exportable(&self, def_id: LocalDefId) -> bool {
48        let has_attr = find_attr!(self.tcx.get_all_attrs(def_id), AttributeKind::ExportStable);
49        if !self.in_exportable_mod && !has_attr {
50            return false;
51        }
52
53        let visibilities = self.tcx.effective_visibilities(());
54        let is_pub = visibilities.is_directly_public(def_id);
55
56        if has_attr && !is_pub {
57            let vis = visibilities.effective_vis(def_id).cloned().unwrap_or_else(|| {
58                EffectiveVisibility::from_vis(Visibility::Restricted(
59                    self.tcx.parent_module_from_def_id(def_id).to_local_def_id(),
60                ))
61            });
62            let vis = vis.at_level(Level::Direct);
63            let span = self.tcx.def_span(def_id);
64
65            self.tcx.dcx().emit_err(UnexportableItem::PrivItem {
66                vis_note: span,
67                vis_descr: &vis.to_string(def_id, self.tcx),
68                span,
69            });
70            return false;
71        }
72
73        is_pub && (has_attr || self.in_exportable_mod)
74    }
75
76    fn add_exportable(&mut self, def_id: LocalDefId) {
77        self.seen_exportable_in_mod = true;
78        self.exportable_items.insert(def_id.to_def_id());
79    }
80
81    fn walk_item_with_mod(&mut self, item: &'tcx hir::Item<'tcx>) {
82        let def_id = item.hir_id().owner.def_id;
83        let old_exportable_mod = self.in_exportable_mod;
84        if find_attr!(self.tcx.get_all_attrs(def_id), AttributeKind::ExportStable) {
85            self.in_exportable_mod = true;
86        }
87        let old_seen_exportable_in_mod = std::mem::replace(&mut self.seen_exportable_in_mod, false);
88
89        intravisit::walk_item(self, item);
90
91        if self.seen_exportable_in_mod || self.in_exportable_mod {
92            self.exportable_items.insert(def_id.to_def_id());
93        }
94
95        self.seen_exportable_in_mod = old_seen_exportable_in_mod;
96        self.in_exportable_mod = old_exportable_mod;
97    }
98}
99
100impl<'tcx> Visitor<'tcx> for ExportableItemCollector<'tcx> {
101    type NestedFilter = nested_filter::All;
102
103    fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
104        self.tcx
105    }
106
107    fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
108        let def_id = item.hir_id().owner.def_id;
109        // Applying #[extern] attribute to modules is simply equivalent to
110        // applying the attribute to every public item within it.
111        match item.kind {
112            hir::ItemKind::Mod(..) => {
113                self.walk_item_with_mod(item);
114                return;
115            }
116            hir::ItemKind::Impl(impl_) if impl_.of_trait.is_none() => {
117                self.walk_item_with_mod(item);
118                return;
119            }
120            _ => {}
121        }
122
123        if !self.item_is_exportable(def_id) {
124            return;
125        }
126
127        match item.kind {
128            hir::ItemKind::Fn { .. }
129            | hir::ItemKind::Struct(..)
130            | hir::ItemKind::Enum(..)
131            | hir::ItemKind::Union(..)
132            | hir::ItemKind::TyAlias(..) => {
133                self.add_exportable(def_id);
134            }
135            hir::ItemKind::Use(path, _) => {
136                for res in path.res.present_items() {
137                    // Only local items are exportable.
138                    if let Some(res_id) = res.opt_def_id()
139                        && let Some(res_id) = res_id.as_local()
140                    {
141                        self.add_exportable(res_id);
142                    }
143                }
144            }
145            // handled above
146            hir::ItemKind::Mod(..) => unreachable!(),
147            hir::ItemKind::Impl(impl_) if impl_.of_trait.is_none() => {
148                unreachable!();
149            }
150            _ => self.report_wrong_site(def_id),
151        }
152    }
153
154    fn visit_impl_item(&mut self, item: &'tcx hir::ImplItem<'tcx>) {
155        let def_id = item.hir_id().owner.def_id;
156        if !self.item_is_exportable(def_id) {
157            return;
158        }
159        match item.kind {
160            hir::ImplItemKind::Fn(..) | hir::ImplItemKind::Type(..) => {
161                self.add_exportable(def_id);
162            }
163            _ => self.report_wrong_site(def_id),
164        }
165    }
166
167    fn visit_foreign_item(&mut self, item: &'tcx hir::ForeignItem<'tcx>) {
168        let def_id = item.hir_id().owner.def_id;
169        if !self.item_is_exportable(def_id) {
170            self.report_wrong_site(def_id);
171        }
172    }
173
174    fn visit_trait_item(&mut self, item: &'tcx hir::TraitItem<'tcx>) {
175        let def_id = item.hir_id().owner.def_id;
176        if !self.item_is_exportable(def_id) {
177            self.report_wrong_site(def_id);
178        }
179    }
180}
181
182struct ExportableItemsChecker<'tcx, 'a> {
183    tcx: TyCtxt<'tcx>,
184    exportable_items: &'a FxIndexSet<DefId>,
185    item_id: DefId,
186}
187
188impl<'tcx, 'a> ExportableItemsChecker<'tcx, 'a> {
189    fn check(&mut self) {
190        match self.tcx.def_kind(self.item_id) {
191            DefKind::Fn | DefKind::AssocFn => self.check_fn(),
192            DefKind::Enum | DefKind::Struct | DefKind::Union => self.check_ty(),
193            _ => {}
194        }
195    }
196
197    fn check_fn(&mut self) {
198        let def_id = self.item_id.expect_local();
199        let span = self.tcx.def_span(def_id);
200
201        if self.tcx.generics_of(def_id).requires_monomorphization(self.tcx) {
202            self.tcx.dcx().emit_err(UnexportableItem::GenericFn(span));
203            return;
204        }
205
206        let sig = self.tcx.fn_sig(def_id).instantiate_identity().skip_binder();
207        if !matches!(sig.abi, ExternAbi::C { .. }) {
208            self.tcx.dcx().emit_err(UnexportableItem::FnAbi(span));
209            return;
210        }
211
212        let sig = self
213            .tcx
214            .try_normalize_erasing_regions(ty::TypingEnv::non_body_analysis(self.tcx, def_id), sig)
215            .unwrap_or(sig);
216
217        let hir_id = self.tcx.local_def_id_to_hir_id(def_id);
218        let decl = self.tcx.hir_fn_decl_by_hir_id(hir_id).unwrap();
219
220        for (input_ty, input_hir) in iter::zip(sig.inputs(), decl.inputs) {
221            self.check_nested_types_are_exportable(*input_ty, input_hir.span);
222        }
223
224        if let hir::FnRetTy::Return(ret_hir) = decl.output {
225            self.check_nested_types_are_exportable(sig.output(), ret_hir.span);
226        }
227    }
228
229    fn check_ty(&mut self) {
230        let ty = self.tcx.type_of(self.item_id).skip_binder();
231        if let ty::Adt(adt_def, _) = ty.kind() {
232            if !adt_def.repr().inhibit_struct_field_reordering() {
233                self.tcx
234                    .dcx()
235                    .emit_err(UnexportableItem::TypeRepr(self.tcx.def_span(self.item_id)));
236            }
237
238            // FIXME: support `#[export(unsafe_stable_abi = "hash")]` syntax
239            for variant in adt_def.variants() {
240                for field in &variant.fields {
241                    if !field.vis.is_public() {
242                        self.tcx.dcx().emit_err(UnexportableItem::AdtWithPrivFields {
243                            span: self.tcx.def_span(self.item_id),
244                            vis_note: self.tcx.def_span(field.did),
245                            field_name: field.name.as_str(),
246                        });
247                    }
248                }
249            }
250        }
251    }
252
253    fn check_nested_types_are_exportable(&mut self, ty: Ty<'tcx>, ty_span: Span) {
254        let res = ty.visit_with(self);
255        if let Some(err_cause) = res.break_value() {
256            self.tcx.dcx().emit_err(UnexportableItem::TypeInInterface {
257                span: self.tcx.def_span(self.item_id),
258                desc: self.tcx.def_descr(self.item_id),
259                ty: &format!("{}", err_cause),
260                ty_span,
261            });
262        }
263    }
264}
265
266impl<'tcx, 'a> TypeVisitor<TyCtxt<'tcx>> for ExportableItemsChecker<'tcx, 'a> {
267    type Result = ControlFlow<Ty<'tcx>>;
268
269    fn visit_ty(&mut self, ty: Ty<'tcx>) -> Self::Result {
270        match ty.kind() {
271            ty::Adt(adt_def, _) => {
272                let did = adt_def.did();
273                let exportable = if did.is_local() {
274                    self.exportable_items.contains(&did)
275                } else {
276                    self.tcx.is_exportable(did)
277                };
278                if !exportable {
279                    return ControlFlow::Break(ty);
280                }
281                for variant in adt_def.variants() {
282                    for field in &variant.fields {
283                        let field_ty = self.tcx.type_of(field.did).instantiate_identity();
284                        field_ty.visit_with(self)?;
285                    }
286                }
287
288                return ty.super_visit_with(self);
289            }
290
291            ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Bool | ty::Char | ty::Error(_) => {}
292
293            ty::Array(_, _)
294            | ty::Ref(_, _, _)
295            | ty::Param(_)
296            | ty::Closure(_, _)
297            | ty::Dynamic(_, _, _)
298            | ty::Coroutine(_, _)
299            | ty::Foreign(_)
300            | ty::Str
301            | ty::Tuple(_)
302            | ty::Pat(..)
303            | ty::Slice(_)
304            | ty::RawPtr(_, _)
305            | ty::FnDef(_, _)
306            | ty::FnPtr(_, _)
307            | ty::CoroutineClosure(_, _)
308            | ty::CoroutineWitness(_, _)
309            | ty::Never
310            | ty::UnsafeBinder(_)
311            | ty::Alias(ty::AliasTyKind::Opaque, _) => {
312                return ControlFlow::Break(ty);
313            }
314
315            ty::Alias(..) | ty::Infer(_) | ty::Placeholder(_) | ty::Bound(..) => unreachable!(),
316        }
317        ControlFlow::Continue(())
318    }
319}
320
321/// Exportable items:
322///
323/// 1. Structs/enums/unions with a stable representation (e.g. repr(i32) or repr(C)).
324/// 2. Primitive types.
325/// 3. Non-generic functions with a stable ABI (e.g. extern "C") for which every user
326///    defined type used in the signature is also marked as `#[export]`.
327fn exportable_items_provider_local<'tcx>(tcx: TyCtxt<'tcx>, _: LocalCrate) -> &'tcx [DefId] {
328    if !tcx.crate_types().contains(&CrateType::Sdylib) && !tcx.is_sdylib_interface_build() {
329        return &[];
330    }
331
332    let mut visitor = ExportableItemCollector::new(tcx);
333    tcx.hir_walk_toplevel_module(&mut visitor);
334    let exportable_items = visitor.exportable_items;
335    for item_id in exportable_items.iter() {
336        let mut validator =
337            ExportableItemsChecker { tcx, exportable_items: &exportable_items, item_id: *item_id };
338        validator.check();
339    }
340
341    tcx.arena.alloc_from_iter(exportable_items.into_iter())
342}
343
344struct ImplsOrderVisitor<'tcx> {
345    tcx: TyCtxt<'tcx>,
346    order: FxIndexMap<DefId, usize>,
347}
348
349impl<'tcx> ImplsOrderVisitor<'tcx> {
350    fn new(tcx: TyCtxt<'tcx>) -> ImplsOrderVisitor<'tcx> {
351        ImplsOrderVisitor { tcx, order: Default::default() }
352    }
353}
354
355impl<'tcx> Visitor<'tcx> for ImplsOrderVisitor<'tcx> {
356    type NestedFilter = nested_filter::All;
357
358    fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
359        self.tcx
360    }
361
362    fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
363        if let hir::ItemKind::Impl(impl_) = item.kind
364            && impl_.of_trait.is_none()
365            && self.tcx.is_exportable(item.owner_id.def_id.to_def_id())
366        {
367            self.order.insert(item.owner_id.def_id.to_def_id(), self.order.len());
368        }
369        intravisit::walk_item(self, item);
370    }
371}
372
373/// During symbol mangling rustc uses a special index to distinguish between two impls of
374/// the same type in the same module(See `DisambiguatedDefPathData`). For exportable items
375/// we cannot use the current approach because it is dependent on the compiler's
376/// implementation.
377///
378/// In order to make disambiguation independent of the compiler version we can assign an
379/// id to each impl according to the relative order of elements in the source code.
380fn stable_order_of_exportable_impls<'tcx>(
381    tcx: TyCtxt<'tcx>,
382    _: LocalCrate,
383) -> &'tcx FxIndexMap<DefId, usize> {
384    if !tcx.crate_types().contains(&CrateType::Sdylib) && !tcx.is_sdylib_interface_build() {
385        return tcx.arena.alloc(FxIndexMap::<DefId, usize>::default());
386    }
387
388    let mut vis = ImplsOrderVisitor::new(tcx);
389    tcx.hir_walk_toplevel_module(&mut vis);
390    tcx.arena.alloc(vis.order)
391}
392
393pub(crate) fn provide(providers: &mut Providers) {
394    *providers = Providers {
395        exportable_items: exportable_items_provider_local,
396        stable_order_of_exportable_impls,
397        ..*providers
398    };
399}