Skip to content

Commit 25cad6f

Browse files
committed
Diagnose more invalid uses of @_section/@_used
1 parent f2cd8e0 commit 25cad6f

File tree

5 files changed

+35
-9
lines changed

5 files changed

+35
-9
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,11 +1541,6 @@ ERROR(attr_expected_colon_after_label,none,
15411541
ERROR(alignment_must_be_positive_integer,none,
15421542
"alignment value must be a positive integer literal", ())
15431543

1544-
ERROR(attr_only_at_non_local_scope, none,
1545-
"attribute '%0' can only be used in a non-local scope", (StringRef))
1546-
ERROR(attr_only_at_non_generic_scope, none,
1547-
"attribute '%0' cannot be used in a generic context", (StringRef))
1548-
15491544
// Access control
15501545
ERROR(attr_access_expected_set,none,
15511546
"expected 'set' as subject of '%0' modifier", (StringRef))

include/swift/AST/DiagnosticsSema.def

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1922,6 +1922,13 @@ ERROR(attr_methods_only,none,
19221922
ERROR(attr_decl_async,none,
19231923
"@%0 %1 cannot be asynchronous", (StringRef, DescriptiveDeclKind))
19241924

1925+
ERROR(attr_only_at_non_local_scope, none,
1926+
"attribute '%0' can only be used in a non-local scope", (StringRef))
1927+
ERROR(attr_only_at_non_generic_scope, none,
1928+
"attribute '%0' cannot be used in a generic context", (StringRef))
1929+
ERROR(attr_only_on_static_properties, none,
1930+
"properties with attribute '_section' must be static", (StringRef))
1931+
19251932
ERROR(access_control_in_protocol,none,
19261933
"%0 modifier cannot be used in protocols", (DeclAttribute))
19271934
NOTE(access_control_in_protocol_detail,none,
@@ -3739,6 +3746,8 @@ ERROR(attr_not_on_variadic_parameters,none,
37393746
"'%0' must not be used on variadic parameters", (StringRef))
37403747
ERROR(attr_not_on_stored_properties,none,
37413748
"'%0' must not be used on stored properties", (DeclAttribute))
3749+
ERROR(attr_not_on_computed_properties,none,
3750+
"'%0' must not be used on computed properties", (DeclAttribute))
37423751

37433752
WARNING(attr_has_no_effect_on_decl_with_access_level,none,
37443753
"'%0' does not have any effect on "

lib/Parse/ParseDecl.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "swift/AST/DebuggerClient.h"
2121
#include "swift/AST/Decl.h"
2222
#include "swift/AST/DiagnosticsParse.h"
23+
#include "swift/AST/DiagnosticsSema.h"
2324
#include "swift/AST/GenericParamList.h"
2425
#include "swift/AST/Initializer.h"
2526
#include "swift/AST/LazyResolver.h"

lib/Sema/TypeCheckAttr.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2084,10 +2084,18 @@ void AttributeChecker::visitUsedAttr(UsedAttr *attr) {
20842084
if (D->getDeclContext()->isLocalContext())
20852085
diagnose(attr->getLocation(), diag::attr_only_at_non_local_scope,
20862086
attr->getAttrName());
2087-
2088-
if (D->getDeclContext()->isGenericContext())
2087+
else if (D->getDeclContext()->isGenericContext())
20892088
diagnose(attr->getLocation(), diag::attr_only_at_non_generic_scope,
20902089
attr->getAttrName());
2090+
else if (auto *VarD = dyn_cast<VarDecl>(D)) {
2091+
if (!VarD->isStatic() && !D->getDeclContext()->isModuleScopeContext()) {
2092+
diagnose(attr->getLocation(), diag::attr_only_on_static_properties,
2093+
attr->getAttrName());
2094+
} else if (!VarD->hasStorageOrWrapsStorage()) {
2095+
diagnose(attr->getLocation(), diag::attr_not_on_computed_properties,
2096+
attr);
2097+
}
2098+
}
20912099
}
20922100

20932101
void AttributeChecker::visitSectionAttr(SectionAttr *attr) {
@@ -2100,9 +2108,20 @@ void AttributeChecker::visitSectionAttr(SectionAttr *attr) {
21002108
if (attr->Name.empty())
21012109
diagnose(attr->getLocation(), diag::section_empty_name);
21022110

2103-
if (D->getDeclContext()->isGenericContext())
2111+
if (D->getDeclContext()->isLocalContext())
2112+
; // Already diagnosed in Parse, don't diagnose again
2113+
else if (D->getDeclContext()->isGenericContext())
21042114
diagnose(attr->getLocation(), diag::attr_only_at_non_generic_scope,
21052115
attr->getAttrName());
2116+
else if (auto *VarD = dyn_cast<VarDecl>(D)) {
2117+
if (!VarD->isStatic() && !D->getDeclContext()->isModuleScopeContext()) {
2118+
diagnose(attr->getLocation(), diag::attr_only_on_static_properties,
2119+
attr->getAttrName());
2120+
} else if (!VarD->hasStorageOrWrapsStorage()) {
2121+
diagnose(attr->getLocation(), diag::attr_not_on_computed_properties,
2122+
attr);
2123+
}
2124+
}
21062125
}
21072126

21082127
void AttributeChecker::visitUnsafeNoObjCTaggedPointerAttr(

test/IRGen/section_errors.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ struct MyStruct {
99
}
1010

1111
struct MyStruct2 {
12-
@_section("__TEXT,__mysection") var member0: Int = 1
12+
@_section("__TEXT,__mysection") var member0: Int = 1 // expected-error {{properties with attribute '_section' must be static}}
13+
14+
@_section("__TEXT,__mysection") static var member0: Int { return 1 } // expected-error {{'@_section' must not be used on computed properties}}
1315
}
1416

1517
struct MyStruct3<T> {

0 commit comments

Comments
 (0)