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.
18 use syntax::visit::Visitor;
21 struct CheckCrateVisitor<'a, 'tcx: 'a> {
22 tcx: &'a ty::ctxt<'tcx>,
26 impl<'a, 'tcx> CheckCrateVisitor<'a, 'tcx> {
27 fn with_const<F>(&mut self, in_const: bool, f: F) where
28 F: FnOnce(&mut CheckCrateVisitor<'a, 'tcx>),
30 let was_const = self.in_const;
31 self.in_const = in_const;
33 self.in_const = was_const;
35 fn inside_const<F>(&mut self, f: F) where
36 F: FnOnce(&mut CheckCrateVisitor<'a, 'tcx>),
38 self.with_const(true, f);
40 fn outside_const<F>(&mut self, f: F) where
41 F: FnOnce(&mut CheckCrateVisitor<'a, 'tcx>),
43 self.with_const(false, f);
47 impl<'a, 'tcx, 'v> Visitor<'v> for CheckCrateVisitor<'a, 'tcx> {
48 fn visit_item(&mut self, i: &ast::Item) {
51 fn visit_pat(&mut self, p: &ast::Pat) {
54 fn visit_expr(&mut self, ex: &ast::Expr) {
55 if check_expr(self, ex) {
56 visit::walk_expr(self, ex);
61 pub fn check_crate(tcx: &ty::ctxt) {
62 visit::walk_crate(&mut CheckCrateVisitor { tcx: tcx, in_const: false },
64 tcx.sess.abort_if_errors();
67 fn check_item(v: &mut CheckCrateVisitor, it: &ast::Item) {
69 ast::ItemStatic(_, _, ref ex) |
70 ast::ItemConst(_, ref ex) => {
71 v.inside_const(|v| v.visit_expr(&**ex));
73 ast::ItemEnum(ref enum_definition, _) => {
74 for var in (*enum_definition).variants.iter() {
75 for ex in var.node.disr_expr.iter() {
76 v.inside_const(|v| v.visit_expr(&**ex));
80 _ => v.outside_const(|v| visit::walk_item(v, it))
84 fn check_pat(v: &mut CheckCrateVisitor, p: &ast::Pat) {
85 fn is_str(e: &ast::Expr) -> bool {
87 ast::ExprBox(_, ref expr) => {
89 ast::ExprLit(ref lit) => ast_util::lit_is_str(&**lit),
97 // Let through plain ~-string literals here
98 ast::PatLit(ref a) => if !is_str(&**a) { v.inside_const(|v| v.visit_expr(&**a)); },
99 ast::PatRange(ref a, ref b) => {
100 if !is_str(&**a) { v.inside_const(|v| v.visit_expr(&**a)); }
101 if !is_str(&**b) { v.inside_const(|v| v.visit_expr(&**b)); }
103 _ => v.outside_const(|v| visit::walk_pat(v, p))
107 fn check_expr(v: &mut CheckCrateVisitor, e: &ast::Expr) -> bool {
108 if !v.in_const { return true }
111 ast::ExprUnary(ast::UnDeref, _) => {}
112 ast::ExprUnary(ast::UnUniq, _) => {
113 span_err!(v.tcx.sess, e.span, E0010,
114 "cannot do allocations in constant expressions");
117 ast::ExprLit(ref lit) if ast_util::lit_is_str(&**lit) => {}
118 ast::ExprBinary(..) | ast::ExprUnary(..) => {
119 let method_call = ty::MethodCall::expr(e.id);
120 if v.tcx.method_map.borrow().contains_key(&method_call) {
121 span_err!(v.tcx.sess, e.span, E0011,
122 "user-defined operators are not allowed in constant \
126 ast::ExprLit(_) => (),
127 ast::ExprCast(ref from, _) => {
128 let toty = ty::expr_ty(v.tcx, e);
129 let fromty = ty::expr_ty(v.tcx, &**from);
131 ty::type_is_numeric(toty) ||
132 ty::type_is_unsafe_ptr(toty) ||
133 (ty::type_is_bare_fn(toty) && ty::type_is_bare_fn_item(fromty));
135 span_err!(v.tcx.sess, e.span, E0012,
136 "can not cast to `{}` in a constant expression",
137 ppaux::ty_to_string(v.tcx, toty));
139 if ty::type_is_unsafe_ptr(fromty) && ty::type_is_numeric(toty) {
140 span_err!(v.tcx.sess, e.span, E0018,
141 "can not cast a pointer to an integer in a constant \
145 ast::ExprPath(ref pth) => {
146 // NB: In the future you might wish to relax this slightly
147 // to handle on-demand instantiation of functions via
148 // foo::<bar> in a const. Currently that is only done on
149 // a path in trans::callee that only works in block contexts.
150 if !pth.segments.iter().all(|segment| segment.parameters.is_empty()) {
151 span_err!(v.tcx.sess, e.span, E0013,
152 "paths in constants may only refer to items without \
155 match v.tcx.def_map.borrow().get(&e.id) {
156 Some(&DefStatic(..)) |
157 Some(&DefConst(..)) |
159 Some(&DefVariant(_, _, _)) |
160 Some(&DefStruct(_)) => { }
163 debug!("(checking const) found bad def: {}", def);
164 span_err!(v.tcx.sess, e.span, E0014,
165 "paths in constants may only refer to constants \
169 v.tcx.sess.span_bug(e.span, "unbound path in const?!");
173 ast::ExprCall(ref callee, _) => {
174 match v.tcx.def_map.borrow().get(&callee.id) {
175 Some(&DefStruct(..)) |
176 Some(&DefVariant(..)) => {} // OK.
179 span_err!(v.tcx.sess, e.span, E0015,
180 "function calls in constants are limited to \
181 struct and enum constructors");
185 ast::ExprBlock(ref block) => {
186 // Check all statements in the block
187 for stmt in block.stmts.iter() {
188 let block_span_err = |&: span|
189 span_err!(v.tcx.sess, span, E0016,
190 "blocks in constants are limited to items and \
193 ast::StmtDecl(ref span, _) => {
195 ast::DeclLocal(_) => block_span_err(span.span),
197 // Item statements are allowed
198 ast::DeclItem(_) => {}
201 ast::StmtExpr(ref expr, _) => block_span_err(expr.span),
202 ast::StmtSemi(ref semi, _) => block_span_err(semi.span),
203 ast::StmtMac(..) => {
204 v.tcx.sess.span_bug(e.span, "unexpanded statement \
210 Some(ref expr) => { check_expr(v, &**expr); }
215 ast::ExprAddrOf(ast::MutImmutable, _) |
218 ast::ExprTupField(..) |
221 ast::ExprRepeat(..) |
222 ast::ExprStruct(..) => {}
224 ast::ExprAddrOf(_, ref inner) => {
226 // Mutable slices are allowed.
227 ast::ExprVec(_) => {}
228 _ => span_err!(v.tcx.sess, e.span, E0017,
229 "references in constants may only refer \
230 to immutable values")
236 span_err!(v.tcx.sess, e.span, E0019,
237 "constant contains unimplemented expression type");