Skip to content

Commit f2d1599

Browse files
committed
Sema: Fix corner case with @_fixed_layout diagnostic
We allow a member of a type to be more accessible than the type itself. In this case, the broader accessibility is ignored, and the effective access of the member is constrained by the accessibility of its parent type.
1 parent fd31747 commit f2d1599

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

lib/Sema/TypeCheckAttr.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,14 +1773,19 @@ void AttributeChecker::visitSpecializeAttr(SpecializeAttr *attr) {
17731773
attr->setRequirements(DC->getASTContext(), convertedRequirements);
17741774
}
17751775

1776+
static Accessibility getAccessForDiagnostics(const ValueDecl *D) {
1777+
return std::min(D->getFormalAccess(),
1778+
D->getEffectiveAccess());
1779+
}
1780+
17761781
void AttributeChecker::visitFixedLayoutAttr(FixedLayoutAttr *attr) {
17771782
auto *VD = cast<ValueDecl>(D);
17781783

17791784
if (VD->getEffectiveAccess() < Accessibility::Public) {
17801785
TC.diagnose(attr->getLocation(),
17811786
diag::fixed_layout_attr_on_internal_type,
17821787
VD->getName(),
1783-
VD->getFormalAccess())
1788+
getAccessForDiagnostics(VD))
17841789
.fixItRemove(attr->getRangeWithAt());
17851790
attr->setInvalid();
17861791
}
@@ -1803,7 +1808,7 @@ void AttributeChecker::visitVersionedAttr(VersionedAttr *attr) {
18031808
TC.diagnose(attr->getLocation(),
18041809
diag::versioned_attr_with_explicit_accessibility,
18051810
VD->getName(),
1806-
VD->getFormalAccess())
1811+
getAccessForDiagnostics(VD))
18071812
.fixItRemove(attr->getRangeWithAt());
18081813
attr->setInvalid();
18091814
return;

test/attr/attr_fixed_layout.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,13 @@ struct Rectangle {
5050
// Diagnostics
5151
//
5252

53-
@_fixed_layout struct InternalStruct {}
53+
@_fixed_layout struct InternalStruct {
5454
// expected-error@-1 {{'@_fixed_layout' attribute can only be applied to '@_versioned' or public declarations, but 'InternalStruct' is internal}}
5555

56+
@_fixed_layout public struct NestedStruct {}
57+
// expected-error@-1 {{'@_fixed_layout' attribute can only be applied to '@_versioned' or public declarations, but 'NestedStruct' is internal}}
58+
}
59+
5660
@_fixed_layout fileprivate struct FileprivateStruct {}
5761
// expected-error@-1 {{'@_fixed_layout' attribute can only be applied to '@_versioned' or public declarations, but 'FileprivateStruct' is fileprivate}}
5862

validation-test/compiler_crashers/28660-false-encountered-error-in-diagnostic-text.swift renamed to validation-test/compiler_crashers_fixed/28660-false-encountered-error-in-diagnostic-text.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

88
// REQUIRES: asserts
9-
// RUN: not --crash %target-swift-frontend %s -emit-ir
9+
// RUN: not %target-swift-frontend %s -emit-ir
1010
class C{@_fixed_layout public struct P

0 commit comments

Comments
 (0)