Skip to content

Commit d8e40a1

Browse files
committed
[CSSimplify] Don't suppress existential opening if as any ... is in parens
1 parent 3691e5c commit d8e40a1

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,13 +1479,14 @@ shouldOpenExistentialCallArgument(
14791479
return None;
14801480

14811481
// An argument expression that explicitly coerces to an existential
1482-
// disables the implicit opening of the existential.
1482+
// disables the implicit opening of the existential unless it's
1483+
// wrapped in parens.
14831484
if (argExpr) {
14841485
if (auto argCast = dyn_cast<ExplicitCastExpr>(
14851486
argExpr->getSemanticsProvidingExpr())) {
14861487
if (auto typeRepr = argCast->getCastTypeRepr()) {
14871488
if (auto toType = cs.getType(typeRepr)) {
1488-
if (toType->isAnyExistentialType())
1489+
if (!isa<ParenExpr>(argExpr) && toType->isAnyExistentialType())
14891490
return None;
14901491
}
14911492
}

test/Constraints/opened_existentials.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,6 @@ func testExplicitCoercionRequirement(v: any B, otherV: any B & D) {
288288
getP(getC(v)) // expected-error {{inferred result type 'any P' requires explicit coercion due to loss of generic requirements}} {{8-8=(}} {{15-15=as any P)}}
289289
getP(v.getC()) // expected-error {{inferred result type 'any P' requires explicit coercion due to loss of generic requirements}} {{8-8=(}} {{14-14=as any P)}}
290290

291-
getP((getC(v) as any P)) // Ok - parens avoid opening suppression
292-
291+
getP((getC(v) as any P)) // Ok - parens avoid opening suppression
292+
getP((v.getC() as any P)) // Ok - parens avoid opening suppression
293293
}

0 commit comments

Comments
 (0)