2013-06-22 18:58:41 -07:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 16:48:01 -08: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.
|
|
|
|
|
2013-05-17 15:28:44 -07:00
|
|
|
|
2012-10-15 14:56:42 -07:00
|
|
|
use driver::session::Session;
|
2012-12-23 17:41:37 -05:00
|
|
|
use middle::resolve;
|
|
|
|
use middle::ty;
|
|
|
|
use middle::typeck;
|
|
|
|
use util::ppaux;
|
|
|
|
|
|
|
|
use syntax::ast::*;
|
2013-01-30 09:56:33 -08:00
|
|
|
use syntax::codemap;
|
2012-12-23 17:41:37 -05:00
|
|
|
use syntax::{visit, ast_util, ast_map};
|
2011-11-08 18:26:02 -08:00
|
|
|
|
2013-01-30 13:44:24 -08:00
|
|
|
pub fn check_crate(sess: Session,
|
2013-06-27 15:04:22 +02:00
|
|
|
crate: &crate,
|
2013-01-30 13:44:24 -08:00
|
|
|
ast_map: ast_map::map,
|
|
|
|
def_map: resolve::DefMap,
|
|
|
|
method_map: typeck::method_map,
|
|
|
|
tcx: ty::ctxt) {
|
2013-06-12 04:55:16 +02:00
|
|
|
visit::visit_crate(crate, (false, visit::mk_vt(@visit::Visitor {
|
|
|
|
visit_item: |a,b| check_item(sess, ast_map, def_map, a, b),
|
2011-12-02 13:42:51 +01:00
|
|
|
visit_pat: check_pat,
|
2013-06-12 04:55:16 +02:00
|
|
|
visit_expr: |a,b|
|
|
|
|
check_expr(sess, def_map, method_map, tcx, a, b),
|
2012-09-04 13:29:32 -07:00
|
|
|
.. *visit::default_visitor()
|
2013-06-12 04:55:16 +02:00
|
|
|
})));
|
2011-12-02 13:42:51 +01:00
|
|
|
sess.abort_if_errors();
|
2011-11-08 18:26:02 -08:00
|
|
|
}
|
|
|
|
|
2013-01-30 13:44:24 -08:00
|
|
|
pub fn check_item(sess: Session,
|
|
|
|
ast_map: ast_map::map,
|
|
|
|
def_map: resolve::DefMap,
|
|
|
|
it: @item,
|
2013-06-12 04:55:16 +02:00
|
|
|
(_is_const, v): (bool,
|
|
|
|
visit::vt<bool>)) {
|
2012-08-06 12:34:08 -07:00
|
|
|
match it.node {
|
2013-06-21 18:46:34 -07:00
|
|
|
item_static(_, _, ex) => {
|
2013-06-12 04:55:16 +02:00
|
|
|
(v.visit_expr)(ex, (true, v));
|
2012-04-04 16:12:57 -07:00
|
|
|
check_item_recursion(sess, ast_map, def_map, it);
|
|
|
|
}
|
2012-12-04 10:50:00 -08:00
|
|
|
item_enum(ref enum_definition, _) => {
|
2013-06-21 08:29:53 -04:00
|
|
|
for (*enum_definition).variants.iter().advance |var| {
|
2013-06-10 17:50:12 -04:00
|
|
|
for var.node.disr_expr.iter().advance |ex| {
|
2013-06-12 04:55:16 +02:00
|
|
|
(v.visit_expr)(*ex, (true, v));
|
2012-01-16 02:36:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-12 04:55:16 +02:00
|
|
|
_ => visit::visit_item(it, (false, v))
|
2011-11-08 18:26:02 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-12 04:55:16 +02:00
|
|
|
pub fn check_pat(p: @pat, (_is_const, v): (bool, visit::vt<bool>)) {
|
2011-12-02 13:42:51 +01:00
|
|
|
fn is_str(e: @expr) -> bool {
|
2012-08-06 12:34:08 -07:00
|
|
|
match e.node {
|
2013-01-15 13:51:43 -08:00
|
|
|
expr_vstore(
|
2013-01-30 09:56:33 -08:00
|
|
|
@expr { node: expr_lit(@codemap::spanned {
|
|
|
|
node: lit_str(_),
|
|
|
|
_}),
|
|
|
|
_ },
|
2013-01-15 13:51:43 -08:00
|
|
|
expr_vstore_uniq
|
|
|
|
) => true,
|
|
|
|
_ => false
|
2012-07-12 12:14:08 -07:00
|
|
|
}
|
2011-12-02 13:42:51 +01:00
|
|
|
}
|
2012-08-06 12:34:08 -07:00
|
|
|
match p.node {
|
2012-07-14 13:55:41 -07:00
|
|
|
// Let through plain ~-string literals here
|
2013-06-12 04:55:16 +02:00
|
|
|
pat_lit(a) => if !is_str(a) { (v.visit_expr)(a, (true, v)); },
|
2012-08-03 19:59:04 -07:00
|
|
|
pat_range(a, b) => {
|
2013-06-12 04:55:16 +02:00
|
|
|
if !is_str(a) { (v.visit_expr)(a, (true, v)); }
|
|
|
|
if !is_str(b) { (v.visit_expr)(b, (true, v)); }
|
2011-12-02 13:42:51 +01:00
|
|
|
}
|
2013-06-12 04:55:16 +02:00
|
|
|
_ => visit::visit_pat(p, (false, v))
|
2011-12-02 13:42:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-30 13:44:24 -08:00
|
|
|
pub fn check_expr(sess: Session,
|
|
|
|
def_map: resolve::DefMap,
|
|
|
|
method_map: typeck::method_map,
|
|
|
|
tcx: ty::ctxt,
|
|
|
|
e: @expr,
|
2013-06-12 04:55:16 +02:00
|
|
|
(is_const, v): (bool,
|
|
|
|
visit::vt<bool>)) {
|
2011-12-02 13:42:51 +01:00
|
|
|
if is_const {
|
2012-08-06 12:34:08 -07:00
|
|
|
match e.node {
|
2013-06-01 15:31:56 -07:00
|
|
|
expr_unary(_, deref, _) => { }
|
2013-06-23 21:12:17 -07:00
|
|
|
expr_unary(_, box(_), _) | expr_unary(_, uniq, _) => {
|
2011-12-02 13:42:51 +01:00
|
|
|
sess.span_err(e.span,
|
2013-05-01 01:47:52 +09:00
|
|
|
"disallowed operator in constant expression");
|
2012-08-01 17:30:05 -07:00
|
|
|
return;
|
2011-12-02 13:42:51 +01:00
|
|
|
}
|
2013-01-30 09:56:33 -08:00
|
|
|
expr_lit(@codemap::spanned {node: lit_str(_), _}) => { }
|
2013-06-01 15:31:56 -07:00
|
|
|
expr_binary(*) | expr_unary(*) => {
|
2013-02-08 17:08:02 -05:00
|
|
|
if method_map.contains_key(&e.id) {
|
2013-05-01 01:47:52 +09:00
|
|
|
sess.span_err(e.span, "user-defined operators are not \
|
2012-01-26 12:26:14 +01:00
|
|
|
allowed in constant expressions");
|
|
|
|
}
|
|
|
|
}
|
2012-08-03 19:59:04 -07:00
|
|
|
expr_lit(_) => (),
|
|
|
|
expr_cast(_, _) => {
|
2012-03-14 18:04:03 +01:00
|
|
|
let ety = ty::expr_ty(tcx, e);
|
2013-03-04 18:22:03 -05:00
|
|
|
if !ty::type_is_numeric(ety) && !ty::type_is_unsafe_ptr(ety) {
|
2012-07-13 22:57:48 -07:00
|
|
|
sess.span_err(e.span, ~"can not cast to `" +
|
2012-12-23 17:41:37 -05:00
|
|
|
ppaux::ty_to_str(tcx, ety) +
|
2013-05-24 01:09:11 +09:00
|
|
|
"` in a constant expression");
|
2012-03-14 18:04:03 +01:00
|
|
|
}
|
|
|
|
}
|
2013-07-05 22:15:21 +12:00
|
|
|
expr_path(ref pth) => {
|
2012-11-06 17:13:52 -08:00
|
|
|
// NB: In the future you might wish to relax this slightly
|
|
|
|
// to handle on-demand instantiation of functions via
|
|
|
|
// foo::<bar> in a const. Currently that is only done on
|
|
|
|
// a path in trans::callee that only works in block contexts.
|
|
|
|
if pth.types.len() != 0 {
|
|
|
|
sess.span_err(
|
2013-05-01 01:47:52 +09:00
|
|
|
e.span, "paths in constants may only refer to \
|
|
|
|
items without type parameters");
|
2012-11-06 17:13:52 -08:00
|
|
|
}
|
2013-02-05 19:41:45 -08:00
|
|
|
match def_map.find(&e.id) {
|
2013-06-21 18:46:34 -07:00
|
|
|
Some(&def_static(*)) |
|
2013-03-22 22:26:41 -04:00
|
|
|
Some(&def_fn(_, _)) |
|
|
|
|
Some(&def_variant(_, _)) |
|
|
|
|
Some(&def_struct(_)) => { }
|
2013-02-13 11:46:14 -08:00
|
|
|
|
2013-03-22 22:26:41 -04:00
|
|
|
Some(&def) => {
|
2012-11-30 16:29:28 -08:00
|
|
|
debug!("(checking const) found bad def: %?", def);
|
2012-04-04 15:02:25 -07:00
|
|
|
sess.span_err(
|
2012-07-13 22:57:48 -07:00
|
|
|
e.span,
|
2013-05-01 01:47:52 +09:00
|
|
|
"paths in constants may only refer to \
|
|
|
|
constants or functions");
|
2012-04-04 15:02:25 -07:00
|
|
|
}
|
2012-11-30 16:29:28 -08:00
|
|
|
None => {
|
2013-05-01 01:47:52 +09:00
|
|
|
sess.span_bug(e.span, "unbound path in const?!");
|
2012-11-30 16:29:28 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-31 17:12:29 -08:00
|
|
|
expr_call(callee, _, NoSugar) => {
|
2013-02-05 19:41:45 -08:00
|
|
|
match def_map.find(&callee.id) {
|
2013-03-22 22:26:41 -04:00
|
|
|
Some(&def_struct(*)) => {} // OK.
|
|
|
|
Some(&def_variant(*)) => {} // OK.
|
2012-11-30 16:29:28 -08:00
|
|
|
_ => {
|
|
|
|
sess.span_err(
|
|
|
|
e.span,
|
2013-05-01 01:47:52 +09:00
|
|
|
"function calls in constants are limited to \
|
|
|
|
struct and enum constructors");
|
2012-11-30 16:29:28 -08:00
|
|
|
}
|
2012-04-04 15:02:25 -07:00
|
|
|
}
|
|
|
|
}
|
2012-10-27 17:14:09 -07:00
|
|
|
expr_paren(e) => { check_expr(sess, def_map, method_map,
|
2013-06-12 04:55:16 +02:00
|
|
|
tcx, e, (is_const, v)); }
|
2012-09-11 21:25:01 -07:00
|
|
|
expr_vstore(_, expr_vstore_slice) |
|
2012-08-03 21:44:42 -07:00
|
|
|
expr_vec(_, m_imm) |
|
2012-08-03 18:07:45 -07:00
|
|
|
expr_addr_of(m_imm, _) |
|
2012-08-08 19:41:50 -07:00
|
|
|
expr_field(*) |
|
|
|
|
expr_index(*) |
|
2012-07-31 18:34:36 -07:00
|
|
|
expr_tup(*) |
|
2013-03-05 18:22:13 -08:00
|
|
|
expr_struct(_, _, None) => { }
|
2012-08-03 19:59:04 -07:00
|
|
|
expr_addr_of(*) => {
|
2012-08-03 18:07:45 -07:00
|
|
|
sess.span_err(
|
|
|
|
e.span,
|
2013-05-01 01:47:52 +09:00
|
|
|
"borrowed pointers in constants may only refer to \
|
|
|
|
immutable values");
|
2012-08-03 18:07:45 -07:00
|
|
|
}
|
2012-08-03 19:59:04 -07:00
|
|
|
_ => {
|
2011-12-02 13:42:51 +01:00
|
|
|
sess.span_err(e.span,
|
2013-05-01 01:47:52 +09:00
|
|
|
"constant contains unimplemented expression type");
|
2012-08-01 17:30:05 -07:00
|
|
|
return;
|
2011-11-09 01:42:30 -08:00
|
|
|
}
|
|
|
|
}
|
2011-11-08 18:26:02 -08:00
|
|
|
}
|
2012-08-06 12:34:08 -07:00
|
|
|
match e.node {
|
2013-01-30 09:56:33 -08:00
|
|
|
expr_lit(@codemap::spanned {node: lit_int(v, t), _}) => {
|
2011-12-07 21:53:05 +01:00
|
|
|
if t != ty_char {
|
|
|
|
if (v as u64) > ast_util::int_ty_max(
|
2012-01-29 21:33:08 -05:00
|
|
|
if t == ty_i { sess.targ_cfg.int_type } else { t }) {
|
2013-05-01 01:47:52 +09:00
|
|
|
sess.span_err(e.span, "literal out of range for its type");
|
2011-12-07 21:53:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-30 09:56:33 -08:00
|
|
|
expr_lit(@codemap::spanned {node: lit_uint(v, t), _}) => {
|
2011-12-07 21:53:05 +01:00
|
|
|
if v > ast_util::uint_ty_max(
|
2012-01-29 21:33:08 -05:00
|
|
|
if t == ty_u { sess.targ_cfg.uint_type } else { t }) {
|
2013-05-01 01:47:52 +09:00
|
|
|
sess.span_err(e.span, "literal out of range for its type");
|
2011-12-07 21:53:05 +01:00
|
|
|
}
|
|
|
|
}
|
2012-08-03 19:59:04 -07:00
|
|
|
_ => ()
|
2011-12-07 21:53:05 +01:00
|
|
|
}
|
2013-06-12 04:55:16 +02:00
|
|
|
visit::visit_expr(e, (is_const, v));
|
2011-11-08 18:26:02 -08:00
|
|
|
}
|
|
|
|
|
2012-04-04 16:12:57 -07:00
|
|
|
// Make sure a const item doesn't recursively refer to itself
|
2012-06-07 13:49:01 -07:00
|
|
|
// FIXME: Should use the dependency graph when it's available (#1356)
|
2013-01-30 13:44:24 -08:00
|
|
|
pub fn check_item_recursion(sess: Session,
|
|
|
|
ast_map: ast_map::map,
|
|
|
|
def_map: resolve::DefMap,
|
|
|
|
it: @item) {
|
2013-02-14 22:36:56 +09:00
|
|
|
struct env {
|
2012-04-04 16:12:57 -07:00
|
|
|
root_it: @item,
|
2012-10-15 14:56:42 -07:00
|
|
|
sess: Session,
|
2012-04-04 16:12:57 -07:00
|
|
|
ast_map: ast_map::map,
|
2012-08-29 13:26:26 -07:00
|
|
|
def_map: resolve::DefMap,
|
2013-02-14 22:36:56 +09:00
|
|
|
idstack: @mut ~[node_id]
|
|
|
|
}
|
2012-04-04 16:12:57 -07:00
|
|
|
|
2013-02-14 22:36:56 +09:00
|
|
|
let env = env {
|
2012-04-04 16:12:57 -07:00
|
|
|
root_it: it,
|
|
|
|
sess: sess,
|
|
|
|
ast_map: ast_map,
|
|
|
|
def_map: def_map,
|
2013-02-14 22:36:56 +09:00
|
|
|
idstack: @mut ~[]
|
2012-04-04 16:12:57 -07:00
|
|
|
};
|
|
|
|
|
2013-01-08 14:00:45 -08:00
|
|
|
let visitor = visit::mk_vt(@visit::Visitor {
|
2012-04-04 16:12:57 -07:00
|
|
|
visit_item: visit_item,
|
2012-09-04 13:29:32 -07:00
|
|
|
visit_expr: visit_expr,
|
|
|
|
.. *visit::default_visitor()
|
2012-04-04 16:12:57 -07:00
|
|
|
});
|
2013-06-12 04:55:16 +02:00
|
|
|
(visitor.visit_item)(it, (env, visitor));
|
2012-04-04 16:12:57 -07:00
|
|
|
|
2013-06-12 04:55:16 +02:00
|
|
|
fn visit_item(it: @item, (env, v): (env, visit::vt<env>)) {
|
2013-07-04 22:13:26 -04:00
|
|
|
if env.idstack.iter().any(|x| x == &(it.id)) {
|
2013-05-01 01:47:52 +09:00
|
|
|
env.sess.span_fatal(env.root_it.span, "recursive constant");
|
2012-04-04 16:12:57 -07:00
|
|
|
}
|
2013-02-14 22:36:56 +09:00
|
|
|
env.idstack.push(it.id);
|
2013-06-12 04:55:16 +02:00
|
|
|
visit::visit_item(it, (env, v));
|
2013-02-14 22:36:56 +09:00
|
|
|
env.idstack.pop();
|
2012-04-04 16:12:57 -07:00
|
|
|
}
|
|
|
|
|
2013-06-12 04:55:16 +02:00
|
|
|
fn visit_expr(e: @expr, (env, v): (env, visit::vt<env>)) {
|
2012-08-06 12:34:08 -07:00
|
|
|
match e.node {
|
2013-06-22 18:58:41 -07:00
|
|
|
expr_path(*) => match env.def_map.find(&e.id) {
|
2013-06-25 19:03:12 -07:00
|
|
|
Some(&def_static(def_id, _)) if ast_util::is_local(def_id) =>
|
2013-06-22 18:58:41 -07:00
|
|
|
match env.ast_map.get_copy(&def_id.node) {
|
|
|
|
ast_map::node_item(it, _) => {
|
|
|
|
(v.visit_item)(it, (env, v));
|
|
|
|
}
|
|
|
|
_ => fail!("const not bound to an item")
|
|
|
|
},
|
|
|
|
_ => ()
|
|
|
|
},
|
|
|
|
_ => ()
|
2012-04-04 16:12:57 -07:00
|
|
|
}
|
2013-06-12 04:55:16 +02:00
|
|
|
visit::visit_expr(e, (env, v));
|
2012-04-04 16:12:57 -07:00
|
|
|
}
|
|
|
|
}
|