2013-06-22 20:58:41 -05:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06: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 17:28:44 -05:00
|
|
|
|
2012-10-15 16:56:42 -05:00
|
|
|
use driver::session::Session;
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::resolve;
|
|
|
|
use middle::ty;
|
|
|
|
use middle::typeck;
|
|
|
|
use util::ppaux;
|
|
|
|
|
|
|
|
use syntax::ast::*;
|
2013-01-30 11:56:33 -06:00
|
|
|
use syntax::codemap;
|
2013-08-12 19:10:10 -05:00
|
|
|
use syntax::{ast_util, ast_map};
|
|
|
|
use syntax::visit::Visitor;
|
|
|
|
use syntax::visit;
|
|
|
|
|
|
|
|
struct CheckCrateVisitor {
|
|
|
|
sess: Session,
|
|
|
|
ast_map: ast_map::map,
|
|
|
|
def_map: resolve::DefMap,
|
|
|
|
method_map: typeck::method_map,
|
|
|
|
tcx: ty::ctxt,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Visitor<bool> for CheckCrateVisitor {
|
|
|
|
fn visit_item(&mut self, i:@item, env:bool) {
|
|
|
|
check_item(self, self.sess, self.ast_map, self.def_map, i, env);
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
fn visit_pat(&mut self, p:@Pat, env:bool) {
|
2013-08-12 19:10:10 -05:00
|
|
|
check_pat(self, p, env);
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
fn visit_expr(&mut self, ex:@Expr, env:bool) {
|
2013-08-12 19:10:10 -05:00
|
|
|
check_expr(self, self.sess, self.def_map, self.method_map,
|
|
|
|
self.tcx, ex, env);
|
|
|
|
}
|
|
|
|
}
|
2011-11-08 20:26:02 -06:00
|
|
|
|
2013-01-30 15:44:24 -06:00
|
|
|
pub fn check_crate(sess: Session,
|
2013-07-19 00:38:55 -05:00
|
|
|
crate: &Crate,
|
2013-01-30 15:44:24 -06:00
|
|
|
ast_map: ast_map::map,
|
|
|
|
def_map: resolve::DefMap,
|
|
|
|
method_map: typeck::method_map,
|
|
|
|
tcx: ty::ctxt) {
|
2013-08-12 19:10:10 -05:00
|
|
|
let mut v = CheckCrateVisitor {
|
|
|
|
sess: sess,
|
|
|
|
ast_map: ast_map,
|
|
|
|
def_map: def_map,
|
|
|
|
method_map: method_map,
|
|
|
|
tcx: tcx,
|
|
|
|
};
|
|
|
|
visit::walk_crate(&mut v, crate, false);
|
2011-12-02 06:42:51 -06:00
|
|
|
sess.abort_if_errors();
|
2011-11-08 20:26:02 -06:00
|
|
|
}
|
|
|
|
|
2013-08-12 19:10:10 -05:00
|
|
|
pub fn check_item(v: &mut CheckCrateVisitor,
|
|
|
|
sess: Session,
|
2013-01-30 15:44:24 -06:00
|
|
|
ast_map: ast_map::map,
|
|
|
|
def_map: resolve::DefMap,
|
|
|
|
it: @item,
|
2013-08-12 19:10:10 -05:00
|
|
|
_is_const: bool) {
|
2012-08-06 14:34:08 -05:00
|
|
|
match it.node {
|
2013-06-21 20:46:34 -05:00
|
|
|
item_static(_, _, ex) => {
|
2013-08-12 19:10:10 -05:00
|
|
|
v.visit_expr(ex, true);
|
2012-04-04 18:12:57 -05:00
|
|
|
check_item_recursion(sess, ast_map, def_map, it);
|
|
|
|
}
|
2012-12-04 12:50:00 -06:00
|
|
|
item_enum(ref enum_definition, _) => {
|
2013-08-03 11:45:23 -05:00
|
|
|
for var in (*enum_definition).variants.iter() {
|
|
|
|
for ex in var.node.disr_expr.iter() {
|
2013-08-12 19:10:10 -05:00
|
|
|
v.visit_expr(*ex, true);
|
2012-01-16 03:36:47 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-12 19:10:10 -05:00
|
|
|
_ => visit::walk_item(v, it, false)
|
2011-11-08 20:26:02 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-01 20:45:37 -05:00
|
|
|
pub fn check_pat(v: &mut CheckCrateVisitor, p: @Pat, _is_const: bool) {
|
|
|
|
fn is_str(e: @Expr) -> bool {
|
2012-08-06 14:34:08 -05:00
|
|
|
match e.node {
|
2013-09-01 20:45:37 -05:00
|
|
|
ExprVstore(
|
|
|
|
@Expr { node: ExprLit(@codemap::Spanned {
|
2013-01-30 11:56:33 -06:00
|
|
|
node: lit_str(_),
|
|
|
|
_}),
|
|
|
|
_ },
|
2013-09-01 20:45:37 -05:00
|
|
|
ExprVstoreUniq
|
2013-01-15 15:51:43 -06:00
|
|
|
) => true,
|
|
|
|
_ => false
|
2012-07-12 14:14:08 -05:00
|
|
|
}
|
2011-12-02 06:42:51 -06:00
|
|
|
}
|
2012-08-06 14:34:08 -05:00
|
|
|
match p.node {
|
2012-07-14 15:55:41 -05:00
|
|
|
// Let through plain ~-string literals here
|
2013-09-01 20:45:37 -05:00
|
|
|
PatLit(a) => if !is_str(a) { v.visit_expr(a, true); },
|
|
|
|
PatRange(a, b) => {
|
2013-08-12 19:10:10 -05:00
|
|
|
if !is_str(a) { v.visit_expr(a, true); }
|
|
|
|
if !is_str(b) { v.visit_expr(b, true); }
|
2011-12-02 06:42:51 -06:00
|
|
|
}
|
2013-08-12 19:10:10 -05:00
|
|
|
_ => visit::walk_pat(v, p, false)
|
2011-12-02 06:42:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-12 19:10:10 -05:00
|
|
|
pub fn check_expr(v: &mut CheckCrateVisitor,
|
|
|
|
sess: Session,
|
2013-01-30 15:44:24 -06:00
|
|
|
def_map: resolve::DefMap,
|
|
|
|
method_map: typeck::method_map,
|
|
|
|
tcx: ty::ctxt,
|
2013-09-01 20:45:37 -05:00
|
|
|
e: @Expr,
|
2013-08-12 19:10:10 -05:00
|
|
|
is_const: bool) {
|
2011-12-02 06:42:51 -06:00
|
|
|
if is_const {
|
2012-08-06 14:34:08 -05:00
|
|
|
match e.node {
|
2013-09-01 20:45:37 -05:00
|
|
|
ExprUnary(_, UnDeref, _) => { }
|
|
|
|
ExprUnary(_, UnBox(_), _) | ExprUnary(_, UnUniq, _) => {
|
2011-12-02 06:42:51 -06:00
|
|
|
sess.span_err(e.span,
|
2013-04-30 11:47:52 -05:00
|
|
|
"disallowed operator in constant expression");
|
2012-08-01 19:30:05 -05:00
|
|
|
return;
|
2011-12-02 06:42:51 -06:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ExprLit(@codemap::Spanned {node: lit_str(_), _}) => { }
|
|
|
|
ExprBinary(*) | ExprUnary(*) => {
|
2013-02-08 16:08:02 -06:00
|
|
|
if method_map.contains_key(&e.id) {
|
2013-04-30 11:47:52 -05:00
|
|
|
sess.span_err(e.span, "user-defined operators are not \
|
2012-01-26 05:26:14 -06:00
|
|
|
allowed in constant expressions");
|
|
|
|
}
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ExprLit(_) => (),
|
|
|
|
ExprCast(_, _) => {
|
2012-03-14 12:04:03 -05:00
|
|
|
let ety = ty::expr_ty(tcx, e);
|
2013-03-04 17:22:03 -06:00
|
|
|
if !ty::type_is_numeric(ety) && !ty::type_is_unsafe_ptr(ety) {
|
2012-07-14 00:57:48 -05:00
|
|
|
sess.span_err(e.span, ~"can not cast to `" +
|
2012-12-23 16:41:37 -06:00
|
|
|
ppaux::ty_to_str(tcx, ety) +
|
2013-05-23 11:09:11 -05:00
|
|
|
"` in a constant expression");
|
2012-03-14 12:04:03 -05:00
|
|
|
}
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ExprPath(ref pth) => {
|
2012-11-06 19:13:52 -06: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.
|
2013-08-07 11:47:28 -05:00
|
|
|
if !pth.segments.iter().all(|segment| segment.types.is_empty()) {
|
2012-11-06 19:13:52 -06:00
|
|
|
sess.span_err(
|
2013-04-30 11:47:52 -05:00
|
|
|
e.span, "paths in constants may only refer to \
|
|
|
|
items without type parameters");
|
2012-11-06 19:13:52 -06:00
|
|
|
}
|
2013-02-05 21:41:45 -06:00
|
|
|
match def_map.find(&e.id) {
|
2013-09-01 20:45:37 -05:00
|
|
|
Some(&DefStatic(*)) |
|
|
|
|
Some(&DefFn(_, _)) |
|
|
|
|
Some(&DefVariant(_, _)) |
|
|
|
|
Some(&DefStruct(_)) => { }
|
2013-02-13 13:46:14 -06:00
|
|
|
|
2013-03-22 21:26:41 -05:00
|
|
|
Some(&def) => {
|
2012-11-30 18:29:28 -06:00
|
|
|
debug!("(checking const) found bad def: %?", def);
|
2012-04-04 17:02:25 -05:00
|
|
|
sess.span_err(
|
2012-07-14 00:57:48 -05:00
|
|
|
e.span,
|
2013-04-30 11:47:52 -05:00
|
|
|
"paths in constants may only refer to \
|
|
|
|
constants or functions");
|
2012-04-04 17:02:25 -05:00
|
|
|
}
|
2012-11-30 18:29:28 -06:00
|
|
|
None => {
|
2013-04-30 11:47:52 -05:00
|
|
|
sess.span_bug(e.span, "unbound path in const?!");
|
2012-11-30 18:29:28 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ExprCall(callee, _, NoSugar) => {
|
2013-02-05 21:41:45 -06:00
|
|
|
match def_map.find(&callee.id) {
|
2013-09-01 20:45:37 -05:00
|
|
|
Some(&DefStruct(*)) => {} // OK.
|
|
|
|
Some(&DefVariant(*)) => {} // OK.
|
2012-11-30 18:29:28 -06:00
|
|
|
_ => {
|
|
|
|
sess.span_err(
|
|
|
|
e.span,
|
2013-04-30 11:47:52 -05:00
|
|
|
"function calls in constants are limited to \
|
|
|
|
struct and enum constructors");
|
2012-11-30 18:29:28 -06:00
|
|
|
}
|
2012-04-04 17:02:25 -05:00
|
|
|
}
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ExprParen(e) => { check_expr(v, sess, def_map, method_map,
|
2013-08-12 19:10:10 -05:00
|
|
|
tcx, e, is_const); }
|
2013-09-01 20:45:37 -05:00
|
|
|
ExprVstore(_, ExprVstoreSlice) |
|
|
|
|
ExprVec(_, MutImmutable) |
|
|
|
|
ExprAddrOf(MutImmutable, _) |
|
|
|
|
ExprField(*) |
|
|
|
|
ExprIndex(*) |
|
|
|
|
ExprTup(*) |
|
|
|
|
ExprRepeat(*) |
|
|
|
|
ExprStruct(*) => { }
|
|
|
|
ExprAddrOf(*) => {
|
2012-08-03 20:07:45 -05:00
|
|
|
sess.span_err(
|
|
|
|
e.span,
|
2013-04-30 11:47:52 -05:00
|
|
|
"borrowed pointers in constants may only refer to \
|
|
|
|
immutable values");
|
2012-08-03 20:07:45 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => {
|
2011-12-02 06:42:51 -06:00
|
|
|
sess.span_err(e.span,
|
2013-04-30 11:47:52 -05:00
|
|
|
"constant contains unimplemented expression type");
|
2012-08-01 19:30:05 -05:00
|
|
|
return;
|
2011-11-09 03:42:30 -06:00
|
|
|
}
|
|
|
|
}
|
2011-11-08 20:26:02 -06:00
|
|
|
}
|
2012-08-06 14:34:08 -05:00
|
|
|
match e.node {
|
2013-09-01 20:45:37 -05:00
|
|
|
ExprLit(@codemap::Spanned {node: lit_int(v, t), _}) => {
|
2011-12-07 14:53:05 -06:00
|
|
|
if t != ty_char {
|
|
|
|
if (v as u64) > ast_util::int_ty_max(
|
2012-01-29 20:33:08 -06:00
|
|
|
if t == ty_i { sess.targ_cfg.int_type } else { t }) {
|
2013-04-30 11:47:52 -05:00
|
|
|
sess.span_err(e.span, "literal out of range for its type");
|
2011-12-07 14:53:05 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ExprLit(@codemap::Spanned {node: lit_uint(v, t), _}) => {
|
2011-12-07 14:53:05 -06:00
|
|
|
if v > ast_util::uint_ty_max(
|
2012-01-29 20:33:08 -06:00
|
|
|
if t == ty_u { sess.targ_cfg.uint_type } else { t }) {
|
2013-04-30 11:47:52 -05:00
|
|
|
sess.span_err(e.span, "literal out of range for its type");
|
2011-12-07 14:53:05 -06:00
|
|
|
}
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => ()
|
2011-12-07 14:53:05 -06:00
|
|
|
}
|
2013-08-12 19:10:10 -05:00
|
|
|
visit::walk_expr(v, e, is_const);
|
2011-11-08 20:26:02 -06:00
|
|
|
}
|
|
|
|
|
2013-07-02 14:47:32 -05:00
|
|
|
#[deriving(Clone)]
|
|
|
|
struct env {
|
|
|
|
root_it: @item,
|
|
|
|
sess: Session,
|
|
|
|
ast_map: ast_map::map,
|
|
|
|
def_map: resolve::DefMap,
|
2013-07-27 03:25:59 -05:00
|
|
|
idstack: @mut ~[NodeId]
|
2013-07-02 14:47:32 -05:00
|
|
|
}
|
|
|
|
|
2013-08-12 19:10:10 -05:00
|
|
|
struct CheckItemRecursionVisitor;
|
|
|
|
|
2012-04-04 18:12:57 -05:00
|
|
|
// Make sure a const item doesn't recursively refer to itself
|
2012-06-07 15:49:01 -05:00
|
|
|
// FIXME: Should use the dependency graph when it's available (#1356)
|
2013-01-30 15:44:24 -06:00
|
|
|
pub fn check_item_recursion(sess: Session,
|
|
|
|
ast_map: ast_map::map,
|
|
|
|
def_map: resolve::DefMap,
|
|
|
|
it: @item) {
|
2013-02-14 07:36:56 -06:00
|
|
|
let env = env {
|
2012-04-04 18:12:57 -05:00
|
|
|
root_it: it,
|
|
|
|
sess: sess,
|
|
|
|
ast_map: ast_map,
|
|
|
|
def_map: def_map,
|
2013-02-14 07:36:56 -06:00
|
|
|
idstack: @mut ~[]
|
2012-04-04 18:12:57 -05:00
|
|
|
};
|
|
|
|
|
2013-08-12 19:10:10 -05:00
|
|
|
let mut visitor = CheckItemRecursionVisitor;
|
|
|
|
visitor.visit_item(it, env);
|
|
|
|
}
|
2012-04-04 18:12:57 -05:00
|
|
|
|
2013-08-12 19:10:10 -05:00
|
|
|
impl Visitor<env> for CheckItemRecursionVisitor {
|
|
|
|
fn visit_item(&mut self, it: @item, env: env) {
|
2013-07-04 21:13:26 -05:00
|
|
|
if env.idstack.iter().any(|x| x == &(it.id)) {
|
2013-04-30 11:47:52 -05:00
|
|
|
env.sess.span_fatal(env.root_it.span, "recursive constant");
|
2012-04-04 18:12:57 -05:00
|
|
|
}
|
2013-02-14 07:36:56 -06:00
|
|
|
env.idstack.push(it.id);
|
2013-08-12 19:10:10 -05:00
|
|
|
visit::walk_item(self, it, env);
|
2013-02-14 07:36:56 -06:00
|
|
|
env.idstack.pop();
|
2012-04-04 18:12:57 -05:00
|
|
|
}
|
|
|
|
|
2013-09-01 20:45:37 -05:00
|
|
|
fn visit_expr(&mut self, e: @Expr, env: env) {
|
2012-08-06 14:34:08 -05:00
|
|
|
match e.node {
|
2013-09-01 20:45:37 -05:00
|
|
|
ExprPath(*) => match env.def_map.find(&e.id) {
|
|
|
|
Some(&DefStatic(def_id, _)) if ast_util::is_local(def_id) =>
|
2013-06-22 20:58:41 -05:00
|
|
|
match env.ast_map.get_copy(&def_id.node) {
|
|
|
|
ast_map::node_item(it, _) => {
|
2013-08-12 19:10:10 -05:00
|
|
|
self.visit_item(it, env);
|
2013-06-22 20:58:41 -05:00
|
|
|
}
|
|
|
|
_ => fail!("const not bound to an item")
|
|
|
|
},
|
|
|
|
_ => ()
|
|
|
|
},
|
|
|
|
_ => ()
|
2012-04-04 18:12:57 -05:00
|
|
|
}
|
2013-08-12 19:10:10 -05:00
|
|
|
visit::walk_expr(self, e, env);
|
2012-04-04 18:12:57 -05:00
|
|
|
}
|
|
|
|
}
|