Skip to content

Commit 18078c1

Browse files
authored
Merge pull request #10476 from huonw/cleanups
Small clean-ups
2 parents 08312d0 + 77781b8 commit 18078c1

File tree

6 files changed

+41
-56
lines changed

6 files changed

+41
-56
lines changed

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2824,7 +2824,7 @@ static Identifier typoCorrectNestedType(
28242824
// Look through all of the associated types of all of the protocols
28252825
// to which the parent conforms.
28262826
llvm::SmallVector<Identifier, 2> bestMatches;
2827-
unsigned bestEditDistance = 0;
2827+
unsigned bestEditDistance = UINT_MAX;
28282828
unsigned maxScore = (name.size() + 1) / 3;
28292829
for (auto proto : pa->getParent()->getConformsTo()) {
28302830
for (auto member : getProtocolMembers(proto)) {
@@ -2836,16 +2836,12 @@ static Identifier typoCorrectNestedType(
28362836
/*AllowReplacements=*/true,
28372837
maxScore);
28382838
assert(dist > 0 && "nested type should have matched associated type");
2839-
if (bestEditDistance == 0 || dist == bestEditDistance) {
2840-
bestEditDistance = dist;
2841-
maxScore = bestEditDistance;
2842-
bestMatches.push_back(assocType->getName());
2843-
} else if (dist < bestEditDistance) {
2844-
bestEditDistance = dist;
2845-
maxScore = bestEditDistance;
2839+
if (dist < bestEditDistance) {
2840+
maxScore = bestEditDistance = dist;
28462841
bestMatches.clear();
2847-
bestMatches.push_back(assocType->getName());
28482842
}
2843+
if (dist == bestEditDistance)
2844+
bestMatches.push_back(assocType->getName());
28492845
}
28502846
}
28512847

lib/Parse/ParseDecl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2762,8 +2762,8 @@ parseIdentifierDeclName(Parser &P, Identifier &Result, SourceLoc &Loc,
27622762

27632763
if (P.Tok.is(tok::integer_literal) || P.Tok.is(tok::floating_literal) ||
27642764
(P.Tok.is(tok::unknown) && isdigit(P.Tok.getText()[0]))) {
2765-
// Per rdar://problem/32316666, using numbers for identifiers is a common
2766-
// error for beginners, so it's worth handling this in a special way.
2765+
// Using numbers for identifiers is a common error for beginners, so it's
2766+
// worth handling this in a special way.
27672767
P.diagnose(P.Tok, diag::number_cant_start_decl_name, DeclKindName);
27682768

27692769
// Pretend this works as an identifier, which shouldn't be observable since

lib/SILGen/SILGenPattern.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,7 @@ chooseNecessaryColumn(const ClauseMatrix &matrix, unsigned firstRow) {
947947
for (unsigned c = 0; c != numColumns; ++c) {
948948
unsigned constructorPrefix = getConstructorPrefix(matrix, firstRow, c);
949949
if (constructorPrefix > longestConstructorPrefix) {
950+
longestConstructorPrefix = constructorPrefix;
950951
bestColumn = c;
951952
}
952953
}

lib/Sema/CSDiag.cpp

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,14 @@ static bool diagnoseAmbiguity(ConstraintSystem &cs,
387387

388388
// Find the locators which have the largest numbers of distinct overloads.
389389
Optional<unsigned> bestOverload;
390-
unsigned maxDistinctOverloads = 0;
391-
unsigned maxDepth = 0;
392-
unsigned minIndex = std::numeric_limits<unsigned>::max();
390+
// Overloads are scored by lexicographical comparison of (# of distinct
391+
// overloads, depth, *reverse* of the index). N.B. - cannot be used for the
392+
// reversing: the score version of index == 0 should be > than that of 1, but
393+
// -0 == 0 < UINT_MAX == -1, whereas ~0 == UINT_MAX > UINT_MAX - 1 == ~1.
394+
auto score = [](unsigned distinctOverloads, unsigned depth, unsigned index) {
395+
return std::make_tuple(distinctOverloads, depth, ~index);
396+
};
397+
auto bestScore = score(0, 0, std::numeric_limits<unsigned>::max());
393398

394399
// Get a map of expressions to their depths and post-order traversal indices.
395400
// Heuristically, all other things being equal, we should complain about the
@@ -428,27 +433,10 @@ static bool diagnoseAmbiguity(ConstraintSystem &cs,
428433

429434
// If we have more distinct overload choices for this locator than for
430435
// prior locators, just keep this locator.
431-
432-
bool better = false;
433-
if (bestOverload) {
434-
if (distinctOverloads > maxDistinctOverloads) {
435-
better = true;
436-
} else if (distinctOverloads == maxDistinctOverloads) {
437-
if (depth > maxDepth) {
438-
better = true;
439-
} else if (depth == maxDepth) {
440-
if (index < minIndex) {
441-
better = true;
442-
}
443-
}
444-
}
445-
}
446-
447-
if (!bestOverload || better) {
436+
auto thisScore = score(distinctOverloads, depth, index);
437+
if (thisScore > bestScore) {
438+
bestScore = thisScore;
448439
bestOverload = i;
449-
maxDistinctOverloads = distinctOverloads;
450-
maxDepth = depth;
451-
minIndex = index;
452440
continue;
453441
}
454442

test/SILGen/indirect_enum.swift

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -199,29 +199,29 @@ func switchTreeA<T>(_ x: TreeA<T>) {
199199
// CHECK: [[TUPLE:%.*]] = load_borrow [[TUPLE_ADDR]]
200200
// CHECK: [[LEFT:%.*]] = tuple_extract [[TUPLE]] {{.*}}, 0
201201
// CHECK: [[RIGHT:%.*]] = tuple_extract [[TUPLE]] {{.*}}, 1
202+
// CHECK: switch_enum [[LEFT]] : $TreeA<T>,
203+
// CHECK: case #TreeA.Leaf!enumelt.1: [[LEAF_CASE_LEFT:bb[0-9]+]],
204+
// CHECK: default [[FAIL_LEFT:bb[0-9]+]]
205+
206+
// CHECK: [[LEAF_CASE_LEFT]]([[LEFT_LEAF_BOX:%.*]] : $<τ_0_0> { var τ_0_0 } <T>):
207+
// CHECK: [[LEFT_LEAF_VALUE:%.*]] = project_box [[LEFT_LEAF_BOX]]
202208
// CHECK: switch_enum [[RIGHT]] : $TreeA<T>,
203209
// CHECK: case #TreeA.Leaf!enumelt.1: [[LEAF_CASE_RIGHT:bb[0-9]+]],
204210
// CHECK: default [[FAIL_RIGHT:bb[0-9]+]]
205211

206212
// CHECK: [[LEAF_CASE_RIGHT]]([[RIGHT_LEAF_BOX:%.*]] : $<τ_0_0> { var τ_0_0 } <T>):
207213
// CHECK: [[RIGHT_LEAF_VALUE:%.*]] = project_box [[RIGHT_LEAF_BOX]]
208-
// CHECK: switch_enum [[LEFT]] : $TreeA<T>,
209-
// CHECK: case #TreeA.Leaf!enumelt.1: [[LEAF_CASE_LEFT:bb[0-9]+]],
210-
// CHECK: default [[FAIL_LEFT:bb[0-9]+]]
211-
212-
// CHECK: [[LEAF_CASE_LEFT]]([[LEFT_LEAF_BOX:%.*]] : $<τ_0_0> { var τ_0_0 } <T>):
213-
// CHECK: [[LEFT_LEAF_VALUE:%.*]] = project_box [[LEFT_LEAF_BOX]]
214214
// CHECK: copy_addr [[LEFT_LEAF_VALUE]]
215215
// CHECK: copy_addr [[RIGHT_LEAF_VALUE]]
216216
// -- x +1
217217
// CHECK: destroy_value [[NODE_BOX]]
218218
// CHECK: end_borrow [[BORROWED_ARG]] from [[ARG]]
219219
// CHECK: br [[OUTER_CONT]]
220220

221-
// CHECK: [[FAIL_LEFT]]:
221+
// CHECK: [[FAIL_RIGHT]]:
222222
// CHECK: br [[DEFAULT:bb[0-9]+]]
223223

224-
// CHECK: [[FAIL_RIGHT]]:
224+
// CHECK: [[FAIL_LEFT]]:
225225
// CHECK: br [[DEFAULT]]
226226

227227
case .Branch(.Leaf(let x), .Leaf(let y)):
@@ -278,27 +278,27 @@ func switchTreeB<T>(_ x: TreeB<T>) {
278278
// CHECK: [[TUPLE:%.*]] = project_box [[BOX]]
279279
// CHECK: [[LEFT:%.*]] = tuple_element_addr [[TUPLE]]
280280
// CHECK: [[RIGHT:%.*]] = tuple_element_addr [[TUPLE]]
281-
// CHECK: switch_enum_addr [[RIGHT]] {{.*}}, default [[RIGHT_FAIL:bb[0-9]+]]
282-
283-
// CHECK: bb{{.*}}:
284-
// CHECK: copy_addr [[RIGHT]] to [initialization] [[RIGHT_COPY:%.*]] :
285-
// CHECK: [[RIGHT_LEAF:%.*]] = unchecked_take_enum_data_addr [[RIGHT_COPY]] : $*TreeB<T>, #TreeB.Leaf
286281
// CHECK: switch_enum_addr [[LEFT]] {{.*}}, default [[LEFT_FAIL:bb[0-9]+]]
287282

288283
// CHECK: bb{{.*}}:
289284
// CHECK: copy_addr [[LEFT]] to [initialization] [[LEFT_COPY:%.*]] :
290285
// CHECK: [[LEFT_LEAF:%.*]] = unchecked_take_enum_data_addr [[LEFT_COPY]] : $*TreeB<T>, #TreeB.Leaf
286+
// CHECK: switch_enum_addr [[RIGHT]] {{.*}}, default [[RIGHT_FAIL:bb[0-9]+]]
287+
288+
// CHECK: bb{{.*}}:
289+
// CHECK: copy_addr [[RIGHT]] to [initialization] [[RIGHT_COPY:%.*]] :
290+
// CHECK: [[RIGHT_LEAF:%.*]] = unchecked_take_enum_data_addr [[RIGHT_COPY]] : $*TreeB<T>, #TreeB.Leaf
291291
// CHECK: copy_addr [take] [[LEFT_LEAF]] to [initialization] [[X:%.*]] :
292292
// CHECK: copy_addr [take] [[RIGHT_LEAF]] to [initialization] [[Y:%.*]] :
293293
// CHECK: function_ref @_T013indirect_enum1c{{[_0-9a-zA-Z]*}}F
294294
// CHECK: destroy_addr [[Y]]
295295
// CHECK: dealloc_stack [[Y]]
296296
// CHECK: destroy_addr [[X]]
297297
// CHECK: dealloc_stack [[X]]
298-
// CHECK-NOT: destroy_addr [[LEFT_COPY]]
299-
// CHECK: dealloc_stack [[LEFT_COPY]]
300298
// CHECK-NOT: destroy_addr [[RIGHT_COPY]]
301299
// CHECK: dealloc_stack [[RIGHT_COPY]]
300+
// CHECK-NOT: destroy_addr [[LEFT_COPY]]
301+
// CHECK: dealloc_stack [[LEFT_COPY]]
302302
// -- box +0
303303
// CHECK: destroy_value [[BOX]]
304304
// CHECK-NOT: destroy_addr [[TREE_COPY]]
@@ -308,16 +308,16 @@ func switchTreeB<T>(_ x: TreeB<T>) {
308308
case .Branch(.Leaf(let x), .Leaf(let y)):
309309
c(x, y)
310310

311-
// CHECK: [[LEFT_FAIL]]:
312-
// CHECK: destroy_addr [[RIGHT_LEAF]]
313-
// CHECK-NOT: destroy_addr [[RIGHT_COPY]]
314-
// CHECK: dealloc_stack [[RIGHT_COPY]]
311+
// CHECK: [[RIGHT_FAIL]]:
312+
// CHECK: destroy_addr [[LEFT_LEAF]]
313+
// CHECK-NOT: destroy_addr [[LEFT_COPY]]
314+
// CHECK: dealloc_stack [[LEFT_COPY]]
315315
// CHECK: destroy_value [[BOX]]
316316
// CHECK-NOT: destroy_addr [[TREE_COPY]]
317317
// CHECK: dealloc_stack [[TREE_COPY]]
318318
// CHECK: br [[INNER_CONT:bb[0-9]+]]
319319

320-
// CHECK: [[RIGHT_FAIL]]:
320+
// CHECK: [[LEFT_FAIL]]:
321321
// CHECK: destroy_value [[BOX]]
322322
// CHECK-NOT: destroy_addr [[TREE_COPY]]
323323
// CHECK: dealloc_stack [[TREE_COPY]]

test/SILGen/switch.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,8 +1071,8 @@ func testMultiPatternsWithOuterScopeSameNamedVar(base: Int?, filter: Int?) {
10711071

10721072
case (.some(let base), .some(let filter)):
10731073
// CHECK: bb2(%10 : $Int):
1074-
// CHECK-NEXT: debug_value %10 : $Int, let, name "base"
1075-
// CHECK-NEXT: debug_value %8 : $Int, let, name "filter"
1074+
// CHECK-NEXT: debug_value %8 : $Int, let, name "base"
1075+
// CHECK-NEXT: debug_value %10 : $Int, let, name "filter"
10761076
print("both: \(base), \(filter)")
10771077
case (.some(let base), .none), (.none, .some(let base)):
10781078
// CHECK: bb3:

0 commit comments

Comments
 (0)