Skip to content

Commit 0783890

Browse files
committed
[CSGen] Fix LinkedExprAnalyzer greedy operator linking
Let's not attempt to link arithmetic operators together in presence of concrete and literal (int, float, string) types. Resolves: rdar://problem/35740653
1 parent 4ab6e76 commit 0783890

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

lib/Sema/CSGen.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@ namespace {
103103
unsigned haveIntLiteral : 1;
104104
unsigned haveFloatLiteral : 1;
105105
unsigned haveStringLiteral : 1;
106-
unsigned haveCollectionLiteral : 1;
107-
106+
108107
llvm::SmallSet<TypeBase*, 16> collectedTypes;
109108

110109
llvm::SmallVector<TypeVariableType *, 16> intLiteralTyvars;
@@ -121,12 +120,10 @@ namespace {
121120
haveIntLiteral = false;
122121
haveFloatLiteral = false;
123122
haveStringLiteral = false;
124-
haveCollectionLiteral = false;
125123
}
126124

127-
bool haveLiteral() {
128-
return haveIntLiteral || haveFloatLiteral || haveStringLiteral ||
129-
haveCollectionLiteral;
125+
bool hasLiteral() {
126+
return haveIntLiteral || haveFloatLiteral || haveStringLiteral;
130127
}
131128
};
132129

@@ -231,7 +228,6 @@ namespace {
231228
}
232229

233230
if (isa<CollectionExpr>(expr)) {
234-
LTI.haveCollectionLiteral = true;
235231
return { true, expr };
236232
}
237233

@@ -408,6 +404,11 @@ namespace {
408404
// argument types, we can directly simplify the associated constraint
409405
// graph.
410406
auto simplifyBinOpExprTyVars = [&]() {
407+
// Don't attempt to do linking if there are
408+
// literals intermingled with other inferred types.
409+
if (lti.hasLiteral())
410+
return;
411+
411412
for (auto binExp1 : lti.binaryExprs) {
412413
for (auto binExp2 : lti.binaryExprs) {
413414
if (binExp1 == binExp2)

test/Constraints/operator.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,21 @@ func + (lhs: B_28688585, rhs: B_28688585) -> B_28688585 {
176176

177177
let var_28688585 = D_28688585(value: 1)
178178
_ = var_28688585 + var_28688585 + var_28688585 // Ok
179+
180+
// rdar://problem/35740653 - Fix `LinkedExprAnalyzer` greedy operator linking
181+
182+
struct S_35740653 {
183+
var v: Double = 42
184+
185+
static func value(_ value: Double) -> S_35740653 {
186+
return S_35740653(v: value)
187+
}
188+
189+
static func / (lhs: S_35740653, rhs: S_35740653) -> Double {
190+
return lhs.v / rhs.v
191+
}
192+
}
193+
194+
func rdar35740653(val: S_35740653) {
195+
let _ = 0...Int(val / .value(1.0 / 42.0)) // Ok
196+
}

0 commit comments

Comments
 (0)