-
Notifications
You must be signed in to change notification settings - Fork 12.1k
/
mod.rs
468 lines (395 loc) · 14.8 KB
/
mod.rs
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
/*
typeck.rs, an introduction
The type checker is responsible for:
1. Determining the type of each expression
2. Resolving methods and traits
3. Guaranteeing that most type rules are met ("most?", you say, "why most?"
Well, dear reader, read on)
The main entry point is `check_crate()`. Type checking operates in
several major phases:
1. The collect phase first passes over all items and determines their
type, without examining their "innards".
2. Variance inference then runs to compute the variance of each parameter
3. Coherence checks for overlapping or orphaned impls
4. Finally, the check phase then checks function bodies and so forth.
Within the check phase, we check each function body one at a time
(bodies of function expressions are checked as part of the
containing function). Inference is used to supply types wherever
they are unknown. The actual checking of a function itself has
several phases (check, regionck, writeback), as discussed in the
documentation for the `check` module.
The type checker is defined into various submodules which are documented
independently:
- astconv: converts the AST representation of types
into the `ty` representation
- collect: computes the types of each top-level item and enters them into
the `cx.tcache` table for later use
- coherence: enforces coherence rules, builds some tables
- variance: variance inference
- check: walks over function bodies and type checks them, inferring types for
local variables, type parameters, etc as necessary.
- infer: finds the types to use for each type variable such that
all subtyping and assignment constraints are met. In essence, the check
module specifies the constraints, and the infer module solves them.
*/
#[allow(non_camel_case_types)];
use driver::session;
use middle::resolve;
use middle::ty;
use util::common::time;
use util::ppaux::Repr;
use util::ppaux;
use std::cell::RefCell;
use collections::HashMap;
use std::rc::Rc;
use collections::List;
use syntax::codemap::Span;
use syntax::print::pprust::*;
use syntax::{ast, ast_map, abi};
pub mod check;
pub mod rscope;
pub mod astconv;
pub mod infer;
pub mod collect;
pub mod coherence;
pub mod variance;
#[deriving(Clone, Encodable, Decodable, Eq, Ord)]
pub enum param_index {
param_numbered(uint),
param_self
}
#[deriving(Clone, Encodable, Decodable)]
pub enum MethodOrigin {
// fully statically resolved method
MethodStatic(ast::DefId),
// method invoked on a type parameter with a bounded trait
MethodParam(MethodParam),
// method invoked on a trait instance
MethodObject(MethodObject),
}
// details for a method invoked with a receiver whose type is a type parameter
// with a bounded trait.
#[deriving(Clone, Encodable, Decodable)]
pub struct MethodParam {
// the trait containing the method to be invoked
trait_id: ast::DefId,
// index of the method to be invoked amongst the trait's methods
method_num: uint,
// index of the type parameter (from those that are in scope) that is
// the type of the receiver
param_num: param_index,
// index of the bound for this type parameter which specifies the trait
bound_num: uint,
}
// details for a method invoked with a receiver whose type is an object
#[deriving(Clone, Encodable, Decodable)]
pub struct MethodObject {
// the (super)trait containing the method to be invoked
trait_id: ast::DefId,
// the actual base trait id of the object
object_trait_id: ast::DefId,
// index of the method to be invoked amongst the trait's methods
method_num: uint,
// index into the actual runtime vtable.
// the vtable is formed by concatenating together the method lists of
// the base object trait and all supertraits; this is the index into
// that vtable
real_index: uint,
}
#[deriving(Clone)]
pub struct MethodCallee {
origin: MethodOrigin,
ty: ty::t,
substs: ty::substs
}
// maps from an expression id that corresponds to a method call to the details
// of the method to be invoked
pub type MethodMap = @RefCell<HashMap<ast::NodeId, MethodCallee>>;
pub type vtable_param_res = @~[vtable_origin];
// Resolutions for bounds of all parameters, left to right, for a given path.
pub type vtable_res = @~[vtable_param_res];
#[deriving(Clone)]
pub enum vtable_origin {
/*
Statically known vtable. def_id gives the class or impl item
from whence comes the vtable, and tys are the type substs.
vtable_res is the vtable itself
*/
vtable_static(ast::DefId, ~[ty::t], vtable_res),
/*
Dynamic vtable, comes from a parameter that has a bound on it:
fn foo<T:quux,baz,bar>(a: T) -- a's vtable would have a
vtable_param origin
The first argument is the param index (identifying T in the example),
and the second is the bound number (identifying baz)
*/
vtable_param(param_index, uint),
}
impl Repr for vtable_origin {
fn repr(&self, tcx: ty::ctxt) -> ~str {
match *self {
vtable_static(def_id, ref tys, ref vtable_res) => {
format!("vtable_static({:?}:{}, {}, {})",
def_id,
ty::item_path_str(tcx, def_id),
tys.repr(tcx),
vtable_res.repr(tcx))
}
vtable_param(x, y) => {
format!("vtable_param({:?}, {:?})", x, y)
}
}
}
}
pub type vtable_map = @RefCell<HashMap<ast::NodeId, vtable_res>>;
// Information about the vtable resolutions for for a trait impl.
// Mostly the information is important for implementing default
// methods.
#[deriving(Clone)]
pub struct impl_res {
// resolutions for any bounded params on the trait definition
trait_vtables: vtable_res,
// resolutions for the trait /itself/ (and for supertraits)
self_vtables: vtable_param_res
}
impl Repr for impl_res {
fn repr(&self, tcx: ty::ctxt) -> ~str {
format!("impl_res \\{trait_vtables={}, self_vtables={}\\}",
self.trait_vtables.repr(tcx),
self.self_vtables.repr(tcx))
}
}
pub type impl_vtable_map = RefCell<HashMap<ast::DefId, impl_res>>;
pub struct CrateCtxt {
// A mapping from method call sites to traits that have that method.
trait_map: resolve::TraitMap,
method_map: MethodMap,
vtable_map: vtable_map,
tcx: ty::ctxt
}
// Functions that write types into the node type table
pub fn write_ty_to_tcx(tcx: ty::ctxt, node_id: ast::NodeId, ty: ty::t) {
debug!("write_ty_to_tcx({}, {})", node_id, ppaux::ty_to_str(tcx, ty));
assert!(!ty::type_needs_infer(ty));
let mut node_types = tcx.node_types.borrow_mut();
node_types.get().insert(node_id as uint, ty);
}
pub fn write_substs_to_tcx(tcx: ty::ctxt,
node_id: ast::NodeId,
substs: ~[ty::t]) {
if substs.len() > 0u {
debug!("write_substs_to_tcx({}, {:?})", node_id,
substs.map(|t| ppaux::ty_to_str(tcx, *t)));
assert!(substs.iter().all(|t| !ty::type_needs_infer(*t)));
let mut node_type_substs = tcx.node_type_substs.borrow_mut();
node_type_substs.get().insert(node_id, substs);
}
}
pub fn write_tpt_to_tcx(tcx: ty::ctxt,
node_id: ast::NodeId,
tpt: &ty::ty_param_substs_and_ty) {
write_ty_to_tcx(tcx, node_id, tpt.ty);
if !tpt.substs.tps.is_empty() {
write_substs_to_tcx(tcx, node_id, tpt.substs.tps.clone());
}
}
pub fn lookup_def_tcx(tcx: ty::ctxt, sp: Span, id: ast::NodeId) -> ast::Def {
let def_map = tcx.def_map.borrow();
match def_map.get().find(&id) {
Some(&x) => x,
_ => {
tcx.sess.span_fatal(sp, "internal error looking up a definition")
}
}
}
pub fn lookup_def_ccx(ccx: &CrateCtxt, sp: Span, id: ast::NodeId)
-> ast::Def {
lookup_def_tcx(ccx.tcx, sp, id)
}
pub fn no_params(t: ty::t) -> ty::ty_param_bounds_and_ty {
ty::ty_param_bounds_and_ty {
generics: ty::Generics {type_param_defs: Rc::new(~[]),
region_param_defs: Rc::new(~[])},
ty: t
}
}
pub fn require_same_types(tcx: ty::ctxt,
maybe_infcx: Option<&infer::InferCtxt>,
t1_is_expected: bool,
span: Span,
t1: ty::t,
t2: ty::t,
msg: || -> ~str)
-> bool {
let result = match maybe_infcx {
None => {
let infcx = infer::new_infer_ctxt(tcx);
infer::mk_eqty(&infcx, t1_is_expected, infer::Misc(span), t1, t2)
}
Some(infcx) => {
infer::mk_eqty(infcx, t1_is_expected, infer::Misc(span), t1, t2)
}
};
match result {
Ok(_) => true,
Err(ref terr) => {
tcx.sess.span_err(span, msg() + ": " +
ty::type_err_to_str(tcx, terr));
ty::note_and_explain_type_err(tcx, terr);
false
}
}
}
// a list of mapping from in-scope-region-names ("isr") to the
// corresponding ty::Region
pub type isr_alist = @List<(ty::BoundRegion, ty::Region)>;
trait get_region<'a, T:'static> {
fn get(&'a self, br: ty::BoundRegion) -> ty::Region;
}
impl<'a, T:'static> get_region <'a, T> for isr_alist {
fn get(&'a self, br: ty::BoundRegion) -> ty::Region {
let mut region = None;
for isr in self.iter() {
let (isr_br, isr_r) = *isr;
if isr_br == br { region = Some(isr_r); break; }
};
region.unwrap()
}
}
fn check_main_fn_ty(ccx: &CrateCtxt,
main_id: ast::NodeId,
main_span: Span) {
let tcx = ccx.tcx;
let main_t = ty::node_id_to_type(tcx, main_id);
match ty::get(main_t).sty {
ty::ty_bare_fn(..) => {
match tcx.map.find(main_id) {
Some(ast_map::NodeItem(it)) => {
match it.node {
ast::ItemFn(_, _, _, ref ps, _)
if ps.is_parameterized() => {
tcx.sess.span_err(
main_span,
"main function is not allowed to have type parameters");
return;
}
_ => ()
}
}
_ => ()
}
let se_ty = ty::mk_bare_fn(tcx, ty::BareFnTy {
purity: ast::ImpureFn,
abis: abi::AbiSet::Rust(),
sig: ty::FnSig {
binder_id: main_id,
inputs: ~[],
output: ty::mk_nil(),
variadic: false
}
});
require_same_types(tcx, None, false, main_span, main_t, se_ty,
|| format!("main function expects type: `{}`",
ppaux::ty_to_str(ccx.tcx, se_ty)));
}
_ => {
tcx.sess.span_bug(main_span,
format!("main has a non-function type: found `{}`",
ppaux::ty_to_str(tcx, main_t)));
}
}
}
fn check_start_fn_ty(ccx: &CrateCtxt,
start_id: ast::NodeId,
start_span: Span) {
let tcx = ccx.tcx;
let start_t = ty::node_id_to_type(tcx, start_id);
match ty::get(start_t).sty {
ty::ty_bare_fn(_) => {
match tcx.map.find(start_id) {
Some(ast_map::NodeItem(it)) => {
match it.node {
ast::ItemFn(_,_,_,ref ps,_)
if ps.is_parameterized() => {
tcx.sess.span_err(
start_span,
"start function is not allowed to have type parameters");
return;
}
_ => ()
}
}
_ => ()
}
let se_ty = ty::mk_bare_fn(tcx, ty::BareFnTy {
purity: ast::ImpureFn,
abis: abi::AbiSet::Rust(),
sig: ty::FnSig {
binder_id: start_id,
inputs: ~[
ty::mk_int(),
ty::mk_imm_ptr(tcx, ty::mk_imm_ptr(tcx, ty::mk_u8()))
],
output: ty::mk_int(),
variadic: false
}
});
require_same_types(tcx, None, false, start_span, start_t, se_ty,
|| format!("start function expects type: `{}`", ppaux::ty_to_str(ccx.tcx, se_ty)));
}
_ => {
tcx.sess.span_bug(start_span,
format!("start has a non-function type: found `{}`",
ppaux::ty_to_str(tcx, start_t)));
}
}
}
fn check_for_entry_fn(ccx: &CrateCtxt) {
let tcx = ccx.tcx;
if !tcx.sess.building_library.get() {
match tcx.sess.entry_fn.get() {
Some((id, sp)) => match tcx.sess.entry_type.get() {
Some(session::EntryMain) => check_main_fn_ty(ccx, id, sp),
Some(session::EntryStart) => check_start_fn_ty(ccx, id, sp),
Some(session::EntryNone) => {}
None => tcx.sess.bug("entry function without a type")
},
None => {}
}
}
}
pub fn check_crate(tcx: ty::ctxt,
trait_map: resolve::TraitMap,
krate: &ast::Crate)
-> (MethodMap, vtable_map) {
let time_passes = tcx.sess.time_passes();
let ccx = @CrateCtxt {
trait_map: trait_map,
method_map: @RefCell::new(HashMap::new()),
vtable_map: @RefCell::new(HashMap::new()),
tcx: tcx
};
time(time_passes, "type collecting", (), |_|
collect::collect_item_types(ccx, krate));
// this ensures that later parts of type checking can assume that items
// have valid types and not error
tcx.sess.abort_if_errors();
time(time_passes, "variance inference", (), |_|
variance::infer_variance(tcx, krate));
time(time_passes, "coherence checking", (), |_|
coherence::check_coherence(ccx, krate));
time(time_passes, "type checking", (), |_|
check::check_item_types(ccx, krate));
check_for_entry_fn(ccx);
tcx.sess.abort_if_errors();
(ccx.method_map, ccx.vtable_map)
}