Skip to content

Commit c0e2d5e

Browse files
authored
Merge pull request #67015 from skrtks/sk-fix-incorrect-optional-types
2 parents 04f6f84 + c210a08 commit c0e2d5e

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

lib/IDE/IDETypeChecking.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,15 @@ class ExpressionTypeCollector: public SourceEntityWalker {
667667
if (E->getType().isNull())
668668
return false;
669669

670+
// We should not report a type for implicit expressions, except for
671+
// - `OptionalEvaluationExpr` to show the correct type when there is optional chaining
672+
// - `DotSyntaxCallExpr` to report the method type without the metatype
673+
if (E->isImplicit() &&
674+
!isa<OptionalEvaluationExpr>(E) &&
675+
!isa<DotSyntaxCallExpr>(E)) {
676+
return false;
677+
}
678+
670679
// If we have already reported types for this source range, we shouldn't
671680
// report again. This makes sure we always report the outtermost type of
672681
// several overlapping expressions.

test/SourceKit/ExpressionType/basic.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ func DictS(_ a: [Int: S]) {
2020
_ = a[2]?.val.advanced(by: 1).byteSwapped
2121
}
2222

23+
func optExpr(str: String?) -> String? {
24+
let a: String? = str
25+
let b: String? = "Hey"
26+
let c: String = "Bye"
27+
return c
28+
}
29+
2330
// RUN: %sourcekitd-test -req=collect-type %s -- %s | %FileCheck %s
2431
// CHECK: (183, 202): Int
2532
// CHECK: (183, 196): String
@@ -31,3 +38,7 @@ func DictS(_ a: [Int: S]) {
3138
// CHECK: (291, 292): Int?
3239
// CHECK: (295, 332): Int?
3340
// CHECK: (295, 320): Int
41+
// CHECK: (395, 398): String?
42+
// CHECK: (418, 423): String
43+
// CHECK: (442, 447): String
44+
// CHECK: (457, 458): String

0 commit comments

Comments
 (0)