1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
use syntax::ast::*;
use syntax::{visit, ast_util, ast_map};
use driver::session::session;
use std::map::hashmap;
use dvec::DVec;
fn check_crate(sess: session, crate: @crate, ast_map: ast_map::map,
def_map: resolve::DefMap,
method_map: typeck::method_map, tcx: ty::ctxt) {
visit::visit_crate(*crate, false, visit::mk_vt(@{
visit_item: |a,b,c| check_item(sess, ast_map, def_map, a, b, c),
visit_pat: check_pat,
visit_expr: |a,b,c|
check_expr(sess, def_map, method_map, tcx, a, b, c),
.. *visit::default_visitor()
}));
sess.abort_if_errors();
}
fn check_item(sess: session, ast_map: ast_map::map,
def_map: resolve::DefMap,
it: @item, &&_is_const: bool, v: visit::vt<bool>) {
match it.node {
item_const(_, ex) => {
v.visit_expr(ex, true, v);
check_item_recursion(sess, ast_map, def_map, it);
}
item_enum(enum_definition, _) => {
for enum_definition.variants.each |var| {
do option::iter(var.node.disr_expr) |ex| {
v.visit_expr(ex, true, v);
}
}
}
_ => visit::visit_item(it, false, v)
}
}
fn check_pat(p: @pat, &&_is_const: bool, v: visit::vt<bool>) {
fn is_str(e: @expr) -> bool {
match e.node {
expr_vstore(@{node: expr_lit(@{node: lit_str(_), _}), _},
vstore_uniq) => true,
_ => false
}
}
match p.node {
// Let through plain ~-string literals here
pat_lit(a) => if !is_str(a) { v.visit_expr(a, true, v); },
pat_range(a, b) => {
if !is_str(a) { v.visit_expr(a, true, v); }
if !is_str(b) { v.visit_expr(b, true, v); }
}
_ => visit::visit_pat(p, false, v)
}
}
fn check_expr(sess: session, def_map: resolve::DefMap,
method_map: typeck::method_map, tcx: ty::ctxt,
e: @expr, &&is_const: bool, v: visit::vt<bool>) {
if is_const {
match e.node {
expr_unary(box(_), _) | expr_unary(uniq(_), _) |
expr_unary(deref, _) => {
sess.span_err(e.span,
~"disallowed operator in constant expression");
return;
}
expr_lit(@{node: lit_str(_), _}) => { }
expr_binary(_, _, _) | expr_unary(_, _) => {
if method_map.contains_key(e.id) {
sess.span_err(e.span, ~"user-defined operators are not \
allowed in constant expressions");
}
}
expr_lit(_) => (),
expr_cast(_, _) => {
let ety = ty::expr_ty(tcx, e);
if !ty::type_is_numeric(ety) {
sess.span_err(e.span, ~"can not cast to `" +
util::ppaux::ty_to_str(tcx, ety) +
~"` in a constant expression");
}
}
expr_path(_) => {
match def_map.find(e.id) {
Some(def_const(def_id)) => {
if !ast_util::is_local(def_id) {
sess.span_err(
e.span, ~"paths in constants may only refer to \
crate-local constants");
}
}
_ => {
sess.span_err(
e.span,
~"paths in constants may only refer to constants");
}
}
}
expr_vstore(_, vstore_slice(_)) |
expr_vstore(_, vstore_fixed(_)) |
expr_vec(_, m_imm) |
expr_addr_of(m_imm, _) |
expr_field(*) |
expr_index(*) |
expr_tup(*) |
expr_struct(*) |
expr_rec(*) => { }
expr_addr_of(*) => {
sess.span_err(
e.span,
~"borrowed pointers in constants may only refer to \
immutable values");
}
_ => {
sess.span_err(e.span,
~"constant contains unimplemented expression type");
return;
}
}
}
match e.node {
expr_lit(@{node: lit_int(v, t), _}) => {
if t != ty_char {
if (v as u64) > ast_util::int_ty_max(
if t == ty_i { sess.targ_cfg.int_type } else { t }) {
sess.span_err(e.span, ~"literal out of range for its type");
}
}
}
expr_lit(@{node: lit_uint(v, t), _}) => {
if v > ast_util::uint_ty_max(
if t == ty_u { sess.targ_cfg.uint_type } else { t }) {
sess.span_err(e.span, ~"literal out of range for its type");
}
}
_ => ()
}
visit::visit_expr(e, is_const, v);
}
// Make sure a const item doesn't recursively refer to itself
// FIXME: Should use the dependency graph when it's available (#1356)
fn check_item_recursion(sess: session, ast_map: ast_map::map,
def_map: resolve::DefMap, it: @item) {
type env = {
root_it: @item,
sess: session,
ast_map: ast_map::map,
def_map: resolve::DefMap,
idstack: @DVec<node_id>,
};
let env = {
root_it: it,
sess: sess,
ast_map: ast_map,
def_map: def_map,
idstack: @DVec()
};
let visitor = visit::mk_vt(@{
visit_item: visit_item,
visit_expr: visit_expr,
.. *visit::default_visitor()
});
visitor.visit_item(it, env, visitor);
fn visit_item(it: @item, &&env: env, v: visit::vt<env>) {
if (*env.idstack).contains(it.id) {
env.sess.span_fatal(env.root_it.span, ~"recursive constant");
}
(*env.idstack).push(it.id);
visit::visit_item(it, env, v);
(*env.idstack).pop();
}
fn visit_expr(e: @expr, &&env: env, v: visit::vt<env>) {
match e.node {
expr_path(*) => {
match env.def_map.find(e.id) {
Some(def_const(def_id)) => {
match env.ast_map.get(def_id.node) {
ast_map::node_item(it, _) => {
v.visit_item(it, env, v);
}
_ => fail ~"const not bound to an item"
}
}
_ => ()
}
}
_ => ()
}
visit::visit_expr(e, env, v);
}
}
// Local Variables:
// mode: rust
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End:
|