rust/src/librustc/middle/reachable.rs

403 lines
16 KiB
Rust
Raw Normal View History

// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// 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.
use dep_graph::DepNode;
2016-03-29 05:50:44 +00:00
use hir::map as ast_map;
use hir::def::Def;
use hir::def_id::DefId;
use ty::{self, TyCtxt};
use middle::privacy;
use session::config;
use util::nodemap::{NodeSet, FxHashSet};
use syntax::abi::Abi;
use syntax::ast;
use syntax::attr;
2016-03-29 05:50:44 +00:00
use hir;
use hir::intravisit::{Visitor, NestedVisitorMap};
use hir::itemlikevisit::ItemLikeVisitor;
2016-03-29 05:50:44 +00:00
use hir::intravisit;
// Returns true if the given set of generics implies that the item it's
// associated with must be inlined.
2015-07-31 07:04:06 +00:00
fn generics_require_inlining(generics: &hir::Generics) -> bool {
!generics.ty_params.is_empty()
}
// 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.
2015-07-31 07:04:06 +00:00
fn item_might_be_inlined(item: &hir::Item) -> bool {
2015-02-18 18:30:17 +00:00
if attr::requests_inline(&item.attrs) {
return true
}
match item.node {
2016-08-26 16:23:42 +00:00
hir::ItemImpl(_, _, ref generics, ..) |
hir::ItemFn(.., ref generics, _) => {
generics_require_inlining(generics)
}
_ => false,
}
}
fn method_might_be_inlined<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
sig: &hir::MethodSig,
impl_item: &hir::ImplItem,
impl_src: DefId) -> bool {
if attr::requests_inline(&impl_item.attrs) ||
generics_require_inlining(&sig.generics) {
return true
}
if let Some(impl_node_id) = tcx.map.as_local_node_id(impl_src) {
match tcx.map.find(impl_node_id) {
Some(ast_map::NodeItem(item)) =>
2016-02-09 21:00:20 +00:00
item_might_be_inlined(&item),
Some(..) | None =>
span_bug!(impl_item.span, "impl did is not an item")
}
} else {
span_bug!(impl_item.span, "found a foreign impl as a parent of a local method")
}
}
// Information needed while computing reachability.
struct ReachableContext<'a, 'tcx: 'a> {
// The type context.
tcx: TyCtxt<'a, 'tcx, 'tcx>,
// The set of items which must be exported in the linkage sense.
2014-03-09 11:42:22 +00:00
reachable_symbols: NodeSet,
// A worklist of item IDs. Each item ID in this worklist will be inlined
// and will be scanned for further references.
2014-03-09 11:42:22 +00:00
worklist: Vec<ast::NodeId>,
// Whether any output of this compilation is a library
any_library: bool,
}
2016-10-28 20:58:32 +00:00
impl<'a, 'tcx> Visitor<'tcx> for ReachableContext<'a, 'tcx> {
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
NestedVisitorMap::OnlyBodies(&self.tcx.map)
2016-10-28 20:58:32 +00:00
}
fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
let def = match expr.node {
hir::ExprPath(ref qpath) => {
Some(self.tcx.tables().qpath_def(qpath, expr.id))
}
2015-07-31 07:04:06 +00:00
hir::ExprMethodCall(..) => {
let method_call = ty::MethodCall::expr(expr.id);
let def_id = self.tcx.tables.borrow().method_map[&method_call].def_id;
Some(Def::Method(def_id))
}
_ => None
};
if let Some(def) = def {
let def_id = def.def_id();
if let Some(node_id) = self.tcx.map.as_local_node_id(def_id) {
if self.def_id_represents_local_inlined_item(def_id) {
self.worklist.push(node_id);
} else {
match def {
// If this path leads to a constant, then we need to
// recurse into the constant to continue finding
// items that are reachable.
Def::Const(..) | Def::AssociatedConst(..) => {
self.worklist.push(node_id);
}
// If this wasn't a static, then the destination is
// surely reachable.
_ => {
self.reachable_symbols.insert(node_id);
}
}
}
}
}
intravisit::walk_expr(self, expr)
}
}
impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
// Creates a new reachability computation context.
fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> ReachableContext<'a, 'tcx> {
let any_library = tcx.sess.crate_types.borrow().iter().any(|ty| {
rustc: Implement custom derive (macros 1.1) This commit is an implementation of [RFC 1681] which adds support to the compiler for first-class user-define custom `#[derive]` modes with a far more stable API than plugins have today. [RFC 1681]: https://github.com/rust-lang/rfcs/blob/master/text/1681-macros-1.1.md The main features added by this commit are: * A new `rustc-macro` crate-type. This crate type represents one which will provide custom `derive` implementations and perhaps eventually flower into the implementation of macros 2.0 as well. * A new `rustc_macro` crate in the standard distribution. This crate will provide the runtime interface between macro crates and the compiler. The API here is particularly conservative right now but has quite a bit of room to expand into any manner of APIs required by macro authors. * The ability to load new derive modes through the `#[macro_use]` annotations on other crates. All support added here is gated behind the `rustc_macro` feature gate, both for the library support (the `rustc_macro` crate) as well as the language features. There are a few minor differences from the implementation outlined in the RFC, such as the `rustc_macro` crate being available as a dylib and all symbols are `dlsym`'d directly instead of having a shim compiled. These should only affect the implementation, however, not the public interface. This commit also ended up touching a lot of code related to `#[derive]`, making a few notable changes: * Recognized derive attributes are no longer desugared to `derive_Foo`. Wasn't sure how to keep this behavior and *not* expose it to custom derive. * Derive attributes no longer have access to unstable features by default, they have to opt in on a granular level. * The `derive(Copy,Clone)` optimization is now done through another "obscure attribute" which is just intended to ferry along in the compiler that such an optimization is possible. The `derive(PartialEq,Eq)` optimization was also updated to do something similar. --- One part of this PR which needs to be improved before stabilizing are the errors and exact interfaces here. The error messages are relatively poor quality and there are surprising spects of this such as `#[derive(PartialEq, Eq, MyTrait)]` not working by default. The custom attributes added by the compiler end up becoming unstable again when going through a custom impl. Hopefully though this is enough to start allowing experimentation on crates.io! syntax-[breaking-change]
2016-08-23 00:07:11 +00:00
*ty == config::CrateTypeRlib || *ty == config::CrateTypeDylib ||
*ty == config::CrateTypeProcMacro || *ty == config::CrateTypeMetadata
});
ReachableContext {
tcx: tcx,
reachable_symbols: NodeSet(),
2014-03-09 11:42:22 +00:00
worklist: Vec::new(),
any_library: any_library,
}
}
// 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 {
let node_id = match self.tcx.map.as_local_node_id(def_id) {
Some(node_id) => node_id,
None => { return false; }
};
2014-03-09 11:42:22 +00:00
match self.tcx.map.find(node_id) {
Some(ast_map::NodeItem(item)) => {
match item.node {
2016-02-09 21:00:20 +00:00
hir::ItemFn(..) => item_might_be_inlined(&item),
_ => false,
}
}
Some(ast_map::NodeTraitItem(trait_method)) => {
match trait_method.node {
2015-07-31 07:04:06 +00:00
hir::ConstTraitItem(_, ref default) => default.is_some(),
hir::MethodTraitItem(_, ref body) => body.is_some(),
hir::TypeTraitItem(..) => false,
}
}
Some(ast_map::NodeImplItem(impl_item)) => {
match impl_item.node {
2015-11-12 14:57:51 +00:00
hir::ImplItemKind::Const(..) => true,
hir::ImplItemKind::Method(ref sig, _) => {
if generics_require_inlining(&sig.generics) ||
attr::requests_inline(&impl_item.attrs) {
true
} else {
let impl_did = self.tcx
.map
.get_parent_did(node_id);
// Check the impl. If the generics on the self
// type of the impl require inlining, this method
// does too.
let impl_node_id = self.tcx.map.as_local_node_id(impl_did).unwrap();
match self.tcx.map.expect_item(impl_node_id).node {
2016-08-26 16:23:42 +00:00
hir::ItemImpl(_, _, ref generics, ..) => {
generics_require_inlining(generics)
}
_ => false
}
}
}
2015-11-12 14:57:51 +00:00
hir::ImplItemKind::Type(_) => false,
}
}
Some(_) => false,
None => false // This will happen for default methods.
}
}
// Step 2: Mark all symbols that the symbols on the worklist touch.
2014-03-09 11:42:22 +00:00
fn propagate(&mut self) {
let mut scanned = FxHashSet();
loop {
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
let search_item = match self.worklist.pop() {
Some(item) => item,
None => break,
};
if !scanned.insert(search_item) {
2014-03-09 11:42:22 +00:00
continue
}
2015-10-29 18:54:55 +00:00
if let Some(ref item) = self.tcx.map.find(search_item) {
self.propagate_node(item, search_item);
}
}
}
2016-10-28 20:58:32 +00:00
fn propagate_node(&mut self, node: &ast_map::Node<'tcx>,
2014-03-09 11:42:22 +00:00
search_item: ast::NodeId) {
if !self.any_library {
// If we are building an executable, only explicitly extern
// types need to be exported.
if let ast_map::NodeItem(item) = *node {
2016-08-26 16:23:42 +00:00
let reachable = if let hir::ItemFn(.., abi, _, _) = item.node {
abi != Abi::Rust
} else {
false
};
let is_extern = attr::contains_extern_indicator(&self.tcx.sess.diagnostic(),
&item.attrs);
if reachable || is_extern {
self.reachable_symbols.insert(search_item);
}
}
} 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);
}
match *node {
ast_map::NodeItem(item) => {
match item.node {
2016-10-28 20:58:32 +00:00
hir::ItemFn(.., body) => {
2016-02-09 21:00:20 +00:00
if item_might_be_inlined(&item) {
2016-10-28 20:58:32 +00:00
self.visit_body(body);
}
}
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.
2015-07-31 07:04:06 +00:00
hir::ItemConst(_, ref init) => {
2016-02-09 21:00:20 +00:00
self.visit_expr(&init);
}
// These are normal, nothing reachable about these
// inherently and their children are already in the
// worklist, as determined by the privacy pass
hir::ItemExternCrate(_) | hir::ItemUse(..) |
2016-08-26 16:23:42 +00:00
hir::ItemTy(..) | hir::ItemStatic(..) |
2015-07-31 07:04:06 +00:00
hir::ItemMod(..) | hir::ItemForeignMod(..) |
hir::ItemImpl(..) | hir::ItemTrait(..) |
hir::ItemStruct(..) | hir::ItemEnum(..) |
2016-08-06 18:36:28 +00:00
hir::ItemUnion(..) | hir::ItemDefaultImpl(..) => {}
}
}
ast_map::NodeTraitItem(trait_method) => {
match trait_method.node {
2015-07-31 07:04:06 +00:00
hir::ConstTraitItem(_, None) |
hir::MethodTraitItem(_, None) => {
// Keep going, nothing to get exported
}
2016-10-28 20:58:32 +00:00
hir::ConstTraitItem(_, Some(ref body)) => {
self.visit_expr(body);
}
2016-10-28 20:58:32 +00:00
hir::MethodTraitItem(_, Some(body_id)) => {
self.visit_body(body_id);
}
2015-07-31 07:04:06 +00:00
hir::TypeTraitItem(..) => {}
}
}
ast_map::NodeImplItem(impl_item) => {
match impl_item.node {
2015-11-12 14:57:51 +00:00
hir::ImplItemKind::Const(_, ref expr) => {
2016-02-09 21:00:20 +00:00
self.visit_expr(&expr);
}
2016-10-28 20:58:32 +00:00
hir::ImplItemKind::Method(ref sig, body) => {
let did = self.tcx.map.get_parent_did(search_item);
if method_might_be_inlined(self.tcx, sig, impl_item, did) {
2016-10-28 20:58:32 +00:00
self.visit_body(body)
}
}
2015-11-12 14:57:51 +00:00
hir::ImplItemKind::Type(_) => {}
}
}
// Nothing to recurse on for these
ast_map::NodeForeignItem(_) |
ast_map::NodeVariant(_) |
ast_map::NodeStructCtor(_) |
2016-11-09 18:57:48 +00:00
ast_map::NodeField(_) |
ast_map::NodeTy(_) => {}
_ => {
bug!("found unexpected thingy in worklist: {}",
self.tcx.map.node_to_string(search_item))
}
}
}
}
// 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.
struct CollectPrivateImplItemsVisitor<'a, 'tcx: 'a> {
tcx: TyCtxt<'a, 'tcx, 'tcx>,
access_levels: &'a privacy::AccessLevels,
worklist: &'a mut Vec<ast::NodeId>,
}
impl<'a, 'tcx: 'a> ItemLikeVisitor<'tcx> for CollectPrivateImplItemsVisitor<'a, 'tcx> {
fn visit_item(&mut self, item: &hir::Item) {
// We need only trait impls here, not inherent impls, and only non-exported ones
if let hir::ItemImpl(.., Some(ref trait_ref), _, ref impl_item_refs) = item.node {
if !self.access_levels.is_reachable(item.id) {
for impl_item_ref in impl_item_refs {
self.worklist.push(impl_item_ref.id.node_id);
2015-08-25 18:52:15 +00:00
}
let trait_def_id = match trait_ref.path.def {
Def::Trait(def_id) => def_id,
_ => unreachable!()
};
if !trait_def_id.is_local() {
return
}
for default_method in self.tcx.provided_trait_methods(trait_def_id) {
let node_id = self.tcx
.map
.as_local_node_id(default_method.def_id)
.unwrap();
self.worklist.push(node_id);
}
}
}
}
fn visit_impl_item(&mut self, _impl_item: &hir::ImplItem) {
// processed in visit_item above
}
}
pub fn find_reachable<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
access_levels: &privacy::AccessLevels)
-> NodeSet {
let _task = tcx.dep_graph.in_task(DepNode::Reachability);
let mut reachable_context = ReachableContext::new(tcx);
// Step 1: Seed the worklist with all nodes which were found to be public as
// 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
// use the lang items, so we need to be sure to mark them as
// exported.
for (id, _) in &access_levels.map {
reachable_context.worklist.push(*id);
}
for item in tcx.lang_items.items().iter() {
if let Some(did) = *item {
if let Some(node_id) = tcx.map.as_local_node_id(did) {
reachable_context.worklist.push(node_id);
}
}
}
{
let mut collect_private_impl_items = CollectPrivateImplItemsVisitor {
tcx: tcx,
access_levels: access_levels,
worklist: &mut reachable_context.worklist,
};
tcx.map.krate().visit_all_item_likes(&mut collect_private_impl_items);
}
// Step 2: Mark all symbols that the symbols on the worklist touch.
reachable_context.propagate();
// Return the set of reachable symbols.
reachable_context.reachable_symbols
}