Skip to content

Commit 2517701

Browse files
committed
SILGen: Emit local types with a separate pass instead of while walking statements
Otherwise we might miss emitting a local type that's inside unreachable code. Normally such a type cannot be found via name lookup either, but IRGen will walk the list of local types and try to emit a class with no SIL vtable, which will crash. Fixes <https://bugs.swift.org/browse/SR-1924>.
1 parent ad01c1e commit 2517701

File tree

6 files changed

+23
-10
lines changed

6 files changed

+23
-10
lines changed

lib/SILGen/SILGen.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,6 +1408,9 @@ void SILGenModule::emitSourceFile(SourceFile *sf, unsigned startElem) {
14081408
for (Decl *D : llvm::makeArrayRef(sf->Decls).slice(startElem))
14091409
visit(D);
14101410

1411+
for (Decl *D : sf->LocalTypeDecls)
1412+
visit(D);
1413+
14111414
// Mark any conformances as "used".
14121415
for (auto conformance : sf->getUsedConformances())
14131416
useConformance(ProtocolConformanceRef(conformance));

lib/SILGen/SILGenFunction.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1545,7 +1545,6 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
15451545
llvm_unreachable("Not yet implemented");
15461546
}
15471547

1548-
void visitNominalTypeDecl(NominalTypeDecl *D);
15491548
void visitFuncDecl(FuncDecl *D);
15501549
void visitPatternBindingDecl(PatternBindingDecl *D);
15511550

@@ -1554,6 +1553,10 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
15541553
std::unique_ptr<Initialization>
15551554
emitPatternBindingInitialization(Pattern *P, JumpDest failureDest);
15561555

1556+
void visitNominalTypeDecl(NominalTypeDecl *D) {
1557+
// No lowering support needed.
1558+
}
1559+
15571560
void visitTypeAliasDecl(TypeAliasDecl *D) {
15581561
// No lowering support needed.
15591562
}

lib/SILGen/SILGenType.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -417,10 +417,6 @@ void SILGenModule::visitNominalTypeDecl(NominalTypeDecl *ntd) {
417417
SILGenType(*this, ntd).emitType();
418418
}
419419

420-
void SILGenFunction::visitNominalTypeDecl(NominalTypeDecl *ntd) {
421-
SILGenType(SGM, ntd).emitType();
422-
}
423-
424420
/// SILGenExtension - an ASTVisitor for generating SIL from method declarations
425421
/// and protocol conformances inside type extensions.
426422
class SILGenExtension : public TypeMemberVisitor<SILGenExtension> {

test/SILGen/local_types.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %target-swift-frontend -parse-as-library -emit-silgen %s -verify | %FileCheck %s
2+
// RUN: %target-swift-frontend -parse-as-library -emit-ir %s
3+
4+
func function() {
5+
return
6+
7+
class UnreachableClass {} // expected-warning {{code after 'return' will never be executed}}
8+
}
9+
10+
// CHECK-LABEL: sil_vtable UnreachableClass

test/SILGen/mangling_private.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ private struct PrivateStruct {
4040

4141
func localTypes() {
4242
struct LocalStruct {
43-
// CHECK-LABEL: sil shared @_TZFVF16mangling_private10localTypesFT_T_L_11LocalStruct13privateMethodfT_T_
4443
private static func privateMethod() {}
4544
}
4645
}
@@ -54,6 +53,7 @@ extension PrivateStruct {
5453
private func extPrivateMethod() {}
5554
}
5655

56+
// CHECK-LABEL: sil shared @_TZFVF16mangling_private10localTypesFT_T_L_11LocalStruct13privateMethodfT_T_
5757

5858
// CHECK-LABEL: sil_vtable Sub {
5959
class Sub : Base {

test/SILGen/types.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,24 @@ struct S {
3333
}
3434

3535
class SC {
36-
// CHECK-LABEL: sil hidden @_TFCV5types1S2SC3bar{{.*}}
36+
// CHECK-LABEL: sil hidden @_TFCV5types1S2SC3bar{{.*}}
3737
func bar() {}
3838
}
3939
}
4040

4141
func f() {
4242
class FC {
43-
// CHECK-LABEL: sil shared @_TFCF5types1fFT_T_L_2FC3zim{{.*}}
4443
func zim() {}
4544
}
4645
}
4746

4847
func g(b b : Bool) {
4948
if (b) {
5049
class FC {
51-
// CHECK-LABEL: sil shared @_TFCF5types1gFT1bSb_T_L_2FC3zim{{.*}}
5250
func zim() {}
5351
}
5452
} else {
5553
class FC {
56-
// CHECK-LABEL: sil shared @_TFCF5types1gFT1bSb_T_L0_2FC3zim{{.*}}
5754
func zim() {}
5855
}
5956
}
@@ -94,3 +91,7 @@ func referencedFromFunctionEnumFields(_ x: ReferencedFromFunctionEnum)
9491
return (nil, g)
9592
}
9693
}
94+
95+
// CHECK-LABEL: sil shared @_TFCF5types1fFT_T_L_2FC3zim{{.*}}
96+
// CHECK-LABEL: sil shared @_TFCF5types1gFT1bSb_T_L_2FC3zim{{.*}}
97+
// CHECK-LABEL: sil shared @_TFCF5types1gFT1bSb_T_L0_2FC3zim{{.*}}

0 commit comments

Comments
 (0)