Skip to content

Commit 225360b

Browse files
committed
Allow us to find stdlib types in the current file when there's no Swift library
Some type checker invariants require that certain types like Error or Never exist in the standard library. When the standard library isn't loaded, we can get crashes. This only happens under `-parse-stdlib`, which is used in some test cases. Allow lookup for standard library types to look into the main module in such cases, and add the appropriate declarations to these tests.
1 parent df4efd4 commit 225360b

15 files changed

+52
-1
lines changed

lib/AST/ASTContext.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,10 @@ DECL_CLASS *ASTContext::get##NAME##Decl() const { \
940940
if (getImpl().NAME##Decl) \
941941
return getImpl().NAME##Decl; \
942942
SmallVector<ValueDecl *, 1> results; \
943-
lookupInSwiftModule(#NAME, results); \
943+
if (auto stdlibModule = getStdlibModule()) \
944+
lookupInModule(stdlibModule, #NAME, results); \
945+
else \
946+
lookupInModule(MainModule, #NAME, results); \
944947
for (auto result : results) { \
945948
if (auto type = dyn_cast<DECL_CLASS>(result)) { \
946949
auto params = type->getGenericParams(); \
@@ -1169,6 +1172,12 @@ ProtocolDecl *ASTContext::getProtocol(KnownProtocolKind kind) const {
11691172
break;
11701173
default:
11711174
M = getStdlibModule();
1175+
1176+
// Fall back to looking into the current module for these types.
1177+
// This only happens with -parse-stdlib when the Swift module isn't
1178+
// imported, e.g., in test cases.
1179+
if (!M)
1180+
M = MainModule;
11721181
break;
11731182
}
11741183

test/IRGen/dllimport.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
// REQUIRES: CODEGENERATOR=ARM
55

6+
protocol Error { }
7+
enum Never { }
8+
69
import dllexport
710

811
public func get_ci() -> dllexport.c {

test/IRGen/ivar_destroyer.sil

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
// REQUIRES: CPU=x86_64
44

5+
protocol Error { }
6+
enum Never { }
7+
58
// CHECK: [[OBJCCLASS:%objc_class]] = type
69
// CHECK: [[OPAQUE:%swift.opaque]] = type opaque
710
// CHECK: [[TYPE:%swift.type]] = type

test/IRGen/protocol_synthesized.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,6 @@ public protocol ExpressibleByArrayLiteral {
302302

303303
public protocol Equatable {
304304
}
305+
306+
protocol Error { }
307+
enum Never { }

test/IRGen/vtable_non_overridden.sil

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ class InternalA {
1010
func bas()
1111
}
1212

13+
protocol Error { }
14+
enum Never { }
15+
1316
sil @InternalA_foo : $@convention(method) (@guaranteed InternalA) -> ()
1417
sil @InternalA_bar : $@convention(method) (@guaranteed InternalA) -> ()
1518
sil @InternalA_bas : $@convention(method) (@guaranteed InternalA) -> ()

test/SIL/OwnershipVerifier/definite_init.sil

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ struct Bool {
1414
var value: Builtin.Int1
1515
}
1616

17+
protocol Error { }
18+
enum Never { }
19+
1720
// This file is meant to contain tests that previously the verifier treated
1821
// incorrectly. This is important to ensure that the verifier does not
1922
// regress. It should only deal with use matching in a DI context.

test/SIL/OwnershipVerifier/objc_use_verifier.sil

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
class Protocol : NSBurrito {}
88

9+
protocol Error { }
10+
enum Never { }
11+
912
sil [ossa] @test_objc_protocol : $@convention(thin) () -> @owned Protocol {
1013
bb0:
1114
%0 = objc_protocol #NSBurrito : $Protocol

test/SIL/Serialization/vtable.sil

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ sil_stage canonical
66

77
import Builtin
88

9+
protocol Error { }
10+
enum Never { }
11+
912
public class Base {
1013
func m1()
1114
func m2()

test/SILGen/auto_closures.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11

22
// RUN: %target-swift-emit-silgen -module-name auto_closures -parse-stdlib -swift-version 5 %s | %FileCheck %s
33

4+
protocol Error { }
5+
enum Never { }
6+
47
struct Bool {}
58
var false_ = Bool()
69

test/SILOptimizer/basic-callee-printer.sil

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
sil_stage canonical
55

6+
protocol Error { }
7+
enum Never { }
8+
69
sil private @private_bottom : $@convention(thin) () -> () {
710
bb0:
811
%0 = tuple ()

test/SILOptimizer/function_order.sil

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ sil_stage canonical
44

55
import Builtin
66

7+
protocol Error { }
8+
enum Never { }
9+
710
// CHECK: Bottom up function order:
811
// CHECK-NEXT: private_bottom
912
// CHECK-NEXT: private_middle

test/SILOptimizer/prune-vtables.sil

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
sil_stage canonical
55

6+
protocol Error { }
7+
enum Never { }
8+
69
private class PrivateA {
710
func noOverrides() {}
811
func yesOverrides() {}

test/SILOptimizer/rcidentity.sil

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import Builtin
44

5+
protocol Error { }
6+
enum Never { }
7+
58
///////////////////////
69
// NonTest Utilities //
710
///////////////////////

test/SILOptimizer/typed-access-tb-aa.sil

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
import Builtin
66

7+
protocol Error { }
8+
enum Never { }
9+
710
///////////////////////
811
// Type Declarations //
912
///////////////////////

test/embedded/classes-methods-no-stdlib.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ public class MySubClass: MyClass {
1111
override func foo() { }
1212
}
1313

14+
protocol Error { }
15+
enum Never { }
16+
1417
// CHECK: @"$s4main7MyClassCN" = {{.*}}<{ ptr, ptr, ptr, ptr, ptr }> <{ ptr null, ptr @"$s4main7MyClassCfD", ptr @"$s4main7MyClassC3fooyyF", ptr @"$s4main7MyClassC3baryyF", ptr @swift_deletedMethodError }>
1518
// CHECK: @"$s4main10MySubClassCN" = {{.*}}<{ ptr, ptr, ptr, ptr, ptr }> <{ ptr @"$s4main7MyClassCN", ptr @"$s4main10MySubClassCfD", ptr @"$s4main10MySubClassC3fooyyF", ptr @"$s4main7MyClassC3baryyF", ptr @"$s4main10MySubClassCACycfC" }>
1619

0 commit comments

Comments
 (0)