Skip to content

Small clean-ups #10476

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions lib/AST/GenericSignatureBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2824,7 +2824,7 @@ static Identifier typoCorrectNestedType(
// Look through all of the associated types of all of the protocols
// to which the parent conforms.
llvm::SmallVector<Identifier, 2> bestMatches;
unsigned bestEditDistance = 0;
unsigned bestEditDistance = UINT_MAX;
unsigned maxScore = (name.size() + 1) / 3;
for (auto proto : pa->getParent()->getConformsTo()) {
for (auto member : getProtocolMembers(proto)) {
Expand All @@ -2836,16 +2836,12 @@ static Identifier typoCorrectNestedType(
/*AllowReplacements=*/true,
maxScore);
assert(dist > 0 && "nested type should have matched associated type");
if (bestEditDistance == 0 || dist == bestEditDistance) {
bestEditDistance = dist;
maxScore = bestEditDistance;
bestMatches.push_back(assocType->getName());
} else if (dist < bestEditDistance) {
bestEditDistance = dist;
maxScore = bestEditDistance;
if (dist < bestEditDistance) {
maxScore = bestEditDistance = dist;
bestMatches.clear();
bestMatches.push_back(assocType->getName());
}
if (dist == bestEditDistance)
bestMatches.push_back(assocType->getName());
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2768,8 +2768,8 @@ parseIdentifierDeclName(Parser &P, Identifier &Result, SourceLoc &Loc,

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

// Pretend this works as an identifier, which shouldn't be observable since
Expand Down
1 change: 1 addition & 0 deletions lib/SILGen/SILGenPattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,7 @@ chooseNecessaryColumn(const ClauseMatrix &matrix, unsigned firstRow) {
for (unsigned c = 0; c != numColumns; ++c) {
unsigned constructorPrefix = getConstructorPrefix(matrix, firstRow, c);
if (constructorPrefix > longestConstructorPrefix) {
longestConstructorPrefix = constructorPrefix;
bestColumn = c;
}
}
Expand Down
34 changes: 11 additions & 23 deletions lib/Sema/CSDiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,9 +387,14 @@ static bool diagnoseAmbiguity(ConstraintSystem &cs,

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

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

// If we have more distinct overload choices for this locator than for
// prior locators, just keep this locator.

bool better = false;
if (bestOverload) {
if (distinctOverloads > maxDistinctOverloads) {
better = true;
} else if (distinctOverloads == maxDistinctOverloads) {
if (depth > maxDepth) {
better = true;
} else if (depth == maxDepth) {
if (index < minIndex) {
better = true;
}
}
}
}

if (!bestOverload || better) {
auto thisScore = score(distinctOverloads, depth, index);
if (thisScore > bestScore) {
bestScore = thisScore;
bestOverload = i;
maxDistinctOverloads = distinctOverloads;
maxDepth = depth;
minIndex = index;
continue;
}

Expand Down
40 changes: 20 additions & 20 deletions test/SILGen/indirect_enum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,29 +199,29 @@ func switchTreeA<T>(_ x: TreeA<T>) {
// CHECK: [[TUPLE:%.*]] = load_borrow [[TUPLE_ADDR]]
// CHECK: [[LEFT:%.*]] = tuple_extract [[TUPLE]] {{.*}}, 0
// CHECK: [[RIGHT:%.*]] = tuple_extract [[TUPLE]] {{.*}}, 1
// CHECK: switch_enum [[LEFT]] : $TreeA<T>,
// CHECK: case #TreeA.Leaf!enumelt.1: [[LEAF_CASE_LEFT:bb[0-9]+]],
// CHECK: default [[FAIL_LEFT:bb[0-9]+]]

// CHECK: [[LEAF_CASE_LEFT]]([[LEFT_LEAF_BOX:%.*]] : $<τ_0_0> { var τ_0_0 } <T>):
// CHECK: [[LEFT_LEAF_VALUE:%.*]] = project_box [[LEFT_LEAF_BOX]]
// CHECK: switch_enum [[RIGHT]] : $TreeA<T>,
// CHECK: case #TreeA.Leaf!enumelt.1: [[LEAF_CASE_RIGHT:bb[0-9]+]],
// CHECK: default [[FAIL_RIGHT:bb[0-9]+]]

// CHECK: [[LEAF_CASE_RIGHT]]([[RIGHT_LEAF_BOX:%.*]] : $<τ_0_0> { var τ_0_0 } <T>):
// CHECK: [[RIGHT_LEAF_VALUE:%.*]] = project_box [[RIGHT_LEAF_BOX]]
// CHECK: switch_enum [[LEFT]] : $TreeA<T>,
// CHECK: case #TreeA.Leaf!enumelt.1: [[LEAF_CASE_LEFT:bb[0-9]+]],
// CHECK: default [[FAIL_LEFT:bb[0-9]+]]

// CHECK: [[LEAF_CASE_LEFT]]([[LEFT_LEAF_BOX:%.*]] : $<τ_0_0> { var τ_0_0 } <T>):
// CHECK: [[LEFT_LEAF_VALUE:%.*]] = project_box [[LEFT_LEAF_BOX]]
// CHECK: copy_addr [[LEFT_LEAF_VALUE]]
// CHECK: copy_addr [[RIGHT_LEAF_VALUE]]
// -- x +1
// CHECK: destroy_value [[NODE_BOX]]
// CHECK: end_borrow [[BORROWED_ARG]] from [[ARG]]
// CHECK: br [[OUTER_CONT]]

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

// CHECK: [[FAIL_RIGHT]]:
// CHECK: [[FAIL_LEFT]]:
// CHECK: br [[DEFAULT]]

case .Branch(.Leaf(let x), .Leaf(let y)):
Expand Down Expand Up @@ -278,27 +278,27 @@ func switchTreeB<T>(_ x: TreeB<T>) {
// CHECK: [[TUPLE:%.*]] = project_box [[BOX]]
// CHECK: [[LEFT:%.*]] = tuple_element_addr [[TUPLE]]
// CHECK: [[RIGHT:%.*]] = tuple_element_addr [[TUPLE]]
// CHECK: switch_enum_addr [[RIGHT]] {{.*}}, default [[RIGHT_FAIL:bb[0-9]+]]

// CHECK: bb{{.*}}:
// CHECK: copy_addr [[RIGHT]] to [initialization] [[RIGHT_COPY:%.*]] :
// CHECK: [[RIGHT_LEAF:%.*]] = unchecked_take_enum_data_addr [[RIGHT_COPY]] : $*TreeB<T>, #TreeB.Leaf
// CHECK: switch_enum_addr [[LEFT]] {{.*}}, default [[LEFT_FAIL:bb[0-9]+]]

// CHECK: bb{{.*}}:
// CHECK: copy_addr [[LEFT]] to [initialization] [[LEFT_COPY:%.*]] :
// CHECK: [[LEFT_LEAF:%.*]] = unchecked_take_enum_data_addr [[LEFT_COPY]] : $*TreeB<T>, #TreeB.Leaf
// CHECK: switch_enum_addr [[RIGHT]] {{.*}}, default [[RIGHT_FAIL:bb[0-9]+]]

// CHECK: bb{{.*}}:
// CHECK: copy_addr [[RIGHT]] to [initialization] [[RIGHT_COPY:%.*]] :
// CHECK: [[RIGHT_LEAF:%.*]] = unchecked_take_enum_data_addr [[RIGHT_COPY]] : $*TreeB<T>, #TreeB.Leaf
// CHECK: copy_addr [take] [[LEFT_LEAF]] to [initialization] [[X:%.*]] :
// CHECK: copy_addr [take] [[RIGHT_LEAF]] to [initialization] [[Y:%.*]] :
// CHECK: function_ref @_T013indirect_enum1c{{[_0-9a-zA-Z]*}}F
// CHECK: destroy_addr [[Y]]
// CHECK: dealloc_stack [[Y]]
// CHECK: destroy_addr [[X]]
// CHECK: dealloc_stack [[X]]
// CHECK-NOT: destroy_addr [[LEFT_COPY]]
// CHECK: dealloc_stack [[LEFT_COPY]]
// CHECK-NOT: destroy_addr [[RIGHT_COPY]]
// CHECK: dealloc_stack [[RIGHT_COPY]]
// CHECK-NOT: destroy_addr [[LEFT_COPY]]
// CHECK: dealloc_stack [[LEFT_COPY]]
// -- box +0
// CHECK: destroy_value [[BOX]]
// CHECK-NOT: destroy_addr [[TREE_COPY]]
Expand All @@ -308,16 +308,16 @@ func switchTreeB<T>(_ x: TreeB<T>) {
case .Branch(.Leaf(let x), .Leaf(let y)):
c(x, y)

// CHECK: [[LEFT_FAIL]]:
// CHECK: destroy_addr [[RIGHT_LEAF]]
// CHECK-NOT: destroy_addr [[RIGHT_COPY]]
// CHECK: dealloc_stack [[RIGHT_COPY]]
// CHECK: [[RIGHT_FAIL]]:
// CHECK: destroy_addr [[LEFT_LEAF]]
// CHECK-NOT: destroy_addr [[LEFT_COPY]]
// CHECK: dealloc_stack [[LEFT_COPY]]
// CHECK: destroy_value [[BOX]]
// CHECK-NOT: destroy_addr [[TREE_COPY]]
// CHECK: dealloc_stack [[TREE_COPY]]
// CHECK: br [[INNER_CONT:bb[0-9]+]]

// CHECK: [[RIGHT_FAIL]]:
// CHECK: [[LEFT_FAIL]]:
// CHECK: destroy_value [[BOX]]
// CHECK-NOT: destroy_addr [[TREE_COPY]]
// CHECK: dealloc_stack [[TREE_COPY]]
Expand Down
4 changes: 2 additions & 2 deletions test/SILGen/switch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1071,8 +1071,8 @@ func testMultiPatternsWithOuterScopeSameNamedVar(base: Int?, filter: Int?) {

case (.some(let base), .some(let filter)):
// CHECK: bb2(%10 : $Int):
// CHECK-NEXT: debug_value %10 : $Int, let, name "base"
// CHECK-NEXT: debug_value %8 : $Int, let, name "filter"
// CHECK-NEXT: debug_value %8 : $Int, let, name "base"
// CHECK-NEXT: debug_value %10 : $Int, let, name "filter"
print("both: \(base), \(filter)")
case (.some(let base), .none), (.none, .some(let base)):
// CHECK: bb3:
Expand Down