From 82d9e4cb5906bae07619a6842e3742d3d491820f Mon Sep 17 00:00:00 2001 From: John McCall Date: Thu, 11 Apr 2024 17:22:10 -0400 Subject: [PATCH] Fix a crash when serializing variadic generic tuple code under -wmo It seems really unfortunate that we use SILCloner to, basically, implement a recursive visitor of the types used in a SIL function, but apparently it's what we do. Fixes #72117. --- include/swift/SIL/SILCloner.h | 8 ++++++-- .../Inputs/cross-module/cross-module.swift | 10 ++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/include/swift/SIL/SILCloner.h b/include/swift/SIL/SILCloner.h index 48f1481ea10f2..60a5c8ee3f42f 100644 --- a/include/swift/SIL/SILCloner.h +++ b/include/swift/SIL/SILCloner.h @@ -293,14 +293,18 @@ class SILCloner : protected SILInstructionVisitor { continue; } - // Substitute the shape class of the expansion. + // Substitute the shape class of the expansion. If this doesn't + // give us a pack (e.g. if this isn't a substituting clone), + // we're never erasing tuple structure. auto newShapeClass = getOpASTType(expansion.getCountType()); auto newShapePack = dyn_cast(newShapeClass); + if (!newShapePack) + return false; // If the element has a name, then the tuple sticks around unless // the expansion disappears completely. if (type->getElement(index).hasName()) { - if (newShapePack && newShapePack->getNumElements() == 0) + if (newShapePack->getNumElements() == 0) continue; return false; } diff --git a/test/SILOptimizer/Inputs/cross-module/cross-module.swift b/test/SILOptimizer/Inputs/cross-module/cross-module.swift index 69107cc1ae106..1b657b0622e00 100644 --- a/test/SILOptimizer/Inputs/cross-module/cross-module.swift +++ b/test/SILOptimizer/Inputs/cross-module/cross-module.swift @@ -295,3 +295,13 @@ public func getEmptySet() -> Set { return Set() } +public protocol Visitable { + func visit() +} +@available(SwiftStdlib 6.0, *) +public struct S { + var storage: (repeat each T) + public func visit() { + _ = (repeat (each storage).visit()) + } +}