2014-09-14 19:21:25 -05:00
|
|
|
// Copyright 2014 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.
|
|
|
|
|
|
|
|
// This compiler pass detects static items that refer to themselves
|
|
|
|
// recursively.
|
|
|
|
|
2015-06-09 18:40:45 -05:00
|
|
|
use ast_map;
|
2014-11-15 19:30:33 -06:00
|
|
|
use session::Session;
|
2015-03-15 20:35:25 -05:00
|
|
|
use middle::def::{DefStatic, DefConst, DefAssociatedConst, DefMap};
|
2014-09-14 19:21:25 -05:00
|
|
|
|
2015-06-09 18:40:45 -05:00
|
|
|
use syntax::{ast, ast_util};
|
2015-03-15 20:35:25 -05:00
|
|
|
use syntax::codemap::Span;
|
2014-09-14 19:21:25 -05:00
|
|
|
use syntax::visit::Visitor;
|
|
|
|
use syntax::visit;
|
|
|
|
|
|
|
|
struct CheckCrateVisitor<'a, 'ast: 'a> {
|
|
|
|
sess: &'a Session,
|
2014-12-18 13:03:56 -06:00
|
|
|
def_map: &'a DefMap,
|
2014-09-14 19:21:25 -05:00
|
|
|
ast_map: &'a ast_map::Map<'ast>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'v, 'a, 'ast> Visitor<'v> for CheckCrateVisitor<'a, 'ast> {
|
2015-03-15 20:35:25 -05:00
|
|
|
fn visit_item(&mut self, it: &ast::Item) {
|
|
|
|
match it.node {
|
|
|
|
ast::ItemStatic(_, _, ref expr) |
|
|
|
|
ast::ItemConst(_, ref expr) => {
|
|
|
|
let mut recursion_visitor =
|
|
|
|
CheckItemRecursionVisitor::new(self, &it.span);
|
|
|
|
recursion_visitor.visit_item(it);
|
|
|
|
visit::walk_expr(self, &*expr)
|
|
|
|
},
|
|
|
|
_ => visit::walk_item(self, it)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_trait_item(&mut self, ti: &ast::TraitItem) {
|
|
|
|
match ti.node {
|
|
|
|
ast::ConstTraitItem(_, ref default) => {
|
|
|
|
if let Some(ref expr) = *default {
|
|
|
|
let mut recursion_visitor =
|
|
|
|
CheckItemRecursionVisitor::new(self, &ti.span);
|
|
|
|
recursion_visitor.visit_trait_item(ti);
|
|
|
|
visit::walk_expr(self, &*expr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => visit::walk_trait_item(self, ti)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_impl_item(&mut self, ii: &ast::ImplItem) {
|
|
|
|
match ii.node {
|
|
|
|
ast::ConstImplItem(_, ref expr) => {
|
|
|
|
let mut recursion_visitor =
|
|
|
|
CheckItemRecursionVisitor::new(self, &ii.span);
|
|
|
|
recursion_visitor.visit_impl_item(ii);
|
|
|
|
visit::walk_expr(self, &*expr)
|
|
|
|
}
|
|
|
|
_ => visit::walk_impl_item(self, ii)
|
|
|
|
}
|
2014-09-14 19:21:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn check_crate<'ast>(sess: &Session,
|
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
|
|
|
krate: &ast::Crate,
|
2014-12-18 13:03:56 -06:00
|
|
|
def_map: &DefMap,
|
2014-09-14 19:21:25 -05:00
|
|
|
ast_map: &ast_map::Map<'ast>) {
|
|
|
|
let mut visitor = CheckCrateVisitor {
|
|
|
|
sess: sess,
|
|
|
|
def_map: def_map,
|
|
|
|
ast_map: ast_map
|
|
|
|
};
|
|
|
|
visit::walk_crate(&mut visitor, krate);
|
|
|
|
sess.abort_if_errors();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CheckItemRecursionVisitor<'a, 'ast: 'a> {
|
2015-03-15 20:35:25 -05:00
|
|
|
root_span: &'a Span,
|
2014-09-14 19:21:25 -05:00
|
|
|
sess: &'a Session,
|
|
|
|
ast_map: &'a ast_map::Map<'ast>,
|
2014-12-18 13:03:56 -06:00
|
|
|
def_map: &'a DefMap,
|
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
|
|
|
idstack: Vec<ast::NodeId>
|
2014-09-14 19:21:25 -05:00
|
|
|
}
|
|
|
|
|
2015-03-15 20:35:25 -05:00
|
|
|
impl<'a, 'ast: 'a> CheckItemRecursionVisitor<'a, 'ast> {
|
|
|
|
fn new(v: &CheckCrateVisitor<'a, 'ast>, span: &'a Span)
|
|
|
|
-> CheckItemRecursionVisitor<'a, 'ast> {
|
|
|
|
CheckItemRecursionVisitor {
|
|
|
|
root_span: span,
|
|
|
|
sess: v.sess,
|
|
|
|
ast_map: v.ast_map,
|
|
|
|
def_map: v.def_map,
|
|
|
|
idstack: Vec::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn with_item_id_pushed<F>(&mut self, id: ast::NodeId, f: F)
|
|
|
|
where F: Fn(&mut Self) {
|
|
|
|
if self.idstack.iter().any(|x| x == &(id)) {
|
|
|
|
span_err!(self.sess, *self.root_span, E0265, "recursive constant");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.idstack.push(id);
|
|
|
|
f(self);
|
|
|
|
self.idstack.pop();
|
|
|
|
}
|
2014-09-14 19:21:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'ast, 'v> Visitor<'v> for CheckItemRecursionVisitor<'a, 'ast> {
|
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
|
|
|
fn visit_item(&mut self, it: &ast::Item) {
|
2015-03-15 20:35:25 -05:00
|
|
|
self.with_item_id_pushed(it.id, |v| visit::walk_item(v, it));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_trait_item(&mut self, ti: &ast::TraitItem) {
|
|
|
|
self.with_item_id_pushed(ti.id, |v| visit::walk_trait_item(v, ti));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_impl_item(&mut self, ii: &ast::ImplItem) {
|
|
|
|
self.with_item_id_pushed(ii.id, |v| visit::walk_impl_item(v, ii));
|
2014-09-14 19:21:25 -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
|
|
|
fn visit_expr(&mut self, e: &ast::Expr) {
|
2014-09-14 19:21:25 -05:00
|
|
|
match e.node {
|
2015-02-17 11:29:13 -06:00
|
|
|
ast::ExprPath(..) => {
|
|
|
|
match self.def_map.borrow().get(&e.id).map(|d| d.base_def) {
|
2015-02-16 22:44:23 -06:00
|
|
|
Some(DefStatic(def_id, _)) |
|
2015-03-15 20:35:25 -05:00
|
|
|
Some(DefAssociatedConst(def_id, _)) |
|
2015-02-16 22:44:23 -06:00
|
|
|
Some(DefConst(def_id)) if
|
2014-09-14 19:21:25 -05:00
|
|
|
ast_util::is_local(def_id) => {
|
2014-10-24 09:32:43 -05:00
|
|
|
match self.ast_map.get(def_id.node) {
|
|
|
|
ast_map::NodeItem(item) =>
|
|
|
|
self.visit_item(item),
|
2015-03-15 20:35:25 -05:00
|
|
|
ast_map::NodeTraitItem(item) =>
|
|
|
|
self.visit_trait_item(item),
|
|
|
|
ast_map::NodeImplItem(item) =>
|
|
|
|
self.visit_impl_item(item),
|
2014-10-24 09:32:43 -05:00
|
|
|
ast_map::NodeForeignItem(_) => {},
|
|
|
|
_ => {
|
2015-01-18 18:58:25 -06:00
|
|
|
span_err!(self.sess, e.span, E0266,
|
|
|
|
"expected item, found {}",
|
|
|
|
self.ast_map.node_to_string(def_id.node));
|
2014-10-24 09:32:43 -05:00
|
|
|
return;
|
|
|
|
},
|
|
|
|
}
|
2014-09-14 19:21:25 -05:00
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
visit::walk_expr(self, e);
|
|
|
|
}
|
|
|
|
}
|