Skip to content

Commit ad8b0f3

Browse files
authored
[Sema] Allow @_section/@_used in concrete generic context (swiftlang#75222)
Allow `@_section` and `@_used` to be used in fully constrained extensions. This aligns with the existing behavior of static properties of generic types. The following is valid: ```swift extension Array where Element == Int { static let specificConstant = 41 } ``` However, adding `@_section` or `@_used` to the above property, will result in an error diagnostic. This change updates the logic of `@_section`/`@_used` to allow their use when the generic context is fully concrete.
1 parent bcfc0b6 commit ad8b0f3

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

lib/Sema/TypeCheckAttr.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2385,7 +2385,10 @@ void AttributeChecker::visitUsedAttr(UsedAttr *attr) {
23852385
if (D->getDeclContext()->isLocalContext())
23862386
diagnose(attr->getLocation(), diag::attr_only_at_non_local_scope,
23872387
attr->getAttrName());
2388-
else if (D->getDeclContext()->isGenericContext())
2388+
else if (D->getDeclContext()->isGenericContext() &&
2389+
!D->getDeclContext()
2390+
->getGenericSignatureOfContext()
2391+
->areAllParamsConcrete())
23892392
diagnose(attr->getLocation(), diag::attr_only_at_non_generic_scope,
23902393
attr->getAttrName());
23912394
else if (auto *VarD = dyn_cast<VarDecl>(D)) {
@@ -2412,7 +2415,10 @@ void AttributeChecker::visitSectionAttr(SectionAttr *attr) {
24122415
if (D->getDeclContext()->isLocalContext())
24132416
return; // already diagnosed
24142417

2415-
if (D->getDeclContext()->isGenericContext())
2418+
if (D->getDeclContext()->isGenericContext() &&
2419+
!D->getDeclContext()
2420+
->getGenericSignatureOfContext()
2421+
->areAllParamsConcrete())
24162422
diagnose(attr->getLocation(), diag::attr_only_at_non_generic_scope,
24172423
attr->getAttrName());
24182424
else if (auto *VarD = dyn_cast<VarDecl>(D)) {

test/IRGen/section_errors.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ struct MyStruct4<T> {
3131
}
3232
}
3333

34+
struct MyStruct5<T> {
35+
}
36+
37+
extension MyStruct5 where T == Never {
38+
@_used @_section("__TEXT,__mysection") static let static3: Int = 1 // ok
39+
}
40+
3441
@_section("__TEXT,__mysection") // expected-error {{'@_section' attribute cannot be applied to this declaration}}
3542
struct SomeStruct {}
3643

0 commit comments

Comments
 (0)