Skip to content

Commit 3d93d8a

Browse files
authored
Merge pull request #41009 from tshortli/no-automatic-enum-raw-values-for-swiftinterfaces
[Sema] Avoid computing raw values for enum cases in swiftinterface files
2 parents dead95f + 49e351f commit 3d93d8a

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

lib/Sema/TypeCheckDecl.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,14 @@ EnumRawValuesRequest::evaluate(Evaluator &eval, EnumDecl *ED,
10941094
if (!rawTy) {
10951095
return std::make_tuple<>();
10961096
}
1097+
1098+
// Avoid computing raw values for enum cases in swiftinterface files since raw
1099+
// values are intentionally omitted from them (unless the enum is @objc).
1100+
// Without bailing here, incorrect raw values can be automatically generated
1101+
// and incorrect diagnostics may be omitted for some decls.
1102+
SourceFile *Parent = ED->getDeclContext()->getParentSourceFile();
1103+
if (Parent && Parent->Kind == SourceFileKind::Interface && !ED->isObjC())
1104+
return std::make_tuple<>();
10971105

10981106
if (!computeAutomaticEnumValueKind(ED)) {
10991107
return std::make_tuple<>();

test/ModuleInterface/Inputs/enums-layout-helper.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ public enum FutureproofEnum: Int {
4646
case d
4747
}
4848

49+
// CHECK-LABEL: public enum FutureproofUnicodeScalarEnum : Swift.Unicode.Scalar
50+
public enum FutureproofUnicodeScalarEnum: Unicode.Scalar {
51+
// CHECK-NEXT: case a{{$}}
52+
case a = "A"
53+
}
54+
4955
// CHECK-LABEL: indirect public enum FutureproofIndirectEnum
5056
public indirect enum FutureproofIndirectEnum {
5157
// CHECK-NEXT: case a{{$}}

test/ModuleInterface/enums-layout.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
// RUN: %empty-directory(%t)
22
// RUN: %target-build-swift -emit-module-interface-path %t/Lib.swiftinterface -emit-module -o %t/unused.swiftmodule -enable-library-evolution -Xfrontend -enable-objc-interop -Xfrontend -disable-objc-attr-requires-foundation-module -swift-version 5 %S/Inputs/enums-layout-helper.swift -module-name Lib
33
// RUN: %FileCheck -check-prefix CHECK -check-prefix CHECK-MULTI-FILE %S/Inputs/enums-layout-helper.swift < %t/Lib.swiftinterface
4+
// RUN: %target-swift-frontend -enable-objc-interop -compile-module-from-interface %t/Lib.swiftinterface -o %t/compiled-from-interface.swiftmodule -module-name Lib
45
// RUN: %target-swift-frontend -enable-objc-interop -O -emit-ir -primary-file %s -I %t -Xllvm -swiftmergefunc-threshold=0 | %FileCheck %s
56

67
// Try again using a single-frontend build.
78
// RUN: %empty-directory(%t)
89
// RUN: %target-build-swift -whole-module-optimization -emit-module-interface-path %t/Lib.swiftinterface -emit-module -o %t/unused.swiftmodule -enable-library-evolution -Xfrontend -enable-objc-interop -Xfrontend -disable-objc-attr-requires-foundation-module -swift-version 5 %S/Inputs/enums-layout-helper.swift -module-name Lib
910
// RUN: %FileCheck -check-prefix CHECK -check-prefix CHECK-SINGLE-FRONTEND %S/Inputs/enums-layout-helper.swift < %t/Lib.swiftinterface
11+
// RUN: %target-swift-frontend -enable-objc-interop -compile-module-from-interface %t/Lib.swiftinterface -o %t/compiled-from-interface.swiftmodule -module-name Lib
1012
// RUN: %target-swift-frontend -enable-objc-interop -O -emit-ir -primary-file %s -I %t -Xllvm -swiftmergefunc-threshold=0 | %FileCheck %s
1113

1214

@@ -42,6 +44,16 @@ func testFrozenObjCEnum() -> FrozenObjCEnum {
4244
return .b
4345
} // CHECK-NEXT: {{^}$}}
4446

47+
// CHECK-LABEL: define{{.+}}testFutureproofUnicodeScalarEnum
48+
func testFutureproofUnicodeScalarEnum() -> FutureproofUnicodeScalarEnum {
49+
// CHECK: [[CASE:%.+]] = load i32, i32* @"$s3Lib28FutureproofUnicodeScalarEnumO1ayA2CmFWC"
50+
// CHECK: [[METADATA_RESPONSE:%.+]] = tail call swiftcc %swift.metadata_response @"$s3Lib28FutureproofUnicodeScalarEnumOMa"
51+
// CHECK: [[METADATA:%.+]] = extractvalue %swift.metadata_response [[METADATA_RESPONSE]], 0
52+
// CHECK: call void {{%.+}}(%swift.opaque* noalias %0, i32 [[CASE]], %swift.type* [[METADATA]])
53+
// CHECK-NEXT: ret void
54+
return .a
55+
}
56+
4557
// CHECK-LABEL: define{{.+}}testFutureproofIndirectEnum
4658
func testFutureproofIndirectEnum() -> FutureproofIndirectEnum {
4759
// CHECK: [[CASE:%.+]] = load i32, i32* @"$s3Lib23FutureproofIndirectEnumO1cyA2CmFWC"

0 commit comments

Comments
 (0)