Skip to content

Commit 3f36309

Browse files
jrose-appletkremenek
authored andcommitted
[SIL] Don't assume we always have an associated DeclContext. (#2730) (#2735)
...in code that I wrote. The integrated REPL, deprecated though it may be, does not have an associated DeclContext because its SourceFile is not considered complete. (The proper LLDB REPL does not suffer from this problem because they use a new SourceFile for every block of input.) Elsewhere, tighten up code that may have hit similar bugs, though we haven't seen anything hit these yet. rdar://problem/26476281 (cherry picked from commit 740ddd8)
1 parent c1b8a1a commit 3f36309

File tree

5 files changed

+18
-5
lines changed

5 files changed

+18
-5
lines changed

lib/SILGen/SILGenExpr.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2304,9 +2304,11 @@ static bool isNullableTypeInC(SILModule &M, Type ty) {
23042304
return true;
23052305

23062306
// Other types like UnsafePointer can also be nullable.
2307+
const DeclContext *DC = M.getAssociatedContext();
2308+
if (!DC)
2309+
DC = M.getSwiftModule();
23072310
ty = OptionalType::get(ty);
2308-
return ty->isTriviallyRepresentableIn(ForeignLanguage::C,
2309-
M.getAssociatedContext());
2311+
return ty->isTriviallyRepresentableIn(ForeignLanguage::C, DC);
23102312
}
23112313

23122314
/// Determine whether the given declaration returns a non-optional object that

lib/SILOptimizer/IPO/DeadFunctionElimination.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,7 @@ class FunctionLivenessComputation {
217217
// If a vtable or witness table (method) is only visible in another module
218218
// it can be accessed inside that module and we don't see this access.
219219
// We hit this case e.g. if a table is imported from the stdlib.
220-
if (decl->getDeclContext()->getParentModule() !=
221-
Module->getAssociatedContext()->getParentModule())
220+
if (decl->getDeclContext()->getParentModule() != Module->getSwiftModule())
222221
return true;
223222

224223
return false;

lib/SILOptimizer/IPO/LetPropertiesOpts.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ static bool isAssignableExternally(VarDecl *Property, SILModule *Module) {
338338
// be analyzed by this pass.
339339
static bool mayHaveUnknownUses(VarDecl *Property, SILModule *Module) {
340340
if (Property->getDeclContext()->getParentModule() !=
341-
Module->getAssociatedContext()->getParentModule()) {
341+
Module->getSwiftModule()) {
342342
DEBUG(llvm::dbgs() << "Property " << *Property
343343
<< " is defined in a different module\n");
344344
// We don't see the bodies of initializers from a different module

lib/Serialization/SerializeSIL.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1817,6 +1817,7 @@ void SILSerializer::writeSILBlock(const SILModule *SILMod) {
18171817
// serialize everything.
18181818
// FIXME: Resilience: could write out vtable for fragile classes.
18191819
const DeclContext *assocDC = SILMod->getAssociatedContext();
1820+
assert(assocDC && "cannot serialize SIL without an associated DeclContext");
18201821
for (const SILVTable &vt : SILMod->getVTables()) {
18211822
if (ShouldSerializeAll &&
18221823
vt.getClass()->isChildContextOf(assocDC))

test/Interpreter/SDK/Cocoa_repl.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,14 @@ extension CGRect: Q {
1818

1919
(CGRect() as Any as! Q).foo()
2020
// CHECK: (0.0, 0.0, 0.0, 0.0)
21+
22+
// Test the "mayLieAboutNonOptionalReturn" hack for both imported and
23+
// non-imported types.
24+
struct Empty {}
25+
let _: Optional = Empty()
26+
// CHECK: Optional(REPL.Empty())
27+
let _: Optional = CGPoint.zero
28+
// CHECK: Optional((0.0, 0.0))
29+
let _: Optional = NSString.availableStringEncodings()
30+
// CHECK: Optional(0x{{[0-9a-fA-F]+}})
31+

0 commit comments

Comments
 (0)