@@ -345,7 +345,7 @@ impl<'a, 'gcx, 'tcx> Expectation<'tcx> {
345
345
match self . resolve ( fcx) {
346
346
ExpectHasType ( ty) => Some ( ty) ,
347
347
ExpectIfCondition => Some ( fcx. tcx . types . bool ) ,
348
- _ => None
348
+ NoExpectation | ExpectCastableToType ( _ ) | ExpectRvalueLikeUnsized ( _ ) => None ,
349
349
}
350
350
}
351
351
@@ -2647,15 +2647,15 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
2647
2647
self . demand_eqtype ( expr. span , expected, ty) ;
2648
2648
}
2649
2649
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) )
2654
2654
}
2655
2655
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 > {
2659
2659
let expected_ty = expected. to_option ( & self ) . unwrap_or ( self . tcx . types . bool ) ;
2660
2660
let mut ty = self . check_expr_with_expectation ( expr, expected) ;
2661
2661
@@ -2865,7 +2865,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
2865
2865
opt_else_expr : Option < & ' gcx hir:: Expr > ,
2866
2866
sp : Span ,
2867
2867
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 ) ;
2869
2869
let cond_diverges = self . diverges . get ( ) ;
2870
2870
self . diverges . set ( Diverges :: Maybe ) ;
2871
2871
@@ -3352,7 +3352,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
3352
3352
self . check_expr_struct_fields ( struct_ty, expected, expr. id , path_span, variant, fields,
3353
3353
base_expr. is_none ( ) ) ;
3354
3354
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) ;
3356
3356
match struct_ty. sty {
3357
3357
ty:: TyAdt ( adt, substs) if adt. is_struct ( ) => {
3358
3358
let fru_field_types = adt. struct_variant ( ) . fields . iter ( ) . map ( |f| {
@@ -3668,7 +3668,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
3668
3668
let rhs_ty = self . check_expr_coercable_to_type ( & rhs, lhs_ty) ;
3669
3669
3670
3670
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
+ }
3672
3675
_ => {
3673
3676
// Only check this if not in an `if` condition, as the
3674
3677
// mistyped comparison help is more appropriate.
@@ -3704,7 +3707,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
3704
3707
} ;
3705
3708
3706
3709
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 ) ;
3708
3711
let cond_diverging = self . diverges . get ( ) ;
3709
3712
self . check_block_no_value ( & body) ;
3710
3713
@@ -3842,7 +3845,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
3842
3845
}
3843
3846
None => {
3844
3847
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) ;
3846
3849
( element_ty, t)
3847
3850
}
3848
3851
} ;
@@ -4097,7 +4100,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
4097
4100
}
4098
4101
hir:: StmtExpr ( ref expr, _) => {
4099
4102
// 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 ( ) ) ;
4101
4104
}
4102
4105
hir:: StmtSemi ( ref expr, _) => {
4103
4106
self . check_expr ( & expr) ;
0 commit comments