rustc_passes/reachable.rs
1//! Finds local items that are "reachable", which means that other crates need access to their
2//! compiled code or their *runtime* MIR. (Compile-time MIR is always encoded anyway, so we don't
3//! worry about that here.)
4//!
5//! An item is "reachable" if codegen that happens in downstream crates can end up referencing this
6//! item. This obviously includes all public items. However, some of these items cannot be codegen'd
7//! (because they are generic), and for some the compiled code is not sufficient (because we want to
8//! cross-crate inline them). These items "need cross-crate MIR". When a reachable function `f`
9//! needs cross-crate MIR, then its MIR may be codegen'd in a downstream crate, and hence items it
10//! mentions need to be considered reachable.
11//!
12//! Furthermore, if a `const`/`const fn` is reachable, then it can return pointers to other items,
13//! making those reachable as well. For instance, consider a `const fn` returning a pointer to an
14//! otherwise entirely private function: if a downstream crate calls that `const fn` to compute the
15//! initial value of a `static`, then it needs to generate a direct reference to this function --
16//! i.e., the function is directly reachable from that downstream crate! Hence we have to recurse
17//! into `const` and `const fn`.
18//!
19//! Conversely, reachability *stops* when it hits a monomorphic non-`const` function that we do not
20//! want to cross-crate inline. That function will just be codegen'd in this crate, which means the
21//! monomorphization collector will consider it a root and then do another graph traversal to
22//! codegen everything called by this function -- but that's a very different graph from what we are
23//! considering here as at that point, everything is monomorphic.
24
25use hir::def_id::LocalDefIdSet;
26use rustc_data_structures::stack::ensure_sufficient_stack;
27use rustc_hir as hir;
28use rustc_hir::Node;
29use rustc_hir::def::{DefKind, Res};
30use rustc_hir::def_id::{DefId, LocalDefId};
31use rustc_hir::intravisit::{self, Visitor};
32use rustc_middle::bug;
33use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
34use rustc_middle::middle::privacy::{self, Level};
35use rustc_middle::mir::interpret::{ConstAllocation, ErrorHandled, GlobalAlloc};
36use rustc_middle::query::Providers;
37use rustc_middle::ty::{self, ExistentialTraitRef, TyCtxt};
38use rustc_privacy::DefIdVisitor;
39use rustc_session::config::CrateType;
40use tracing::debug;
41
42/// Determines whether this item is recursive for reachability. See `is_recursively_reachable_local`
43/// below for details.
44fn recursively_reachable(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
45 tcx.generics_of(def_id).requires_monomorphization(tcx)
46 || tcx.cross_crate_inlinable(def_id)
47 || tcx.is_const_fn(def_id)
48}
49
50// Information needed while computing reachability.
51struct ReachableContext<'tcx> {
52 // The type context.
53 tcx: TyCtxt<'tcx>,
54 maybe_typeck_results: Option<&'tcx ty::TypeckResults<'tcx>>,
55 // The set of items which must be exported in the linkage sense.
56 reachable_symbols: LocalDefIdSet,
57 // A worklist of item IDs. Each item ID in this worklist will be inlined
58 // and will be scanned for further references.
59 // FIXME(eddyb) benchmark if this would be faster as a `VecDeque`.
60 worklist: Vec<LocalDefId>,
61 // Whether any output of this compilation is a library
62 any_library: bool,
63}
64
65impl<'tcx> Visitor<'tcx> for ReachableContext<'tcx> {
66 fn visit_nested_body(&mut self, body: hir::BodyId) {
67 let old_maybe_typeck_results =
68 self.maybe_typeck_results.replace(self.tcx.typeck_body(body));
69 let body = self.tcx.hir_body(body);
70 self.visit_body(body);
71 self.maybe_typeck_results = old_maybe_typeck_results;
72 }
73
74 fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
75 let res = match expr.kind {
76 hir::ExprKind::Path(ref qpath) => {
77 // This covers fn ptr casts but also "non-method" calls.
78 Some(self.typeck_results().qpath_res(qpath, expr.hir_id))
79 }
80 hir::ExprKind::MethodCall(..) => {
81 // Method calls don't involve a full "path", so we need to determine the callee
82 // based on the receiver type.
83 // If this is a method call on a generic type, we might not be able to find the
84 // callee. That's why `reachable_set` also adds all potential callees for such
85 // calls, i.e. all trait impl items, to the reachable set. So here we only worry
86 // about the calls we can identify.
87 self.typeck_results()
88 .type_dependent_def(expr.hir_id)
89 .map(|(kind, def_id)| Res::Def(kind, def_id))
90 }
91 hir::ExprKind::Closure(&hir::Closure { def_id, .. }) => {
92 self.reachable_symbols.insert(def_id);
93 None
94 }
95 _ => None,
96 };
97
98 if let Some(res) = res {
99 self.propagate_item(res);
100 }
101
102 intravisit::walk_expr(self, expr)
103 }
104
105 fn visit_inline_asm(&mut self, asm: &'tcx hir::InlineAsm<'tcx>, id: hir::HirId) {
106 for (op, _) in asm.operands {
107 if let hir::InlineAsmOperand::SymStatic { def_id, .. } = op
108 && let Some(def_id) = def_id.as_local()
109 {
110 self.reachable_symbols.insert(def_id);
111 }
112 }
113 intravisit::walk_inline_asm(self, asm, id);
114 }
115}
116
117impl<'tcx> ReachableContext<'tcx> {
118 /// Gets the type-checking results for the current body.
119 /// As this will ICE if called outside bodies, only call when working with
120 /// `Expr` or `Pat` nodes (they are guaranteed to be found only in bodies).
121 #[track_caller]
122 fn typeck_results(&self) -> &'tcx ty::TypeckResults<'tcx> {
123 self.maybe_typeck_results
124 .expect("`ReachableContext::typeck_results` called outside of body")
125 }
126
127 /// Returns true if the given def ID represents a local item that is recursive for reachability,
128 /// i.e. whether everything mentioned in here also needs to be considered reachable.
129 ///
130 /// There are two reasons why an item may be recursively reachable:
131 /// - It needs cross-crate MIR (see the module-level doc comment above).
132 /// - It is a `const` or `const fn`. This is *not* because we need the MIR to interpret them
133 /// (MIR for const-eval and MIR for codegen is separate, and MIR for const-eval is always
134 /// encoded). Instead, it is because `const fn` can create `fn()` pointers to other items
135 /// which end up in the evaluated result of the constant and can then be called from other
136 /// crates. Those items must be considered reachable.
137 fn is_recursively_reachable_local(&self, def_id: DefId) -> bool {
138 let Some(def_id) = def_id.as_local() else {
139 return false;
140 };
141
142 match self.tcx.hir_node_by_def_id(def_id) {
143 Node::Item(item) => match item.kind {
144 hir::ItemKind::Fn { .. } => recursively_reachable(self.tcx, def_id.into()),
145 _ => false,
146 },
147 Node::TraitItem(trait_method) => match trait_method.kind {
148 hir::TraitItemKind::Const(_, ref default) => default.is_some(),
149 hir::TraitItemKind::Fn(_, hir::TraitFn::Provided(_)) => true,
150 hir::TraitItemKind::Fn(_, hir::TraitFn::Required(_))
151 | hir::TraitItemKind::Type(..) => false,
152 },
153 Node::ImplItem(impl_item) => match impl_item.kind {
154 hir::ImplItemKind::Const(..) => true,
155 hir::ImplItemKind::Fn(..) => {
156 recursively_reachable(self.tcx, impl_item.hir_id().owner.to_def_id())
157 }
158 hir::ImplItemKind::Type(_) => false,
159 },
160 Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure(..), .. }) => true,
161 _ => false,
162 }
163 }
164
165 // Step 2: Mark all symbols that the symbols on the worklist touch.
166 fn propagate(&mut self) {
167 let mut scanned = LocalDefIdSet::default();
168 while let Some(search_item) = self.worklist.pop() {
169 if !scanned.insert(search_item) {
170 continue;
171 }
172
173 self.propagate_node(&self.tcx.hir_node_by_def_id(search_item), search_item);
174 }
175 }
176
177 fn propagate_node(&mut self, node: &Node<'tcx>, search_item: LocalDefId) {
178 if !self.any_library {
179 // If we are building an executable, only explicitly extern
180 // types need to be exported.
181 let codegen_attrs = if self.tcx.def_kind(search_item).has_codegen_attrs() {
182 self.tcx.codegen_fn_attrs(search_item)
183 } else {
184 CodegenFnAttrs::EMPTY
185 };
186 let is_extern = codegen_attrs.contains_extern_indicator();
187 // Right now, the only way to get "foreign item symbol aliases" is by being an EII-implementation.
188 // EII implementations will generate under their own name but also under the name of some foreign item
189 // (hence alias) that may be in another crate. These functions are marked as always-reachable since
190 // it's very hard to track whether the original foreign item was reachable. It may live in another crate
191 // and may be reachable from sibling crates.
192 let has_foreign_aliases_eii = !codegen_attrs.foreign_item_symbol_aliases.is_empty();
193 if is_extern || has_foreign_aliases_eii {
194 self.reachable_symbols.insert(search_item);
195 }
196 } else {
197 // If we are building a library, then reachable symbols will
198 // continue to participate in linkage after this product is
199 // produced. In this case, we traverse the ast node, recursing on
200 // all reachable nodes from this one.
201 self.reachable_symbols.insert(search_item);
202 }
203
204 match *node {
205 Node::Item(item) => {
206 match item.kind {
207 hir::ItemKind::Fn { body, .. } => {
208 if recursively_reachable(self.tcx, item.owner_id.into()) {
209 self.visit_nested_body(body);
210 }
211 }
212
213 hir::ItemKind::Const(_, _, _, init) => {
214 // Only things actually ending up in the final constant value are reachable
215 // for codegen. Everything else is only needed during const-eval, so even if
216 // const-eval happens in a downstream crate, all they need is
217 // `mir_for_ctfe`.
218 match self.tcx.const_eval_poly_to_alloc(item.owner_id.def_id.into()) {
219 Ok(alloc) => {
220 let alloc = self.tcx.global_alloc(alloc.alloc_id).unwrap_memory();
221 self.propagate_from_alloc(alloc);
222 }
223 // We can't figure out which value the constant will evaluate to. In
224 // lieu of that, we have to consider everything mentioned in the const
225 // initializer reachable, since it *may* end up in the final value.
226 Err(ErrorHandled::TooGeneric(_)) => self.visit_const_item_rhs(init),
227 // If there was an error evaluating the const, nothing can be reachable
228 // via it, and anyway compilation will fail.
229 Err(ErrorHandled::Reported(..)) => {}
230 }
231 }
232 hir::ItemKind::Static(..) => {
233 if let Ok(alloc) = self.tcx.eval_static_initializer(item.owner_id.def_id) {
234 self.propagate_from_alloc(alloc);
235 }
236 }
237
238 // These are normal, nothing reachable about these
239 // inherently and their children are already in the
240 // worklist, as determined by the privacy pass
241 hir::ItemKind::ExternCrate(..)
242 | hir::ItemKind::Use(..)
243 | hir::ItemKind::TyAlias(..)
244 | hir::ItemKind::Macro(..)
245 | hir::ItemKind::Mod(..)
246 | hir::ItemKind::ForeignMod { .. }
247 | hir::ItemKind::Impl { .. }
248 | hir::ItemKind::Trait(..)
249 | hir::ItemKind::TraitAlias(..)
250 | hir::ItemKind::Struct(..)
251 | hir::ItemKind::Enum(..)
252 | hir::ItemKind::Union(..)
253 | hir::ItemKind::GlobalAsm { .. } => {}
254 }
255 }
256 Node::TraitItem(trait_method) => {
257 match trait_method.kind {
258 hir::TraitItemKind::Const(_, None)
259 | hir::TraitItemKind::Fn(_, hir::TraitFn::Required(_)) => {
260 // Keep going, nothing to get exported
261 }
262 hir::TraitItemKind::Const(_, Some(rhs)) => self.visit_const_item_rhs(rhs),
263 hir::TraitItemKind::Fn(_, hir::TraitFn::Provided(body_id)) => {
264 self.visit_nested_body(body_id);
265 }
266 hir::TraitItemKind::Type(..) => {}
267 }
268 }
269 Node::ImplItem(impl_item) => match impl_item.kind {
270 hir::ImplItemKind::Const(_, rhs) => {
271 self.visit_const_item_rhs(rhs);
272 }
273 hir::ImplItemKind::Fn(_, body) => {
274 if recursively_reachable(self.tcx, impl_item.hir_id().owner.to_def_id()) {
275 self.visit_nested_body(body)
276 }
277 }
278 hir::ImplItemKind::Type(_) => {}
279 },
280 Node::Expr(&hir::Expr {
281 kind: hir::ExprKind::Closure(&hir::Closure { body, .. }),
282 ..
283 }) => {
284 self.visit_nested_body(body);
285 }
286 // Nothing to recurse on for these
287 Node::ForeignItem(_)
288 | Node::Variant(_)
289 | Node::Ctor(..)
290 | Node::Field(_)
291 | Node::Ty(_)
292 | Node::Crate(_)
293 | Node::Synthetic
294 | Node::OpaqueTy(..) => {}
295 _ => {
296 bug!(
297 "found unexpected node kind in worklist: {} ({:?})",
298 self.tcx.hir_id_to_string(self.tcx.local_def_id_to_hir_id(search_item)),
299 node,
300 );
301 }
302 }
303 }
304
305 /// Finds things to add to `reachable_symbols` within allocations.
306 /// In contrast to visit_nested_body this ignores things that were only needed to evaluate
307 /// the allocation.
308 fn propagate_from_alloc(&mut self, alloc: ConstAllocation<'tcx>) {
309 if !self.any_library {
310 return;
311 }
312 for (_, prov) in alloc.0.provenance().ptrs().iter() {
313 match self.tcx.global_alloc(prov.alloc_id()) {
314 GlobalAlloc::Static(def_id) => {
315 self.propagate_item(Res::Def(self.tcx.def_kind(def_id), def_id))
316 }
317 GlobalAlloc::Function { instance, .. } => {
318 // Manually visit to actually see the instance's `DefId`. Type visitors won't see it
319 self.propagate_item(Res::Def(
320 self.tcx.def_kind(instance.def_id()),
321 instance.def_id(),
322 ));
323 self.visit(instance.args);
324 }
325 GlobalAlloc::VTable(ty, dyn_ty) => {
326 self.visit(ty);
327 // Manually visit to actually see the trait's `DefId`. Type visitors won't see it
328 if let Some(trait_ref) = dyn_ty.principal() {
329 let ExistentialTraitRef { def_id, args, .. } = trait_ref.skip_binder();
330 self.visit_def_id(def_id, "", &"");
331 self.visit(args);
332 }
333 }
334 GlobalAlloc::TypeId { ty, .. } => self.visit(ty),
335 GlobalAlloc::Memory(alloc) => self.propagate_from_alloc(alloc),
336 }
337 }
338 }
339
340 fn propagate_item(&mut self, res: Res) {
341 let Res::Def(kind, def_id) = res else { return };
342 let Some(def_id) = def_id.as_local() else { return };
343 match kind {
344 DefKind::Static { nested: true, .. } => {
345 // This is the main purpose of this function: add the def_id we find
346 // to `reachable_symbols`.
347 if self.reachable_symbols.insert(def_id) {
348 if let Ok(alloc) = self.tcx.eval_static_initializer(def_id) {
349 // This cannot cause infinite recursion, because we abort by inserting into the
350 // work list once we hit a normal static. Nested statics, even if they somehow
351 // become recursive, are also not infinitely recursing, because of the
352 // `reachable_symbols` check above.
353 // We still need to protect against stack overflow due to deeply nested statics.
354 ensure_sufficient_stack(|| self.propagate_from_alloc(alloc));
355 }
356 }
357 }
358 // Reachable constants and reachable statics can have their contents inlined
359 // into other crates. Mark them as reachable and recurse into their body.
360 DefKind::Const | DefKind::AssocConst | DefKind::Static { .. } => {
361 self.worklist.push(def_id);
362 }
363 _ => {
364 if self.is_recursively_reachable_local(def_id.to_def_id()) {
365 self.worklist.push(def_id);
366 } else {
367 self.reachable_symbols.insert(def_id);
368 }
369 }
370 }
371 }
372}
373
374impl<'tcx> DefIdVisitor<'tcx> for ReachableContext<'tcx> {
375 type Result = ();
376
377 fn tcx(&self) -> TyCtxt<'tcx> {
378 self.tcx
379 }
380
381 fn visit_def_id(
382 &mut self,
383 def_id: DefId,
384 _kind: &str,
385 _descr: &dyn std::fmt::Display,
386 ) -> Self::Result {
387 self.propagate_item(Res::Def(self.tcx.def_kind(def_id), def_id))
388 }
389}
390
391fn check_item<'tcx>(
392 tcx: TyCtxt<'tcx>,
393 id: hir::ItemId,
394 worklist: &mut Vec<LocalDefId>,
395 effective_visibilities: &privacy::EffectiveVisibilities,
396) {
397 if has_custom_linkage(tcx, id.owner_id.def_id) {
398 worklist.push(id.owner_id.def_id);
399 }
400
401 if !matches!(tcx.def_kind(id.owner_id), DefKind::Impl { of_trait: true }) {
402 return;
403 }
404
405 // We need only trait impls here, not inherent impls, and only non-exported ones
406 if effective_visibilities.is_reachable(id.owner_id.def_id) {
407 return;
408 }
409
410 let items = tcx.associated_item_def_ids(id.owner_id);
411 worklist.extend(items.iter().map(|ii_ref| ii_ref.expect_local()));
412
413 let trait_def_id = tcx.impl_trait_id(id.owner_id.to_def_id());
414
415 if !trait_def_id.is_local() {
416 return;
417 }
418
419 worklist
420 .extend(tcx.provided_trait_methods(trait_def_id).map(|assoc| assoc.def_id.expect_local()));
421}
422
423fn has_custom_linkage(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
424 // Anything which has custom linkage gets thrown on the worklist no
425 // matter where it is in the crate, along with "special std symbols"
426 // which are currently akin to allocator symbols.
427 if !tcx.def_kind(def_id).has_codegen_attrs() {
428 return false;
429 }
430
431 let codegen_attrs = tcx.codegen_fn_attrs(def_id);
432 codegen_attrs.contains_extern_indicator()
433 // FIXME(nbdd0121): `#[used]` are marked as reachable here so it's picked up by
434 // `linked_symbols` in cg_ssa. They won't be exported in binary or cdylib due to their
435 // `SymbolExportLevel::Rust` export level but may end up being exported in dylibs.
436 || codegen_attrs.flags.contains(CodegenFnAttrFlags::USED_COMPILER)
437 || codegen_attrs.flags.contains(CodegenFnAttrFlags::USED_LINKER)
438 // Right now, the only way to get "foreign item symbol aliases" is by being an EII-implementation.
439 // EII implementations will generate under their own name but also under the name of some foreign item
440 // (hence alias) that may be in another crate. These functions are marked as always-reachable since
441 // it's very hard to track whether the original foreign item was reachable. It may live in another crate
442 // and may be reachable from sibling crates.
443 || !codegen_attrs.foreign_item_symbol_aliases.is_empty()
444}
445
446/// See module-level doc comment above.
447fn reachable_set(tcx: TyCtxt<'_>, (): ()) -> LocalDefIdSet {
448 let effective_visibilities = &tcx.effective_visibilities(());
449
450 let any_library = tcx.crate_types().iter().any(|ty| {
451 *ty == CrateType::Rlib
452 || *ty == CrateType::Dylib
453 || *ty == CrateType::ProcMacro
454 || *ty == CrateType::Sdylib
455 });
456 let mut reachable_context = ReachableContext {
457 tcx,
458 maybe_typeck_results: None,
459 reachable_symbols: Default::default(),
460 worklist: Vec::new(),
461 any_library,
462 };
463
464 // Step 1: Seed the worklist with all nodes which were found to be public as
465 // a result of the privacy pass along with all local lang items and impl items.
466 // If other crates link to us, they're going to expect to be able to
467 // use the lang items, so we need to be sure to mark them as
468 // exported.
469 reachable_context.worklist = effective_visibilities
470 .iter()
471 .filter_map(|(&id, effective_vis)| {
472 effective_vis.is_public_at_level(Level::ReachableThroughImplTrait).then_some(id)
473 })
474 .collect::<Vec<_>>();
475
476 for (_, def_id) in tcx.lang_items().iter() {
477 if let Some(def_id) = def_id.as_local() {
478 reachable_context.worklist.push(def_id);
479 }
480 }
481 {
482 // As explained above, we have to mark all functions called from reachable
483 // `item_might_be_inlined` items as reachable. The issue is, when those functions are
484 // generic and call a trait method, we have no idea where that call goes! So, we
485 // conservatively mark all trait impl items as reachable.
486 // FIXME: One possible strategy for pruning the reachable set is to avoid marking impl
487 // items of non-exported traits (or maybe all local traits?) unless their respective
488 // trait items are used from inlinable code through method call syntax or UFCS, or their
489 // trait is a lang item.
490 // (But if you implement this, don't forget to take into account that vtables can also
491 // make trait methods reachable!)
492 let crate_items = tcx.hir_crate_items(());
493
494 for id in crate_items.free_items() {
495 check_item(tcx, id, &mut reachable_context.worklist, effective_visibilities);
496 }
497
498 for id in crate_items.impl_items() {
499 if has_custom_linkage(tcx, id.owner_id.def_id) {
500 reachable_context.worklist.push(id.owner_id.def_id);
501 }
502 }
503 }
504
505 // Step 2: Mark all symbols that the symbols on the worklist touch.
506 reachable_context.propagate();
507
508 debug!("Inline reachability shows: {:?}", reachable_context.reachable_symbols);
509
510 // Return the set of reachable symbols.
511 reachable_context.reachable_symbols
512}
513
514pub(crate) fn provide(providers: &mut Providers) {
515 *providers = Providers { reachable_set, ..*providers };
516}