Skip to content

Commit dfdd3a7

Browse files
committed
SILGen: Fix bug with NSString import-as-member globals
This is not really the right fix if we want to have physically addressed lvalues support bridging in general, but doing so requires adding a new LValue component type and doing a bunch more refactoring, so just hack in a narrow fix for now since it only seems to occur in one case. Fixes <rdar://problem/34913892>.
1 parent a6444b2 commit dfdd3a7

File tree

3 files changed

+62
-28
lines changed

3 files changed

+62
-28
lines changed

lib/SILGen/SILGenLValue.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3213,6 +3213,9 @@ RValue SILGenFunction::emitLoadOfLValue(SILLocation loc, LValue &&src,
32133213
// Any writebacks should be scoped to after the load.
32143214
FormalEvaluationScope scope(*this);
32153215

3216+
// We shouldn't need to re-abstract here, but we might have to bridge.
3217+
// This should only happen if we have a global variable of NSString type.
3218+
auto origFormalType = src.getOrigFormalType();
32163219
auto substFormalType = src.getSubstFormalType();
32173220
auto &rvalueTL = getTypeLowering(src.getTypeOfRValue());
32183221

@@ -3226,6 +3229,7 @@ RValue SILGenFunction::emitLoadOfLValue(SILLocation loc, LValue &&src,
32263229
.offset(*this, loc, addr, AccessKind::Read);
32273230
return RValue(*this, loc, substFormalType,
32283231
emitLoad(loc, addr.getValue(),
3232+
origFormalType, substFormalType,
32293233
rvalueTL, C, IsNotTake,
32303234
isBaseGuaranteed));
32313235
}

test/IDE/Inputs/custom-modules/ImportAsMemberClass.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,15 @@ UnavailableDefaultInit * _Nonnull MakeUnavailableDefaultInit(void);
3232
__attribute__((swift_name("UnavailableDefaultInitSub.init()")))
3333
UnavailableDefaultInitSub * _Nonnull MakeUnavailableDefaultInitSub(void);
3434

35+
#pragma clang assume_nonnull begin
36+
37+
extern NSString * PKPandaCutenessFactor __attribute__((swift_name("Panda.cutenessFactor")));
38+
extern NSString * _Nullable PKPandaCuddlynessFactor __attribute__((swift_name("Panda.cuddlynessFactor")));
39+
40+
__attribute__((swift_name("Panda")))
41+
@interface PKPanda : NSObject
42+
@end
43+
44+
#pragma clang assume_nonnull end
45+
3546
#endif

test/SILGen/import_as_member.swift

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,64 @@
1-
// RUN: %target-swift-frontend -emit-silgen -I %S/../IDE/Inputs/custom-modules %s 2>&1 | %FileCheck --check-prefix=SIL %s
1+
// RUN: %target-swift-frontend -emit-silgen -I %S/../IDE/Inputs/custom-modules %s | %FileCheck %s
22
// REQUIRES: objc_interop
33
import ImportAsMember.A
44
import ImportAsMember.Class
55

66
public func returnGlobalVar() -> Double {
77
return Struct1.globalVar
88
}
9-
// SIL-LABEL: sil {{.*}}returnGlobalVar{{.*}} () -> Double {
10-
// SIL: %0 = global_addr @IAMStruct1GlobalVar : $*Double
11-
// SIL: [[READ:%.*]] = begin_access [read] [dynamic] %0 : $*Double
12-
// SIL: [[VAL:%.*]] = load [trivial] [[READ]] : $*Double
13-
// SIL: return [[VAL]] : $Double
14-
// SIL-NEXT: }
15-
16-
// SIL-LABEL: sil {{.*}}anchor{{.*}} () -> () {
17-
func anchor() {}
18-
19-
// SIL-LABEL: sil {{.*}}useClass{{.*}}
20-
// SIL: bb0([[D:%[0-9]+]] : $Double, [[OPTS:%[0-9]+]] : $SomeClass.Options):
9+
// CHECK-LABEL: sil {{.*}}returnGlobalVar{{.*}} () -> Double {
10+
// CHECK: %0 = global_addr @IAMStruct1GlobalVar : $*Double
11+
// CHECK: [[READ:%.*]] = begin_access [read] [dynamic] %0 : $*Double
12+
// CHECK: [[VAL:%.*]] = load [trivial] [[READ]] : $*Double
13+
// CHECK: return [[VAL]] : $Double
14+
// CHECK-NEXT: }
15+
16+
// N.B. Whether by design or due to a bug, nullable NSString globals
17+
// still import as non-null.
18+
public func returnStringGlobalVar() -> String {
19+
return Panda.cutenessFactor
20+
}
21+
// CHECK-LABEL: sil {{.*}}returnStringGlobalVar{{.*}} () -> @owned String {
22+
// CHECK: %0 = global_addr @PKPandaCutenessFactor : $*NSString
23+
// CHECK: [[VAL:%.*]] = load [copy] %0 : $*NSString
24+
// CHECK: [[BRIDGE:%.*]] = function_ref @_T0SS10FoundationE36_unconditionallyBridgeFromObjectiveCSSSo8NSStringCSgFZ
25+
// CHECK: [[RESULT:%.*]] = apply [[BRIDGE]](
26+
// CHECK: return [[RESULT]] : $String
27+
// CHECK-NEXT: }
28+
29+
public func returnNullableStringGlobalVar() -> String? {
30+
return Panda.cuddlynessFactor
31+
}
32+
// CHECK-LABEL: sil {{.*}}returnNullableStringGlobalVar{{.*}} () -> @owned Optional<String> {
33+
// CHECK: %0 = global_addr @PKPandaCuddlynessFactor : $*NSString
34+
// CHECK: [[VAL:%.*]] = load [copy] %0 : $*NSString
35+
// CHECK: [[BRIDGE:%.*]] = function_ref @_T0SS10FoundationE36_unconditionallyBridgeFromObjectiveCSSSo8NSStringCSgFZ
36+
// CHECK: [[RESULT:%.*]] = apply [[BRIDGE]](
37+
// CHECK: [[SOME:%.*]] = enum $Optional<String>, #Optional.some!enumelt.1, [[RESULT]]
38+
// CHECK: return [[SOME]] : $Optional<String>
39+
// CHECK-NEXT: }
40+
41+
// CHECK-LABEL: sil {{.*}}useClass{{.*}}
42+
// CHECK: bb0([[D:%[0-9]+]] : $Double, [[OPTS:%[0-9]+]] : $SomeClass.Options):
2143
public func useClass(d: Double, opts: SomeClass.Options) {
22-
// SIL: [[CTOR:%[0-9]+]] = function_ref @MakeIAMSomeClass : $@convention(c) (Double) -> @autoreleased SomeClass
23-
// SIL: [[OBJ:%[0-9]+]] = apply [[CTOR]]([[D]])
44+
// CHECK: [[CTOR:%[0-9]+]] = function_ref @MakeIAMSomeClass : $@convention(c) (Double) -> @autoreleased SomeClass
45+
// CHECK: [[OBJ:%[0-9]+]] = apply [[CTOR]]([[D]])
2446
let o = SomeClass(value: d)
2547

26-
// SIL: [[APPLY_FN:%[0-9]+]] = function_ref @IAMSomeClassApplyOptions : $@convention(c) (SomeClass, SomeClass.Options) -> ()
27-
// SIL: [[BORROWED_OBJ:%.*]] = begin_borrow [[OBJ]]
28-
// SIL: apply [[APPLY_FN]]([[BORROWED_OBJ]], [[OPTS]])
29-
// SIL: end_borrow [[BORROWED_OBJ]] from [[OBJ]]
30-
// SIL: destroy_value [[OBJ]]
48+
// CHECK: [[APPLY_FN:%[0-9]+]] = function_ref @IAMSomeClassApplyOptions : $@convention(c) (SomeClass, SomeClass.Options) -> ()
49+
// CHECK: [[BORROWED_OBJ:%.*]] = begin_borrow [[OBJ]]
50+
// CHECK: apply [[APPLY_FN]]([[BORROWED_OBJ]], [[OPTS]])
51+
// CHECK: end_borrow [[BORROWED_OBJ]] from [[OBJ]]
52+
// CHECK: destroy_value [[OBJ]]
3153
o.applyOptions(opts)
3254
}
3355

3456
extension SomeClass {
35-
// SIL-LABEL: sil hidden @_T0So9SomeClassC16import_as_memberEABSd6double_tcfc
36-
// SIL: bb0([[DOUBLE:%[0-9]+]] : $Double
37-
// SIL-NOT: value_metatype
38-
// SIL: [[FNREF:%[0-9]+]] = function_ref @MakeIAMSomeClass
39-
// SIL: apply [[FNREF]]([[DOUBLE]])
57+
// CHECK-LABEL: sil hidden @_T0So9SomeClassC16import_as_memberEABSd6double_tcfc
58+
// CHECK: bb0([[DOUBLE:%[0-9]+]] : $Double
59+
// CHECK-NOT: value_metatype
60+
// CHECK: [[FNREF:%[0-9]+]] = function_ref @MakeIAMSomeClass
61+
// CHECK: apply [[FNREF]]([[DOUBLE]])
4062
convenience init(double: Double) {
4163
self.init(value: double)
4264
}
@@ -47,6 +69,3 @@ public func useSpecialInit() -> Struct1 {
4769
// return Struct1(specialLabel:())
4870
}
4971

50-
public func useGlobal() -> Double {
51-
return Struct1.globalVar
52-
}

0 commit comments

Comments
 (0)