Skip to content

Commit a8da5b8

Browse files
authored
Merge pull request #6514 from xedin/SR_3506
[Diagnostics] When checking AssignExpr properly diagnose destination
2 parents dfed050 + fb35153 commit a8da5b8

File tree

5 files changed

+19
-8
lines changed

5 files changed

+19
-8
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1990,8 +1990,10 @@ class FailureDiagnosis :public ASTVisitor<FailureDiagnosis, /*exprresult*/bool>{
19901990
TCCOptions options = TCCOptions(),
19911991
ExprTypeCheckListener *listener = nullptr,
19921992
bool allowFreeTypeVariables = true);
1993-
Expr *typeCheckChildIndependently(Expr *subExpr, TCCOptions options) {
1994-
return typeCheckChildIndependently(subExpr, Type(), CTP_Unused, options);
1993+
Expr *typeCheckChildIndependently(Expr *subExpr, TCCOptions options,
1994+
bool allowFreeTypeVariables = true) {
1995+
return typeCheckChildIndependently(subExpr, Type(), CTP_Unused, options,
1996+
nullptr, allowFreeTypeVariables);
19951997
}
19961998

19971999
Type getTypeOfTypeCheckedChildIndependently(Expr *subExpr,
@@ -6052,13 +6054,21 @@ bool FailureDiagnosis::visitAssignExpr(AssignExpr *assignExpr) {
60526054

60536055
auto destType = destExpr->getType();
60546056
if (destType->is<UnresolvedType>() || destType->hasTypeVariable()) {
6055-
// If we have no useful type information from the destination, just type
6057+
// Look closer into why destination has unresolved types since such
6058+
// means that destination has diagnosible structural problems, and it's
6059+
// better to diagnose destination (if possible) before moving on to
6060+
// the source of the assignment.
6061+
destExpr = typeCheckChildIndependently(
6062+
destExpr, TCC_AllowLValue | TCC_ForceRecheck, false);
6063+
if (!destExpr)
6064+
return true;
6065+
6066+
// If re-checking destination didn't produce diagnostic, let's just type
60566067
// check the source without contextual information. If it succeeds, then we
60576068
// win, but if it fails, we'll have to diagnose this another way.
60586069
return !typeCheckChildIndependently(assignExpr->getSrc());
60596070
}
6060-
6061-
6071+
60626072
// If the result type is a non-lvalue, then we are failing because it is
60636073
// immutable and that's not a great thing to assign to.
60646074
if (!destType->isLValueType()) {

test/Constraints/assignment.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,4 @@ func f23798944() {
5757
}
5858
}
5959

60+
.sr_3506 = 0 // expected-error {{reference to member 'sr_3506' cannot be resolved without a contextual type}}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: not --crash %target-sil-opt %s
1+
// RUN: not %target-sil-opt %s
22
// REQUIRES: asserts
33
.x.a=Int
44
import Swift

validation-test/compiler_crashers/28368-swift-expr-propagatelvalueaccesskind.swift renamed to validation-test/compiler_crashers_fixed/28368-swift-expr-propagatelvalueaccesskind.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
// See https://swift.org/LICENSE.txt for license information
66
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

8-
// RUN: not --crash %target-swift-frontend %s -typecheck
8+
// RUN: not %target-swift-frontend %s -typecheck
99
guard let f=.h.a=[]{

validation-test/compiler_crashers/28395-swift-expr-propagatelvalueaccesskind.swift renamed to validation-test/compiler_crashers_fixed/28395-swift-expr-propagatelvalueaccesskind.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
// See https://swift.org/LICENSE.txt for license information
66
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

8-
// RUN: not --crash %target-swift-frontend %s -typecheck
8+
// RUN: not %target-swift-frontend %s -typecheck
99
{guard let b=.h.h=.n?(){

0 commit comments

Comments
 (0)