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 {
26 def_map: resolve::DefMap,
27 method_map: typeck::MethodMap,
31 impl Visitor<bool> for CheckCrateVisitor {
32 fn visit_item(&mut self, i: &Item, env: bool) {
33 check_item(self, self.sess, self.def_map, i, env);
35 fn visit_pat(&mut self, p: &Pat, env: bool) {
36 check_pat(self, p, env);
38 fn visit_expr(&mut self, ex: &Expr, env: bool) {
39 check_expr(self, self.sess, self.def_map, self.method_map,
44 pub fn check_crate(sess: Session,
46 def_map: resolve::DefMap,
47 method_map: typeck::MethodMap,
49 let mut v = CheckCrateVisitor {
52 method_map: method_map,
55 visit::walk_crate(&mut v, krate, false);
56 sess.abort_if_errors();
59 pub fn check_item(v: &mut CheckCrateVisitor,
61 def_map: resolve::DefMap,
65 ItemStatic(_, _, ex) => {
66 v.visit_expr(ex, true);
67 check_item_recursion(sess, &v.tcx.map, def_map, it);
69 ItemEnum(ref enum_definition, _) => {
70 for var in (*enum_definition).variants.iter() {
71 for ex in var.node.disr_expr.iter() {
72 v.visit_expr(*ex, true);
76 _ => visit::walk_item(v, it, false)
80 pub fn check_pat(v: &mut CheckCrateVisitor, p: &Pat, _is_const: bool) {
81 fn is_str(e: @Expr) -> bool {
83 ExprVstore(expr, ExprVstoreUniq) => {
85 ExprLit(lit) => ast_util::lit_is_str(lit),
93 // Let through plain ~-string literals here
94 PatLit(a) => if !is_str(a) { v.visit_expr(a, true); },
96 if !is_str(a) { v.visit_expr(a, true); }
97 if !is_str(b) { v.visit_expr(b, true); }
99 _ => visit::walk_pat(v, p, false)
103 pub fn check_expr(v: &mut CheckCrateVisitor,
105 def_map: resolve::DefMap,
106 method_map: typeck::MethodMap,
112 ExprUnary(UnDeref, _) => { }
113 ExprUnary(UnBox, _) | ExprUnary(UnUniq, _) => {
114 sess.span_err(e.span,
115 "cannot do allocations in constant expressions");
118 ExprLit(lit) if ast_util::lit_is_str(lit) => {}
119 ExprBinary(..) | ExprUnary(..) => {
120 let method_map = method_map.borrow();
121 if method_map.get().contains_key(&e.id) {
122 sess.span_err(e.span, "user-defined operators are not \
123 allowed in constant expressions");
128 let ety = ty::expr_ty(tcx, e);
129 if !ty::type_is_numeric(ety) && !ty::type_is_unsafe_ptr(ety) {
130 sess.span_err(e.span, ~"can not cast to `" +
131 ppaux::ty_to_str(tcx, ety) +
132 "` in a constant expression");
135 ExprPath(ref pth) => {
136 // NB: In the future you might wish to relax this slightly
137 // to handle on-demand instantiation of functions via
138 // foo::<bar> in a const. Currently that is only done on
139 // a path in trans::callee that only works in block contexts.
140 if !pth.segments.iter().all(|segment| segment.types.is_empty()) {
142 e.span, "paths in constants may only refer to \
143 items without type parameters");
145 let def_map = def_map.borrow();
146 match def_map.get().find(&e.id) {
147 Some(&DefStatic(..)) |
149 Some(&DefVariant(_, _, _)) |
150 Some(&DefStruct(_)) => { }
153 debug!("(checking const) found bad def: {:?}", def);
156 "paths in constants may only refer to \
157 constants or functions");
160 sess.span_bug(e.span, "unbound path in const?!");
164 ExprCall(callee, _) => {
165 let def_map = def_map.borrow();
166 match def_map.get().find(&callee.id) {
167 Some(&DefStruct(..)) => {} // OK.
168 Some(&DefVariant(..)) => {} // OK.
172 "function calls in constants are limited to \
173 struct and enum constructors");
177 ExprVstore(_, ExprVstoreSlice) |
178 ExprVec(_, MutImmutable) |
179 ExprAddrOf(MutImmutable, _) |
185 ExprStruct(..) => { }
189 "references in constants may only refer to \
192 ExprVstore(_, ExprVstoreUniq) => {
193 sess.span_err(e.span, "cannot allocate vectors in constant expressions")
197 sess.span_err(e.span,
198 "constant contains unimplemented expression type");
203 visit::walk_expr(v, e, is_const);
206 struct CheckItemRecursionVisitor<'a> {
209 ast_map: &'a ast_map::Map,
210 def_map: resolve::DefMap,
211 idstack: Vec<NodeId> }
213 // Make sure a const item doesn't recursively refer to itself
214 // FIXME: Should use the dependency graph when it's available (#1356)
215 pub fn check_item_recursion<'a>(sess: Session,
216 ast_map: &'a ast_map::Map,
217 def_map: resolve::DefMap,
220 let mut visitor = CheckItemRecursionVisitor {
227 visitor.visit_item(it, ());
230 impl<'a> Visitor<()> for CheckItemRecursionVisitor<'a> {
231 fn visit_item(&mut self, it: &Item, _: ()) {
232 if self.idstack.iter().any(|x| x == &(it.id)) {
233 self.sess.span_fatal(self.root_it.span, "recursive constant");
235 self.idstack.push(it.id);
236 visit::walk_item(self, it, ());
240 fn visit_expr(&mut self, e: &Expr, _: ()) {
243 let def_map = self.def_map.borrow();
244 match def_map.get().find(&e.id) {
245 Some(&DefStatic(def_id, _)) if
246 ast_util::is_local(def_id) => {
247 self.visit_item(self.ast_map.expect_item(def_id.node), ());
254 visit::walk_expr(self, e, ());