Skip to content

Commit da78b4d

Browse files
committed
Review comments
- exhaustive match - rename method to `check_expr_meets_expectation_or_error` - formatting - add `delay_span_bug` - add test
1 parent 028b5f9 commit da78b4d

File tree

5 files changed

+38
-21
lines changed

5 files changed

+38
-21
lines changed

src/librustc_typeck/check/_match.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
413413
// discriminant. This is sort of a workaround, see note (*) in
414414
// `check_pat` for some details.
415415
discrim_ty = self.next_ty_var(TypeVariableOrigin::TypeInference(discrim.span));
416-
self.check_expr_has_type(discrim, discrim_ty);
416+
self.check_expr_has_type_or_error(discrim, discrim_ty);
417417
};
418418

419419
// If the discriminant diverges, the match is pointless (e.g.,
@@ -480,7 +480,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
480480
for (i, (arm, pats_diverge)) in arms.iter().zip(all_arm_pats_diverge).enumerate() {
481481
if let Some(ref e) = arm.guard {
482482
self.diverges.set(pats_diverge);
483-
self.check_expr_has_type(e, tcx.types.bool);
483+
self.check_expr_has_type_or_error(e, tcx.types.bool);
484484
}
485485

486486
self.diverges.set(pats_diverge);

src/librustc_typeck/check/demand.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
2929
self.demand_suptype_diag(sp, expected, actual).map(|mut e| e.emit());
3030
}
3131

32-
pub fn demand_suptype_diag(&self, sp: Span,
32+
pub fn demand_suptype_diag(&self,
33+
sp: Span,
3334
expected: Ty<'tcx>,
3435
actual: Ty<'tcx>) -> Option<DiagnosticBuilder<'tcx>> {
3536
let cause = &self.misc(sp);

src/librustc_typeck/check/mod.rs

+17-14
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ impl<'a, 'gcx, 'tcx> Expectation<'tcx> {
345345
match self.resolve(fcx) {
346346
ExpectHasType(ty) => Some(ty),
347347
ExpectIfCondition => Some(fcx.tcx.types.bool),
348-
_ => None
348+
NoExpectation | ExpectCastableToType(_) | ExpectRvalueLikeUnsized(_) => None,
349349
}
350350
}
351351

@@ -2647,15 +2647,15 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
26472647
self.demand_eqtype(expr.span, expected, ty);
26482648
}
26492649

2650-
pub fn check_expr_has_type(&self,
2651-
expr: &'gcx hir::Expr,
2652-
expected: Ty<'tcx>) -> Ty<'tcx> {
2653-
self.check_expr_expect_type(expr, ExpectHasType(expected))
2650+
pub fn check_expr_has_type_or_error(&self,
2651+
expr: &'gcx hir::Expr,
2652+
expected: Ty<'tcx>) -> Ty<'tcx> {
2653+
self.check_expr_meets_expectation_or_error(expr, ExpectHasType(expected))
26542654
}
26552655

2656-
fn check_expr_expect_type(&self,
2657-
expr: &'gcx hir::Expr,
2658-
expected: Expectation<'tcx>) -> Ty<'tcx> {
2656+
fn check_expr_meets_expectation_or_error(&self,
2657+
expr: &'gcx hir::Expr,
2658+
expected: Expectation<'tcx>) -> Ty<'tcx> {
26592659
let expected_ty = expected.to_option(&self).unwrap_or(self.tcx.types.bool);
26602660
let mut ty = self.check_expr_with_expectation(expr, expected);
26612661

@@ -2865,7 +2865,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
28652865
opt_else_expr: Option<&'gcx hir::Expr>,
28662866
sp: Span,
28672867
expected: Expectation<'tcx>) -> Ty<'tcx> {
2868-
let cond_ty = self.check_expr_expect_type(cond_expr, ExpectIfCondition);
2868+
let cond_ty = self.check_expr_meets_expectation_or_error(cond_expr, ExpectIfCondition);
28692869
let cond_diverges = self.diverges.get();
28702870
self.diverges.set(Diverges::Maybe);
28712871

@@ -3352,7 +3352,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
33523352
self.check_expr_struct_fields(struct_ty, expected, expr.id, path_span, variant, fields,
33533353
base_expr.is_none());
33543354
if let &Some(ref base_expr) = base_expr {
3355-
self.check_expr_has_type(base_expr, struct_ty);
3355+
self.check_expr_has_type_or_error(base_expr, struct_ty);
33563356
match struct_ty.sty {
33573357
ty::TyAdt(adt, substs) if adt.is_struct() => {
33583358
let fru_field_types = adt.struct_variant().fields.iter().map(|f| {
@@ -3668,7 +3668,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
36683668
let rhs_ty = self.check_expr_coercable_to_type(&rhs, lhs_ty);
36693669

36703670
match expected {
3671-
ExpectIfCondition => (),
3671+
ExpectIfCondition => {
3672+
self.tcx.sess.delay_span_bug(lhs.span, "invalid lhs expression in if;\
3673+
expected error elsehwere");
3674+
}
36723675
_ => {
36733676
// Only check this if not in an `if` condition, as the
36743677
// mistyped comparison help is more appropriate.
@@ -3704,7 +3707,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
37043707
};
37053708

37063709
self.with_breakable_ctxt(expr.id, ctxt, || {
3707-
self.check_expr_has_type(&cond, tcx.types.bool);
3710+
self.check_expr_has_type_or_error(&cond, tcx.types.bool);
37083711
let cond_diverging = self.diverges.get();
37093712
self.check_block_no_value(&body);
37103713

@@ -3842,7 +3845,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
38423845
}
38433846
None => {
38443847
let t: Ty = self.next_ty_var(TypeVariableOrigin::MiscVariable(element.span));
3845-
let element_ty = self.check_expr_has_type(&element, t);
3848+
let element_ty = self.check_expr_has_type_or_error(&element, t);
38463849
(element_ty, t)
38473850
}
38483851
};
@@ -4097,7 +4100,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
40974100
}
40984101
hir::StmtExpr(ref expr, _) => {
40994102
// Check with expected type of ()
4100-
self.check_expr_has_type(&expr, self.tcx.mk_nil());
4103+
self.check_expr_has_type_or_error(&expr, self.tcx.mk_nil());
41014104
}
41024105
hir::StmtSemi(ref expr, _) => {
41034106
self.check_expr(&expr);

src/test/ui/type-check/issue-17283.rs renamed to src/test/ui/type-check/assignment-in-if.rs

+4
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,8 @@ fn main() {
4545
//~| HELP did you mean to compare equality?
4646
println!("{}", x);
4747
}
48+
if (if true { x = 4 } else { x = 5 }) {
49+
//~^ ERROR mismatched types
50+
println!("{}", x);
51+
}
4852
}

src/test/ui/type-check/issue-17283.stderr renamed to src/test/ui/type-check/assignment-in-if.stderr

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0308]: mismatched types
2-
--> $DIR/issue-17283.rs:25:8
2+
--> $DIR/assignment-in-if.rs:25:8
33
|
44
25 | if x = x {
55
| ^^^^^
@@ -11,7 +11,7 @@ error[E0308]: mismatched types
1111
found type `()`
1212

1313
error[E0308]: mismatched types
14-
--> $DIR/issue-17283.rs:31:8
14+
--> $DIR/assignment-in-if.rs:31:8
1515
|
1616
31 | if (x = x) {
1717
| ^^^^^^^
@@ -23,7 +23,7 @@ error[E0308]: mismatched types
2323
found type `()`
2424

2525
error[E0308]: mismatched types
26-
--> $DIR/issue-17283.rs:37:8
26+
--> $DIR/assignment-in-if.rs:37:8
2727
|
2828
37 | if y = (Foo { foo: x }) {
2929
| ^^^^^^^^^^^^^^^^^^^^
@@ -35,7 +35,7 @@ error[E0308]: mismatched types
3535
found type `()`
3636

3737
error[E0308]: mismatched types
38-
--> $DIR/issue-17283.rs:43:8
38+
--> $DIR/assignment-in-if.rs:43:8
3939
|
4040
43 | if 3 = x {
4141
| ^^^^^
@@ -46,5 +46,14 @@ error[E0308]: mismatched types
4646
= note: expected type `bool`
4747
found type `()`
4848

49+
error[E0308]: mismatched types
50+
--> $DIR/assignment-in-if.rs:48:8
51+
|
52+
48 | if (if true { x = 4 } else { x = 5 }) {
53+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected bool, found ()
54+
|
55+
= note: expected type `bool`
56+
found type `()`
57+
4958
error: aborting due to previous error(s)
5059

0 commit comments

Comments
 (0)