swift
CastOptimizer: handle isolated conformances
#80543
Merged

CastOptimizer: handle isolated conformances #80543

eeckstein
eeckstein78 days ago (edited 78 days ago)

Check the isolation of conformances to avoid wrong folding of dynamic casts

rdar://147417762

eeckstein eeckstein requested a review from jckarter jckarter 78 days ago
eeckstein eeckstein removed review request from jckarter jckarter 78 days ago
eeckstein eeckstein requested a review from slavapestov slavapestov 78 days ago
eeckstein eeckstein requested a review from DougGregor DougGregor 78 days ago
eeckstein
eeckstein78 days ago

@swift-ci smoke test

DougGregor
DougGregor commented on 2025-04-04
lib/SIL/Utils/DynamicCasts.cpp
117117 // requirements were satisfied.
118 if (checkConformance(source, TargetProtocol))
118 if (auto conformance = checkConformance(source, TargetProtocol)) {
119 if (conformance.isConcrete()) {
120
if (!matchesActorIsolation(conformance.getConcrete(), function))
121
return DynamicCastFeasibility::MaySucceed;
DougGregor78 days ago👍 1

You also need to check whether any conditional requirements have been satisfied by isolated conformances here. ProtocolConformanceRef::forEachIsolatedConformance knows how to enumerate all of the isolated conformances, including those satisfying conditional requirements. You could build on that determine the isolation of a given ProtocolConformanceRef and use that instead of the isConcrete check.

lib/SIL/Utils/DynamicCasts.cpp
881888 return DynamicCastFeasibility::WillFail;
882889}
883890
891
bool swift::matchesActorIsolation(ProtocolConformance *conformance, SILFunction *inFunction) {
DougGregor78 days ago👍 1

Per the previous comment, I recommend passing a ProtocolConformanceRef in here so the callers don't do the isConcrete/getConcrete dance.

eeckstein eeckstein force pushed from e61d17a6 to 24af1228 78 days ago
eeckstein
eeckstein78 days ago

@swift-ci smoke test

DougGregor
DougGregor approved these changes on 2025-04-04
DougGregor78 days ago

Looks great with these changes

eeckstein SIL: add `matchesActorIsolation`
9df10393
eeckstein CastOptimizer: pass the SIL function to the cast-classify APIs, inste…
e18bedaa
eeckstein CastOptimizer: handle isolated conformances
75a0cc03
eeckstein Simplification: check for isolated conformances when simplifying meta…
f5ca12d4
eeckstein eeckstein force pushed from 24af1228 to f5ca12d4 77 days ago
eeckstein
eeckstein77 days ago

@swift-ci smoke test

eeckstein eeckstein merged 98739a41 into main 76 days ago
eeckstein eeckstein deleted the isolated-conformance-opt branch 76 days ago

Login to write a write a comment.

Login via GitHub

Assignees
No one assigned
Labels
Milestone