Skip to content

[Sema] Increase impact of ternary then branch aiming better diagnostics #66208

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 1 commit into from
May 31, 2023
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
9 changes: 8 additions & 1 deletion lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14753,8 +14753,15 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
// means that result would attempt a type from each side if
// one is available and that would result in two fixes - one for
// each mismatched branch.
if (branchElt->forElse())
if (branchElt->forElse()) {
impact = 10;
} else {
// Also increase impact for `then` branch lower than `else` to still
// eliminate ambiguity, but slightly worst than the average fix to avoid
// so the solution which record this fix wouldn't be picked over one
// that has contextual mismatch fix on the result of ternary expression.
impact = 5;
}
}
using SingleValueStmtBranch = LocatorPathElt::SingleValueStmtBranch;
if (auto branchElt = locator->getLastElementAs<SingleValueStmtBranch>()) {
Expand Down
4 changes: 4 additions & 0 deletions test/expr/expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -951,3 +951,7 @@ let _ = "foo \(42 /*
// expected-error @-3 {{cannot find ')' to match opening '(' in string interpolation}} expected-error @-3 {{unterminated string literal}}
// expected-error @-2 {{expected expression}}
// expected-error @-3 {{unterminated string literal}}

// https://github.com/apple/swift/issues/66192
func I66192(_: Int) {}
I66192(true ? "yes" : "no") // expected-error{{cannot convert value of type 'String' to expected argument type 'Int'}}