Skip to content

Commit 646a3a5

Browse files
committed
[Sema] Avoid computing raw values for enum cases in swiftinterface files
Raw values of enum cases from another module are not specified in the declaration of the enum unless that enum is `@objc`. This meant that `EnumRawValuesRequest` was computing potentially incorrect raw values when the enum declaration supported it and was emitting incorrect diagnostics for other enum decls. Resolves SR-14355 and rdar://75451691
1 parent 7dc36c3 commit 646a3a5

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ func testFrozenObjCEnum() -> FrozenObjCEnum {
4242
return .b
4343
} // CHECK-NEXT: {{^}$}}
4444

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

0 commit comments

Comments
 (0)