1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 use driver::session::Session;
20 use syntax::{ast_util, ast_map};
21 use syntax::visit::Visitor;
24 pub struct CheckCrateVisitor<'a> {
25 def_map: resolve::DefMap,
26 method_map: typeck::MethodMap,
30 impl<'a> Visitor<bool> for CheckCrateVisitor<'a> {
31 fn visit_item(&mut self, i: &Item, env: bool) {
32 check_item(self, i, env);
34 fn visit_pat(&mut self, p: &Pat, env: bool) {
35 check_pat(self, p, env);
37 fn visit_expr(&mut self, ex: &Expr, env: bool) {
38 check_expr(self, ex, env);
42 pub fn check_crate(krate: &Crate,
43 def_map: resolve::DefMap,
44 method_map: typeck::MethodMap,
46 let mut v = CheckCrateVisitor {
48 method_map: method_map,
51 visit::walk_crate(&mut v, krate, false);
52 tcx.sess.abort_if_errors();
55 fn check_item(v: &mut CheckCrateVisitor, it: &Item, _is_const: bool) {
57 ItemStatic(_, _, ex) => {
58 v.visit_expr(ex, true);
59 check_item_recursion(&v.tcx.sess, &v.tcx.map, v.def_map, it);
61 ItemEnum(ref enum_definition, _) => {
62 for var in (*enum_definition).variants.iter() {
63 for ex in var.node.disr_expr.iter() {
64 v.visit_expr(*ex, true);
68 _ => visit::walk_item(v, it, false)
72 fn check_pat(v: &mut CheckCrateVisitor, p: &Pat, _is_const: bool) {
73 fn is_str(e: &Expr) -> bool {
75 ExprVstore(expr, ExprVstoreUniq) => {
77 ExprLit(lit) => ast_util::lit_is_str(lit),
85 // Let through plain ~-string literals here
86 PatLit(a) => if !is_str(a) { v.visit_expr(a, true); },
88 if !is_str(a) { v.visit_expr(a, true); }
89 if !is_str(b) { v.visit_expr(b, true); }
91 _ => visit::walk_pat(v, p, false)
95 fn check_expr(v: &mut CheckCrateVisitor, e: &Expr, is_const: bool) {
98 ExprUnary(UnDeref, _) => { }
99 ExprUnary(UnBox, _) | ExprUnary(UnUniq, _) => {
100 v.tcx.sess.span_err(e.span,
101 "cannot do allocations in constant expressions");
104 ExprLit(lit) if ast_util::lit_is_str(lit) => {}
105 ExprBinary(..) | ExprUnary(..) => {
106 let method_call = typeck::MethodCall::expr(e.id);
107 if v.method_map.borrow().get().contains_key(&method_call) {
108 v.tcx.sess.span_err(e.span, "user-defined operators are not \
109 allowed in constant expressions");
114 let ety = ty::expr_ty(v.tcx, e);
115 if !ty::type_is_numeric(ety) && !ty::type_is_unsafe_ptr(ety) {
116 v.tcx.sess.span_err(e.span, ~"can not cast to `" +
117 ppaux::ty_to_str(v.tcx, ety) +
118 "` in a constant expression");
121 ExprPath(ref pth) => {
122 // NB: In the future you might wish to relax this slightly
123 // to handle on-demand instantiation of functions via
124 // foo::<bar> in a const. Currently that is only done on
125 // a path in trans::callee that only works in block contexts.
126 if !pth.segments.iter().all(|segment| segment.types.is_empty()) {
127 v.tcx.sess.span_err(e.span,
128 "paths in constants may only refer to \
129 items without type parameters");
131 match v.def_map.borrow().get().find(&e.id) {
132 Some(&DefStatic(..)) |
134 Some(&DefVariant(_, _, _)) |
135 Some(&DefStruct(_)) => { }
138 debug!("(checking const) found bad def: {:?}", def);
139 v.tcx.sess.span_err(e.span,
140 "paths in constants may only refer to \
141 constants or functions");
144 v.tcx.sess.span_bug(e.span, "unbound path in const?!");
148 ExprCall(callee, _) => {
149 match v.def_map.borrow().get().find(&callee.id) {
150 Some(&DefStruct(..)) => {} // OK.
151 Some(&DefVariant(..)) => {} // OK.
153 v.tcx.sess.span_err(e.span,
154 "function calls in constants are limited to \
155 struct and enum constructors");
159 ExprVstore(_, ExprVstoreMutSlice) |
160 ExprVstore(_, ExprVstoreSlice) |
161 ExprVec(_, MutImmutable) |
162 ExprAddrOf(MutImmutable, _) |
168 ExprStruct(..) => { }
170 v.tcx.sess.span_err(e.span,
171 "references in constants may only refer to \
174 ExprVstore(_, ExprVstoreUniq) => {
175 v.tcx.sess.span_err(e.span, "cannot allocate vectors in constant expressions")
179 v.tcx.sess.span_err(e.span,
180 "constant contains unimplemented expression type");
185 visit::walk_expr(v, e, is_const);
188 struct CheckItemRecursionVisitor<'a> {
191 ast_map: &'a ast_map::Map,
192 def_map: resolve::DefMap,
193 idstack: Vec<NodeId> }
195 // Make sure a const item doesn't recursively refer to itself
196 // FIXME: Should use the dependency graph when it's available (#1356)
197 pub fn check_item_recursion<'a>(sess: &'a Session,
198 ast_map: &'a ast_map::Map,
199 def_map: resolve::DefMap,
202 let mut visitor = CheckItemRecursionVisitor {
209 visitor.visit_item(it, ());
212 impl<'a> Visitor<()> for CheckItemRecursionVisitor<'a> {
213 fn visit_item(&mut self, it: &Item, _: ()) {
214 if self.idstack.iter().any(|x| x == &(it.id)) {
215 self.sess.span_fatal(self.root_it.span, "recursive constant");
217 self.idstack.push(it.id);
218 visit::walk_item(self, it, ());
222 fn visit_expr(&mut self, e: &Expr, _: ()) {
225 let def_map = self.def_map.borrow();
226 match def_map.get().find(&e.id) {
227 Some(&DefStatic(def_id, _)) if
228 ast_util::is_local(def_id) => {
229 self.visit_item(self.ast_map.expect_item(def_id.node), ());
236 visit::walk_expr(self, e, ());