Skip to content

Commit 10d672e

Browse files
authored
Merge pull request swiftlang#70249 from apple/ApolloZhu/const-extract/attribute-on-all-nominals
[Compile Time Constant Extraction] Allow attribute on all nominal decls
2 parents 69d1c16 + a8fd837 commit 10d672e

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

include/swift/AST/Attr.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ SIMPLE_DECL_ATTR(_staticExclusiveOnly, StaticExclusiveOnly,
534534
OnStruct | UserInaccessible | ABIStableToAdd | ABIStableToRemove | APIBreakingToAdd | APIStableToRemove,
535535
151)
536536
SIMPLE_DECL_ATTR(extractConstantsFromMembers, ExtractConstantsFromMembers,
537-
OnProtocol | UserInaccessible | ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,
537+
OnClass | OnEnum | OnProtocol | OnStruct | UserInaccessible | ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,
538538
152)
539539
SIMPLE_DECL_ATTR(_noRuntime, NoRuntime,
540540
OnAbstractFunction | OnSubscript | UserInaccessible | ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,

lib/ConstExtract/ConstExtract.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,17 @@ class NominalTypeConformanceCollector : public ASTWalker {
5858
if (auto *ETD = dyn_cast<ExtensionDecl>(D))
5959
NTD = ETD->getExtendedNominal();
6060
if (NTD)
61-
if (!isa<ProtocolDecl>(NTD) && CheckedDecls.insert(NTD).second)
61+
if (!isa<ProtocolDecl>(NTD) && CheckedDecls.insert(NTD).second) {
62+
if (NTD->getAttrs().hasAttribute<ExtractConstantsFromMembersAttr>()) {
63+
ConformanceTypeDecls.push_back(NTD);
64+
return Action::Continue();
65+
}
66+
6267
for (auto &Protocol : NTD->getAllProtocols())
6368
if (Protocol->getAttrs().hasAttribute<ExtractConstantsFromMembersAttr>() ||
6469
Protocols.count(Protocol->getName().str().str()) != 0)
6570
ConformanceTypeDecls.push_back(NTD);
71+
}
6672
return Action::Continue();
6773
}
6874

lib/Serialization/ModuleFormat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
5858
/// describe what change you made. The content of this comment isn't important;
5959
/// it just ensures a conflict if two people change the module format.
6060
/// Don't worry about adhering to the 80-column limit for this line.
61-
const uint16_t SWIFTMODULE_VERSION_MINOR = 821; // extractConstantsFromMembers
61+
const uint16_t SWIFTMODULE_VERSION_MINOR = 822; // extractConstantsFromMembers on all nominals
6262

6363
/// A standard hash seed used for all string hashes in a serialized module.
6464
///

test/ConstExtraction/ExtractConstantsFromMembersAttribute.swift

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,56 @@ public struct TestStruct : MyProto {
1212
let cane: [String] = ["bar", "baz"]
1313
}
1414

15+
@extractConstantsFromMembers
16+
struct DirectOnStruct {
17+
var really = true
18+
}
19+
20+
@extractConstantsFromMembers
21+
class DirectOnClass {
22+
static let answer = 42
23+
}
24+
25+
@extractConstantsFromMembers
26+
enum DirectOnEnum {
27+
case yes
28+
}
29+
30+
// CHECK: "typeName": "ExtractConstantsFromMembersAttribute.TestStruct",
31+
// CHECK: "kind": "struct",
32+
// CHECK: "conformances": [
33+
// CHECK-NEXT: "ExtractConstantsFromMembersAttribute.MyProto"
34+
// CHECK-NEXT: ],
35+
// CHECK: "properties": [
1536
// CHECK: "label": "foo",
37+
// CHECK-NEXT: "type": "Swift.String",
1638
// CHECK: "valueKind": "RawLiteral",
1739
// CHECK: "value": "foo"
1840

1941
// CHECK: "label": "cane",
2042
// CHECK-NEXT: "type": "Swift.Array<Swift.String>",
43+
// CHECK: "valueKind": "Array",
44+
45+
46+
// CHECK: "typeName": "ExtractConstantsFromMembersAttribute.DirectOnStruct",
47+
// CHECK: "kind": "struct",
48+
// CHECK: "properties": [
49+
// CHECK: "label": "really",
50+
// CHECK-NEXT: "type": "Swift.Bool",
51+
// CHECK: "valueKind": "RawLiteral",
52+
// CHECK: "value": "true"
53+
54+
55+
// CHECK: "ExtractConstantsFromMembersAttribute.DirectOnClass",
56+
// CHECK: "kind": "class",
57+
// CHECK: "properties": [
58+
// CHECK: "label": "answer",
59+
// CHECK-NEXT: "type": "Swift.Int",
60+
// CHECK: "valueKind": "RawLiteral",
61+
// CHECK: "value": "42"
62+
63+
64+
// CHECK: "ExtractConstantsFromMembersAttribute.DirectOnEnum",
65+
// CHECK: "kind": "enum",
66+
// CHECK: "cases": [
67+
// CHECK: "name": "yes"

0 commit comments

Comments
 (0)