From 32ca35cb64df028bda90dfe9d42ab8c8e146bab9 Mon Sep 17 00:00:00 2001 From: Andrew Trick Date: Thu, 4 Jul 2024 23:43:45 -0700 Subject: [PATCH] Fix SILBridging for GlobalAddr_getDecl and RefElementAddr_getDecl. Fixes: Assertion failed: (isa(Val) && "cast() argument of incompatible type!"), function cast, file Casting.h While running pass #224 SILFunctionTransform "LifetimeDependenceDiagnostics" #7 0x0000000102d3efcc decltype(auto) llvm::cast(swift::SILInstruction*) #8 0x0000000102d01e24 swift::DebugValueInst* BridgedInstruction::getAs() const #9 0x0000000102d01ee4 BridgedInstruction::GlobalAddr_getDecl() const --- include/swift/SIL/SILBridgingImpl.h | 4 ++-- .../lifetime_dependence_borrow_fail.swift | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/include/swift/SIL/SILBridgingImpl.h b/include/swift/SIL/SILBridgingImpl.h index d0aa56029608c..ce7f353ac620e 100644 --- a/include/swift/SIL/SILBridgingImpl.h +++ b/include/swift/SIL/SILBridgingImpl.h @@ -1362,11 +1362,11 @@ BridgedNullableVarDecl BridgedInstruction::AllocBox_getDecl() const { } BridgedNullableVarDecl BridgedInstruction::GlobalAddr_getDecl() const { - return {getAs()->getDecl()}; + return {getAs()->getReferencedGlobal()->getDecl()}; } BridgedNullableVarDecl BridgedInstruction::RefElementAddr_getDecl() const { - return {getAs()->getDecl()}; + return {getAs()->getField()}; } OptionalBridgedSILDebugVariable diff --git a/test/SILOptimizer/lifetime_dependence/lifetime_dependence_borrow_fail.swift b/test/SILOptimizer/lifetime_dependence/lifetime_dependence_borrow_fail.swift index d05f469194ada..03387dea2b3d5 100644 --- a/test/SILOptimizer/lifetime_dependence/lifetime_dependence_borrow_fail.swift +++ b/test/SILOptimizer/lifetime_dependence/lifetime_dependence_borrow_fail.swift @@ -64,3 +64,9 @@ func bv_incorrect_annotation2(_ w1: borrowing Wrapper, _ w2: borrowing Wrapper) return w1.bv // expected-note @-1{{it depends on the lifetime of argument 'w1'}} } // expected-note @-1{{this use causes the lifetime-dependent value to escape}} +let ptr = UnsafeRawPointer(bitPattern: 1)! +let nc = NC(ptr, 0) // expected-error {{lifetime-dependent variable 'nc' escapes its scope}} + +func bv_global(dummy: BV) -> BV { + nc.getBV() +} // expected-note {{this use causes the lifetime-dependent value to escape}}