2013-11-11 22:17:47 -06:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2013-06-14 20:21:47 -05:00
|
|
|
// 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.
|
|
|
|
|
2014-05-14 14:31:30 -05:00
|
|
|
use middle::def;
|
2013-06-14 20:21:47 -05:00
|
|
|
use middle::ty;
|
2013-10-09 23:16:51 -05:00
|
|
|
use middle::privacy;
|
2014-11-15 19:30:33 -06:00
|
|
|
use session::config;
|
2014-02-28 16:34:26 -06:00
|
|
|
use util::nodemap::NodeSet;
|
2013-06-14 20:21:47 -05:00
|
|
|
|
2014-05-29 21:03:06 -05:00
|
|
|
use std::collections::HashSet;
|
2014-05-06 20:43:56 -05:00
|
|
|
use syntax::abi;
|
2013-10-21 12:37:36 -05:00
|
|
|
use syntax::ast;
|
2013-06-14 20:21:47 -05:00
|
|
|
use syntax::ast_map;
|
2015-03-11 16:38:58 -05:00
|
|
|
use syntax::ast_util::is_local;
|
2013-06-14 20:21:47 -05:00
|
|
|
use syntax::attr;
|
2013-08-13 06:53:13 -05:00
|
|
|
use syntax::visit::Visitor;
|
|
|
|
use syntax::visit;
|
2013-06-14 20:21:47 -05:00
|
|
|
|
|
|
|
// Returns true if the given set of generics implies that the item it's
|
|
|
|
// associated with must be inlined.
|
2013-10-21 12:37:36 -05:00
|
|
|
fn generics_require_inlining(generics: &ast::Generics) -> bool {
|
2013-06-14 20:21:47 -05:00
|
|
|
!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.
|
2014-01-09 07:05:33 -06:00
|
|
|
fn item_might_be_inlined(item: &ast::Item) -> bool {
|
2015-02-18 12:30:17 -06:00
|
|
|
if attr::requests_inline(&item.attrs) {
|
2013-06-14 20:21:47 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
match item.node {
|
2014-12-28 16:33:18 -06:00
|
|
|
ast::ItemImpl(_, _, ref generics, _, _, _) |
|
2014-01-09 07:05:33 -06:00
|
|
|
ast::ItemFn(_, _, _, ref generics, _) => {
|
2013-06-14 20:21:47 -05:00
|
|
|
generics_require_inlining(generics)
|
|
|
|
}
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-11 16:38:58 -05:00
|
|
|
fn method_might_be_inlined(tcx: &ty::ctxt, sig: &ast::MethodSig,
|
2015-03-10 05:28:44 -05:00
|
|
|
impl_item: &ast::ImplItem,
|
2013-10-31 22:47:23 -05:00
|
|
|
impl_src: ast::DefId) -> bool {
|
2015-03-10 05:28:44 -05:00
|
|
|
if attr::requests_inline(&impl_item.attrs) ||
|
2015-03-11 16:38:58 -05:00
|
|
|
generics_require_inlining(&sig.generics) {
|
2013-10-31 22:47:23 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
if is_local(impl_src) {
|
2013-12-27 18:09:29 -06:00
|
|
|
{
|
2014-02-13 23:07:09 -06:00
|
|
|
match tcx.map.find(impl_src.node) {
|
|
|
|
Some(ast_map::NodeItem(item)) => {
|
2014-05-16 12:15:33 -05:00
|
|
|
item_might_be_inlined(&*item)
|
2013-12-27 18:09:29 -06:00
|
|
|
}
|
|
|
|
Some(..) | None => {
|
2015-03-10 05:28:44 -05:00
|
|
|
tcx.sess.span_bug(impl_item.span, "impl did is not an item")
|
2013-12-27 18:09:29 -06:00
|
|
|
}
|
2013-10-31 22:47:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2015-03-10 05:28:44 -05:00
|
|
|
tcx.sess.span_bug(impl_item.span, "found a foreign impl as a parent \
|
|
|
|
of a local method")
|
2013-10-31 22:47:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-14 20:21:47 -05:00
|
|
|
// Information needed while computing reachability.
|
2014-04-22 07:56:37 -05:00
|
|
|
struct ReachableContext<'a, 'tcx: 'a> {
|
2013-06-14 20:21:47 -05:00
|
|
|
// The type context.
|
2014-04-22 07:56:37 -05:00
|
|
|
tcx: &'a ty::ctxt<'tcx>,
|
2013-06-14 20:21:47 -05:00
|
|
|
// The set of items which must be exported in the linkage sense.
|
2014-03-09 06:42:22 -05:00
|
|
|
reachable_symbols: NodeSet,
|
2013-06-14 20:21:47 -05:00
|
|
|
// A worklist of item IDs. Each item ID in this worklist will be inlined
|
|
|
|
// and will be scanned for further references.
|
2014-03-09 06:42:22 -05:00
|
|
|
worklist: Vec<ast::NodeId>,
|
2014-05-02 17:26:45 -05:00
|
|
|
// Whether any output of this compilation is a library
|
|
|
|
any_library: bool,
|
2013-08-13 06:53:13 -05:00
|
|
|
}
|
|
|
|
|
2014-09-09 17:54:36 -05:00
|
|
|
impl<'a, 'tcx, 'v> Visitor<'v> for ReachableContext<'a, 'tcx> {
|
2013-06-14 20:21:47 -05:00
|
|
|
|
2014-09-12 05:10:30 -05:00
|
|
|
fn visit_expr(&mut self, expr: &ast::Expr) {
|
2013-08-13 06:53:13 -05:00
|
|
|
|
2013-10-31 22:47:23 -05:00
|
|
|
match expr.node {
|
2015-02-17 11:29:13 -06:00
|
|
|
ast::ExprPath(..) => {
|
2014-11-06 11:25:16 -06:00
|
|
|
let def = match self.tcx.def_map.borrow().get(&expr.id) {
|
2015-02-16 22:44:23 -06:00
|
|
|
Some(d) => d.full_def(),
|
2013-10-31 22:47:23 -05:00
|
|
|
None => {
|
|
|
|
self.tcx.sess.span_bug(expr.span,
|
|
|
|
"def ID not in def map?!")
|
|
|
|
}
|
|
|
|
};
|
2013-08-13 06:53:13 -05:00
|
|
|
|
2014-05-14 14:31:30 -05:00
|
|
|
let def_id = def.def_id();
|
2013-12-01 19:56:55 -06:00
|
|
|
if is_local(def_id) {
|
2014-03-09 06:42:22 -05:00
|
|
|
if self.def_id_represents_local_inlined_item(def_id) {
|
|
|
|
self.worklist.push(def_id.node)
|
2013-12-01 19:56:55 -06:00
|
|
|
} else {
|
|
|
|
match def {
|
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 10:17:01 -05:00
|
|
|
// If this path leads to a constant, then we need to
|
|
|
|
// recurse into the constant to continue finding
|
|
|
|
// items that are reachable.
|
|
|
|
def::DefConst(..) => {
|
2014-03-09 06:42:22 -05:00
|
|
|
self.worklist.push(def_id.node);
|
2013-12-01 19:56:55 -06: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 10:17:01 -05:00
|
|
|
// If this wasn't a static, then the destination is
|
2013-12-01 19:56:55 -06:00
|
|
|
// surely reachable.
|
|
|
|
_ => {
|
2014-03-09 06:42:22 -05:00
|
|
|
self.reachable_symbols.insert(def_id.node);
|
2013-12-01 19:56:55 -06:00
|
|
|
}
|
|
|
|
}
|
2013-10-31 22:47:23 -05:00
|
|
|
}
|
2013-12-01 19:56:55 -06:00
|
|
|
}
|
2013-10-31 22:47:23 -05:00
|
|
|
}
|
2013-11-28 14:22:53 -06:00
|
|
|
ast::ExprMethodCall(..) => {
|
2014-11-25 13:21:20 -06:00
|
|
|
let method_call = ty::MethodCall::expr(expr.id);
|
2015-03-21 20:15:47 -05:00
|
|
|
match (*self.tcx.method_map.borrow()).get(&method_call).unwrap().origin {
|
2014-11-25 13:21:20 -06:00
|
|
|
ty::MethodStatic(def_id) => {
|
2013-12-01 19:56:55 -06:00
|
|
|
if is_local(def_id) {
|
2014-03-09 06:42:22 -05:00
|
|
|
if self.def_id_represents_local_inlined_item(def_id) {
|
|
|
|
self.worklist.push(def_id.node)
|
2013-12-20 22:14:51 -06:00
|
|
|
}
|
2014-03-09 06:42:22 -05:00
|
|
|
self.reachable_symbols.insert(def_id.node);
|
2013-12-01 19:56:55 -06:00
|
|
|
}
|
2013-08-13 06:53:13 -05:00
|
|
|
}
|
2014-02-26 08:06:45 -06:00
|
|
|
_ => {}
|
2013-08-13 06:53:13 -05:00
|
|
|
}
|
2013-10-31 22:47:23 -05:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2013-08-13 06:53:13 -05:00
|
|
|
|
2014-09-12 05:10:30 -05:00
|
|
|
visit::walk_expr(self, expr)
|
2013-10-31 22:47:23 -05:00
|
|
|
}
|
|
|
|
|
2014-09-12 05:10:30 -05:00
|
|
|
fn visit_item(&mut self, _item: &ast::Item) {
|
2013-10-31 22:47:23 -05:00
|
|
|
// Do not recurse into items. These items will be added to the worklist
|
|
|
|
// and recursed into manually if necessary.
|
2013-08-13 06:53:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-22 07:56:37 -05:00
|
|
|
impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
|
2013-08-13 06:53:13 -05:00
|
|
|
// Creates a new reachability computation context.
|
2014-04-22 07:56:37 -05:00
|
|
|
fn new(tcx: &'a ty::ctxt<'tcx>) -> ReachableContext<'a, 'tcx> {
|
2014-05-02 17:26:45 -05:00
|
|
|
let any_library = tcx.sess.crate_types.borrow().iter().any(|ty| {
|
2014-05-06 06:38:01 -05:00
|
|
|
*ty != config::CrateTypeExecutable
|
2014-05-02 17:26:45 -05:00
|
|
|
});
|
2013-08-13 06:53:13 -05:00
|
|
|
ReachableContext {
|
|
|
|
tcx: tcx,
|
2015-01-16 16:27:43 -06:00
|
|
|
reachable_symbols: NodeSet(),
|
2014-03-09 06:42:22 -05:00
|
|
|
worklist: Vec::new(),
|
2014-05-02 17:26:45 -05:00
|
|
|
any_library: any_library,
|
2013-08-13 06:53:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-14 20:21:47 -05:00
|
|
|
// Returns true if the given def ID represents a local item that is
|
|
|
|
// eligible for inlining and false otherwise.
|
2014-03-09 06:42:22 -05:00
|
|
|
fn def_id_represents_local_inlined_item(&self, def_id: ast::DefId) -> bool {
|
2014-02-05 15:15:24 -06:00
|
|
|
if def_id.krate != ast::LOCAL_CRATE {
|
2013-06-14 20:21:47 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
let node_id = def_id.node;
|
2014-03-09 06:42:22 -05:00
|
|
|
match self.tcx.map.find(node_id) {
|
2014-02-13 23:07:09 -06:00
|
|
|
Some(ast_map::NodeItem(item)) => {
|
2013-06-14 20:21:47 -05:00
|
|
|
match item.node {
|
2014-05-16 12:15:33 -05:00
|
|
|
ast::ItemFn(..) => item_might_be_inlined(&*item),
|
2013-06-14 20:21:47 -05:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2014-08-04 15:56:56 -05:00
|
|
|
Some(ast_map::NodeTraitItem(trait_method)) => {
|
2015-03-10 05:28:44 -05:00
|
|
|
match trait_method.node {
|
2015-03-11 16:38:58 -05:00
|
|
|
ast::MethodTraitItem(_, ref body) => body.is_some(),
|
2015-03-10 05:28:44 -05:00
|
|
|
ast::TypeTraitItem(..) => false,
|
2013-06-14 20:21:47 -05:00
|
|
|
}
|
|
|
|
}
|
2014-08-04 15:56:56 -05:00
|
|
|
Some(ast_map::NodeImplItem(impl_item)) => {
|
2015-03-10 05:28:44 -05:00
|
|
|
match impl_item.node {
|
2015-03-11 16:38:58 -05:00
|
|
|
ast::MethodImplItem(ref sig, _) => {
|
|
|
|
if generics_require_inlining(&sig.generics) ||
|
2015-03-10 05:28:44 -05:00
|
|
|
attr::requests_inline(&impl_item.attrs) {
|
2014-08-04 15:56:56 -05:00
|
|
|
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.
|
|
|
|
assert!(impl_did.krate == ast::LOCAL_CRATE);
|
|
|
|
match self.tcx
|
|
|
|
.map
|
|
|
|
.expect_item(impl_did.node)
|
|
|
|
.node {
|
2014-12-28 16:33:18 -06:00
|
|
|
ast::ItemImpl(_, _, ref generics, _, _, _) => {
|
2014-08-04 15:56:56 -05:00
|
|
|
generics_require_inlining(generics)
|
|
|
|
}
|
|
|
|
_ => false
|
|
|
|
}
|
2013-06-14 20:21:47 -05:00
|
|
|
}
|
|
|
|
}
|
2014-08-05 21:44:21 -05:00
|
|
|
ast::TypeImplItem(_) => false,
|
2015-03-11 16:38:58 -05:00
|
|
|
ast::MacImplItem(_) => self.tcx.sess.bug("unexpanded macro")
|
2013-06-14 20:21:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(_) => false,
|
2013-06-18 11:39:16 -05:00
|
|
|
None => false // This will happen for default methods.
|
2013-06-14 20:21:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 2: Mark all symbols that the symbols on the worklist touch.
|
2014-03-09 06:42:22 -05:00
|
|
|
fn propagate(&mut self) {
|
2013-06-14 20:21:47 -05:00
|
|
|
let mut scanned = HashSet::new();
|
2013-12-22 16:24:09 -06:00
|
|
|
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 10:17:01 -05:00
|
|
|
let search_item = match self.worklist.pop() {
|
|
|
|
Some(item) => item,
|
|
|
|
None => break,
|
|
|
|
};
|
|
|
|
if !scanned.insert(search_item) {
|
2014-03-09 06:42:22 -05:00
|
|
|
continue
|
|
|
|
}
|
2013-12-22 16:24:09 -06:00
|
|
|
|
2014-02-13 23:07:09 -06:00
|
|
|
match self.tcx.map.find(search_item) {
|
2014-03-09 06:42:22 -05:00
|
|
|
Some(ref item) => self.propagate_node(item, search_item),
|
2013-11-18 01:19:44 -06:00
|
|
|
None if search_item == ast::CRATE_NODE_ID => {}
|
|
|
|
None => {
|
2015-01-07 10:58:31 -06:00
|
|
|
self.tcx.sess.bug(&format!("found unmapped ID in worklist: \
|
2013-11-18 01:19:44 -06:00
|
|
|
{}",
|
2015-02-20 13:08:14 -06:00
|
|
|
search_item))
|
2013-11-18 01:19:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-09 06:42:22 -05:00
|
|
|
fn propagate_node(&mut self, node: &ast_map::Node,
|
|
|
|
search_item: ast::NodeId) {
|
2014-05-02 17:26:45 -05:00
|
|
|
if !self.any_library {
|
2013-11-18 01:19:44 -06:00
|
|
|
// If we are building an executable, then there's no need to flag
|
|
|
|
// anything as external except for `extern fn` types. These
|
|
|
|
// functions may still participate in some form of native interface,
|
|
|
|
// but all other rust-only interfaces can be private (they will not
|
|
|
|
// participate in linkage after this product is produced)
|
2014-11-29 15:41:21 -06:00
|
|
|
if let ast_map::NodeItem(item) = *node {
|
|
|
|
if let ast::ItemFn(_, _, abi, _, _) = item.node {
|
|
|
|
if abi != abi::Rust {
|
|
|
|
self.reachable_symbols.insert(search_item);
|
2013-11-18 01:19:44 -06: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 06:42:22 -05:00
|
|
|
self.reachable_symbols.insert(search_item);
|
2013-11-18 01:19:44 -06:00
|
|
|
}
|
2013-10-31 22:47:23 -05:00
|
|
|
|
2013-11-18 01:19:44 -06:00
|
|
|
match *node {
|
2014-02-13 23:07:09 -06:00
|
|
|
ast_map::NodeItem(item) => {
|
2013-11-18 01:19:44 -06:00
|
|
|
match item.node {
|
2014-05-16 12:15:33 -05:00
|
|
|
ast::ItemFn(_, _, _, _, ref search_block) => {
|
|
|
|
if item_might_be_inlined(&*item) {
|
2014-09-12 05:10:30 -05:00
|
|
|
visit::walk_block(self, &**search_block)
|
2013-10-21 12:37:36 -05:00
|
|
|
}
|
2013-11-18 01:19:44 -06:00
|
|
|
}
|
2013-10-21 12:37:36 -05: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 10:17:01 -05:00
|
|
|
// Reachable constants will be inlined into other crates
|
|
|
|
// unconditionally, so we need to make sure that their
|
|
|
|
// contents are also reachable.
|
|
|
|
ast::ItemConst(_, ref init) => {
|
|
|
|
self.visit_expr(&**init);
|
2013-12-01 19:56:55 -06:00
|
|
|
}
|
|
|
|
|
2013-11-18 01:19:44 -06:00
|
|
|
// These are normal, nothing reachable about these
|
|
|
|
// inherently and their children are already in the
|
2013-11-20 17:15:34 -06:00
|
|
|
// worklist, as determined by the privacy pass
|
2014-12-23 13:33:44 -06:00
|
|
|
ast::ItemExternCrate(_) | ast::ItemUse(_) |
|
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 10:17:01 -05:00
|
|
|
ast::ItemTy(..) | ast::ItemStatic(_, _, _) |
|
2014-01-09 07:05:33 -06:00
|
|
|
ast::ItemMod(..) | ast::ItemForeignMod(..) |
|
|
|
|
ast::ItemImpl(..) | ast::ItemTrait(..) |
|
2015-01-22 15:15:02 -06:00
|
|
|
ast::ItemStruct(..) | ast::ItemEnum(..) |
|
2015-02-07 07:24:34 -06:00
|
|
|
ast::ItemDefaultImpl(..) => {}
|
2013-10-21 12:37:36 -05:00
|
|
|
|
2013-11-18 01:19:44 -06:00
|
|
|
_ => {
|
|
|
|
self.tcx.sess.span_bug(item.span,
|
|
|
|
"found non-function item \
|
|
|
|
in worklist?!")
|
2013-06-14 20:21:47 -05:00
|
|
|
}
|
|
|
|
}
|
2013-11-18 01:19:44 -06:00
|
|
|
}
|
2014-08-04 15:56:56 -05:00
|
|
|
ast_map::NodeTraitItem(trait_method) => {
|
2015-03-10 05:28:44 -05:00
|
|
|
match trait_method.node {
|
2015-03-11 16:38:58 -05:00
|
|
|
ast::MethodTraitItem(_, None) => {
|
2013-11-18 01:19:44 -06:00
|
|
|
// Keep going, nothing to get exported
|
2013-06-14 20:21:47 -05:00
|
|
|
}
|
2015-03-11 16:38:58 -05:00
|
|
|
ast::MethodTraitItem(_, Some(ref body)) => {
|
|
|
|
visit::walk_block(self, body);
|
2013-10-31 22:47:23 -05:00
|
|
|
}
|
2015-03-10 05:28:44 -05:00
|
|
|
ast::TypeTraitItem(..) => {}
|
2013-06-14 20:21:47 -05:00
|
|
|
}
|
2013-11-18 01:19:44 -06:00
|
|
|
}
|
2014-08-04 15:56:56 -05:00
|
|
|
ast_map::NodeImplItem(impl_item) => {
|
2015-03-10 05:28:44 -05:00
|
|
|
match impl_item.node {
|
2015-03-11 16:38:58 -05:00
|
|
|
ast::MethodImplItem(ref sig, ref body) => {
|
2014-08-04 15:56:56 -05:00
|
|
|
let did = self.tcx.map.get_parent_did(search_item);
|
2015-03-11 16:38:58 -05:00
|
|
|
if method_might_be_inlined(self.tcx, sig, impl_item, did) {
|
|
|
|
visit::walk_block(self, body)
|
2014-08-04 15:56:56 -05:00
|
|
|
}
|
|
|
|
}
|
2014-08-05 21:44:21 -05:00
|
|
|
ast::TypeImplItem(_) => {}
|
2015-03-11 16:38:58 -05:00
|
|
|
ast::MacImplItem(_) => self.tcx.sess.bug("unexpanded macro")
|
2013-06-14 20:21:47 -05:00
|
|
|
}
|
|
|
|
}
|
2013-11-18 01:19:44 -06:00
|
|
|
// Nothing to recurse on for these
|
2014-02-13 23:07:09 -06:00
|
|
|
ast_map::NodeForeignItem(_) |
|
|
|
|
ast_map::NodeVariant(_) |
|
|
|
|
ast_map::NodeStructCtor(_) => {}
|
2013-11-18 01:19:44 -06:00
|
|
|
_ => {
|
2014-05-16 12:45:16 -05:00
|
|
|
self.tcx
|
|
|
|
.sess
|
2015-01-07 10:58:31 -06:00
|
|
|
.bug(&format!("found unexpected thingy in worklist: {}",
|
2014-05-16 12:45:16 -05:00
|
|
|
self.tcx
|
|
|
|
.map
|
2015-02-20 13:08:14 -06:00
|
|
|
.node_to_string(search_item)))
|
2013-11-18 01:19:44 -06:00
|
|
|
}
|
2013-06-14 20:21:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 3: Mark all destructors as reachable.
|
|
|
|
//
|
2014-01-26 02:43:42 -06:00
|
|
|
// FIXME(pcwalton): This is a conservative overapproximation, but fixing
|
2013-06-14 20:21:47 -05:00
|
|
|
// this properly would result in the necessity of computing *type*
|
|
|
|
// reachability, which might result in a compile time loss.
|
2014-03-09 06:42:22 -05:00
|
|
|
fn mark_destructors_reachable(&mut self) {
|
2015-01-31 11:20:46 -06:00
|
|
|
for (_, destructor_def_id) in &*self.tcx.destructor_for_type.borrow() {
|
2014-02-05 15:15:24 -06:00
|
|
|
if destructor_def_id.krate == ast::LOCAL_CRATE {
|
2014-03-09 06:42:22 -05:00
|
|
|
self.reachable_symbols.insert(destructor_def_id.node);
|
2013-06-14 20:21:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-05 21:07:47 -06:00
|
|
|
pub fn find_reachable(tcx: &ty::ctxt,
|
2013-10-09 23:16:51 -05:00
|
|
|
exported_items: &privacy::ExportedItems)
|
2014-03-09 06:42:22 -05:00
|
|
|
-> NodeSet {
|
2014-04-09 10:18:40 -05:00
|
|
|
let mut reachable_context = ReachableContext::new(tcx);
|
2013-06-14 20:21:47 -05:00
|
|
|
|
2013-10-09 23:16:51 -05:00
|
|
|
// Step 1: Seed the worklist with all nodes which were found to be public as
|
2014-01-16 12:12:31 -06:00
|
|
|
// a result of the privacy pass along with all local lang 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.
|
2015-01-31 11:20:46 -06:00
|
|
|
for id in exported_items {
|
2014-07-21 22:54:28 -05:00
|
|
|
reachable_context.worklist.push(*id);
|
2013-10-09 23:16:51 -05:00
|
|
|
}
|
2014-01-16 12:12:31 -06:00
|
|
|
for (_, item) in tcx.lang_items.items() {
|
|
|
|
match *item {
|
|
|
|
Some(did) if is_local(did) => {
|
2014-03-09 06:42:22 -05:00
|
|
|
reachable_context.worklist.push(did.node);
|
2014-01-16 12:12:31 -06:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2013-06-14 20:21:47 -05:00
|
|
|
|
|
|
|
// Step 2: Mark all symbols that the symbols on the worklist touch.
|
|
|
|
reachable_context.propagate();
|
|
|
|
|
|
|
|
// Step 3: Mark all destructors as reachable.
|
|
|
|
reachable_context.mark_destructors_reachable();
|
|
|
|
|
|
|
|
// Return the set of reachable symbols.
|
|
|
|
reachable_context.reachable_symbols
|
|
|
|
}
|