Skip to main content

rustc_passes/
check_export.rs

1use std::iter;
2use std::ops::ControlFlow;
3
4use rustc_abi::ExternAbi;
5use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
6use rustc_hir as hir;
7use rustc_hir::def::DefKind;
8use rustc_hir::def_id::{DefId, LocalDefId};
9use rustc_hir::find_attr;
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, Unnormalized, 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: &::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}", def_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 = {
        {
            'done:
                {
                for i in
                    ::rustc_hir::attrs::HasAttrs::get_attrs(def_id, &self.tcx) {
                    #[allow(unused_imports)]
                    use rustc_hir::attrs::AttributeKind::*;
                    let i: &rustc_hir::Attribute = i;
                    match i {
                        rustc_hir::Attribute::Parsed(ExportStable) => {
                            break 'done Some(());
                        }
                        rustc_hir::Attribute::Unparsed(..) =>
                            {}
                            #[deny(unreachable_patterns)]
                            _ => {}
                    }
                }
                None
            }
        }
    }.is_some()find_attr!(self.tcx, def_id, 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 {
        {
            'done:
                {
                for i in
                    ::rustc_hir::attrs::HasAttrs::get_attrs(def_id, &self.tcx) {
                    #[allow(unused_imports)]
                    use rustc_hir::attrs::AttributeKind::*;
                    let i: &rustc_hir::Attribute = i;
                    match i {
                        rustc_hir::Attribute::Parsed(ExportStable) => {
                            break 'done Some(());
                        }
                        rustc_hir::Attribute::Unparsed(..) =>
                            {}
                            #[deny(unreachable_patterns)]
                            _ => {}
                    }
                }
                None
            }
        }
    }.is_some()find_attr!(self.tcx, def_id, 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(..) => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
147            hir::ItemKind::Impl(impl_) if impl_.of_trait.is_none() => {
148                ::core::panicking::panic("internal error: entered unreachable code");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 !#[allow(non_exhaustive_omitted_patterns)] match sig.abi() {
    ExternAbi::C { .. } => true,
    _ => false,
}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(
215                ty::TypingEnv::non_body_analysis(self.tcx, def_id),
216                Unnormalized::new_wip(sig),
217            )
218            .unwrap_or(sig);
219
220        let hir_id = self.tcx.local_def_id_to_hir_id(def_id);
221        let decl = self.tcx.hir_fn_decl_by_hir_id(hir_id).unwrap();
222
223        for (input_ty, input_hir) in iter::zip(sig.inputs(), decl.inputs) {
224            self.check_nested_types_are_exportable(*input_ty, input_hir.span);
225        }
226
227        if let hir::FnRetTy::Return(ret_hir) = decl.output {
228            self.check_nested_types_are_exportable(sig.output(), ret_hir.span);
229        }
230    }
231
232    fn check_ty(&mut self) {
233        let ty = self.tcx.type_of(self.item_id).skip_binder();
234        if let ty::Adt(adt_def, _) = ty.kind() {
235            if !adt_def.repr().inhibit_struct_field_reordering() {
236                self.tcx
237                    .dcx()
238                    .emit_err(UnexportableItem::TypeRepr(self.tcx.def_span(self.item_id)));
239            }
240
241            // FIXME: support `#[export(unsafe_stable_abi = "hash")]` syntax
242            for variant in adt_def.variants() {
243                for field in &variant.fields {
244                    if !field.vis.is_public() {
245                        self.tcx.dcx().emit_err(UnexportableItem::AdtWithPrivFields {
246                            span: self.tcx.def_span(self.item_id),
247                            vis_note: self.tcx.def_span(field.did),
248                            field_name: field.name.as_str(),
249                        });
250                    }
251                }
252            }
253        }
254    }
255
256    fn check_nested_types_are_exportable(&mut self, ty: Ty<'tcx>, ty_span: Span) {
257        let res = ty.visit_with(self);
258        if let Some(err_cause) = res.break_value() {
259            self.tcx.dcx().emit_err(UnexportableItem::TypeInInterface {
260                span: self.tcx.def_span(self.item_id),
261                desc: self.tcx.def_descr(self.item_id),
262                ty: &::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}", err_cause))
    })format!("{}", err_cause),
263                ty_span,
264            });
265        }
266    }
267}
268
269impl<'tcx, 'a> TypeVisitor<TyCtxt<'tcx>> for ExportableItemsChecker<'tcx, 'a> {
270    type Result = ControlFlow<Ty<'tcx>>;
271
272    fn visit_ty(&mut self, ty: Ty<'tcx>) -> Self::Result {
273        match ty.kind() {
274            ty::Adt(adt_def, _) => {
275                let did = adt_def.did();
276                let exportable = if did.is_local() {
277                    self.exportable_items.contains(&did)
278                } else {
279                    self.tcx.is_exportable(did)
280                };
281                if !exportable {
282                    return ControlFlow::Break(ty);
283                }
284                for variant in adt_def.variants() {
285                    for field in &variant.fields {
286                        let field_ty =
287                            self.tcx.type_of(field.did).instantiate_identity().skip_norm_wip();
288                        field_ty.visit_with(self)?;
289                    }
290                }
291
292                return ty.super_visit_with(self);
293            }
294
295            ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Bool | ty::Char | ty::Error(_) => {}
296
297            ty::Array(_, _)
298            | ty::Ref(_, _, _)
299            | ty::Param(_)
300            | ty::Closure(_, _)
301            | ty::Dynamic(_, _)
302            | ty::Coroutine(_, _)
303            | ty::Foreign(_)
304            | ty::Str
305            | ty::Tuple(_)
306            | ty::Pat(..)
307            | ty::Slice(_)
308            | ty::RawPtr(_, _)
309            | ty::FnDef(_, _)
310            | ty::FnPtr(_, _)
311            | ty::CoroutineClosure(_, _)
312            | ty::CoroutineWitness(_, _)
313            | ty::Never
314            | ty::UnsafeBinder(_)
315            | ty::Alias(ty::AliasTy { kind: ty::AliasTyKind::Opaque { .. }, .. }) => {
316                return ControlFlow::Break(ty);
317            }
318
319            ty::Alias(..) | ty::Infer(_) | ty::Placeholder(_) | ty::Bound(..) => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
320        }
321        ControlFlow::Continue(())
322    }
323}
324
325/// Exportable items:
326///
327/// 1. Structs/enums/unions with a stable representation (e.g. repr(i32) or repr(C)).
328/// 2. Primitive types.
329/// 3. Non-generic functions with a stable ABI (e.g. extern "C") for which every user
330///    defined type used in the signature is also marked as `#[export]`.
331fn exportable_items_provider_local<'tcx>(tcx: TyCtxt<'tcx>, _: LocalCrate) -> &'tcx [DefId] {
332    if !tcx.crate_types().contains(&CrateType::Sdylib) && !tcx.is_sdylib_interface_build() {
333        return &[];
334    }
335
336    let mut visitor = ExportableItemCollector::new(tcx);
337    tcx.hir_walk_toplevel_module(&mut visitor);
338    let exportable_items = visitor.exportable_items;
339    for item_id in exportable_items.iter() {
340        let mut validator =
341            ExportableItemsChecker { tcx, exportable_items: &exportable_items, item_id: *item_id };
342        validator.check();
343    }
344
345    tcx.arena.alloc_from_iter(exportable_items.into_iter())
346}
347
348struct ImplsOrderVisitor<'tcx> {
349    tcx: TyCtxt<'tcx>,
350    order: FxIndexMap<DefId, usize>,
351}
352
353impl<'tcx> ImplsOrderVisitor<'tcx> {
354    fn new(tcx: TyCtxt<'tcx>) -> ImplsOrderVisitor<'tcx> {
355        ImplsOrderVisitor { tcx, order: Default::default() }
356    }
357}
358
359impl<'tcx> Visitor<'tcx> for ImplsOrderVisitor<'tcx> {
360    type NestedFilter = nested_filter::All;
361
362    fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
363        self.tcx
364    }
365
366    fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
367        if let hir::ItemKind::Impl(impl_) = item.kind
368            && impl_.of_trait.is_none()
369            && self.tcx.is_exportable(item.owner_id.def_id.to_def_id())
370        {
371            self.order.insert(item.owner_id.def_id.to_def_id(), self.order.len());
372        }
373        intravisit::walk_item(self, item);
374    }
375}
376
377/// During symbol mangling rustc uses a special index to distinguish between two impls of
378/// the same type in the same module(See `DisambiguatedDefPathData`). For exportable items
379/// we cannot use the current approach because it is dependent on the compiler's
380/// implementation.
381///
382/// In order to make disambiguation independent of the compiler version we can assign an
383/// id to each impl according to the relative order of elements in the source code.
384fn stable_order_of_exportable_impls<'tcx>(
385    tcx: TyCtxt<'tcx>,
386    _: LocalCrate,
387) -> &'tcx FxIndexMap<DefId, usize> {
388    if !tcx.crate_types().contains(&CrateType::Sdylib) && !tcx.is_sdylib_interface_build() {
389        return tcx.arena.alloc(FxIndexMap::<DefId, usize>::default());
390    }
391
392    let mut vis = ImplsOrderVisitor::new(tcx);
393    tcx.hir_walk_toplevel_module(&mut vis);
394    tcx.arena.alloc(vis.order)
395}
396
397pub(crate) fn provide(providers: &mut Providers) {
398    *providers = Providers {
399        exportable_items: exportable_items_provider_local,
400        stable_order_of_exportable_impls,
401        ..*providers
402    };
403}