Diagnose @safe @unsafe when used together

Fixes rdar://147943857.
This commit is contained in:
Doug Gregor
2025-03-27 16:28:44 -07:00
parent d86f41a922
commit 8789871035
3 changed files with 16 additions and 2 deletions

View File

@@ -8233,7 +8233,7 @@ NOTE(sending_function_result_with_sending_param_note, none,
())
//------------------------------------------------------------------------------
// MARK: Strict Safety Diagnostics
// MARK: Strict Memory Safety Diagnostics
//------------------------------------------------------------------------------
NOTE(note_reference_to_unsafe_decl,none,
"%select{reference|call}0 to unsafe %kind1",
@@ -8272,6 +8272,9 @@ NOTE(decl_storage_mark_safe,none,
"add '@safe' if this type encapsulates the unsafe storage in "
"a safe interface", ())
ERROR(safe_and_unsafe_attr,none,
"%kindbase0 cannot be both @safe and @unsafe", (const Decl *))
GROUPED_WARNING(unsafe_superclass,StrictMemorySafety,none,
"%kindbase0 has superclass involving unsafe type %1",
(const ValueDecl *, Type))

View File

@@ -192,7 +192,6 @@ public:
IGNORED_ATTR(AllowFeatureSuppression)
IGNORED_ATTR(PreInverseGenerics)
IGNORED_ATTR(Safe)
IGNORED_ATTR(Unsafe)
#undef IGNORED_ATTR
void visitABIAttr(ABIAttr *attr) {
@@ -445,6 +444,7 @@ public:
void visitLifetimeAttr(LifetimeAttr *attr);
void visitAddressableSelfAttr(AddressableSelfAttr *attr);
void visitAddressableForDependenciesAttr(AddressableForDependenciesAttr *attr);
void visitUnsafeAttr(UnsafeAttr *attr);
};
} // end anonymous namespace
@@ -8140,6 +8140,14 @@ AttributeChecker::visitAddressableForDependenciesAttr(
}
}
void AttributeChecker::visitUnsafeAttr(UnsafeAttr *attr) {
if (auto safeAttr = D->getAttrs().getAttribute<SafeAttr>()) {
D->diagnose(diag::safe_and_unsafe_attr, D)
.highlight(attr->getRange())
.highlight(safeAttr->getRange());
}
}
namespace {
class ClosureAttributeChecker

View File

@@ -268,3 +268,6 @@ struct UnsafeWrapTest {
}
}
}
@safe @unsafe
struct ConfusedStruct { } // expected-error{{struct 'ConfusedStruct' cannot be both @safe and @unsafe}}