Skip to content

Commit 5f9fe6f

Browse files
committed
SILGen: Fix crash with non-scalar casts requiring re-abstraction
This might only come up in invalid code, but for example casting a function type to String would trigger it.
1 parent 4daf56b commit 5f9fe6f

File tree

4 files changed

+13
-17
lines changed

4 files changed

+13
-17
lines changed

lib/SILGen/SILGenDynamicCast.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -357,20 +357,18 @@ adjustForConditionalCheckedCastOperand(SILLocation loc, ManagedValue src,
357357
}
358358

359359
std::unique_ptr<TemporaryInitialization> init;
360-
SGFContext ctx;
361360
if (requiresAddress) {
362361
init = SGF.emitTemporary(loc, srcAbstractTL);
363-
362+
363+
if (hasAbstraction)
364+
src = SGF.emitSubstToOrigValue(loc, src, abstraction, sourceType);
365+
364366
// Okay, if all we need to do is drop the value in an address,
365367
// this is easy.
366-
if (!hasAbstraction) {
367-
SGF.B.emitStoreValueOperation(loc, src.forward(SGF), init->getAddress(),
368-
StoreOwnershipQualifier::Init);
369-
init->finishInitialization(SGF);
370-
return init->getManagedAddress();
371-
}
372-
373-
ctx = SGFContext(init.get());
368+
SGF.B.emitStoreValueOperation(loc, src.forward(SGF), init->getAddress(),
369+
StoreOwnershipQualifier::Init);
370+
init->finishInitialization(SGF);
371+
return init->getManagedAddress();
374372
}
375373

376374
assert(hasAbstraction);

validation-test/compiler_crashers_2/0038-lowering-CheckedCastEmitter-emitConditional.swift

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// RUN: %target-swift-frontend %s -emit-silgen
2+
3+
let a: () -> Int? = { return nil }
4+
a as? Int

validation-test/compiler_crashers/28420-swift-lowering-emitconditionalcheckedcast.swift renamed to validation-test/compiler_crashers_fixed/28420-swift-lowering-emitconditionalcheckedcast.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// See https://swift.org/LICENSE.txt for license information
66
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

8-
// RUN: not --crash %target-swift-frontend %s -emit-ir
8+
// RUN: not %target-swift-frontend %s -emit-ir
99
// REQUIRES: asserts
1010
struct A{
1111
func a()->String{

0 commit comments

Comments
 (0)