File tree Expand file tree Collapse file tree 2 files changed +36
-1
lines changed Expand file tree Collapse file tree 2 files changed +36
-1
lines changed Original file line number Diff line number Diff line change @@ -383,6 +383,9 @@ struct RangeResolver::Implementation {
383
383
384
384
// / Collect the type that an ASTNode should be evaluated to.
385
385
Type resolveNodeType (ASTNode N, RangeKind Kind) {
386
+ auto VoidTy = Ctx.getVoidDecl ()->getDeclaredInterfaceType ();
387
+ if (N.isNull ())
388
+ return VoidTy;
386
389
switch (Kind) {
387
390
case RangeKind::Invalid:
388
391
case RangeKind::SingleDecl:
@@ -407,9 +410,24 @@ struct RangeResolver::Implementation {
407
410
RangeKind::SingleStatement);
408
411
}
409
412
}
413
+
414
+ // Unbox the if statement to find its type.
415
+ if (auto *IS = dyn_cast<IfStmt>(N.get <Stmt*>())) {
416
+ auto ThenTy = resolveNodeType (IS->getThenStmt (),
417
+ RangeKind::SingleStatement);
418
+ auto ElseTy = resolveNodeType (IS->getElseStmt (),
419
+ RangeKind::SingleStatement);
420
+
421
+ // If two branches agree on the return type, return that type.
422
+ if (ThenTy->isEqual (ElseTy))
423
+ return ThenTy;
424
+
425
+ // Otherwise, return the error type.
426
+ return Ctx.TheErrorType ;
427
+ }
410
428
}
411
429
// For other statements, the type should be void.
412
- return Ctx. getVoidDecl ()-> getDeclaredInterfaceType () ;
430
+ return VoidTy ;
413
431
}
414
432
}
415
433
}
Original file line number Diff line number Diff line change
1
+ func foo( _ a: Bool ) -> Int {
2
+ if a {
3
+ return 1
4
+ } else {
5
+ }
6
+ if a {
7
+ return 0
8
+ } else {
9
+ return 1
10
+ }
11
+ }
12
+
13
+ // RUN: %target-swift-ide-test -range -pos=2:1 -end-pos 5:4 -source-filename %s | %FileCheck %s -check-prefix=CHECK-ERR
14
+ // RUN: %target-swift-ide-test -range -pos=6:1 -end-pos 10:4 -source-filename %s | %FileCheck %s -check-prefix=CHECK-INT
15
+
16
+ // CHECK-ERR: <Type><<error type>></Type>
17
+ // CHECK-INT: <Type>Int</Type>
You can’t perform that action at this time.
0 commit comments