2013-06-15 01:21:47 +00:00
|
|
|
// Finds items that are externally reachable, to determine which items
|
|
|
|
// need to have their metadata (and possibly their AST) serialized.
|
|
|
|
// All items that can be referred to through an exported name are
|
|
|
|
// reachable, and when a reachable thing is inline or generic, it
|
|
|
|
// makes all other generics or inline functions that it references
|
|
|
|
// reachable as well.
|
|
|
|
|
2019-12-24 04:02:53 +00:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2020-01-05 01:37:57 +00:00
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_hir::def::{DefKind, Res};
|
2021-05-11 09:35:50 +00:00
|
|
|
use rustc_hir::def_id::{DefId, LocalDefId};
|
2020-03-11 11:05:32 +00:00
|
|
|
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
|
2020-01-05 01:37:57 +00:00
|
|
|
use rustc_hir::itemlikevisit::ItemLikeVisitor;
|
2020-07-14 23:51:46 +00:00
|
|
|
use rustc_hir::Node;
|
2020-03-29 15:19:48 +00:00
|
|
|
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
|
|
|
|
use rustc_middle::middle::privacy;
|
|
|
|
use rustc_middle::ty::query::Providers;
|
2020-07-14 23:51:46 +00:00
|
|
|
use rustc_middle::ty::{self, DefIdTree, TyCtxt};
|
2020-05-01 22:30:23 +00:00
|
|
|
use rustc_session::config::CrateType;
|
2019-12-22 22:42:04 +00:00
|
|
|
use rustc_target::spec::abi::Abi;
|
2013-06-15 01:21:47 +00:00
|
|
|
|
|
|
|
// Returns true if the given item must be inlined because it may be
|
|
|
|
// monomorphized or it was marked with `#[inline]`. This will only return
|
|
|
|
// true for functions.
|
2020-04-14 22:07:31 +00:00
|
|
|
fn item_might_be_inlined(tcx: TyCtxt<'tcx>, item: &hir::Item<'_>, attrs: &CodegenFnAttrs) -> bool {
|
2018-01-31 03:39:23 +00:00
|
|
|
if attrs.requests_inline() {
|
2019-12-22 22:42:04 +00:00
|
|
|
return true;
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
|
2019-09-26 16:51:36 +00:00
|
|
|
match item.kind {
|
2020-03-20 14:03:11 +00:00
|
|
|
hir::ItemKind::Fn(ref sig, ..) if sig.header.is_const() => true,
|
2020-01-18 00:14:29 +00:00
|
|
|
hir::ItemKind::Impl { .. } | hir::ItemKind::Fn(..) => {
|
2021-01-30 16:47:51 +00:00
|
|
|
let generics = tcx.generics_of(item.def_id);
|
2018-06-28 21:13:34 +00:00
|
|
|
generics.requires_monomorphization(tcx)
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-21 21:49:03 +00:00
|
|
|
fn method_might_be_inlined(
|
|
|
|
tcx: TyCtxt<'_>,
|
2019-11-28 21:16:44 +00:00
|
|
|
impl_item: &hir::ImplItem<'_>,
|
2020-04-08 14:42:53 +00:00
|
|
|
impl_src: LocalDefId,
|
2019-06-11 21:11:55 +00:00
|
|
|
) -> bool {
|
2021-01-30 22:25:03 +00:00
|
|
|
let codegen_fn_attrs = tcx.codegen_fn_attrs(impl_item.hir_id().owner.to_def_id());
|
|
|
|
let generics = tcx.generics_of(impl_item.def_id);
|
2018-06-28 21:13:34 +00:00
|
|
|
if codegen_fn_attrs.requests_inline() || generics.requires_monomorphization(tcx) {
|
2019-12-22 22:42:04 +00:00
|
|
|
return true;
|
2013-11-01 03:47:23 +00:00
|
|
|
}
|
2020-03-05 15:57:34 +00:00
|
|
|
if let hir::ImplItemKind::Fn(method_sig, _) = &impl_item.kind {
|
2019-08-15 08:33:21 +00:00
|
|
|
if method_sig.header.is_const() {
|
2019-12-22 22:42:04 +00:00
|
|
|
return true;
|
2019-08-15 04:56:57 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-12 10:22:56 +00:00
|
|
|
match tcx.hir().find(tcx.hir().local_def_id_to_hir_id(impl_src)) {
|
2020-04-16 19:36:32 +00:00
|
|
|
Some(Node::Item(item)) => item_might_be_inlined(tcx, &item, codegen_fn_attrs),
|
|
|
|
Some(..) | None => span_bug!(impl_item.span, "impl did is not an item"),
|
2013-11-01 03:47:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 01:21:47 +00:00
|
|
|
// Information needed while computing reachability.
|
2020-06-25 17:25:46 +00:00
|
|
|
struct ReachableContext<'tcx> {
|
2013-06-15 01:21:47 +00:00
|
|
|
// The type context.
|
2019-06-13 21:48:52 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-07-17 08:47:04 +00:00
|
|
|
maybe_typeck_results: Option<&'tcx ty::TypeckResults<'tcx>>,
|
2013-06-15 01:21:47 +00:00
|
|
|
// The set of items which must be exported in the linkage sense.
|
2020-07-14 23:51:46 +00:00
|
|
|
reachable_symbols: FxHashSet<LocalDefId>,
|
2013-06-15 01:21:47 +00:00
|
|
|
// A worklist of item IDs. Each item ID in this worklist will be inlined
|
|
|
|
// and will be scanned for further references.
|
2020-07-14 23:51:46 +00:00
|
|
|
// FIXME(eddyb) benchmark if this would be faster as a `VecDeque`.
|
|
|
|
worklist: Vec<LocalDefId>,
|
2014-05-02 22:26:45 +00:00
|
|
|
// Whether any output of this compilation is a library
|
|
|
|
any_library: bool,
|
2013-08-13 11:53:13 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 17:25:46 +00:00
|
|
|
impl<'tcx> Visitor<'tcx> for ReachableContext<'tcx> {
|
2020-03-11 11:05:32 +00:00
|
|
|
type Map = intravisit::ErasedMap<'tcx>;
|
2020-01-07 16:25:33 +00:00
|
|
|
|
2020-02-09 14:32:00 +00:00
|
|
|
fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
|
2017-01-06 19:54:24 +00:00
|
|
|
NestedVisitorMap::None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_nested_body(&mut self, body: hir::BodyId) {
|
2020-07-17 08:47:04 +00:00
|
|
|
let old_maybe_typeck_results =
|
|
|
|
self.maybe_typeck_results.replace(self.tcx.typeck_body(body));
|
2018-12-04 12:45:36 +00:00
|
|
|
let body = self.tcx.hir().body(body);
|
2017-01-06 19:54:24 +00:00
|
|
|
self.visit_body(body);
|
2020-07-17 08:47:04 +00:00
|
|
|
self.maybe_typeck_results = old_maybe_typeck_results;
|
2016-10-28 20:58:32 +00:00
|
|
|
}
|
|
|
|
|
2019-11-29 12:43:03 +00:00
|
|
|
fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
|
2019-09-26 13:39:48 +00:00
|
|
|
let res = match expr.kind {
|
2020-07-17 08:47:04 +00:00
|
|
|
hir::ExprKind::Path(ref qpath) => {
|
|
|
|
Some(self.typeck_results().qpath_res(qpath, expr.hir_id))
|
|
|
|
}
|
2019-12-22 22:42:04 +00:00
|
|
|
hir::ExprKind::MethodCall(..) => self
|
2020-07-17 08:47:04 +00:00
|
|
|
.typeck_results()
|
2019-12-22 22:42:04 +00:00
|
|
|
.type_dependent_def(expr.hir_id)
|
|
|
|
.map(|(kind, def_id)| Res::Def(kind, def_id)),
|
|
|
|
_ => None,
|
2016-11-25 11:21:19 +00:00
|
|
|
};
|
|
|
|
|
2020-07-14 23:51:46 +00:00
|
|
|
if let Some(res) = res {
|
|
|
|
if let Some(def_id) = res.opt_def_id().and_then(|def_id| def_id.as_local()) {
|
|
|
|
if self.def_id_represents_local_inlined_item(def_id.to_def_id()) {
|
|
|
|
self.worklist.push(def_id);
|
|
|
|
} else {
|
|
|
|
match res {
|
|
|
|
// If this path leads to a constant, then we need to
|
|
|
|
// recurse into the constant to continue finding
|
|
|
|
// items that are reachable.
|
|
|
|
Res::Def(DefKind::Const | DefKind::AssocConst, _) => {
|
|
|
|
self.worklist.push(def_id);
|
|
|
|
}
|
2015-11-05 15:17:33 +00:00
|
|
|
|
2020-07-14 23:51:46 +00:00
|
|
|
// If this wasn't a static, then the destination is
|
|
|
|
// surely reachable.
|
|
|
|
_ => {
|
|
|
|
self.reachable_symbols.insert(def_id);
|
2016-11-25 11:21:19 +00:00
|
|
|
}
|
2013-08-13 11:53:13 +00:00
|
|
|
}
|
|
|
|
}
|
2013-11-01 03:47:23 +00:00
|
|
|
}
|
|
|
|
}
|
2013-08-13 11:53:13 +00:00
|
|
|
|
2015-11-17 22:51:44 +00:00
|
|
|
intravisit::walk_expr(self, expr)
|
2013-08-13 11:53:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 17:25:46 +00:00
|
|
|
impl<'tcx> ReachableContext<'tcx> {
|
2020-07-17 08:47:04 +00:00
|
|
|
/// Gets the type-checking results for the current body.
|
2020-06-25 17:25:46 +00:00
|
|
|
/// As this will ICE if called outside bodies, only call when working with
|
|
|
|
/// `Expr` or `Pat` nodes (they are guaranteed to be found only in bodies).
|
|
|
|
#[track_caller]
|
2020-07-17 08:47:04 +00:00
|
|
|
fn typeck_results(&self) -> &'tcx ty::TypeckResults<'tcx> {
|
|
|
|
self.maybe_typeck_results
|
|
|
|
.expect("`ReachableContext::typeck_results` called outside of body")
|
2020-06-25 17:25:46 +00:00
|
|
|
}
|
|
|
|
|
2013-06-15 01:21:47 +00:00
|
|
|
// Returns true if the given def ID represents a local item that is
|
|
|
|
// eligible for inlining and false otherwise.
|
2015-08-16 10:32:28 +00:00
|
|
|
fn def_id_represents_local_inlined_item(&self, def_id: DefId) -> bool {
|
2020-04-12 12:45:41 +00:00
|
|
|
let hir_id = match def_id.as_local() {
|
2020-08-12 10:22:56 +00:00
|
|
|
Some(def_id) => self.tcx.hir().local_def_id_to_hir_id(def_id),
|
2019-12-22 22:42:04 +00:00
|
|
|
None => {
|
|
|
|
return false;
|
|
|
|
}
|
2015-09-04 17:52:28 +00:00
|
|
|
};
|
2013-06-15 01:21:47 +00:00
|
|
|
|
2019-06-24 07:58:49 +00:00
|
|
|
match self.tcx.hir().find(hir_id) {
|
2019-12-22 22:42:04 +00:00
|
|
|
Some(Node::Item(item)) => match item.kind {
|
|
|
|
hir::ItemKind::Fn(..) => {
|
|
|
|
item_might_be_inlined(self.tcx, &item, self.tcx.codegen_fn_attrs(def_id))
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
2019-12-22 22:42:04 +00:00
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
Some(Node::TraitItem(trait_method)) => match trait_method.kind {
|
|
|
|
hir::TraitItemKind::Const(_, ref default) => default.is_some(),
|
2020-03-05 15:57:34 +00:00
|
|
|
hir::TraitItemKind::Fn(_, hir::TraitFn::Provided(_)) => true,
|
|
|
|
hir::TraitItemKind::Fn(_, hir::TraitFn::Required(_))
|
2019-12-22 22:42:04 +00:00
|
|
|
| hir::TraitItemKind::Type(..) => false,
|
|
|
|
},
|
2018-08-25 14:56:16 +00:00
|
|
|
Some(Node::ImplItem(impl_item)) => {
|
2019-09-26 15:38:13 +00:00
|
|
|
match impl_item.kind {
|
2015-11-12 14:57:51 +00:00
|
|
|
hir::ImplItemKind::Const(..) => true,
|
2020-03-05 15:57:34 +00:00
|
|
|
hir::ImplItemKind::Fn(..) => {
|
2018-05-08 13:10:16 +00:00
|
|
|
let attrs = self.tcx.codegen_fn_attrs(def_id);
|
2018-06-15 10:45:12 +00:00
|
|
|
let generics = self.tcx.generics_of(def_id);
|
2018-06-28 21:13:34 +00:00
|
|
|
if generics.requires_monomorphization(self.tcx) || attrs.requests_inline() {
|
2014-08-04 20:56:56 +00:00
|
|
|
true
|
|
|
|
} else {
|
2020-04-12 12:45:41 +00:00
|
|
|
let impl_did = self.tcx.hir().get_parent_did(hir_id);
|
2014-08-04 20:56:56 +00:00
|
|
|
// Check the impl. If the generics on the self
|
|
|
|
// type of the impl require inlining, this method
|
|
|
|
// does too.
|
2020-08-12 10:22:56 +00:00
|
|
|
let impl_hir_id = self.tcx.hir().local_def_id_to_hir_id(impl_did);
|
2019-09-26 16:51:36 +00:00
|
|
|
match self.tcx.hir().expect_item(impl_hir_id).kind {
|
2020-01-18 00:14:29 +00:00
|
|
|
hir::ItemKind::Impl { .. } => {
|
2018-06-15 10:45:12 +00:00
|
|
|
let generics = self.tcx.generics_of(impl_did);
|
2018-06-28 21:13:34 +00:00
|
|
|
generics.requires_monomorphization(self.tcx)
|
2014-08-04 20:56:56 +00:00
|
|
|
}
|
2019-12-22 22:42:04 +00:00
|
|
|
_ => false,
|
2014-08-04 20:56:56 +00:00
|
|
|
}
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-10 11:15:51 +00:00
|
|
|
hir::ImplItemKind::TyAlias(_) => false,
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(_) => false,
|
2019-12-22 22:42:04 +00:00
|
|
|
None => false, // This will happen for default methods.
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 2: Mark all symbols that the symbols on the worklist touch.
|
2014-03-09 11:42:22 +00:00
|
|
|
fn propagate(&mut self) {
|
2018-10-16 08:44:26 +00:00
|
|
|
let mut scanned = FxHashSet::default();
|
2018-03-05 18:58:54 +00:00
|
|
|
while let Some(search_item) = self.worklist.pop() {
|
rustc: Add `const` globals to the language
This change is an implementation of [RFC 69][rfc] which adds a third kind of
global to the language, `const`. This global is most similar to what the old
`static` was, and if you're unsure about what to use then you should use a
`const`.
The semantics of these three kinds of globals are:
* A `const` does not represent a memory location, but only a value. Constants
are translated as rvalues, which means that their values are directly inlined
at usage location (similar to a #define in C/C++). Constant values are, well,
constant, and can not be modified. Any "modification" is actually a
modification to a local value on the stack rather than the actual constant
itself.
Almost all values are allowed inside constants, whether they have interior
mutability or not. There are a few minor restrictions listed in the RFC, but
they should in general not come up too often.
* A `static` now always represents a memory location (unconditionally). Any
references to the same `static` are actually a reference to the same memory
location. Only values whose types ascribe to `Sync` are allowed in a `static`.
This restriction is in place because many threads may access a `static`
concurrently. Lifting this restriction (and allowing unsafe access) is a
future extension not implemented at this time.
* A `static mut` continues to always represent a memory location. All references
to a `static mut` continue to be `unsafe`.
This is a large breaking change, and many programs will need to be updated
accordingly. A summary of the breaking changes is:
* Statics may no longer be used in patterns. Statics now always represent a
memory location, which can sometimes be modified. To fix code, repurpose the
matched-on-`static` to a `const`.
static FOO: uint = 4;
match n {
FOO => { /* ... */ }
_ => { /* ... */ }
}
change this code to:
const FOO: uint = 4;
match n {
FOO => { /* ... */ }
_ => { /* ... */ }
}
* Statics may no longer refer to other statics by value. Due to statics being
able to change at runtime, allowing them to reference one another could
possibly lead to confusing semantics. If you are in this situation, use a
constant initializer instead. Note, however, that statics may reference other
statics by address, however.
* Statics may no longer be used in constant expressions, such as array lengths.
This is due to the same restrictions as listed above. Use a `const` instead.
[breaking-change]
[rfc]: https://github.com/rust-lang/rfcs/pull/246
2014-10-06 15:17:01 +00:00
|
|
|
if !scanned.insert(search_item) {
|
2019-12-22 22:42:04 +00:00
|
|
|
continue;
|
2014-03-09 11:42:22 +00:00
|
|
|
}
|
2013-12-22 22:24:09 +00:00
|
|
|
|
2020-07-14 23:51:46 +00:00
|
|
|
if let Some(ref item) =
|
|
|
|
self.tcx.hir().find(self.tcx.hir().local_def_id_to_hir_id(search_item))
|
|
|
|
{
|
2015-10-29 18:54:55 +00:00
|
|
|
self.propagate_node(item, search_item);
|
2013-11-18 07:19:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-14 23:51:46 +00:00
|
|
|
fn propagate_node(&mut self, node: &Node<'tcx>, search_item: LocalDefId) {
|
2014-05-02 22:26:45 +00:00
|
|
|
if !self.any_library {
|
2015-11-07 17:22:04 +00:00
|
|
|
// If we are building an executable, only explicitly extern
|
|
|
|
// types need to be exported.
|
2021-08-14 15:24:33 +00:00
|
|
|
let reachable =
|
|
|
|
if let Node::Item(hir::Item { kind: hir::ItemKind::Fn(sig, ..), .. })
|
|
|
|
| Node::ImplItem(hir::ImplItem {
|
|
|
|
kind: hir::ImplItemKind::Fn(sig, ..), ..
|
|
|
|
}) = *node
|
|
|
|
{
|
|
|
|
sig.header.abi != Abi::Rust
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
let codegen_attrs = self.tcx.codegen_fn_attrs(search_item);
|
|
|
|
let is_extern = codegen_attrs.contains_extern_indicator();
|
|
|
|
let std_internal =
|
|
|
|
codegen_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL);
|
|
|
|
if reachable || is_extern || std_internal {
|
|
|
|
self.reachable_symbols.insert(search_item);
|
2013-11-18 07:19:44 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If we are building a library, then reachable symbols will
|
|
|
|
// continue to participate in linkage after this product is
|
|
|
|
// produced. In this case, we traverse the ast node, recursing on
|
|
|
|
// all reachable nodes from this one.
|
2014-03-09 11:42:22 +00:00
|
|
|
self.reachable_symbols.insert(search_item);
|
2013-11-18 07:19:44 +00:00
|
|
|
}
|
2013-11-01 03:47:23 +00:00
|
|
|
|
2013-11-18 07:19:44 +00:00
|
|
|
match *node {
|
2018-08-25 14:56:16 +00:00
|
|
|
Node::Item(item) => {
|
2019-09-26 16:51:36 +00:00
|
|
|
match item.kind {
|
2018-07-11 15:36:06 +00:00
|
|
|
hir::ItemKind::Fn(.., body) => {
|
2021-01-30 16:47:51 +00:00
|
|
|
if item_might_be_inlined(
|
|
|
|
self.tcx,
|
|
|
|
&item,
|
|
|
|
self.tcx.codegen_fn_attrs(item.def_id),
|
|
|
|
) {
|
2016-12-21 10:32:59 +00:00
|
|
|
self.visit_nested_body(body);
|
2013-10-21 17:37:36 +00:00
|
|
|
}
|
2013-11-18 07:19:44 +00:00
|
|
|
}
|
2013-10-21 17:37:36 +00:00
|
|
|
|
rustc: Add `const` globals to the language
This change is an implementation of [RFC 69][rfc] which adds a third kind of
global to the language, `const`. This global is most similar to what the old
`static` was, and if you're unsure about what to use then you should use a
`const`.
The semantics of these three kinds of globals are:
* A `const` does not represent a memory location, but only a value. Constants
are translated as rvalues, which means that their values are directly inlined
at usage location (similar to a #define in C/C++). Constant values are, well,
constant, and can not be modified. Any "modification" is actually a
modification to a local value on the stack rather than the actual constant
itself.
Almost all values are allowed inside constants, whether they have interior
mutability or not. There are a few minor restrictions listed in the RFC, but
they should in general not come up too often.
* A `static` now always represents a memory location (unconditionally). Any
references to the same `static` are actually a reference to the same memory
location. Only values whose types ascribe to `Sync` are allowed in a `static`.
This restriction is in place because many threads may access a `static`
concurrently. Lifting this restriction (and allowing unsafe access) is a
future extension not implemented at this time.
* A `static mut` continues to always represent a memory location. All references
to a `static mut` continue to be `unsafe`.
This is a large breaking change, and many programs will need to be updated
accordingly. A summary of the breaking changes is:
* Statics may no longer be used in patterns. Statics now always represent a
memory location, which can sometimes be modified. To fix code, repurpose the
matched-on-`static` to a `const`.
static FOO: uint = 4;
match n {
FOO => { /* ... */ }
_ => { /* ... */ }
}
change this code to:
const FOO: uint = 4;
match n {
FOO => { /* ... */ }
_ => { /* ... */ }
}
* Statics may no longer refer to other statics by value. Due to statics being
able to change at runtime, allowing them to reference one another could
possibly lead to confusing semantics. If you are in this situation, use a
constant initializer instead. Note, however, that statics may reference other
statics by address, however.
* Statics may no longer be used in constant expressions, such as array lengths.
This is due to the same restrictions as listed above. Use a `const` instead.
[breaking-change]
[rfc]: https://github.com/rust-lang/rfcs/pull/246
2014-10-06 15:17:01 +00:00
|
|
|
// Reachable constants will be inlined into other crates
|
|
|
|
// unconditionally, so we need to make sure that their
|
|
|
|
// contents are also reachable.
|
2021-04-25 00:00:00 +00:00
|
|
|
hir::ItemKind::Const(_, init) | hir::ItemKind::Static(_, _, init) => {
|
2016-12-21 10:32:59 +00:00
|
|
|
self.visit_nested_body(init);
|
2013-12-02 01:56:55 +00:00
|
|
|
}
|
|
|
|
|
2013-11-18 07:19:44 +00:00
|
|
|
// These are normal, nothing reachable about these
|
|
|
|
// inherently and their children are already in the
|
2013-11-20 23:15:34 +00:00
|
|
|
// worklist, as determined by the privacy pass
|
2019-12-22 22:42:04 +00:00
|
|
|
hir::ItemKind::ExternCrate(_)
|
|
|
|
| hir::ItemKind::Use(..)
|
|
|
|
| hir::ItemKind::OpaqueTy(..)
|
|
|
|
| hir::ItemKind::TyAlias(..)
|
|
|
|
| hir::ItemKind::Mod(..)
|
2020-11-11 21:40:09 +00:00
|
|
|
| hir::ItemKind::ForeignMod { .. }
|
2020-01-18 00:14:29 +00:00
|
|
|
| hir::ItemKind::Impl { .. }
|
2019-12-22 22:42:04 +00:00
|
|
|
| hir::ItemKind::Trait(..)
|
|
|
|
| hir::ItemKind::TraitAlias(..)
|
|
|
|
| hir::ItemKind::Struct(..)
|
|
|
|
| hir::ItemKind::Enum(..)
|
|
|
|
| hir::ItemKind::Union(..)
|
|
|
|
| hir::ItemKind::GlobalAsm(..) => {}
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
2013-11-18 07:19:44 +00:00
|
|
|
}
|
2018-08-25 14:56:16 +00:00
|
|
|
Node::TraitItem(trait_method) => {
|
2019-09-26 16:07:54 +00:00
|
|
|
match trait_method.kind {
|
2019-12-22 22:42:04 +00:00
|
|
|
hir::TraitItemKind::Const(_, None)
|
2020-03-05 15:57:34 +00:00
|
|
|
| hir::TraitItemKind::Fn(_, hir::TraitFn::Required(_)) => {
|
2013-11-18 07:19:44 +00:00
|
|
|
// Keep going, nothing to get exported
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
2019-12-22 22:42:04 +00:00
|
|
|
hir::TraitItemKind::Const(_, Some(body_id))
|
2020-03-05 15:57:34 +00:00
|
|
|
| hir::TraitItemKind::Fn(_, hir::TraitFn::Provided(body_id)) => {
|
2016-12-21 10:32:59 +00:00
|
|
|
self.visit_nested_body(body_id);
|
2016-10-28 20:58:32 +00:00
|
|
|
}
|
2016-12-04 02:21:06 +00:00
|
|
|
hir::TraitItemKind::Type(..) => {}
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
2013-11-18 07:19:44 +00:00
|
|
|
}
|
2019-12-22 22:42:04 +00:00
|
|
|
Node::ImplItem(impl_item) => match impl_item.kind {
|
|
|
|
hir::ImplItemKind::Const(_, body) => {
|
|
|
|
self.visit_nested_body(body);
|
|
|
|
}
|
2020-03-05 15:57:34 +00:00
|
|
|
hir::ImplItemKind::Fn(_, body) => {
|
2020-07-14 23:51:46 +00:00
|
|
|
let impl_def_id =
|
|
|
|
self.tcx.parent(search_item.to_def_id()).unwrap().expect_local();
|
|
|
|
if method_might_be_inlined(self.tcx, impl_item, impl_def_id) {
|
2019-12-22 22:42:04 +00:00
|
|
|
self.visit_nested_body(body)
|
2014-08-04 20:56:56 +00:00
|
|
|
}
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
2020-05-10 11:15:51 +00:00
|
|
|
hir::ImplItemKind::TyAlias(_) => {}
|
2019-12-22 22:42:04 +00:00
|
|
|
},
|
2019-09-26 13:39:48 +00:00
|
|
|
Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure(.., body, _, _), .. }) => {
|
2017-08-14 10:04:52 +00:00
|
|
|
self.visit_nested_body(body);
|
|
|
|
}
|
2013-11-18 07:19:44 +00:00
|
|
|
// Nothing to recurse on for these
|
2019-12-22 22:42:04 +00:00
|
|
|
Node::ForeignItem(_)
|
|
|
|
| Node::Variant(_)
|
|
|
|
| Node::Ctor(..)
|
|
|
|
| Node::Field(_)
|
|
|
|
| Node::Ty(_)
|
2021-01-18 18:15:53 +00:00
|
|
|
| Node::Crate(_)
|
2019-12-22 22:42:04 +00:00
|
|
|
| Node::MacroDef(_) => {}
|
2013-11-18 07:19:44 +00:00
|
|
|
_ => {
|
2019-02-15 22:26:05 +00:00
|
|
|
bug!(
|
|
|
|
"found unexpected node kind in worklist: {} ({:?})",
|
2020-07-14 23:51:46 +00:00
|
|
|
self.tcx
|
|
|
|
.hir()
|
|
|
|
.node_to_string(self.tcx.hir().local_def_id_to_hir_id(search_item)),
|
2019-02-15 22:26:05 +00:00
|
|
|
node,
|
|
|
|
);
|
2013-11-18 07:19:44 +00:00
|
|
|
}
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-05 15:17:33 +00:00
|
|
|
}
|
2013-06-15 01:21:47 +00:00
|
|
|
|
2015-11-05 15:17:33 +00:00
|
|
|
// Some methods from non-exported (completely private) trait impls still have to be
|
|
|
|
// reachable if they are called from inlinable code. Generally, it's not known until
|
|
|
|
// monomorphization if a specific trait impl item can be reachable or not. So, we
|
|
|
|
// conservatively mark all of them as reachable.
|
|
|
|
// FIXME: One possible strategy for pruning the reachable set is to avoid marking impl
|
|
|
|
// items of non-exported traits (or maybe all local traits?) unless their respective
|
|
|
|
// trait items are used from inlinable code through method call syntax or UFCS, or their
|
|
|
|
// trait is a lang item.
|
2019-06-14 16:39:39 +00:00
|
|
|
struct CollectPrivateImplItemsVisitor<'a, 'tcx> {
|
2019-06-13 21:48:52 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2015-11-19 11:16:35 +00:00
|
|
|
access_levels: &'a privacy::AccessLevels,
|
2020-07-14 23:51:46 +00:00
|
|
|
worklist: &'a mut Vec<LocalDefId>,
|
2015-11-05 15:17:33 +00:00
|
|
|
}
|
|
|
|
|
2021-06-20 13:49:08 +00:00
|
|
|
impl CollectPrivateImplItemsVisitor<'_, '_> {
|
|
|
|
fn push_to_worklist_if_has_custom_linkage(&mut self, def_id: LocalDefId) {
|
2017-10-10 22:54:49 +00:00
|
|
|
// Anything which has custom linkage gets thrown on the worklist no
|
2018-08-02 19:30:43 +00:00
|
|
|
// matter where it is in the crate, along with "special std symbols"
|
|
|
|
// which are currently akin to allocator symbols.
|
2021-06-20 13:49:08 +00:00
|
|
|
let codegen_attrs = self.tcx.codegen_fn_attrs(def_id);
|
2019-12-22 22:42:04 +00:00
|
|
|
if codegen_attrs.contains_extern_indicator()
|
|
|
|
|| codegen_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL)
|
|
|
|
{
|
2021-06-20 13:49:08 +00:00
|
|
|
self.worklist.push(def_id);
|
2017-10-10 22:54:49 +00:00
|
|
|
}
|
2021-06-20 13:49:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> ItemLikeVisitor<'tcx> for CollectPrivateImplItemsVisitor<'a, 'tcx> {
|
|
|
|
fn visit_item(&mut self, item: &hir::Item<'_>) {
|
|
|
|
self.push_to_worklist_if_has_custom_linkage(item.def_id);
|
2017-10-10 22:54:49 +00:00
|
|
|
|
2015-11-05 15:17:33 +00:00
|
|
|
// We need only trait impls here, not inherent impls, and only non-exported ones
|
2020-11-22 22:46:21 +00:00
|
|
|
if let hir::ItemKind::Impl(hir::Impl { of_trait: Some(ref trait_ref), ref items, .. }) =
|
|
|
|
item.kind
|
|
|
|
{
|
2021-07-28 15:23:40 +00:00
|
|
|
if !self.access_levels.is_reachable(item.def_id) {
|
2020-07-14 23:51:46 +00:00
|
|
|
// FIXME(#53488) remove `let`
|
|
|
|
let tcx = self.tcx;
|
2021-01-30 22:25:03 +00:00
|
|
|
self.worklist.extend(items.iter().map(|ii_ref| ii_ref.id.def_id));
|
2016-12-08 21:55:51 +00:00
|
|
|
|
2019-04-20 16:36:05 +00:00
|
|
|
let trait_def_id = match trait_ref.path.res {
|
|
|
|
Res::Def(DefKind::Trait, def_id) => def_id,
|
2019-12-22 22:42:04 +00:00
|
|
|
_ => unreachable!(),
|
2016-12-08 21:55:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if !trait_def_id.is_local() {
|
2019-12-22 22:42:04 +00:00
|
|
|
return;
|
2016-12-08 21:55:51 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 23:51:46 +00:00
|
|
|
self.worklist.extend(
|
|
|
|
tcx.provided_trait_methods(trait_def_id)
|
|
|
|
.map(|assoc| assoc.def_id.expect_local()),
|
|
|
|
);
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
2015-11-05 15:17:33 +00:00
|
|
|
}
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
2016-11-04 22:20:15 +00:00
|
|
|
|
2019-11-28 20:47:10 +00:00
|
|
|
fn visit_trait_item(&mut self, _trait_item: &hir::TraitItem<'_>) {}
|
2016-12-04 02:21:06 +00:00
|
|
|
|
2021-06-20 13:49:08 +00:00
|
|
|
fn visit_impl_item(&mut self, impl_item: &hir::ImplItem<'_>) {
|
|
|
|
self.push_to_worklist_if_has_custom_linkage(impl_item.def_id);
|
2016-11-04 22:20:15 +00:00
|
|
|
}
|
2020-11-11 20:57:54 +00:00
|
|
|
|
2020-11-26 18:11:43 +00:00
|
|
|
fn visit_foreign_item(&mut self, _foreign_item: &hir::ForeignItem<'_>) {
|
|
|
|
// We never export foreign functions as they have no body to export.
|
|
|
|
}
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
|
|
|
|
2021-05-11 09:35:50 +00:00
|
|
|
fn reachable_set<'tcx>(tcx: TyCtxt<'tcx>, (): ()) -> FxHashSet<LocalDefId> {
|
2021-05-11 11:49:00 +00:00
|
|
|
let access_levels = &tcx.privacy_access_levels(());
|
2017-03-23 19:13:29 +00:00
|
|
|
|
2020-05-01 22:30:23 +00:00
|
|
|
let any_library =
|
2020-05-16 04:44:28 +00:00
|
|
|
tcx.sess.crate_types().iter().any(|ty| {
|
2020-05-01 22:30:23 +00:00
|
|
|
*ty == CrateType::Rlib || *ty == CrateType::Dylib || *ty == CrateType::ProcMacro
|
|
|
|
});
|
2017-01-06 19:54:24 +00:00
|
|
|
let mut reachable_context = ReachableContext {
|
2017-07-03 18:19:51 +00:00
|
|
|
tcx,
|
2020-07-17 08:47:04 +00:00
|
|
|
maybe_typeck_results: None,
|
2018-07-21 19:15:11 +00:00
|
|
|
reachable_symbols: Default::default(),
|
2017-01-06 19:54:24 +00:00
|
|
|
worklist: Vec::new(),
|
2017-07-03 18:19:51 +00:00
|
|
|
any_library,
|
2017-01-06 19:54:24 +00:00
|
|
|
};
|
2013-06-15 01:21:47 +00:00
|
|
|
|
2013-10-10 04:16:51 +00:00
|
|
|
// Step 1: Seed the worklist with all nodes which were found to be public as
|
2015-11-05 15:17:33 +00:00
|
|
|
// a result of the privacy pass along with all local lang items and impl items.
|
|
|
|
// If other crates link to us, they're going to expect to be able to
|
2014-01-16 18:12:31 +00:00
|
|
|
// use the lang items, so we need to be sure to mark them as
|
|
|
|
// exported.
|
2021-07-28 15:23:40 +00:00
|
|
|
reachable_context.worklist.extend(access_levels.map.keys());
|
2017-08-31 15:57:41 +00:00
|
|
|
for item in tcx.lang_items().items().iter() {
|
2020-07-14 23:51:46 +00:00
|
|
|
if let Some(def_id) = *item {
|
|
|
|
if let Some(def_id) = def_id.as_local() {
|
|
|
|
reachable_context.worklist.push(def_id);
|
2014-01-16 18:12:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-05 15:17:33 +00:00
|
|
|
{
|
|
|
|
let mut collect_private_impl_items = CollectPrivateImplItemsVisitor {
|
2017-07-03 18:19:51 +00:00
|
|
|
tcx,
|
|
|
|
access_levels,
|
2015-11-05 15:17:33 +00:00
|
|
|
worklist: &mut reachable_context.worklist,
|
|
|
|
};
|
2018-12-04 12:45:36 +00:00
|
|
|
tcx.hir().krate().visit_all_item_likes(&mut collect_private_impl_items);
|
2015-11-05 15:17:33 +00:00
|
|
|
}
|
2013-06-15 01:21:47 +00:00
|
|
|
|
|
|
|
// Step 2: Mark all symbols that the symbols on the worklist touch.
|
|
|
|
reachable_context.propagate();
|
|
|
|
|
2018-08-20 21:39:47 +00:00
|
|
|
debug!("Inline reachability shows: {:?}", reachable_context.reachable_symbols);
|
|
|
|
|
2013-06-15 01:21:47 +00:00
|
|
|
// Return the set of reachable symbols.
|
2020-07-14 23:51:46 +00:00
|
|
|
reachable_context.reachable_symbols
|
2013-06-15 01:21:47 +00:00
|
|
|
}
|
2017-03-27 22:55:56 +00:00
|
|
|
|
2020-07-05 20:00:14 +00:00
|
|
|
pub fn provide(providers: &mut Providers) {
|
2019-12-22 22:42:04 +00:00
|
|
|
*providers = Providers { reachable_set, ..*providers };
|
2017-03-27 22:55:56 +00:00
|
|
|
}
|