Skip to content

Commit d329988

Browse files
committed
[Sema] Allow @_section/@_used in concrete generic context (#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. (cherry picked from commit ad8b0f3)
1 parent c5a9738 commit d329988

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
@@ -2318,7 +2318,10 @@ void AttributeChecker::visitUsedAttr(UsedAttr *attr) {
23182318
if (D->getDeclContext()->isLocalContext())
23192319
diagnose(attr->getLocation(), diag::attr_only_at_non_local_scope,
23202320
attr->getAttrName());
2321-
else if (D->getDeclContext()->isGenericContext())
2321+
else if (D->getDeclContext()->isGenericContext() &&
2322+
!D->getDeclContext()
2323+
->getGenericSignatureOfContext()
2324+
->areAllParamsConcrete())
23222325
diagnose(attr->getLocation(), diag::attr_only_at_non_generic_scope,
23232326
attr->getAttrName());
23242327
else if (auto *VarD = dyn_cast<VarDecl>(D)) {
@@ -2345,7 +2348,10 @@ void AttributeChecker::visitSectionAttr(SectionAttr *attr) {
23452348
if (D->getDeclContext()->isLocalContext())
23462349
return; // already diagnosed
23472350

2348-
if (D->getDeclContext()->isGenericContext())
2351+
if (D->getDeclContext()->isGenericContext() &&
2352+
!D->getDeclContext()
2353+
->getGenericSignatureOfContext()
2354+
->areAllParamsConcrete())
23492355
diagnose(attr->getLocation(), diag::attr_only_at_non_generic_scope,
23502356
attr->getAttrName());
23512357
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)