Skip to content

Commit b6c29f1

Browse files
authored
Merge pull request #85136 from kubamracek/section-top-level
SE-0492: Handle top-level `@section`-annotated globals
2 parents e55d18a + eaa3f8a commit b6c29f1

File tree

6 files changed

+42
-29
lines changed

6 files changed

+42
-29
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,10 @@ SWIFT_NAME("BridgedDeclAttributes.add(self:_:)")
780780
void BridgedDeclAttributes_add(BridgedDeclAttributes *_Nonnull attrs,
781781
BridgedDeclAttribute add);
782782

783+
SWIFT_NAME("BridgedDeclAttributes.hasAttribute(self:_:)")
784+
bool BridgedDeclAttributes_hasAttribute(
785+
const BridgedDeclAttributes *_Nonnull attrs, swift::DeclAttrKind kind);
786+
783787
SWIFT_NAME("BridgedDeclAttribute.createSimple(_:kind:atLoc:nameLoc:)")
784788
BridgedDeclAttribute BridgedDeclAttribute_createSimple(
785789
BridgedASTContext cContext, swift::DeclAttrKind kind,

lib/AST/Bridging/DeclAttributeBridging.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ void BridgedDeclAttributes_add(BridgedDeclAttributes *cAttrs,
9898
*cAttrs = attrs;
9999
}
100100

101+
bool BridgedDeclAttributes_hasAttribute(
102+
const BridgedDeclAttributes *_Nonnull attrs, swift::DeclAttrKind kind) {
103+
return attrs->unbridged().getAttribute(kind) != nullptr;
104+
}
105+
101106
static AvailableAttr::Kind unbridge(BridgedAvailableAttrKind value) {
102107
switch (value) {
103108
case BridgedAvailableAttrKindDefault:

lib/ASTGen/Sources/ASTGen/Decls.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,8 +610,12 @@ extension ASTGenVisitor {
610610
// TODO: Diagnostics
611611
fatalError("invalid pattern binding introducer")
612612
}
613+
614+
// @const/@section globals are not top-level, per SE-0492.
615+
let isConst = attrs.attributes.hasAttribute(.Section) || attrs.attributes.hasAttribute(.ConstVal)
616+
613617
let topLevelDecl: BridgedTopLevelCodeDecl?
614-
if self.declContext.isModuleScopeContext, self.declContext.parentSourceFile.isScriptMode {
618+
if self.declContext.isModuleScopeContext, self.declContext.parentSourceFile.isScriptMode, !isConst {
615619
topLevelDecl = BridgedTopLevelCodeDecl.create(self.ctx, declContext: self.declContext)
616620
} else {
617621
topLevelDecl = nil

lib/Parse/ParseDecl.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8746,13 +8746,16 @@ Parser::parseDeclVar(ParseDeclOptions Flags,
87468746

87478747
SourceLoc VarLoc = newBindingContext.getIntroducer() ? consumeToken() : Tok.getLoc();
87488748

8749+
bool IsConst = Attributes.hasAttribute<SectionAttr>() ||
8750+
Attributes.hasAttribute<ConstValAttr>();
8751+
87498752
// If this is a var in the top-level of script/repl source file, wrap the
87508753
// PatternBindingDecl in a TopLevelCodeDecl, since it represents executable
87518754
// code. The VarDecl and any accessor decls (for computed properties) go in
8752-
// CurDeclContext.
8753-
//
8755+
// CurDeclContext. @const/@section globals are not top-level, per SE-0492.
87548756
TopLevelCodeDecl *topLevelDecl = nullptr;
8755-
if (allowTopLevelCode() && CurDeclContext->isModuleScopeContext()) {
8757+
if (allowTopLevelCode() && CurDeclContext->isModuleScopeContext() &&
8758+
!IsConst) {
87568759
// The body of topLevelDecl will get set later.
87578760
topLevelDecl = new (Context) TopLevelCodeDecl(CurDeclContext);
87588761
}
Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Constant globals using @section
22
// RUN: %target-swift-frontend -emit-ir %s | %FileCheck %s
3+
// RUN: %target-swift-frontend -emit-ir %s -enable-experimental-feature ParserASTGen | %FileCheck %s
34

4-
// TODO
5-
// XFAIL: *
5+
// REQUIRES: swift_feature_ParserASTGen
66

77
// integer literals
88
@section("mysection") let intLiteral1 = 42 // ok
@@ -31,30 +31,27 @@ func bar(x: Int) -> String { return "test" }
3131
@section("mysection") let funcRef1 = foo // ok
3232
@section("mysection") let funcRef2 = bar // ok
3333

34-
// metatypes - TODO
35-
//@section("mysection") let metatype1 = Int.self
36-
3734
// tuples
3835
@section("mysection") let tuple1 = (1, 2, 3, 2.718, true) // ok
3936
@section("mysection") let tuple2: (Int, Float, Bool) = (42, 3.14, false) // ok
4037
@section("mysection") let tuple3 = (foo, bar) // ok (function references in tuple)
4138

42-
// CHECK: @"$s9SectionIR11intLiteral1Sivp" = {{.*}}constant %TSi <{ i64 42 }>, section "mysection"
43-
// CHECK: @"$s9SectionIR11intLiteral2s4Int8Vvp" = {{.*}}constant %Ts4Int8V <{ i8 127 }>, section "mysection"
44-
// CHECK: @"$s9SectionIR11intLiteral3s5Int16Vvp" = {{.*}}constant %Ts5Int16V <{ i16 32767 }>, section "mysection"
45-
// CHECK: @"$s9SectionIR11intLiteral4s5Int32Vvp" = {{.*}}constant %Ts5Int32V <{ i32 2147483647 }>, section "mysection"
46-
// CHECK: @"$s9SectionIR11intLiteral5s5Int64Vvp" = {{.*}}constant %Ts5Int64V <{ i64 9223372036854775807 }>, section "mysection"
47-
// CHECK: @"$s9SectionIR11intLiteral6Suvp" = {{.*}}constant %TSu <{ i64 42 }>, section "mysection"
48-
// CHECK: @"$s9SectionIR11intLiteral7s5UInt8Vvp" = {{.*}}constant %Ts5UInt8V <{ i8 -1 }>, section "mysection"
49-
// CHECK: @"$s9SectionIR11intLiteral8s6UInt16Vvp" = {{.*}}constant %Ts6UInt16V <{ i16 -1 }>, section "mysection"
50-
// CHECK: @"$s9SectionIR11intLiteral9s6UInt32Vvp" = {{.*}}constant %Ts6UInt32V <{ i32 -1 }>, section "mysection"
51-
// CHECK: @"$s9SectionIR12intLiteral10s6UInt64Vvp" = {{.*}}constant %Ts6UInt64V <{ i64 -1 }>, section "mysection"
52-
// CHECK: @"$s9SectionIR13floatLiteral1Sfvp" = {{.*}}constant %TSf <{ float 0x40091EB860000000 }>, section "mysection"
53-
// CHECK: @"$s9SectionIR13floatLiteral2Sdvp" = {{.*}}constant %TSd <{ double 2.718000e+00 }>, section "mysection"
54-
// CHECK: @"$s9SectionIR12boolLiteral1Sbvp" = {{.*}}constant %TSb <{ i1 true }>, section "mysection"
55-
// CHECK: @"$s9SectionIR12boolLiteral2Sbvp" = {{.*}}constant %TSb zeroinitializer, section "mysection"
56-
// CHECK: @"$s9SectionIR8funcRef1Siycvp" = {{.*}}constant %swift.function { ptr @"$s9SectionIR3fooSiyF", ptr null }, section "mysection"
57-
// CHECK: @"$s9SectionIR8funcRef2ySSSicvp" = {{.*}}constant %swift.function { ptr @"$s9SectionIR3bar1xSSSi_tF", ptr null }, section "mysection"
58-
// CHECK: @"$s9SectionIR6tuple1Si_S2iSdSbtvp" = {{.*}}constant <{ %TSi, %TSi, %TSi, %TSd, %TSb }> <{ %TSi <{ i64 1 }>, %TSi <{ i64 2 }>, %TSi <{ i64 3 }>, %TSd <{ double 2.718000e+00 }>, %TSb <{ i1 true }> }>, section "mysection"
59-
// CHECK: @"$s9SectionIR6tuple2Si_SfSbtvp" = {{.*}}constant <{ %TSi, %TSf, %TSb }> <{ %TSi <{ i64 42 }>, %TSf <{ float 0x40091EB860000000 }>, %TSb zeroinitializer }>, section "mysection"
60-
// CHECK: @"$s9SectionIR6tuple3Siyc_SSSictvp" = {{.*}}constant <{ %swift.function, %swift.function }> <{ %swift.function { ptr @"$s9SectionIR3fooSiyF", ptr null }, %swift.function { ptr @"$s9SectionIR3bar1xSSSi_tF", ptr null } }>, section "mysection"
39+
// CHECK: @"$s15SectionTopLevel11intLiteral1Sivp" = {{.*}}constant %TSi <{ i64 42 }>, section "mysection"
40+
// CHECK: @"$s15SectionTopLevel11intLiteral2s4Int8Vvp" = {{.*}}constant %Ts4Int8V <{ i8 127 }>, section "mysection"
41+
// CHECK: @"$s15SectionTopLevel11intLiteral3s5Int16Vvp" = {{.*}}constant %Ts5Int16V <{ i16 32767 }>, section "mysection"
42+
// CHECK: @"$s15SectionTopLevel11intLiteral4s5Int32Vvp" = {{.*}}constant %Ts5Int32V <{ i32 2147483647 }>, section "mysection"
43+
// CHECK: @"$s15SectionTopLevel11intLiteral5s5Int64Vvp" = {{.*}}constant %Ts5Int64V <{ i64 9223372036854775807 }>, section "mysection"
44+
// CHECK: @"$s15SectionTopLevel11intLiteral6Suvp" = {{.*}}constant %TSu <{ i64 42 }>, section "mysection"
45+
// CHECK: @"$s15SectionTopLevel11intLiteral7s5UInt8Vvp" = {{.*}}constant %Ts5UInt8V <{ i8 -1 }>, section "mysection"
46+
// CHECK: @"$s15SectionTopLevel11intLiteral8s6UInt16Vvp" = {{.*}}constant %Ts6UInt16V <{ i16 -1 }>, section "mysection"
47+
// CHECK: @"$s15SectionTopLevel11intLiteral9s6UInt32Vvp" = {{.*}}constant %Ts6UInt32V <{ i32 -1 }>, section "mysection"
48+
// CHECK: @"$s15SectionTopLevel12intLiteral10s6UInt64Vvp" = {{.*}}constant %Ts6UInt64V <{ i64 -1 }>, section "mysection"
49+
// CHECK: @"$s15SectionTopLevel13floatLiteral1Sfvp" = {{.*}}constant %TSf <{ float 0x40091EB860000000 }>, section "mysection"
50+
// CHECK: @"$s15SectionTopLevel13floatLiteral2Sdvp" = {{.*}}constant %TSd <{ double 2.718000e+00 }>, section "mysection"
51+
// CHECK: @"$s15SectionTopLevel12boolLiteral1Sbvp" = {{.*}}constant %TSb <{ i1 true }>, section "mysection"
52+
// CHECK: @"$s15SectionTopLevel12boolLiteral2Sbvp" = {{.*}}constant %TSb zeroinitializer, section "mysection"
53+
// CHECK: @"$s15SectionTopLevel8funcRef1Siycvp" = {{.*}}constant %swift.function { ptr @"$s15SectionTopLevel3fooSiyF", ptr null }, section "mysection"
54+
// CHECK: @"$s15SectionTopLevel8funcRef2ySSSicvp" = {{.*}}constant %swift.function { ptr @"$s15SectionTopLevel3bar1xSSSi_tF", ptr null }, section "mysection"
55+
// CHECK: @"$s15SectionTopLevel6tuple1Si_S2iSdSbtvp" = {{.*}}constant <{ %TSi, %TSi, %TSi, %TSd, %TSb }> <{ %TSi <{ i64 1 }>, %TSi <{ i64 2 }>, %TSi <{ i64 3 }>, %TSd <{ double 2.718000e+00 }>, %TSb <{ i1 true }> }>, section "mysection"
56+
// CHECK: @"$s15SectionTopLevel6tuple2Si_SfSbtvp" = {{.*}}constant <{ %TSi, %TSf, %TSb }> <{ %TSi <{ i64 42 }>, %TSf <{ float 0x40091EB860000000 }>, %TSb zeroinitializer }>, section "mysection"
57+
// CHECK: @"$s15SectionTopLevel6tuple3Siyc_SSSictvp" = {{.*}}constant <{ %swift.function, %swift.function }> <{ %swift.function { ptr @"$s15SectionTopLevel3fooSiyF", ptr null }, %swift.function { ptr @"$s15SectionTopLevel3bar1xSSSi_tF", ptr null } }>, section "mysection"

test/ConstValues/TopLevel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Constant globals should "work" even in top-level code mode.
22
// REQUIRES: swift_feature_CompileTimeValues
33
// REQUIRES: swift_feature_CompileTimeValuesPreview
4-
// REQUIRES: rdar146954355
4+
55
// RUN: %target-swift-frontend -emit-ir -primary-file %s -enable-experimental-feature CompileTimeValues -enable-experimental-feature CompileTimeValuesPreview
66
// RUN: %target-swift-frontend -emit-ir -primary-file %s -parse-as-library -enable-experimental-feature CompileTimeValues -enable-experimental-feature CompileTimeValuesPreview
77

0 commit comments

Comments
 (0)