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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
use check::FnCtxt;
use rustc::hir;
use rustc::hir::def_id::{DefId, DefIndex};
use rustc::hir::intravisit::{self, NestedVisitorMap, Visitor};
use rustc::infer::InferCtxt;
use rustc::ty::{self, Ty, TyCtxt};
use rustc::ty::adjustment::{Adjust, Adjustment};
use rustc::ty::fold::{TypeFoldable, TypeFolder};
use rustc::util::nodemap::DefIdSet;
use syntax::ast;
use syntax_pos::Span;
use std::mem;
use rustc_data_structures::sync::Lrc;
impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
pub fn resolve_type_vars_in_body(&self, body: &'gcx hir::Body) -> &'gcx ty::TypeckTables<'gcx> {
let item_id = self.tcx.hir.body_owner(body.id());
let item_def_id = self.tcx.hir.local_def_id(item_id);
let mut wbcx = WritebackCx::new(self, body);
for arg in &body.arguments {
wbcx.visit_node_id(arg.pat.span, arg.hir_id);
}
wbcx.visit_body(body);
wbcx.visit_upvar_borrow_map();
wbcx.visit_closures();
wbcx.visit_liberated_fn_sigs();
wbcx.visit_fru_field_types();
wbcx.visit_anon_types();
wbcx.visit_cast_types();
wbcx.visit_free_region_map();
wbcx.visit_user_provided_tys();
let used_trait_imports = mem::replace(
&mut self.tables.borrow_mut().used_trait_imports,
Lrc::new(DefIdSet()),
);
debug!(
"used_trait_imports({:?}) = {:?}",
item_def_id,
used_trait_imports
);
wbcx.tables.used_trait_imports = used_trait_imports;
wbcx.tables.tainted_by_errors = self.is_tainted_by_errors();
debug!(
"writeback: tables for {:?} are {:#?}",
item_def_id,
wbcx.tables
);
self.tcx.alloc_tables(wbcx.tables)
}
}
struct WritebackCx<'cx, 'gcx: 'cx + 'tcx, 'tcx: 'cx> {
fcx: &'cx FnCtxt<'cx, 'gcx, 'tcx>,
tables: ty::TypeckTables<'gcx>,
body: &'gcx hir::Body,
}
impl<'cx, 'gcx, 'tcx> WritebackCx<'cx, 'gcx, 'tcx> {
fn new(
fcx: &'cx FnCtxt<'cx, 'gcx, 'tcx>,
body: &'gcx hir::Body,
) -> WritebackCx<'cx, 'gcx, 'tcx> {
let owner = fcx.tcx.hir.definitions().node_to_hir_id(body.id().node_id);
WritebackCx {
fcx,
tables: ty::TypeckTables::empty(Some(DefId::local(owner.owner))),
body,
}
}
fn tcx(&self) -> TyCtxt<'cx, 'gcx, 'tcx> {
self.fcx.tcx
}
fn write_ty_to_tables(&mut self, hir_id: hir::HirId, ty: Ty<'gcx>) {
debug!("write_ty_to_tables({:?}, {:?})", hir_id, ty);
assert!(!ty.needs_infer() && !ty.has_skol());
self.tables.node_types_mut().insert(hir_id, ty);
}
fn fix_scalar_builtin_expr(&mut self, e: &hir::Expr) {
match e.node {
hir::ExprUnary(hir::UnNeg, ref inner) | hir::ExprUnary(hir::UnNot, ref inner) => {
let inner_ty = self.fcx.node_ty(inner.hir_id);
let inner_ty = self.fcx.resolve_type_vars_if_possible(&inner_ty);
if inner_ty.is_scalar() {
let mut tables = self.fcx.tables.borrow_mut();
tables.type_dependent_defs_mut().remove(e.hir_id);
tables.node_substs_mut().remove(e.hir_id);
}
}
hir::ExprBinary(ref op, ref lhs, ref rhs)
| hir::ExprAssignOp(ref op, ref lhs, ref rhs) => {
let lhs_ty = self.fcx.node_ty(lhs.hir_id);
let lhs_ty = self.fcx.resolve_type_vars_if_possible(&lhs_ty);
let rhs_ty = self.fcx.node_ty(rhs.hir_id);
let rhs_ty = self.fcx.resolve_type_vars_if_possible(&rhs_ty);
if lhs_ty.is_scalar() && rhs_ty.is_scalar() {
let mut tables = self.fcx.tables.borrow_mut();
tables.type_dependent_defs_mut().remove(e.hir_id);
tables.node_substs_mut().remove(e.hir_id);
match e.node {
hir::ExprBinary(..) => {
if !op.node.is_by_value() {
let mut adjustments = tables.adjustments_mut();
adjustments.get_mut(lhs.hir_id).map(|a| a.pop());
adjustments.get_mut(rhs.hir_id).map(|a| a.pop());
}
}
hir::ExprAssignOp(..) => {
tables
.adjustments_mut()
.get_mut(lhs.hir_id)
.map(|a| a.pop());
}
_ => {}
}
}
}
_ => {}
}
}
fn fix_index_builtin_expr(&mut self, e: &hir::Expr) {
if let hir::ExprIndex(ref base, ref index) = e.node {
let mut tables = self.fcx.tables.borrow_mut();
match tables.expr_ty_adjusted(&base).sty {
ty::TyRef(_, ty::TypeAndMut { ty: ref base_ty, .. }) => {
let index_ty = tables.expr_ty_adjusted(&index);
let index_ty = self.fcx.resolve_type_vars_if_possible(&index_ty);
if base_ty.builtin_index().is_some()
&& index_ty == self.fcx.tcx.types.usize {
tables.type_dependent_defs_mut().remove(e.hir_id);
tables.node_substs_mut().remove(e.hir_id);
tables.adjustments_mut().get_mut(base.hir_id).map(|a| {
match a.pop() {
Some(Adjustment { kind: Adjust::Unsize, .. }) => {
a.pop();
},
_ => {}
}
});
}
},
_ => {},
}
}
}
}
impl<'cx, 'gcx, 'tcx> Visitor<'gcx> for WritebackCx<'cx, 'gcx, 'tcx> {
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'gcx> {
NestedVisitorMap::None
}
fn visit_expr(&mut self, e: &'gcx hir::Expr) {
self.fix_scalar_builtin_expr(e);
self.fix_index_builtin_expr(e);
self.visit_node_id(e.span, e.hir_id);
match e.node {
hir::ExprClosure(_, _, body, _, _) => {
let body = self.fcx.tcx.hir.body(body);
for arg in &body.arguments {
self.visit_node_id(e.span, arg.hir_id);
}
self.visit_body(body);
}
hir::ExprStruct(_, ref fields, _) => {
for field in fields {
self.visit_field_id(field.id);
}
}
hir::ExprField(..) => {
self.visit_field_id(e.id);
}
_ => {}
}
intravisit::walk_expr(self, e);
}
fn visit_block(&mut self, b: &'gcx hir::Block) {
self.visit_node_id(b.span, b.hir_id);
intravisit::walk_block(self, b);
}
fn visit_pat(&mut self, p: &'gcx hir::Pat) {
match p.node {
hir::PatKind::Binding(..) => {
let bm = *self.fcx
.tables
.borrow()
.pat_binding_modes()
.get(p.hir_id)
.expect("missing binding mode");
self.tables.pat_binding_modes_mut().insert(p.hir_id, bm);
}
hir::PatKind::Struct(_, ref fields, _) => {
for field in fields {
self.visit_field_id(field.node.id);
}
}
_ => {}
};
self.visit_pat_adjustments(p.span, p.hir_id);
self.visit_node_id(p.span, p.hir_id);
intravisit::walk_pat(self, p);
}
fn visit_local(&mut self, l: &'gcx hir::Local) {
intravisit::walk_local(self, l);
let var_ty = self.fcx.local_ty(l.span, l.id);
let var_ty = self.resolve(&var_ty, &l.span);
self.write_ty_to_tables(l.hir_id, var_ty);
}
fn visit_ty(&mut self, hir_ty: &'gcx hir::Ty) {
intravisit::walk_ty(self, hir_ty);
let ty = self.fcx.node_ty(hir_ty.hir_id);
let ty = self.resolve(&ty, &hir_ty.span);
self.write_ty_to_tables(hir_ty.hir_id, ty);
}
}
impl<'cx, 'gcx, 'tcx> WritebackCx<'cx, 'gcx, 'tcx> {
fn visit_upvar_borrow_map(&mut self) {
for (upvar_id, upvar_capture) in self.fcx.tables.borrow().upvar_capture_map.iter() {
let new_upvar_capture = match *upvar_capture {
ty::UpvarCapture::ByValue => ty::UpvarCapture::ByValue,
ty::UpvarCapture::ByRef(ref upvar_borrow) => {
let r = upvar_borrow.region;
let r = self.resolve(&r, &upvar_id.var_id);
ty::UpvarCapture::ByRef(ty::UpvarBorrow {
kind: upvar_borrow.kind,
region: r,
})
}
};
debug!(
"Upvar capture for {:?} resolved to {:?}",
upvar_id,
new_upvar_capture
);
self.tables
.upvar_capture_map
.insert(*upvar_id, new_upvar_capture);
}
}
fn visit_closures(&mut self) {
let fcx_tables = self.fcx.tables.borrow();
debug_assert_eq!(fcx_tables.local_id_root, self.tables.local_id_root);
let common_local_id_root = fcx_tables.local_id_root.unwrap();
for (&id, &origin) in fcx_tables.closure_kind_origins().iter() {
let hir_id = hir::HirId {
owner: common_local_id_root.index,
local_id: id,
};
self.tables
.closure_kind_origins_mut()
.insert(hir_id, origin);
}
}
fn visit_cast_types(&mut self) {
let fcx_tables = self.fcx.tables.borrow();
let fcx_cast_kinds = fcx_tables.cast_kinds();
debug_assert_eq!(fcx_tables.local_id_root, self.tables.local_id_root);
let mut self_cast_kinds = self.tables.cast_kinds_mut();
let common_local_id_root = fcx_tables.local_id_root.unwrap();
for (&local_id, &cast_kind) in fcx_cast_kinds.iter() {
let hir_id = hir::HirId {
owner: common_local_id_root.index,
local_id,
};
self_cast_kinds.insert(hir_id, cast_kind);
}
}
fn visit_free_region_map(&mut self) {
let free_region_map = self.tcx()
.lift_to_global(&self.fcx.tables.borrow().free_region_map);
let free_region_map = free_region_map.expect("all regions in free-region-map are global");
self.tables.free_region_map = free_region_map;
}
fn visit_user_provided_tys(&mut self) {
let fcx_tables = self.fcx.tables.borrow();
debug_assert_eq!(fcx_tables.local_id_root, self.tables.local_id_root);
let common_local_id_root = fcx_tables.local_id_root.unwrap();
for (&local_id, c_ty) in fcx_tables.user_provided_tys().iter() {
let hir_id = hir::HirId {
owner: common_local_id_root.index,
local_id,
};
let c_ty = if let Some(c_ty) = self.tcx().lift_to_global(c_ty) {
c_ty
} else {
span_bug!(
hir_id.to_span(&self.fcx.tcx),
"writeback: `{:?}` missing from the global type context",
c_ty
);
};
self.tables
.user_provided_tys_mut()
.insert(hir_id, c_ty.clone());
}
}
fn visit_anon_types(&mut self) {
let gcx = self.tcx().global_tcx();
for (&def_id, anon_defn) in self.fcx.anon_types.borrow().iter() {
let node_id = gcx.hir.as_local_node_id(def_id).unwrap();
let instantiated_ty = self.resolve(&anon_defn.concrete_ty, &node_id);
let definition_ty = self.fcx.infer_anon_definition_from_instantiation(
def_id,
anon_defn,
instantiated_ty,
);
let hir_id = self.tcx().hir.node_to_hir_id(node_id);
self.tables.node_types_mut().insert(hir_id, definition_ty);
}
}
fn visit_field_id(&mut self, node_id: ast::NodeId) {
let hir_id = self.tcx().hir.node_to_hir_id(node_id);
if let Some(index) = self.fcx.tables.borrow_mut().field_indices_mut().remove(hir_id) {
self.tables.field_indices_mut().insert(hir_id, index);
}
}
fn visit_node_id(&mut self, span: Span, hir_id: hir::HirId) {
if let Some(def) = self.fcx
.tables
.borrow_mut()
.type_dependent_defs_mut()
.remove(hir_id)
{
self.tables.type_dependent_defs_mut().insert(hir_id, def);
}
self.visit_adjustments(span, hir_id);
let n_ty = self.fcx.node_ty(hir_id);
let n_ty = self.resolve(&n_ty, &span);
self.write_ty_to_tables(hir_id, n_ty);
debug!("Node {:?} has type {:?}", hir_id, n_ty);
if let Some(substs) = self.fcx.tables.borrow().node_substs_opt(hir_id) {
let substs = self.resolve(&substs, &span);
debug!("write_substs_to_tcx({:?}, {:?})", hir_id, substs);
assert!(!substs.needs_infer() && !substs.has_skol());
self.tables.node_substs_mut().insert(hir_id, substs);
}
}
fn visit_adjustments(&mut self, span: Span, hir_id: hir::HirId) {
let adjustment = self.fcx
.tables
.borrow_mut()
.adjustments_mut()
.remove(hir_id);
match adjustment {
None => {
debug!("No adjustments for node {:?}", hir_id);
}
Some(adjustment) => {
let resolved_adjustment = self.resolve(&adjustment, &span);
debug!(
"Adjustments for node {:?}: {:?}",
hir_id,
resolved_adjustment
);
self.tables
.adjustments_mut()
.insert(hir_id, resolved_adjustment);
}
}
}
fn visit_pat_adjustments(&mut self, span: Span, hir_id: hir::HirId) {
let adjustment = self.fcx
.tables
.borrow_mut()
.pat_adjustments_mut()
.remove(hir_id);
match adjustment {
None => {
debug!("No pat_adjustments for node {:?}", hir_id);
}
Some(adjustment) => {
let resolved_adjustment = self.resolve(&adjustment, &span);
debug!(
"pat_adjustments for node {:?}: {:?}",
hir_id,
resolved_adjustment
);
self.tables
.pat_adjustments_mut()
.insert(hir_id, resolved_adjustment);
}
}
}
fn visit_liberated_fn_sigs(&mut self) {
let fcx_tables = self.fcx.tables.borrow();
debug_assert_eq!(fcx_tables.local_id_root, self.tables.local_id_root);
let common_local_id_root = fcx_tables.local_id_root.unwrap();
for (&local_id, fn_sig) in fcx_tables.liberated_fn_sigs().iter() {
let hir_id = hir::HirId {
owner: common_local_id_root.index,
local_id,
};
let fn_sig = self.resolve(fn_sig, &hir_id);
self.tables
.liberated_fn_sigs_mut()
.insert(hir_id, fn_sig.clone());
}
}
fn visit_fru_field_types(&mut self) {
let fcx_tables = self.fcx.tables.borrow();
debug_assert_eq!(fcx_tables.local_id_root, self.tables.local_id_root);
let common_local_id_root = fcx_tables.local_id_root.unwrap();
for (&local_id, ftys) in fcx_tables.fru_field_types().iter() {
let hir_id = hir::HirId {
owner: common_local_id_root.index,
local_id,
};
let ftys = self.resolve(ftys, &hir_id);
self.tables.fru_field_types_mut().insert(hir_id, ftys);
}
}
fn resolve<T>(&self, x: &T, span: &Locatable) -> T::Lifted
where
T: TypeFoldable<'tcx> + ty::Lift<'gcx>,
{
let x = x.fold_with(&mut Resolver::new(self.fcx, span, self.body));
if let Some(lifted) = self.tcx().lift_to_global(&x) {
lifted
} else {
span_bug!(
span.to_span(&self.fcx.tcx),
"writeback: `{:?}` missing from the global type context",
x
);
}
}
}
trait Locatable {
fn to_span(&self, tcx: &TyCtxt) -> Span;
}
impl Locatable for Span {
fn to_span(&self, _: &TyCtxt) -> Span {
*self
}
}
impl Locatable for ast::NodeId {
fn to_span(&self, tcx: &TyCtxt) -> Span {
tcx.hir.span(*self)
}
}
impl Locatable for DefIndex {
fn to_span(&self, tcx: &TyCtxt) -> Span {
let node_id = tcx.hir.def_index_to_node_id(*self);
tcx.hir.span(node_id)
}
}
impl Locatable for hir::HirId {
fn to_span(&self, tcx: &TyCtxt) -> Span {
let node_id = tcx.hir.definitions().find_node_for_hir_id(*self);
tcx.hir.span(node_id)
}
}
struct Resolver<'cx, 'gcx: 'cx + 'tcx, 'tcx: 'cx> {
tcx: TyCtxt<'cx, 'gcx, 'tcx>,
infcx: &'cx InferCtxt<'cx, 'gcx, 'tcx>,
span: &'cx Locatable,
body: &'gcx hir::Body,
}
impl<'cx, 'gcx, 'tcx> Resolver<'cx, 'gcx, 'tcx> {
fn new(
fcx: &'cx FnCtxt<'cx, 'gcx, 'tcx>,
span: &'cx Locatable,
body: &'gcx hir::Body,
) -> Resolver<'cx, 'gcx, 'tcx> {
Resolver {
tcx: fcx.tcx,
infcx: fcx,
span,
body,
}
}
fn report_error(&self, t: Ty<'tcx>) {
if !self.tcx.sess.has_errors() {
self.infcx
.need_type_info(Some(self.body.id()), self.span.to_span(&self.tcx), t);
}
}
}
impl<'cx, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for Resolver<'cx, 'gcx, 'tcx> {
fn tcx<'a>(&'a self) -> TyCtxt<'a, 'gcx, 'tcx> {
self.tcx
}
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
match self.infcx.fully_resolve(&t) {
Ok(t) => t,
Err(_) => {
debug!(
"Resolver::fold_ty: input type `{:?}` not fully resolvable",
t
);
self.report_error(t);
self.tcx().types.err
}
}
}
fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
match self.infcx.fully_resolve(&r) {
Ok(r) => r,
Err(_) => self.tcx.types.re_static,
}
}
}