Skip to content

Commit 7b2d712

Browse files
committed
[SILGen] do not clear parent module when emitting stored initializer function
When SILGen was emitting the stored initializer function, it updated the location to a new autogenerated location tied to the initialization expression. That in turn reset the function's parent module to null, as the initialization expression is not a decl, and thus it didn't produce a decl context SIL could use when looking for the parent module. That lead to a this function having no parent module when it was serialized, and thus a user that used this function from code in another Swift module thought that this function should use 'dllimport' on Windows, even when the parent module was built with as the static library (i.e. using the -static flag). This lead to spurious LNK4217 diagnostics when building the Swift package manager using CMake on windows. This change fixes that by preserving the parent module for such function.
1 parent daebcc3 commit 7b2d712

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

lib/SILGen/SILGen.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,13 +967,20 @@ void SILGenModule::emitFunctionDefinition(SILDeclRef constant, SILFunction *f) {
967967
auto *init = constant.getInitializationExpr();
968968
assert(init);
969969

970+
auto *parentMod = f->getParentModule();
970971
auto loc = RegularLocation::getAutoGeneratedLocation(init);
971972
preEmitFunction(constant, f, loc);
972973
PrettyStackTraceSILFunction X("silgen emitStoredPropertyInitialization", f);
973974
f->createProfiler(constant);
974975
SILGenFunction SGF(*this, *f, initDC);
975976
SGF.emitGeneratorFunction(constant, init, /*EmitProfilerIncrement=*/true);
976977
postEmitFunction(constant, f);
978+
// Ensure that the SIL function has a module associated with it. This
979+
// ensures that SIL serializer serializes the module id for this function
980+
// correctly. The parent module can be reset when the function's location is
981+
// updated to the autogenerated location above.
982+
if (!f->getParentModule())
983+
f->setParentModule(parentMod);
977984
break;
978985
}
979986

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: split-file %s %t
3+
4+
// RUN: %target-swift-frontend -c %t/inlinedStruct.swift -static -O -parse-as-library -module-name InlinedStructs -emit-module-path %t/InlinedStructs.swiftmodule -o %t/inlinedStruct.swift.o
5+
// RUN: %target-swift-frontend -c -emit-ir -O %t/use.swift -I %t -o %t/use.swift.ir
6+
// RUN: cat %t/use.swift.ir | %FileCheck %s
7+
8+
// RUN: rm -rf %t/InlinedStructs.swiftmodule
9+
// RUN: %target-swift-frontend -c %t/inlinedStruct.swift -O -parse-as-library -module-name InlinedStructs -emit-module-path %t/InlinedStructs.swiftmodule -o %t/inlinedStruct.swift.o
10+
// RUN: %target-swift-frontend -c -emit-ir -O %t/use.swift -I %t -o %t/use.swift.ir
11+
// RUN: cat %t/use.swift.ir | %FileCheck --check-prefix=DLLIMPORT %s
12+
13+
// REQUIRES: OS=windows-msvc
14+
15+
//--- inlinedStruct.swift
16+
17+
@usableFromInline
18+
struct CMSSignedData {
19+
@usableFromInline var field: Bool?
20+
21+
@inlinable
22+
init(field: Bool?) {
23+
self.field = field
24+
}
25+
}
26+
27+
public struct TestS {
28+
@usableFromInline
29+
let x: CMSSignedData = CMSSignedData(field: false)
30+
31+
@inlinable
32+
public init() { }
33+
34+
@inlinable
35+
public var field: Bool {
36+
return x.field!
37+
}
38+
}
39+
40+
//--- use.swift
41+
42+
import InlinedStructs
43+
44+
public struct TestTwo {
45+
let field: TestS = TestS()
46+
}
47+
48+
public func testTwo() -> Bool {
49+
let x = TestTwo()
50+
return x.field.field
51+
}
52+
53+
// Ensure that the variable initialization expression is not dllimported on Windows.
54+
// CHECK: declare swiftcc i8 @"$s14InlinedStructs5TestSV1xAA13CMSSignedDataVvpfi"()
55+
// DLLIMPORT: declare dllimport swiftcc i8 @"$s14InlinedStructs5TestSV1xAA13CMSSignedDataVvpfi"()

0 commit comments

Comments
 (0)