Skip to content

[semantics] Add support for annotating VarDecls with @_semantics. #28100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/swift/AST/Attr.def
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ DECL_ATTR(inline, Inline,
ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,
20)
DECL_ATTR(_semantics, Semantics,
OnAbstractFunction | OnSubscript | OnNominalType |
OnAbstractFunction | OnSubscript | OnNominalType | OnVar |
AllowMultipleAttributes | UserInaccessible |
ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,
21)
Expand Down
14 changes: 14 additions & 0 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -5179,6 +5179,20 @@ class VarDecl : public AbstractStorageDecl {
/// backing property will be treated as the member-initialized property.
bool isMemberwiseInitialized(bool preferDeclaredProperties) const;

/// Return the range of semantics attributes attached to this VarDecl.
auto getSemanticsAttrs() const
-> decltype(getAttrs().getAttributes<SemanticsAttr>()) {
return getAttrs().getAttributes<SemanticsAttr>();
}

/// Returns true if this VarDelc has the string \p attrValue as a semantics
/// attribute.
bool hasSemanticsAttr(StringRef attrValue) const {
return llvm::any_of(getSemanticsAttrs(), [&](const SemanticsAttr *attr) {
return attrValue.equals(attr->Value);
});
}

// Implement isa/cast/dyncast/etc.
static bool classof(const Decl *D) {
return D->getKind() == DeclKind::Var || D->getKind() == DeclKind::Param;
Expand Down
8 changes: 0 additions & 8 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1263,14 +1263,6 @@ bool Parser::parseNewDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc,
return false;
}

// Diagnose using @_semantics in a local scope. These don't
// actually work.
if (CurDeclContext->isLocalContext()) {
// Emit an error, but do not discard the attribute. This enables
// better recovery in the parser.
diagnose(Loc, diag::attr_only_at_non_local_scope, AttrName);
}

if (!DiscardAttribute)
Attributes.add(new (Context) SemanticsAttr(Value.getValue(), AtLoc,
AttrRange,
Expand Down
35 changes: 33 additions & 2 deletions test/attr/attr_semantics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
func duplicatesemantics() {}

func func_with_nested_semantics_1() {
@_semantics("exit") // expected-error {{attribute '_semantics' can only be used in a non-local scope}}
@_semantics("exit")
func exit(_ code : UInt32) -> Void
exit(0)
}
Expand All @@ -15,7 +15,7 @@ func func_with_nested_semantics_1() {
func somethingThatShouldParseFine() {}

func func_with_nested_semantics_2() {
@_semantics("exit") // expected-error {{attribute '_semantics' can only be used in a non-local scope}}
@_semantics("exit")
func exit(_ code : UInt32) -> Void
exit(0)
}
Expand All @@ -32,3 +32,34 @@ enum EnumWithSemantics {}
@_semantics("struct1")
@_semantics("struct2")
struct StructWithDuplicateSemantics {}

@_semantics("globalVar1")
@_semantics("globalVar2")
var globalVarWithSemantics : Int = 5

@_semantics("globalLet1")
@_semantics("globalLet2")
let globalLetWithSemantics : Int = 5

func varDeclLocalVars() {
@_semantics("localVar1")
@_semantics("localVar2")
var localVarWithSemantics : Int = 5
localVarWithSemantics = 6
let _ = localVarWithSemantics

@_semantics("localLet1")
@_semantics("localLet2")
let localLetWithSemantics : Int = 5
let _ = localLetWithSemantics
}

struct IVarTest {
@_semantics("localVar1")
@_semantics("localVar2")
var localVarWithSemantics : Int = 5

@_semantics("localLet1")
@_semantics("localLet2")
let localLetWithSemantics : Int = 5
}