Skip to content

Commit adb19ff

Browse files
committed
Add AdjustNeverToAny in check_expr
1 parent 9cd8d7a commit adb19ff

File tree

1 file changed

+57
-73
lines changed
  • src/librustc_typeck/check

1 file changed

+57
-73
lines changed

src/librustc_typeck/check/mod.rs

Lines changed: 57 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,21 +1530,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
15301530
}
15311531

15321532
#[inline]
1533-
pub fn write_ty(&self, node_id: ast::NodeId, ty: Ty<'tcx>) -> Ty<'tcx> {
1533+
pub fn write_ty(&self, node_id: ast::NodeId, ty: Ty<'tcx>) {
15341534
debug!("write_ty({}, {:?}) in fcx {}",
15351535
node_id, ty, self.tag());
15361536
self.tables.borrow_mut().node_types.insert(node_id, ty);
1537-
1538-
// Add adjustments to !-expressions
1539-
if ty.is_never() {
1540-
if let Some(hir::map::NodeExpr(_)) = self.tcx.map.find(node_id) {
1541-
let adj_ty = self.next_diverging_ty_var();
1542-
let adj = adjustment::AdjustNeverToAny(adj_ty);
1543-
self.write_adjustment(node_id, adj);
1544-
return adj_ty;
1545-
}
1546-
}
1547-
ty
15481537
}
15491538

15501539
pub fn write_substs(&self, node_id: ast::NodeId, substs: ty::ItemSubsts<'tcx>) {
@@ -1717,16 +1706,16 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
17171706
ty_substituted
17181707
}
17191708

1720-
pub fn write_nil(&self, node_id: ast::NodeId) -> Ty<'tcx> {
1721-
self.write_ty(node_id, self.tcx.mk_nil())
1709+
pub fn write_nil(&self, node_id: ast::NodeId) {
1710+
self.write_ty(node_id, self.tcx.mk_nil());
17221711
}
17231712

1724-
pub fn write_never(&self, node_id: ast::NodeId) -> Ty<'tcx> {
1725-
self.write_ty(node_id, self.tcx.types.never)
1713+
pub fn write_never(&self, node_id: ast::NodeId) {
1714+
self.write_ty(node_id, self.tcx.types.never);
17261715
}
17271716

1728-
pub fn write_error(&self, node_id: ast::NodeId) -> Ty<'tcx> {
1729-
self.write_ty(node_id, self.tcx.types.err)
1717+
pub fn write_error(&self, node_id: ast::NodeId) {
1718+
self.write_ty(node_id, self.tcx.types.err);
17301719
}
17311720

17321721
pub fn require_type_meets(&self,
@@ -3314,20 +3303,17 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
33143303
}
33153304
});
33163305
let referent_ty = self.check_expr_with_expectation(subexpr, expected_inner);
3317-
self.write_ty(id, tcx.mk_box(referent_ty))
3306+
tcx.mk_box(referent_ty)
33183307
}
33193308

33203309
hir::ExprLit(ref lit) => {
3321-
let typ = self.check_lit(&lit, expected);
3322-
self.write_ty(id, typ)
3310+
self.check_lit(&lit, expected)
33233311
}
33243312
hir::ExprBinary(op, ref lhs, ref rhs) => {
3325-
let ty = self.check_binop(expr, op, lhs, rhs);
3326-
self.write_ty(id, ty)
3313+
self.check_binop(expr, op, lhs, rhs)
33273314
}
33283315
hir::ExprAssignOp(op, ref lhs, ref rhs) => {
3329-
let ty = self.check_binop_assign(expr, op, lhs, rhs);
3330-
self.write_ty(id, ty)
3316+
self.check_binop_assign(expr, op, lhs, rhs)
33313317
}
33323318
hir::ExprUnary(unop, ref oprnd) => {
33333319
let expected_inner = match unop {
@@ -3386,7 +3372,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
33863372
}
33873373
}
33883374
}
3389-
self.write_ty(id, oprnd_t)
3375+
oprnd_t
33903376
}
33913377
hir::ExprAddrOf(mutbl, ref oprnd) => {
33923378
let hint = expected.only_has_type(self).map_or(NoExpectation, |ty| {
@@ -3408,7 +3394,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
34083394
let ty = self.check_expr_with_expectation_and_lvalue_pref(&oprnd, hint, lvalue_pref);
34093395

34103396
let tm = ty::TypeAndMut { ty: ty, mutbl: mutbl };
3411-
let oprnd_t = if tm.ty.references_error() {
3397+
if tm.ty.references_error() {
34123398
tcx.types.err
34133399
} else {
34143400
// Note: at this point, we cannot say what the best lifetime
@@ -3426,8 +3412,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
34263412
// as long as it needs to live.
34273413
let region = self.next_region_var(infer::AddrOfRegion(expr.span));
34283414
tcx.mk_ref(region, tm)
3429-
};
3430-
self.write_ty(id, oprnd_t)
3415+
}
34313416
}
34323417
hir::ExprPath(ref opt_qself, ref path) => {
34333418
let opt_self_ty = opt_qself.as_ref().map(|qself| self.to_ty(&qself.ty));
@@ -3446,7 +3431,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
34463431
self.add_wf_bounds(&item_substs.substs, expr);
34473432
});
34483433

3449-
self.write_ty(id, ty)
3434+
ty
34503435
}
34513436
hir::ExprInlineAsm(_, ref outputs, ref inputs) => {
34523437
for output in outputs {
@@ -3455,10 +3440,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
34553440
for input in inputs {
34563441
self.check_expr(input);
34573442
}
3458-
self.write_nil(id)
3443+
tcx.mk_nil()
34593444
}
3460-
hir::ExprBreak(_) => { self.write_never(id) }
3461-
hir::ExprAgain(_) => { self.write_never(id) }
3445+
hir::ExprBreak(_) => { tcx.types.never }
3446+
hir::ExprAgain(_) => { tcx.types.never }
34623447
hir::ExprRet(ref expr_opt) => {
34633448
if let Some(ref e) = *expr_opt {
34643449
self.check_expr_coercable_to_type(&e, self.ret_ty);
@@ -3476,7 +3461,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
34763461
.emit();
34773462
}
34783463
}
3479-
self.write_never(id)
3464+
tcx.types.never
34803465
}
34813466
hir::ExprAssign(ref lhs, ref rhs) => {
34823467
let lhs_ty = self.check_expr_with_lvalue_pref(&lhs, PreferMutLvalue);
@@ -3497,55 +3482,49 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
34973482
self.require_type_is_sized(lhs_ty, lhs.span, traits::AssignmentLhsSized);
34983483

34993484
if lhs_ty.references_error() || rhs_ty.references_error() {
3500-
self.write_error(id)
3485+
tcx.types.err
35013486
} else {
3502-
self.write_nil(id)
3487+
tcx.mk_nil()
35033488
}
35043489
}
35053490
hir::ExprIf(ref cond, ref then_blk, ref opt_else_expr) => {
3506-
let if_ty = self.check_then_else(&cond, &then_blk, opt_else_expr.as_ref().map(|e| &**e),
3507-
expr.span, expected);
3508-
self.write_ty(id, if_ty)
3491+
self.check_then_else(&cond, &then_blk, opt_else_expr.as_ref().map(|e| &**e),
3492+
expr.span, expected)
35093493
}
35103494
hir::ExprWhile(ref cond, ref body, _) => {
35113495
let cond_ty = self.check_expr_has_type(&cond, tcx.types.bool);
35123496
self.check_block_no_value(&body);
35133497
let body_ty = self.node_ty(body.id);
35143498
if cond_ty.references_error() || body_ty.references_error() {
3515-
self.write_error(id)
3499+
tcx.types.err
35163500
}
35173501
else {
3518-
self.write_nil(id)
3502+
tcx.mk_nil()
35193503
}
35203504
}
35213505
hir::ExprLoop(ref body, _) => {
35223506
self.check_block_no_value(&body);
35233507
if !may_break(tcx, expr.id, &body) {
3524-
self.write_never(id)
3508+
tcx.types.never
35253509
} else {
3526-
self.write_nil(id)
3510+
tcx.mk_nil()
35273511
}
35283512
}
35293513
hir::ExprMatch(ref discrim, ref arms, match_src) => {
3530-
let result_ty = self.check_match(expr, &discrim, arms, expected, match_src);
3531-
self.write_ty(expr.id, result_ty)
3514+
self.check_match(expr, &discrim, arms, expected, match_src)
35323515
}
35333516
hir::ExprClosure(capture, ref decl, ref body, _) => {
3534-
let ty = self.check_expr_closure(expr, capture, &decl, &body, expected);
3535-
self.write_ty(id, ty)
3517+
self.check_expr_closure(expr, capture, &decl, &body, expected)
35363518
}
35373519
hir::ExprBlock(ref b) => {
35383520
self.check_block_with_expected(&b, expected);
3539-
let ty = self.node_ty(b.id);
3540-
self.write_ty(id, ty)
3521+
self.node_ty(b.id)
35413522
}
35423523
hir::ExprCall(ref callee, ref args) => {
3543-
let ret_ty = self.check_call(expr, &callee, &args[..], expected);
3544-
self.write_ty(id, ret_ty)
3524+
self.check_call(expr, &callee, &args[..], expected)
35453525
}
35463526
hir::ExprMethodCall(name, ref tps, ref args) => {
3547-
let ret_ty = self.check_method_call(expr, name, &args[..], &tps[..], expected, lvalue_pref);
3548-
self.write_ty(id, ret_ty)
3527+
self.check_method_call(expr, name, &args[..], &tps[..], expected, lvalue_pref)
35493528
}
35503529
hir::ExprCast(ref e, ref t) => {
35513530
if let hir::TyFixedLengthVec(_, ref count_expr) = t.node {
@@ -3561,25 +3540,25 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
35613540

35623541
// Eagerly check for some obvious errors.
35633542
if t_expr.references_error() || t_cast.references_error() {
3564-
self.write_error(id)
3543+
tcx.types.err
35653544
} else {
35663545
// Defer other checks until we're done type checking.
35673546
let mut deferred_cast_checks = self.deferred_cast_checks.borrow_mut();
35683547
match cast::CastCheck::new(self, e, t_expr, t_cast, t.span, expr.span) {
35693548
Ok(cast_check) => {
35703549
deferred_cast_checks.push(cast_check);
3571-
self.write_ty(id, t_cast)
3550+
t_cast
35723551
}
35733552
Err(ErrorReported) => {
3574-
self.write_error(id)
3553+
tcx.types.err
35753554
}
35763555
}
35773556
}
35783557
}
35793558
hir::ExprType(ref e, ref t) => {
35803559
let typ = self.to_ty(&t);
35813560
self.check_expr_eq_type(&e, typ);
3582-
self.write_ty(id, typ)
3561+
typ
35833562
}
35843563
hir::ExprVec(ref args) => {
35853564
let uty = expected.to_option(self).and_then(|uty| {
@@ -3611,7 +3590,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
36113590
}
36123591
}
36133592
}
3614-
self.write_ty(id, tcx.mk_array(unified, args.len()))
3593+
tcx.mk_array(unified, args.len())
36153594
}
36163595
hir::ExprRepeat(ref element, ref count_expr) => {
36173596
self.check_expr_has_type(&count_expr, tcx.types.usize);
@@ -3647,10 +3626,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
36473626
}
36483627

36493628
if element_ty.references_error() {
3650-
self.write_error(id)
3629+
tcx.types.err
36513630
} else {
3652-
let t = tcx.mk_array(t, count);
3653-
self.write_ty(id, t)
3631+
tcx.mk_array(t, count)
36543632
}
36553633
}
36563634
hir::ExprTup(ref elts) => {
@@ -3677,29 +3655,25 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
36773655
t
36783656
}).collect();
36793657
if err_field {
3680-
self.write_error(id)
3658+
tcx.types.err
36813659
} else {
3682-
let typ = tcx.mk_tup(elt_ts);
3683-
self.write_ty(id, typ)
3660+
tcx.mk_tup(elt_ts)
36843661
}
36853662
}
36863663
hir::ExprStruct(ref path, ref fields, ref base_expr) => {
3687-
let ty = self.check_expr_struct(expr, path, fields, base_expr);
3688-
self.write_ty(id, ty)
3664+
self.check_expr_struct(expr, path, fields, base_expr)
36893665
}
36903666
hir::ExprField(ref base, ref field) => {
3691-
let ty = self.check_field(expr, lvalue_pref, &base, field);
3692-
self.write_ty(id, ty)
3667+
self.check_field(expr, lvalue_pref, &base, field)
36933668
}
36943669
hir::ExprTupField(ref base, idx) => {
3695-
let ty = self.check_tup_field(expr, lvalue_pref, &base, idx);
3696-
self.write_ty(id, ty)
3670+
self.check_tup_field(expr, lvalue_pref, &base, idx)
36973671
}
36983672
hir::ExprIndex(ref base, ref idx) => {
36993673
let base_t = self.check_expr_with_lvalue_pref(&base, lvalue_pref);
37003674
let idx_t = self.check_expr(&idx);
37013675

3702-
let ty = if base_t.references_error() {
3676+
if base_t.references_error() {
37033677
base_t
37043678
} else if idx_t.references_error() {
37053679
idx_t
@@ -3747,16 +3721,26 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
37473721
self.tcx().types.err
37483722
}
37493723
}
3750-
};
3751-
self.write_ty(id, ty)
3724+
}
37523725
}
37533726
};
3727+
self.write_ty(id, ty);
37543728

37553729
debug!("type of expr({}) {} is...", expr.id,
37563730
pprust::expr_to_string(expr));
37573731
debug!("... {:?}, expected is {:?}",
37583732
ty,
37593733
expected);
3734+
3735+
// Add adjustments to !-expressions
3736+
if ty.is_never() {
3737+
if let Some(hir::map::NodeExpr(_)) = self.tcx.map.find(id) {
3738+
let adj_ty = self.next_diverging_ty_var();
3739+
let adj = adjustment::AdjustNeverToAny(adj_ty);
3740+
self.write_adjustment(id, adj);
3741+
return adj_ty;
3742+
}
3743+
}
37603744
ty
37613745
}
37623746

0 commit comments

Comments
 (0)