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
#![cfg_attr(stage0, feature(custom_attribute))]
#![crate_name = "rustc_typeck"]
#![unstable(feature = "rustc_private")]
#![staged_api]
#![crate_type = "dylib"]
#![crate_type = "rlib"]
#![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
html_favicon_url = "http://www.rust-lang.org/favicon.ico",
html_root_url = "http://doc.rust-lang.org/nightly/")]
#![allow(non_camel_case_types)]
#![feature(box_patterns)]
#![feature(box_syntax)]
#![feature(collections)]
#![feature(core)]
#![feature(quote)]
#![feature(rustc_diagnostic_macros)]
#![feature(rustc_private)]
#![feature(unsafe_destructor)]
#![feature(staged_api)]
#[macro_use] extern crate log;
#[macro_use] extern crate syntax;
extern crate arena;
extern crate fmt_macros;
extern crate rustc;
pub use rustc::lint;
pub use rustc::metadata;
pub use rustc::middle;
pub use rustc::session;
pub use rustc::util;
use middle::def;
use middle::infer;
use middle::subst;
use middle::ty::{self, Ty};
use session::config;
use util::common::time;
use util::ppaux::Repr;
use util::ppaux;
use syntax::codemap::Span;
use syntax::print::pprust::*;
use syntax::{ast, ast_map, abi};
use syntax::ast_util::local_def;
use std::cell::RefCell;
pub mod diagnostics;
mod check;
mod rscope;
mod astconv;
mod collect;
mod constrained_type_params;
mod coherence;
mod variance;
pub struct TypeAndSubsts<'tcx> {
pub substs: subst::Substs<'tcx>,
pub ty: Ty<'tcx>,
}
pub struct CrateCtxt<'a, 'tcx: 'a> {
trait_map: ty::TraitMap,
all_traits: RefCell<Option<check::method::AllTraitsVec>>,
tcx: &'a ty::ctxt<'tcx>,
}
fn write_ty_to_tcx<'tcx>(tcx: &ty::ctxt<'tcx>, node_id: ast::NodeId, ty: Ty<'tcx>) {
debug!("write_ty_to_tcx({}, {})", node_id, ppaux::ty_to_string(tcx, ty));
assert!(!ty::type_needs_infer(ty));
tcx.node_type_insert(node_id, ty);
}
fn write_substs_to_tcx<'tcx>(tcx: &ty::ctxt<'tcx>,
node_id: ast::NodeId,
item_substs: ty::ItemSubsts<'tcx>) {
if !item_substs.is_noop() {
debug!("write_substs_to_tcx({}, {})",
node_id,
item_substs.repr(tcx));
assert!(item_substs.substs.types.all(|t| !ty::type_needs_infer(*t)));
tcx.item_substs.borrow_mut().insert(node_id, item_substs);
}
}
fn lookup_full_def(tcx: &ty::ctxt, sp: Span, id: ast::NodeId) -> def::Def {
match tcx.def_map.borrow().get(&id) {
Some(x) => x.full_def(),
None => {
span_fatal!(tcx.sess, sp, E0242, "internal error looking up a definition")
}
}
}
fn require_same_types<'a, 'tcx, M>(tcx: &ty::ctxt<'tcx>,
maybe_infcx: Option<&infer::InferCtxt<'a, 'tcx>>,
t1_is_expected: bool,
span: Span,
t1: Ty<'tcx>,
t2: Ty<'tcx>,
msg: M)
-> bool where
M: FnOnce() -> String,
{
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) => {
span_err!(tcx.sess, span, E0211,
"{}: {}",
msg(),
ty::type_err_to_str(tcx,
terr));
ty::note_and_explain_type_err(tcx, terr, span);
false
}
}
}
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 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() => {
span_err!(ccx.tcx.sess, main_span, E0131,
"main function is not allowed to have type parameters");
return;
}
_ => ()
}
}
_ => ()
}
let se_ty = ty::mk_bare_fn(tcx, Some(local_def(main_id)), tcx.mk_bare_fn(ty::BareFnTy {
unsafety: ast::Unsafety::Normal,
abi: abi::Rust,
sig: ty::Binder(ty::FnSig {
inputs: Vec::new(),
output: ty::FnConverging(ty::mk_nil(tcx)),
variadic: false
})
}));
require_same_types(tcx, None, false, main_span, main_t, se_ty,
|| {
format!("main function expects type: `{}`",
ppaux::ty_to_string(ccx.tcx, se_ty))
});
}
_ => {
tcx.sess.span_bug(main_span,
&format!("main has a non-function type: found \
`{}`",
ppaux::ty_to_string(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 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() => {
span_err!(tcx.sess, start_span, E0132,
"start function is not allowed to have type parameters");
return;
}
_ => ()
}
}
_ => ()
}
let se_ty = ty::mk_bare_fn(tcx, Some(local_def(start_id)), tcx.mk_bare_fn(ty::BareFnTy {
unsafety: ast::Unsafety::Normal,
abi: abi::Rust,
sig: ty::Binder(ty::FnSig {
inputs: vec!(
tcx.types.isize,
ty::mk_imm_ptr(tcx, ty::mk_imm_ptr(tcx, tcx.types.u8))
),
output: ty::FnConverging(tcx.types.isize),
variadic: false,
}),
}));
require_same_types(tcx, None, false, start_span, start_t, se_ty,
|| {
format!("start function expects type: `{}`",
ppaux::ty_to_string(ccx.tcx, se_ty))
});
}
_ => {
tcx.sess.span_bug(start_span,
&format!("start has a non-function type: found \
`{}`",
ppaux::ty_to_string(tcx, start_t)));
}
}
}
fn check_for_entry_fn(ccx: &CrateCtxt) {
let tcx = ccx.tcx;
match *tcx.sess.entry_fn.borrow() {
Some((id, sp)) => match tcx.sess.entry_type.get() {
Some(config::EntryMain) => check_main_fn_ty(ccx, id, sp),
Some(config::EntryStart) => check_start_fn_ty(ccx, id, sp),
Some(config::EntryNone) => {}
None => tcx.sess.bug("entry function without a type")
},
None => {}
}
}
pub fn check_crate(tcx: &ty::ctxt, trait_map: ty::TraitMap) {
let time_passes = tcx.sess.time_passes();
let ccx = CrateCtxt {
trait_map: trait_map,
all_traits: RefCell::new(None),
tcx: tcx
};
time(time_passes, "type collecting", (), |_|
collect::collect_item_types(tcx));
tcx.sess.abort_if_errors();
time(time_passes, "variance inference", (), |_|
variance::infer_variance(tcx));
time(time_passes, "coherence checking", (), |_|
coherence::check_coherence(&ccx));
time(time_passes, "type checking", (), |_|
check::check_item_types(&ccx));
check_for_entry_fn(&ccx);
tcx.sess.abort_if_errors();
}