@@ -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) ;
0 commit comments