Skip to content

Commit 039acb6

Browse files
committed
Sema: Kill some duplication using diagnoseAndRemoveAttr()
1 parent f769729 commit 039acb6

File tree

2 files changed

+22
-43
lines changed

2 files changed

+22
-43
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3758,7 +3758,7 @@ ERROR(class_designated_init_inlinable_resilient,none,
37583758
"delegate to another initializer", (Type, unsigned))
37593759

37603760
ERROR(attribute_invalid_on_stored_property,
3761-
none, "'@%0' attribute cannot be applied to stored properties", (StringRef))
3761+
none, "'%0' attribute cannot be applied to stored properties", (DeclAttribute))
37623762

37633763
ERROR(inlinable_dynamic_not_supported,
37643764
none, "'@inlinable' attribute cannot be applied to 'dynamic' declarations", ())

lib/Sema/TypeCheckAttr.cpp

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ void AttributeEarlyChecker::visitTransparentAttr(TransparentAttr *attr) {
292292
if (VD->hasStorage())
293293
return diagnoseAndRemoveAttr(attr,
294294
diag::attribute_invalid_on_stored_property,
295-
attr->getAttrName());
295+
attr);
296296
}
297297
}
298298

@@ -1948,12 +1948,8 @@ void AttributeChecker::visitFixedLayoutAttr(FixedLayoutAttr *attr) {
19481948
auto access = VD->getFormalAccess(/*useDC=*/nullptr,
19491949
/*isUsageFromInline=*/true);
19501950
if (access < AccessLevel::Public) {
1951-
TC.diagnose(attr->getLocation(),
1952-
diag::fixed_layout_attr_on_internal_type,
1953-
VD->getFullName(),
1954-
access)
1955-
.fixItRemove(attr->getRangeWithAt());
1956-
attr->setInvalid();
1951+
diagnoseAndRemoveAttr(attr, diag::fixed_layout_attr_on_internal_type,
1952+
VD->getFullName(), access);
19571953
}
19581954
}
19591955

@@ -1963,29 +1959,22 @@ void AttributeChecker::visitUsableFromInlineAttr(UsableFromInlineAttr *attr) {
19631959
// FIXME: Once protocols can contain nominal types, do we want to allow
19641960
// these nominal types to have access control (and also @usableFromInline)?
19651961
if (isa<ProtocolDecl>(VD->getDeclContext())) {
1966-
TC.diagnose(attr->getLocation(),
1967-
diag::versioned_attr_in_protocol)
1968-
.fixItRemove(attr->getRangeWithAt());
1969-
attr->setInvalid();
1962+
diagnoseAndRemoveAttr(attr, diag::versioned_attr_in_protocol);
19701963
return;
19711964
}
19721965

19731966
// @usableFromInline can only be applied to internal declarations.
19741967
if (VD->getFormalAccess() != AccessLevel::Internal) {
1975-
TC.diagnose(attr->getLocation(), diag::versioned_attr_with_explicit_access,
1976-
VD->getFullName(),
1977-
VD->getFormalAccess())
1978-
.fixItRemove(attr->getRangeWithAt());
1979-
attr->setInvalid();
1968+
diagnoseAndRemoveAttr(attr, diag::versioned_attr_with_explicit_access,
1969+
VD->getFullName(),
1970+
VD->getFormalAccess());
19801971
return;
19811972
}
19821973

19831974
// Symbols of dynamically-dispatched declarations are never referenced
19841975
// directly, so marking them as @usableFromInline does not make sense.
19851976
if (VD->isDynamic()) {
1986-
TC.diagnose(attr->getLocation(),
1987-
diag::versioned_dynamic_not_supported);
1988-
attr->setInvalid();
1977+
diagnoseAndRemoveAttr(attr, diag::versioned_dynamic_not_supported);
19891978
return;
19901979
}
19911980
}
@@ -1998,11 +1987,9 @@ void AttributeChecker::visitInlinableAttr(InlinableAttr *attr) {
19981987
// because clients cannot directly access storage.
19991988
if (auto *VD = dyn_cast<VarDecl>(D)) {
20001989
if (VD->hasStorage() || VD->getAttrs().hasAttribute<LazyAttr>()) {
2001-
TC.diagnose(attr->getLocation(),
2002-
diag::attribute_invalid_on_stored_property,
2003-
attr->getAttrName())
2004-
.fixItRemove(attr->getRangeWithAt());
2005-
attr->setInvalid();
1990+
diagnoseAndRemoveAttr(attr,
1991+
diag::attribute_invalid_on_stored_property,
1992+
attr);
20061993
return;
20071994
}
20081995
}
@@ -2012,9 +1999,7 @@ void AttributeChecker::visitInlinableAttr(InlinableAttr *attr) {
20121999
// Calls to dynamically-dispatched declarations are never devirtualized,
20132000
// so marking them as @inlinable does not make sense.
20142001
if (VD->isDynamic()) {
2015-
TC.diagnose(attr->getLocation(),
2016-
diag::inlinable_dynamic_not_supported);
2017-
attr->setInvalid();
2002+
diagnoseAndRemoveAttr(attr, diag::inlinable_dynamic_not_supported);
20182003
return;
20192004
}
20202005

@@ -2023,24 +2008,19 @@ void AttributeChecker::visitInlinableAttr(InlinableAttr *attr) {
20232008
auto access = VD->getFormalAccess(/*useDC=*/nullptr,
20242009
/*isUsageFromInline=*/true);
20252010
if (access < AccessLevel::Public) {
2026-
TC.diagnose(attr->getLocation(),
2027-
diag::inlinable_decl_not_public,
2028-
VD->getBaseName(),
2029-
access)
2030-
.fixItRemove(attr->getRangeWithAt());
2031-
attr->setInvalid();
2011+
diagnoseAndRemoveAttr(attr, diag::inlinable_decl_not_public,
2012+
VD->getBaseName(),
2013+
access);
20322014
return;
20332015
}
20342016
}
20352017

20362018
void AttributeChecker::visitOptimizeAttr(OptimizeAttr *attr) {
20372019
if (auto *VD = dyn_cast<VarDecl>(D)) {
20382020
if (VD->hasStorage()) {
2039-
TC.diagnose(attr->getLocation(),
2040-
diag::attribute_invalid_on_stored_property,
2041-
attr->getAttrName())
2042-
.fixItRemove(attr->getRangeWithAt());
2043-
attr->setInvalid();
2021+
diagnoseAndRemoveAttr(attr,
2022+
diag::attribute_invalid_on_stored_property,
2023+
attr);
20442024
return;
20452025
}
20462026
}
@@ -2051,10 +2031,9 @@ void AttributeChecker::visitDiscardableResultAttr(DiscardableResultAttr *attr) {
20512031
if (auto result = FD->getResultInterfaceType()) {
20522032
auto resultIsVoid = result->isVoid();
20532033
if (resultIsVoid || result->isUninhabited()) {
2054-
auto warn = diag::discardable_result_on_void_never_function;
2055-
auto diagnostic = TC.diagnose(D->getStartLoc(), warn, resultIsVoid);
2056-
diagnostic.fixItRemove(attr->getRangeWithAt());
2057-
attr->setInvalid();
2034+
diagnoseAndRemoveAttr(attr,
2035+
diag::discardable_result_on_void_never_function,
2036+
resultIsVoid);
20582037
}
20592038
}
20602039
}

0 commit comments

Comments
 (0)