11use either:: Either ;
22use hir:: { db:: ExpandDatabase , ClosureStyle , HirDisplay , InFile , Type } ;
33use ide_db:: { famous_defs:: FamousDefs , source_change:: SourceChange } ;
4+ use stdx:: never;
45use syntax:: {
56 ast:: { self , BlockExpr , ExprStmt } ,
67 AstNode , AstPtr ,
@@ -15,15 +16,29 @@ use crate::{adjusted_display_range, fix, Assist, Diagnostic, DiagnosticsContext}
1516// the expected type.
1617pub ( crate ) fn type_mismatch ( ctx : & DiagnosticsContext < ' _ > , d : & hir:: TypeMismatch ) -> Diagnostic {
1718 let display_range = match & d. expr_or_pat {
18- Either :: Left ( expr) => adjusted_display_range :: < ast:: BlockExpr > (
19- ctx,
20- expr. clone ( ) . map ( |it| it. into ( ) ) ,
21- & |block| {
22- let r_curly_range = block. stmt_list ( ) ?. r_curly_token ( ) ?. text_range ( ) ;
19+ Either :: Left ( expr) => {
20+ adjusted_display_range :: < ast:: Expr > ( ctx, expr. clone ( ) . map ( |it| it. into ( ) ) , & |expr| {
21+ if !expr. is_block_like ( ) {
22+ return None ;
23+ }
24+
25+ let salient_token_range = match expr {
26+ ast:: Expr :: IfExpr ( it) => it. if_token ( ) ?. text_range ( ) ,
27+ ast:: Expr :: LoopExpr ( it) => it. loop_token ( ) ?. text_range ( ) ,
28+ ast:: Expr :: ForExpr ( it) => it. for_token ( ) ?. text_range ( ) ,
29+ ast:: Expr :: WhileExpr ( it) => it. while_token ( ) ?. text_range ( ) ,
30+ ast:: Expr :: BlockExpr ( it) => it. stmt_list ( ) ?. r_curly_token ( ) ?. text_range ( ) ,
31+ ast:: Expr :: MatchExpr ( it) => it. match_token ( ) ?. text_range ( ) ,
32+ _ => {
33+ never ! ( ) ;
34+ return None ;
35+ }
36+ } ;
37+
2338 cov_mark:: hit!( type_mismatch_on_block) ;
24- Some ( r_curly_range )
25- } ,
26- ) ,
39+ Some ( salient_token_range )
40+ } )
41+ }
2742 Either :: Right ( pat) => {
2843 ctx. sema . diagnostics_display_range ( pat. clone ( ) . map ( |it| it. into ( ) ) ) . range
2944 }
@@ -620,6 +635,10 @@ fn f() -> i32 {
620635 let _ = x + y;
621636 }
622637//^ error: expected i32, found ()
638+
639+ fn h() -> i32 {
640+ while true {}
641+ } //^^^^^ error: expected i32, found ()
623642"# ,
624643 ) ;
625644 }
0 commit comments