Skip to content

Commit 479598a

Browse files
Check for literal and explicity coercions
1 parent 42f50eb commit 479598a

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

lib/Sema/CSSimplify.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -7767,7 +7767,16 @@ void ConstraintSystem::addExplicitConversionConstraint(
77677767
auto locatorPtr = getConstraintLocator(locator);
77687768

77697769
// Coercion (the common case).
7770-
auto coerceLocator = getConstraintLocator(locator.getBaseLocator(), LocatorPathElt::TypeCoercion());
7770+
auto coerceLocator = [&]() {
7771+
if (auto expr = dyn_cast<CoerceExpr>(locatorPtr->getAnchor())) {
7772+
// Only adding this path for explicty coercions e.g _ = a as Int
7773+
// and for non lireal subExpr.
7774+
if (!expr->isImplicit() && !isa<LiteralExpr>(expr->getSubExpr()))
7775+
return getConstraintLocator(locator.getBaseLocator(), LocatorPathElt::TypeCoercion());
7776+
}
7777+
return locatorPtr;
7778+
}();
7779+
77717780
Constraint *coerceConstraint =
77727781
Constraint::create(*this, ConstraintKind::Conversion,
77737782
fromType, toType, coerceLocator);

test/expr/cast/as_coerce.swift

+3
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,6 @@ _ = sr11295c as String // expected-warning {{casting expression to 'String' does
150150

151151
let sr11295d = "Hello Typealias"
152152
_ = sr11295d as Type // expected-warning {{casting expression to 'Type' (aka 'String') doesn't change the type}} {{14-22=}}
153+
154+
_ = "Hello" as String // Ok
155+
_ = 1 as Int64 // Ok

0 commit comments

Comments
 (0)