Skip to content

Commit ee623f8

Browse files
committed
Add @section/@const handling of globals to ASTGen too
1 parent 5f81c1b commit ee623f8

File tree

4 files changed

+15
-1
lines changed

4 files changed

+15
-1
lines changed

include/swift/AST/ASTBridging.h

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

780+
SWIFT_NAME("BridgedDeclAttributes.hasAttribute(self:_:)")
781+
bool BridgedDeclAttributes_hasAttribute(
782+
const BridgedDeclAttributes *_Nonnull attrs, swift::DeclAttrKind kind);
783+
780784
SWIFT_NAME("BridgedDeclAttribute.createSimple(_:kind:atLoc:nameLoc:)")
781785
BridgedDeclAttribute BridgedDeclAttribute_createSimple(
782786
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

test/ConstValues/SectionTopLevel.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
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

45
// integer literals
56
@section("mysection") let intLiteral1 = 42 // ok

0 commit comments

Comments
 (0)