Skip to content

Commit 4f8584f

Browse files
authored
Merge pull request #40214 from xedin/rdar-85277993
[CSGen] Rework nil-coalescing operator handling in `LinkedExprAnalyzer`
2 parents 1ca3b93 + 14939cf commit 4f8584f

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

lib/Sema/CSGen.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -213,14 +213,6 @@ namespace {
213213
}
214214

215215
if (auto *binaryExpr = dyn_cast<BinaryExpr>(expr)) {
216-
if (auto *overload = dyn_cast<OverloadedDeclRefExpr>(binaryExpr->getFn())) {
217-
// Don't walk into nil coalescing operators. Attempting to favor
218-
// based on operand types is wrong for this operator.
219-
auto identifier = overload->getDecls().front()->getBaseIdentifier();
220-
if (identifier.isNilCoalescingOperator())
221-
return { false, expr };
222-
}
223-
224216
LTI.binaryExprs.push_back(dyn_cast<BinaryExpr>(expr));
225217
}
226218

@@ -292,11 +284,25 @@ namespace {
292284

293285
/// For a given expression, given information that is global to the
294286
/// expression, attempt to derive a favored type for it.
295-
bool computeFavoredTypeForExpr(Expr *expr, ConstraintSystem &CS) {
287+
void computeFavoredTypeForExpr(Expr *expr, ConstraintSystem &CS) {
296288
LinkedTypeInfo lti;
297289

298290
expr->walk(LinkedExprAnalyzer(lti, CS));
299291

292+
// Check whether we can proceed with favoring.
293+
if (llvm::any_of(lti.binaryExprs, [](const BinaryExpr *op) {
294+
auto *ODRE = dyn_cast<OverloadedDeclRefExpr>(op->getFn());
295+
if (!ODRE)
296+
return false;
297+
298+
// Attempting to favor based on operand types is wrong for
299+
// nil-coalescing operator.
300+
auto identifier = ODRE->getDecls().front()->getBaseIdentifier();
301+
return identifier.isNilCoalescingOperator();
302+
})) {
303+
return;
304+
}
305+
300306
if (lti.collectedTypes.size() == 1) {
301307
// TODO: Compute the BCT.
302308

@@ -371,13 +377,9 @@ namespace {
371377
};
372378

373379
simplifyBinOpExprTyVars();
374-
375-
return true;
376380
}
377-
378-
return false;
379381
}
380-
382+
381383
/// Determine whether the given parameter type and argument should be
382384
/// "favored" because they match exactly.
383385
bool isFavoredParamAndArg(ConstraintSystem &CS, Type paramTy, Type argTy,
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %target-typecheck-verify-swift
2+
// REQUIRES: objc_interop
3+
// REQUIRES: OS=macosx
4+
5+
import simd
6+
import CoreGraphics
7+
8+
let m = simd_float3x3(1)
9+
10+
func foo(point: SIMD2<Float>, depth: Float, a: simd_float3x3? = nil) -> SIMD3<Float> {
11+
(a ?? m) * float3(point.x, point.y, 1) * depth
12+
}

0 commit comments

Comments
 (0)