1
1
use either:: Either ;
2
2
use hir:: { db:: ExpandDatabase , ClosureStyle , HirDisplay , InFile , Type } ;
3
3
use ide_db:: { famous_defs:: FamousDefs , source_change:: SourceChange } ;
4
+ use stdx:: never;
4
5
use syntax:: {
5
6
ast:: { self , BlockExpr , ExprStmt } ,
6
7
AstNode , AstPtr ,
@@ -15,15 +16,29 @@ use crate::{adjusted_display_range, fix, Assist, Diagnostic, DiagnosticsContext}
15
16
// the expected type.
16
17
pub ( crate ) fn type_mismatch ( ctx : & DiagnosticsContext < ' _ > , d : & hir:: TypeMismatch ) -> Diagnostic {
17
18
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
+
23
38
cov_mark:: hit!( type_mismatch_on_block) ;
24
- Some ( r_curly_range )
25
- } ,
26
- ) ,
39
+ Some ( salient_token_range )
40
+ } )
41
+ }
27
42
Either :: Right ( pat) => {
28
43
ctx. sema . diagnostics_display_range ( pat. clone ( ) . map ( |it| it. into ( ) ) ) . range
29
44
}
@@ -620,6 +635,10 @@ fn f() -> i32 {
620
635
let _ = x + y;
621
636
}
622
637
//^ error: expected i32, found ()
638
+
639
+ fn h() -> i32 {
640
+ while true {}
641
+ } //^^^^^ error: expected i32, found ()
623
642
"# ,
624
643
) ;
625
644
}
0 commit comments