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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
use hair::*;
use rustc_data_structures::indexed_vec::Idx;
use rustc::hir::def_id::{DefId, LOCAL_CRATE};
use rustc::hir::map::blocks::FnLikeNode;
use rustc::middle::region;
use rustc::infer::InferCtxt;
use rustc::ty::subst::Subst;
use rustc::ty::{self, Ty, TyCtxt};
use rustc::ty::subst::{Kind, Substs};
use syntax::ast::{self, LitKind};
use syntax::attr;
use syntax::symbol::Symbol;
use rustc::hir;
use rustc_data_structures::sync::Lrc;
use hair::pattern::parse_float;
#[derive(Clone)]
pub struct Cx<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
tcx: TyCtxt<'a, 'gcx, 'tcx>,
infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
pub root_lint_level: ast::NodeId,
pub param_env: ty::ParamEnv<'gcx>,
pub identity_substs: &'gcx Substs<'gcx>,
pub region_scope_tree: Lrc<region::ScopeTree>,
pub tables: &'a ty::TypeckTables<'gcx>,
constness: hir::Constness,
pub body_owner_kind: hir::BodyOwnerKind,
check_overflow: bool,
}
impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
pub fn new(infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
src_id: ast::NodeId) -> Cx<'a, 'gcx, 'tcx> {
let tcx = infcx.tcx;
let src_def_id = tcx.hir.local_def_id(src_id);
let body_owner_kind = tcx.hir.body_owner_kind(src_id);
let constness = match body_owner_kind {
hir::BodyOwnerKind::Const |
hir::BodyOwnerKind::Static(_) => hir::Constness::Const,
hir::BodyOwnerKind::Fn => {
let fn_like = FnLikeNode::from_node(infcx.tcx.hir.get(src_id));
fn_like.map_or(hir::Constness::NotConst, |f| f.constness())
}
};
let attrs = tcx.hir.attrs(src_id);
let mut check_overflow = attr::contains_name(attrs, "rustc_inherit_overflow_checks");
check_overflow |= tcx.sess.overflow_checks();
check_overflow |= constness == hir::Constness::Const;
let lint_level = lint_level_for_hir_id(tcx, src_id);
Cx {
tcx,
infcx,
root_lint_level: lint_level,
param_env: tcx.param_env(src_def_id),
identity_substs: Substs::identity_for_item(tcx.global_tcx(), src_def_id),
region_scope_tree: tcx.region_scope_tree(src_def_id),
tables: tcx.typeck_tables_of(src_def_id),
constness,
body_owner_kind,
check_overflow,
}
}
}
impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
pub fn mirror<M: Mirror<'tcx>>(&mut self, ast: M) -> M::Output {
ast.make_mirror(self)
}
pub fn usize_ty(&mut self) -> Ty<'tcx> {
self.tcx.types.usize
}
pub fn usize_literal(&mut self, value: u64) -> Literal<'tcx> {
Literal::Value {
value: ty::Const::from_usize(self.tcx, value),
}
}
pub fn bool_ty(&mut self) -> Ty<'tcx> {
self.tcx.types.bool
}
pub fn unit_ty(&mut self) -> Ty<'tcx> {
self.tcx.mk_nil()
}
pub fn true_literal(&mut self) -> Literal<'tcx> {
Literal::Value {
value: ty::Const::from_bool(self.tcx, true),
}
}
pub fn false_literal(&mut self) -> Literal<'tcx> {
Literal::Value {
value: ty::Const::from_bool(self.tcx, false),
}
}
pub fn const_eval_literal(
&mut self,
lit: &'tcx ast::LitKind,
ty: Ty<'tcx>,
sp: Span,
neg: bool,
) -> Literal<'tcx> {
trace!("const_eval_literal: {:#?}, {:?}, {:?}, {:?}", lit, ty, sp, neg);
let parse_float = |num, fty| -> ConstValue<'tcx> {
parse_float(num, fty, neg).unwrap_or_else(|_| {
self.tcx.sess.span_fatal(sp, "could not evaluate float literal (see issue #31407)");
})
};
let trunc = |n| {
let param_ty = self.param_env.and(self.tcx.lift_to_global(&ty).unwrap());
let bit_width = self.tcx.layout_of(param_ty).unwrap().size.bits();
trace!("trunc {} with size {} and shift {}", n, bit_width, 128 - bit_width);
let shift = 128 - bit_width;
let result = (n << shift) >> shift;
trace!("trunc result: {}", result);
ConstValue::Scalar(Scalar::Bits {
bits: result,
defined: bit_width as u8,
})
};
use rustc::mir::interpret::*;
let lit = match *lit {
LitKind::Str(ref s, _) => {
let s = s.as_str();
let id = self.tcx.allocate_bytes(s.as_bytes());
let value = Scalar::Ptr(id.into()).to_value_with_len(s.len() as u64, self.tcx);
ConstValue::from_byval_value(value)
},
LitKind::ByteStr(ref data) => {
let id = self.tcx.allocate_bytes(data);
ConstValue::Scalar(Scalar::Ptr(id.into()))
},
LitKind::Byte(n) => ConstValue::Scalar(Scalar::Bits {
bits: n as u128,
defined: 8,
}),
LitKind::Int(n, _) if neg => {
let n = n as i128;
let n = n.overflowing_neg().0;
trunc(n as u128)
},
LitKind::Int(n, _) => trunc(n),
LitKind::Float(n, fty) => {
parse_float(n, fty)
}
LitKind::FloatUnsuffixed(n) => {
let fty = match ty.sty {
ty::TyFloat(fty) => fty,
_ => bug!()
};
parse_float(n, fty)
}
LitKind::Bool(b) => ConstValue::Scalar(Scalar::Bits {
bits: b as u128,
defined: 8,
}),
LitKind::Char(c) => ConstValue::Scalar(Scalar::Bits {
bits: c as u128,
defined: 32,
}),
};
Literal::Value {
value: ty::Const::from_const_value(self.tcx, lit, ty)
}
}
pub fn pattern_from_hir(&mut self, p: &hir::Pat) -> Pattern<'tcx> {
let tcx = self.tcx.global_tcx();
let p = match tcx.hir.get(p.id) {
hir::map::NodePat(p) | hir::map::NodeBinding(p) => p,
node => bug!("pattern became {:?}", node)
};
Pattern::from_hir(tcx,
self.param_env.and(self.identity_substs),
self.tables(),
p)
}
pub fn trait_method(&mut self,
trait_def_id: DefId,
method_name: &str,
self_ty: Ty<'tcx>,
params: &[Kind<'tcx>])
-> (Ty<'tcx>, Literal<'tcx>) {
let method_name = Symbol::intern(method_name);
let substs = self.tcx.mk_substs_trait(self_ty, params);
for item in self.tcx.associated_items(trait_def_id) {
if item.kind == ty::AssociatedKind::Method && item.name == method_name {
let method_ty = self.tcx.type_of(item.def_id);
let method_ty = method_ty.subst(self.tcx, substs);
return (method_ty,
Literal::Value {
value: ty::Const::zero_sized(self.tcx, method_ty)
});
}
}
bug!("found no method `{}` in `{:?}`", method_name, trait_def_id);
}
pub fn all_fields(&mut self, adt_def: &ty::AdtDef, variant_index: usize) -> Vec<Field> {
(0..adt_def.variants[variant_index].fields.len())
.map(Field::new)
.collect()
}
pub fn needs_drop(&mut self, ty: Ty<'tcx>) -> bool {
let (ty, param_env) = self.tcx.lift_to_global(&(ty, self.param_env)).unwrap_or_else(|| {
bug!("MIR: Cx::needs_drop({:?}, {:?}) got \
type with inference types/regions",
ty, self.param_env);
});
ty.needs_drop(self.tcx.global_tcx(), param_env)
}
fn lint_level_of(&self, node_id: ast::NodeId) -> LintLevel {
let hir_id = self.tcx.hir.definitions().node_to_hir_id(node_id);
let has_lint_level = self.tcx.dep_graph.with_ignore(|| {
self.tcx.lint_levels(LOCAL_CRATE).lint_level_set(hir_id).is_some()
});
if has_lint_level {
LintLevel::Explicit(node_id)
} else {
LintLevel::Inherited
}
}
pub fn tcx(&self) -> TyCtxt<'a, 'gcx, 'tcx> {
self.tcx
}
pub fn tables(&self) -> &'a ty::TypeckTables<'gcx> {
self.tables
}
pub fn check_overflow(&self) -> bool {
self.check_overflow
}
pub fn type_moves_by_default(&self, ty: Ty<'tcx>, span: Span) -> bool {
self.infcx.type_moves_by_default(self.param_env, ty, span)
}
}
fn lint_level_for_hir_id(tcx: TyCtxt, mut id: ast::NodeId) -> ast::NodeId {
tcx.dep_graph.with_ignore(|| {
let sets = tcx.lint_levels(LOCAL_CRATE);
loop {
let hir_id = tcx.hir.definitions().node_to_hir_id(id);
if sets.lint_level_set(hir_id).is_some() {
return id
}
let next = tcx.hir.get_parent_node(id);
if next == id {
bug!("lint traversal reached the root of the crate");
}
id = next;
}
})
}
mod block;
mod expr;
mod to_ref;