Skip to content

Commit b7b5a2a

Browse files
committedFeb 26, 2025
[SE-0458] Enable unsafe expressions / attributes / for..in effects by default
With the acceptance of SE-0458, allow the use of unsafe expressions, the @safe and @unsafe attributes, and the `unsafe` effect on the for..in loop in all Swift code. Introduce the `-strict-memory-safety` flag detailed in the proposal to enable strict memory safety checking. This enables a new class of feature, an optional feature (that is *not* upcoming or experimental), and which can be detected via `hasFeature(StrictMemorySafety)`.

37 files changed

+112
-119
lines changed
 

‎include/swift/AST/DiagnosticsSema.def

-3
Original file line numberDiff line numberDiff line change
@@ -8165,9 +8165,6 @@ NOTE(sending_function_result_with_sending_param_note, none,
81658165
//------------------------------------------------------------------------------
81668166
// MARK: Strict Safety Diagnostics
81678167
//------------------------------------------------------------------------------
8168-
ERROR(unsafe_attr_disabled,none,
8169-
"attribute requires '-enable-experimental-feature AllowUnsafeAttribute'", ())
8170-
81718168
NOTE(note_reference_to_unsafe_decl,none,
81728169
"%select{reference|call}0 to unsafe %kind1",
81738170
(bool, const ValueDecl *))

‎include/swift/Basic/Features.def

+21-6
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@
4848
// for features that can be assumed to be available in any Swift compiler that
4949
// will be used to process the textual interface files produced by this
5050
// Swift compiler.
51+
//
52+
// OPTIONAL_LANGUAGE_FEATURE is the same as LANGUAGE_FEATURE, but describes
53+
// accepted features that can be enabled independently of language version and
54+
// are not scheduled to be enabled in some specific language version. Examples
55+
// of optional language features include strict memory safety checking (SE-0458)
56+
// and Embedded Swift.
5157
//===----------------------------------------------------------------------===//
5258

5359
#ifndef LANGUAGE_FEATURE
@@ -89,6 +95,11 @@
8995
LANGUAGE_FEATURE(FeatureName, SENumber, Description)
9096
#endif
9197

98+
#ifndef OPTIONAL_LANGUAGE_FEATURE
99+
# define OPTIONAL_LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
100+
LANGUAGE_FEATURE(FeatureName, SENumber, Description)
101+
#endif
102+
92103
// A feature that's both conditionally-suppressible and experimental.
93104
// Delegates to whichever the includer defines.
94105
#ifndef CONDITIONALLY_SUPPRESSIBLE_EXPERIMENTAL_FEATURE
@@ -203,6 +214,7 @@ LANGUAGE_FEATURE(IsolatedAny2, 431, "@isolated(any) function types")
203214
LANGUAGE_FEATURE(ObjCImplementation, 436, "@objc @implementation extensions")
204215
LANGUAGE_FEATURE(NonescapableTypes, 446, "Nonescapable types")
205216
LANGUAGE_FEATURE(BuiltinEmplaceTypedThrows, 0, "Builtin.emplace typed throws")
217+
SUPPRESSIBLE_LANGUAGE_FEATURE(MemorySafetyAttributes, 458, "@unsafe attribute")
206218

207219
// Swift 6
208220
UPCOMING_FEATURE(ConciseMagicFile, 274, 6)
@@ -226,6 +238,14 @@ UPCOMING_FEATURE(ExistentialAny, 335, 7)
226238
UPCOMING_FEATURE(InternalImportsByDefault, 409, 7)
227239
UPCOMING_FEATURE(MemberImportVisibility, 444, 7)
228240

241+
// Optional language features / modes
242+
243+
/// Diagnose uses of language constructs and APIs that can violate memory
244+
/// safety.
245+
OPTIONAL_LANGUAGE_FEATURE(StrictMemorySafety, 458, "Strict memory safety")
246+
247+
// Experimental features
248+
229249
EXPERIMENTAL_FEATURE(StaticAssert, false)
230250
EXPERIMENTAL_FEATURE(NamedOpaqueTypes, false)
231251
EXPERIMENTAL_FEATURE(FlowSensitiveConcurrencyCaptures, false)
@@ -396,12 +416,6 @@ EXPERIMENTAL_FEATURE(Extern, true)
396416
// Enable trailing comma for comma-separated lists.
397417
EXPERIMENTAL_FEATURE(TrailingComma, false)
398418

399-
/// Allow the @unsafe attribute.
400-
SUPPRESSIBLE_EXPERIMENTAL_FEATURE(AllowUnsafeAttribute, true)
401-
402-
/// Warn on use of unsafe constructs.
403-
EXPERIMENTAL_FEATURE(WarnUnsafe, true)
404-
405419
// Import bounds safety and lifetime attributes from interop headers to
406420
// generate Swift wrappers with safe pointer types.
407421
EXPERIMENTAL_FEATURE(SafeInteropWrappers, false)
@@ -463,6 +477,7 @@ EXPERIMENTAL_FEATURE(IsolatedConformances, true)
463477
#undef EXPERIMENTAL_FEATURE
464478
#undef UPCOMING_FEATURE
465479
#undef BASELINE_LANGUAGE_FEATURE
480+
#undef OPTIONAL_LANGUAGE_FEATURE
466481
#undef CONDITIONALLY_SUPPRESSIBLE_EXPERIMENTAL_FEATURE
467482
#undef CONDITIONALLY_SUPPRESSIBLE_LANGUAGE_FEATURE
468483
#undef SUPPRESSIBLE_EXPERIMENTAL_FEATURE

‎include/swift/Option/Options.td

+5
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,11 @@ def disable_upcoming_feature : Separate<["-"], "disable-upcoming-feature">,
10051005
HelpText<"Disable a feature that will be introduced in an upcoming language "
10061006
"version">;
10071007

1008+
def strict_memory_safety : Flag<["-"], "strict-memory-safety">,
1009+
Flags<[FrontendOption, ModuleInterfaceOptionIgnorable,
1010+
SwiftAPIDigesterOption, SwiftSynthesizeInterfaceOption]>,
1011+
HelpText<"Enable strict memory safety checking">;
1012+
10081013
def Rpass_EQ : Joined<["-"], "Rpass=">,
10091014
Flags<[FrontendOption]>,
10101015
HelpText<"Report performed transformations by optimization passes whose "

‎lib/AST/ASTPrinter.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -3214,9 +3214,10 @@ struct ExcludeAttrRAII {
32143214
}
32153215

32163216
static void
3217-
suppressingFeatureAllowUnsafeAttribute(PrintOptions &options,
3217+
suppressingFeatureMemorySafetyAttributes(PrintOptions &options,
32183218
llvm::function_ref<void()> action) {
32193219
ExcludeAttrRAII scope(options.ExcludeAttrList, DeclAttrKind::Unsafe);
3220+
ExcludeAttrRAII scope2(options.ExcludeAttrList, DeclAttrKind::Safe);
32203221
action();
32213222
}
32223223

‎lib/AST/FeatureSet.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,6 @@ UNINTERESTING_FEATURE(ReinitializeConsumeInMultiBlockDefer)
330330
UNINTERESTING_FEATURE(SE427NoInferenceOnExtension)
331331
UNINTERESTING_FEATURE(TrailingComma)
332332

333-
static bool usesFeatureAllowUnsafeAttribute(Decl *decl) {
334-
return decl->getAttrs().hasAttribute<UnsafeAttr>();
335-
}
336-
337333
static ABIAttr *getABIAttr(Decl *decl) {
338334
if (auto pbd = dyn_cast<PatternBindingDecl>(decl))
339335
for (auto i : range(pbd->getNumPatternEntries()))
@@ -353,7 +349,12 @@ static bool usesFeatureIsolatedConformances(Decl *decl) {
353349
return false;
354350
}
355351

356-
UNINTERESTING_FEATURE(WarnUnsafe)
352+
static bool usesFeatureMemorySafetyAttributes(Decl *decl) {
353+
return decl->getAttrs().hasAttribute<SafeAttr>() ||
354+
decl->getAttrs().hasAttribute<UnsafeAttr>();
355+
}
356+
357+
UNINTERESTING_FEATURE(StrictMemorySafety)
357358
UNINTERESTING_FEATURE(SafeInteropWrappers)
358359
UNINTERESTING_FEATURE(AssumeResilientCxxTypes)
359360
UNINTERESTING_FEATURE(CoroutineAccessorsUnwindOnCallerError)

‎lib/ASTGen/Sources/ASTGen/SourceFile.swift

-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ extension Parser.ExperimentalFeatures {
7777
mapFeature(.CoroutineAccessors, to: .coroutineAccessors)
7878
mapFeature(.ValueGenerics, to: .valueGenerics)
7979
mapFeature(.ABIAttribute, to: .abiAttribute)
80-
mapFeature(.WarnUnsafe, to: .unsafeExpression)
8180
}
8281
}
8382

‎lib/Basic/LangOptions.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ LangOptions::LangOptions() {
3838
Features.insert(Feature::FeatureName);
3939
#define UPCOMING_FEATURE(FeatureName, SENumber, Version)
4040
#define EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd)
41+
#define OPTIONAL_LANGUAGE_FEATURE(FeatureName, SENumber, Description)
4142
#include "swift/Basic/Features.def"
4243

4344
// Special case: remove macro support if the compiler wasn't built with a
@@ -636,6 +637,8 @@ bool swift::isFeatureAvailableInProduction(Feature feature) {
636637
return true;
637638
#define EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd) \
638639
case Feature::FeatureName: return AvailableInProd;
640+
#define OPTIONAL_LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
641+
LANGUAGE_FEATURE(FeatureName, SENumber, Description)
639642
#include "swift/Basic/Features.def"
640643
}
641644
llvm_unreachable("covered switch");
@@ -655,6 +658,7 @@ std::optional<Feature> swift::getExperimentalFeature(llvm::StringRef name) {
655658
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description)
656659
#define EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd) \
657660
.Case(#FeatureName, Feature::FeatureName)
661+
#define OPTIONAL_LANGUAGE_FEATURE(FeatureName, SENumber, Description)
658662
#include "swift/Basic/Features.def"
659663
.Default(std::nullopt);
660664
}
@@ -664,6 +668,7 @@ std::optional<unsigned> swift::getFeatureLanguageVersion(Feature feature) {
664668
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description)
665669
#define UPCOMING_FEATURE(FeatureName, SENumber, Version) \
666670
case Feature::FeatureName: return Version;
671+
#define OPTIONAL_LANGUAGE_FEATURE(FeatureName, SENumber, Description)
667672
#include "swift/Basic/Features.def"
668673
default:
669674
return std::nullopt;
@@ -677,6 +682,8 @@ bool swift::includeInModuleInterface(Feature feature) {
677682
return true;
678683
#define EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE(FeatureName, AvailableInProd) \
679684
case Feature::FeatureName: return false;
685+
#define OPTIONAL_LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
686+
LANGUAGE_FEATURE(FeatureName, SENumber, Description)
680687
#include "swift/Basic/Features.def"
681688
}
682689
llvm_unreachable("covered switch");

‎lib/ClangImporter/ImportDecl.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -2039,7 +2039,7 @@ namespace {
20392039
fd->getAttrs().add(new (Impl.SwiftContext)
20402040
UnsafeNonEscapableResultAttr(/*Implicit=*/true));
20412041
if (Impl.SwiftContext.LangOpts.hasFeature(
2042-
Feature::AllowUnsafeAttribute))
2042+
Feature::StrictMemorySafety))
20432043
fd->getAttrs().add(new (Impl.SwiftContext)
20442044
UnsafeAttr(/*Implicit=*/true));
20452045
}
@@ -2201,7 +2201,7 @@ namespace {
22012201
// We have to do this after populating ImportedDecls to avoid importing
22022202
// the same multiple times.
22032203
if (Impl.SwiftContext.LangOpts.hasFeature(
2204-
Feature::AllowUnsafeAttribute)) {
2204+
Feature::StrictMemorySafety)) {
22052205
if (const auto *ctsd =
22062206
dyn_cast<clang::ClassTemplateSpecializationDecl>(decl)) {
22072207
for (auto arg : ctsd->getTemplateArgs().asArray()) {
@@ -4178,13 +4178,13 @@ namespace {
41784178
LifetimeDependenceInfoRequest{result},
41794179
Impl.SwiftContext.AllocateCopy(lifetimeDependencies));
41804180
}
4181-
if (ASTContext.LangOpts.hasFeature(Feature::AllowUnsafeAttribute)) {
4181+
if (ASTContext.LangOpts.hasFeature(Feature::StrictMemorySafety)) {
41824182
for (auto [idx, param] : llvm::enumerate(decl->parameters())) {
41834183
if (swiftParams->get(idx)->getInterfaceType()->isEscapable())
41844184
continue;
41854185
if (param->hasAttr<clang::NoEscapeAttr>() || paramHasAnnotation[idx])
41864186
continue;
4187-
// We have a nonescapabe parameter that does not have its lifetime
4187+
// We have a nonescapable parameter that does not have its lifetime
41884188
// annotated nor is it marked noescape.
41894189
auto attr = new (ASTContext) UnsafeAttr(/*implicit=*/true);
41904190
result->getAttrs().add(attr);
@@ -8722,8 +8722,6 @@ ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
87228722
}
87238723

87248724
if (swiftAttr->getAttribute() == "unsafe") {
8725-
if (!SwiftContext.LangOpts.hasFeature(Feature::AllowUnsafeAttribute))
8726-
continue;
87278725
seenUnsafe = true;
87288726
continue;
87298727
}

‎lib/Driver/ToolChains.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
282282
options::OPT_disable_experimental_feature,
283283
options::OPT_enable_upcoming_feature,
284284
options::OPT_disable_upcoming_feature});
285+
inputArgs.AddLastArg(arguments, options::OPT_strict_memory_safety);
285286
inputArgs.AddLastArg(arguments, options::OPT_warn_implicit_overrides);
286287
inputArgs.AddLastArg(arguments, options::OPT_typo_correction_limit);
287288
inputArgs.AddLastArg(arguments, options::OPT_enable_app_extension);

‎lib/Frontend/CompilerInvocation.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,9 @@ static bool ParseEnabledFeatureArgs(LangOptions &Opts, ArgList &Args,
878878

879879
Opts.enableFeature(Feature::LayoutPrespecialization);
880880

881+
if (Args.hasArg(OPT_strict_memory_safety))
882+
Opts.enableFeature(Feature::StrictMemorySafety);
883+
881884
return HadError;
882885
}
883886

@@ -3909,7 +3912,7 @@ bool CompilerInvocation::parseArgs(
39093912
}
39103913
}
39113914

3912-
if (LangOpts.hasFeature(Feature::WarnUnsafe)) {
3915+
if (LangOpts.hasFeature(Feature::StrictMemorySafety)) {
39133916
if (SILOpts.RemoveRuntimeAsserts ||
39143917
SILOpts.AssertConfig == SILOptions::Unchecked) {
39153918
Diags.diagnose(SourceLoc(),

‎lib/Frontend/Frontend.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1455,7 +1455,7 @@ ModuleDecl *CompilerInstance::getMainModule() const {
14551455
MainModule->setAllowNonResilientAccess();
14561456
if (Invocation.getSILOptions().EnableSerializePackage)
14571457
MainModule->setSerializePackageEnabled();
1458-
if (Invocation.getLangOptions().hasFeature(Feature::WarnUnsafe))
1458+
if (Invocation.getLangOptions().hasFeature(Feature::StrictMemorySafety))
14591459
MainModule->setStrictMemorySafety(true);
14601460
if (Invocation.getLangOptions().hasFeature(Feature::ExtensibleEnums))
14611461
MainModule->setSupportsExtensibleEnums(true);

‎lib/Parse/ParseExpr.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,7 @@ ParserResult<Expr> Parser::parseExprSequenceElement(Diag<> message,
436436
return sub;
437437
}
438438

439-
if (Context.LangOpts.hasFeature(Feature::WarnUnsafe) &&
440-
Tok.isContextualKeyword("unsafe")) {
439+
if (Tok.isContextualKeyword("unsafe")) {
441440
Tok.setKind(tok::contextual_keyword);
442441
SourceLoc unsafeLoc = consumeToken();
443442
ParserResult<Expr> sub =

‎lib/Parse/ParseStmt.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -2379,8 +2379,7 @@ ParserResult<Stmt> Parser::parseStmtForEach(LabeledStmtInfo LabelInfo) {
23792379
}
23802380
}
23812381

2382-
if (Context.LangOpts.hasFeature(Feature::WarnUnsafe) &&
2383-
Tok.isContextualKeyword("unsafe")) {
2382+
if (Tok.isContextualKeyword("unsafe")) {
23842383
UnsafeLoc = consumeToken();
23852384
}
23862385

‎lib/Sema/TypeCheckAttr.cpp

+2-16
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ class AttributeChecker : public AttributeVisitor<AttributeChecker> {
189189
IGNORED_ATTR(LexicalLifetimes)
190190
IGNORED_ATTR(AllowFeatureSuppression)
191191
IGNORED_ATTR(PreInverseGenerics)
192+
IGNORED_ATTR(Safe)
193+
IGNORED_ATTR(Unsafe)
192194
#undef IGNORED_ATTR
193195

194196
private:
@@ -564,8 +566,6 @@ class AttributeChecker : public AttributeVisitor<AttributeChecker> {
564566
void visitStaticExclusiveOnlyAttr(StaticExclusiveOnlyAttr *attr);
565567
void visitWeakLinkedAttr(WeakLinkedAttr *attr);
566568
void visitSILGenNameAttr(SILGenNameAttr *attr);
567-
void visitUnsafeAttr(UnsafeAttr *attr);
568-
void visitSafeAttr(SafeAttr *attr);
569569
void visitLifetimeAttr(LifetimeAttr *attr);
570570
void visitAddressableSelfAttr(AddressableSelfAttr *attr);
571571
void visitAddressableForDependenciesAttr(AddressableForDependenciesAttr *attr);
@@ -8125,20 +8125,6 @@ void AttributeChecker::visitWeakLinkedAttr(WeakLinkedAttr *attr) {
81258125
attr->getAttrName(), Ctx.LangOpts.Target.str());
81268126
}
81278127

8128-
void AttributeChecker::visitUnsafeAttr(UnsafeAttr *attr) {
8129-
if (Ctx.LangOpts.hasFeature(Feature::AllowUnsafeAttribute))
8130-
return;
8131-
8132-
diagnoseAndRemoveAttr(attr, diag::unsafe_attr_disabled);
8133-
}
8134-
8135-
void AttributeChecker::visitSafeAttr(SafeAttr *attr) {
8136-
if (Ctx.LangOpts.hasFeature(Feature::AllowUnsafeAttribute))
8137-
return;
8138-
8139-
diagnoseAndRemoveAttr(attr, diag::unsafe_attr_disabled);
8140-
}
8141-
81428128
void AttributeChecker::visitLifetimeAttr(LifetimeAttr *attr) {}
81438129

81448130
void AttributeChecker::visitAddressableSelfAttr(AddressableSelfAttr *attr) {

‎lib/Sema/TypeCheckDeclOverride.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2259,7 +2259,7 @@ static bool checkSingleOverride(ValueDecl *override, ValueDecl *base) {
22592259
diagnoseOverrideForAvailability(override, base);
22602260
}
22612261

2262-
if (ctx.LangOpts.hasFeature(Feature::WarnUnsafe)) {
2262+
if (ctx.LangOpts.hasFeature(Feature::StrictMemorySafety)) {
22632263
// If the override is unsafe but the base declaration is not, then the
22642264
// inheritance itself is unsafe.
22652265
auto subs = SubstitutionMap::getOverrideSubstitutions(base, override);

‎lib/Sema/TypeCheckDeclPrimary.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -2395,7 +2395,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
23952395

23962396
// If strict memory safety checking is enabled, check the storage
23972397
// of the nominal type.
2398-
if (Ctx.LangOpts.hasFeature(Feature::WarnUnsafe) &&
2398+
if (Ctx.LangOpts.hasFeature(Feature::StrictMemorySafety) &&
23992399
!isa<ProtocolDecl>(nominal)) {
24002400
checkUnsafeStorage(nominal);
24012401
}
@@ -2468,7 +2468,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
24682468
// concurrency checking enabled.
24692469
if (ID->preconcurrency() &&
24702470
Ctx.LangOpts.StrictConcurrencyLevel == StrictConcurrency::Complete &&
2471-
Ctx.LangOpts.hasFeature(Feature::WarnUnsafe)) {
2471+
Ctx.LangOpts.hasFeature(Feature::StrictMemorySafety)) {
24722472
diagnoseUnsafeUse(UnsafeUse::forPreconcurrencyImport(ID));
24732473
}
24742474
}

‎lib/Sema/TypeCheckEffects.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,7 @@ class Classification {
11011101
bool considerAsync = !onlyEffect || *onlyEffect == EffectKind::Async;
11021102
bool considerThrows = !onlyEffect || *onlyEffect == EffectKind::Throws;
11031103
bool considerUnsafe = (!onlyEffect || *onlyEffect == EffectKind::Unsafe) &&
1104-
ctx.LangOpts.hasFeature(Feature::WarnUnsafe);
1104+
ctx.LangOpts.hasFeature(Feature::StrictMemorySafety);
11051105

11061106
// If we're tracking "unsafe" effects, compute them here.
11071107
if (considerUnsafe) {

‎lib/Sema/TypeCheckProtocol.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2641,7 +2641,7 @@ checkIndividualConformance(NormalProtocolConformance *conformance) {
26412641
// If we're enforcing strict memory safety and this conformance hasn't
26422642
// opted out, look for safe/unsafe witness mismatches.
26432643
if (conformance->getExplicitSafety() == ExplicitSafety::Unspecified &&
2644-
Context.LangOpts.hasFeature(Feature::WarnUnsafe)) {
2644+
Context.LangOpts.hasFeature(Feature::StrictMemorySafety)) {
26452645
// Collect all of the unsafe uses for this conformance.
26462646
SmallVector<UnsafeUse, 2> unsafeUses;
26472647
for (auto requirement: Proto->getMembers()) {

‎lib/Sema/TypeCheckUnsafe.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ bool swift::enumerateUnsafeUses(ArrayRef<ProtocolConformanceRef> conformances,
300300
continue;
301301

302302
ASTContext &ctx = conformance.getRequirement()->getASTContext();
303-
if (!ctx.LangOpts.hasFeature(Feature::WarnUnsafe))
303+
if (!ctx.LangOpts.hasFeature(Feature::StrictMemorySafety))
304304
return false;
305305

306306
if (!conformance.hasEffect(EffectKind::Unsafe))
@@ -365,7 +365,7 @@ bool swift::isUnsafeInConformance(const ValueDecl *requirement,
365365

366366
void swift::diagnoseUnsafeType(ASTContext &ctx, SourceLoc loc, Type type,
367367
llvm::function_ref<void(Type)> diagnose) {
368-
if (!ctx.LangOpts.hasFeature(Feature::WarnUnsafe))
368+
if (!ctx.LangOpts.hasFeature(Feature::StrictMemorySafety))
369369
return;
370370

371371
if (!type->isUnsafe() && !type->getCanonicalType()->isUnsafe())

‎test/Interop/Cxx/class/safe-interop-mode.swift

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11

22
// RUN: rm -rf %t
33
// RUN: split-file %s %t
4-
// RUN: %target-swift-frontend -typecheck -verify -I %swift_src_root/lib/ClangImporter/SwiftBridging -Xcc -std=c++20 -I %t/Inputs %t/test.swift -enable-experimental-feature AllowUnsafeAttribute -enable-experimental-feature WarnUnsafe -enable-experimental-feature LifetimeDependence -cxx-interoperability-mode=default -diagnostic-style llvm 2>&1
4+
// RUN: %target-swift-frontend -typecheck -verify -I %swift_src_root/lib/ClangImporter/SwiftBridging -Xcc -std=c++20 -I %t/Inputs %t/test.swift -strict-memory-safety -enable-experimental-feature LifetimeDependence -cxx-interoperability-mode=default -diagnostic-style llvm 2>&1
55

66
// REQUIRES: objc_interop
7-
// REQUIRES: swift_feature_AllowUnsafeAttribute
8-
// REQUIRES: swift_feature_WarnUnsafe
97
// REQUIRES: swift_feature_LifetimeDependence
108

119
//--- Inputs/module.modulemap

‎test/SILOptimizer/mutable_span_bounds_check_tests.swift

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %target-swift-frontend -emit-module-path %t/SpanExtras.swiftmodule %S/Inputs/SpanExtras.swift -enable-builtin-module -enable-experimental-feature LifetimeDependence -enable-experimental-feature AllowUnsafeAttribute -O
2+
// RUN: %target-swift-frontend -emit-module-path %t/SpanExtras.swiftmodule %S/Inputs/SpanExtras.swift -enable-builtin-module -enable-experimental-feature LifetimeDependence -O
33
// RUN: %target-swift-frontend -I %t -O -emit-sil %s -disable-availability-checking | %FileCheck %s --check-prefix=CHECK-SIL
44
// RUN: %target-swift-frontend -I %t -O -emit-ir %s -disable-availability-checking | %FileCheck %s --check-prefix=CHECK-IR
55

66
// REQUIRES: swift_in_compiler
77
// REQUIRES: swift_feature_LifetimeDependence
8-
// REQUIRES: swift_feature_AllowUnsafeAttribute
98

109
// REQUIRES: swift_stdlib_no_asserts, optimized_stdlib
1110

‎test/Unsafe/interface_printing.swift

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
// RUN: %empty-directory(%t)
22

3-
// RUN: %target-swift-frontend -swift-version 5 -enable-library-evolution -module-name unsafe -emit-module -o %t/unsafe.swiftmodule -emit-module-interface-path - %s -enable-experimental-feature AllowUnsafeAttribute | %FileCheck %s
3+
// RUN: %target-swift-frontend -swift-version 5 -enable-library-evolution -module-name unsafe -emit-module -o %t/unsafe.swiftmodule -emit-module-interface-path - %s | %FileCheck %s
44

5-
// REQUIRES: swift_feature_AllowUnsafeAttribute
6-
7-
// CHECK: #if compiler(>=5.3) && $AllowUnsafeAttribute
5+
// CHECK: #if compiler(>=5.3) && $MemorySafetyAttributes
86
// CHECK: @unsafe public func testFunction()
97
// CHECK: #else
108
// CHECK: public func testFunction()

‎test/Unsafe/module-interface.swift

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
// RUN: %target-swift-emit-module-interface(%t.swiftinterface) %s -module-name UserModule -enable-experimental-feature AllowUnsafeAttribute -enable-experimental-feature WarnUnsafe
1+
// RUN: %target-swift-emit-module-interface(%t.swiftinterface) %s -module-name UserModule -strict-memory-safety
22
// RUN: %target-swift-typecheck-module-from-interface(%t.swiftinterface) -module-name UserModule
33
// RUN: %FileCheck %s < %t.swiftinterface
44

5-
// REQUIRES: swift_feature_AllowUnsafeAttribute
6-
// REQUIRES: swift_feature_WarnUnsafe
7-
8-
// CHECK: #if compiler(>=5.3) && $AllowUnsafeAttribute
5+
// CHECK: #if compiler(>=5.3) && $MemorySafetyAttributes
96
// CHECK: @unsafe public func getIntUnsafely() -> Swift.Int
107
// CHECK: #else
118
// CHECK: public func getIntUnsafely() -> Swift.Int
@@ -25,7 +22,7 @@ public protocol P {
2522

2623
// CHECK: public struct X : @unsafe UserModule.P
2724
public struct X: @unsafe P {
28-
// CHECK: #if compiler(>=5.3) && $AllowUnsafeAttribute
25+
// CHECK: #if compiler(>=5.3) && $MemorySafetyAttributes
2926
// CHECK: @unsafe public func f()
3027
// CHECK: #else
3128
// CHECK: public func f()

‎test/Unsafe/module-trace.swift

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %target-swift-frontend -emit-module-path %t/unsafe_swift_decls.swiftmodule %S/Inputs/unsafe_swift_decls.swift -enable-experimental-feature AllowUnsafeAttribute
3-
// RUN: %target-swift-frontend -emit-module-path %t/safe_swift_decls.swiftmodule %S/Inputs/safe_swift_decls.swift -enable-experimental-feature AllowUnsafeAttribute -enable-experimental-feature WarnUnsafe
2+
// RUN: %target-swift-frontend -emit-module-path %t/unsafe_swift_decls.swiftmodule %S/Inputs/unsafe_swift_decls.swift
3+
// RUN: %target-swift-frontend -emit-module-path %t/safe_swift_decls.swiftmodule %S/Inputs/safe_swift_decls.swift -strict-memory-safety
44

5-
// RUN: %target-typecheck-verify-swift -enable-experimental-feature AllowUnsafeAttribute -enable-experimental-feature WarnUnsafe -I %S/Inputs -I %t -emit-loaded-module-trace-path %t/unsafe.trace
5+
// RUN: %target-typecheck-verify-swift -strict-memory-safety -I %S/Inputs -I %t -emit-loaded-module-trace-path %t/unsafe.trace
66

77
// RUN: %FileCheck -check-prefix TRACE %s < %t/unsafe.trace
88

9-
// REQUIRES: swift_feature_AllowUnsafeAttribute
10-
// REQUIRES: swift_feature_WarnUnsafe
11-
129
import unsafe_decls
1310
import unsafe_swift_decls
1411
import safe_swift_decls

‎test/Unsafe/safe.swift

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
// RUN: %target-typecheck-verify-swift -enable-experimental-feature AllowUnsafeAttribute -enable-experimental-feature WarnUnsafe -print-diagnostic-groups
1+
// RUN: %target-typecheck-verify-swift -strict-memory-safety -print-diagnostic-groups
2+
3+
// The feature flag should be enabled.
4+
#if !hasFeature(StrictMemorySafety)
5+
#error("Strict memory safety is not enabled!")
6+
#endif
27

3-
// REQUIRES: swift_feature_AllowUnsafeAttribute
4-
// REQUIRES: swift_feature_WarnUnsafe
58

69
@unsafe
710
func unsafeFunction() { }

‎test/Unsafe/safe_argument_suppression.swift

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
// RUN: %target-typecheck-verify-swift -enable-experimental-feature AllowUnsafeAttribute -enable-experimental-feature WarnUnsafe -print-diagnostic-groups
2-
3-
// REQUIRES: swift_feature_AllowUnsafeAttribute
4-
// REQUIRES: swift_feature_WarnUnsafe
1+
// RUN: %target-typecheck-verify-swift -strict-memory-safety -print-diagnostic-groups
52

63
@unsafe
74
class NotSafe {

‎test/Unsafe/unsafe-suppression.swift

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
// RUN: %target-typecheck-verify-swift -enable-experimental-feature AllowUnsafeAttribute -enable-experimental-feature WarnUnsafe -print-diagnostic-groups
2-
3-
// REQUIRES: swift_feature_AllowUnsafeAttribute
4-
// REQUIRES: swift_feature_WarnUnsafe
1+
// RUN: %target-typecheck-verify-swift -strict-memory-safety -print-diagnostic-groups
52

63
@unsafe
74
func iAmUnsafe() { }

‎test/Unsafe/unsafe.swift

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %target-swift-frontend -emit-module-path %t/unsafe_swift_decls.swiftmodule %S/Inputs/unsafe_swift_decls.swift -enable-experimental-feature AllowUnsafeAttribute
2+
// RUN: %target-swift-frontend -emit-module-path %t/unsafe_swift_decls.swiftmodule %S/Inputs/unsafe_swift_decls.swift
33

4-
// RUN: %target-typecheck-verify-swift -enable-experimental-feature AllowUnsafeAttribute -enable-experimental-feature WarnUnsafe -I %t -print-diagnostic-groups
4+
// RUN: %target-typecheck-verify-swift -strict-memory-safety -I %t -print-diagnostic-groups
55

66
// Make sure everything compiles without error when unsafe code is allowed.
7-
// RUN: %target-swift-frontend -typecheck -enable-experimental-feature AllowUnsafeAttribute %s -I %t
8-
9-
// REQUIRES: swift_feature_AllowUnsafeAttribute
10-
// REQUIRES: swift_feature_WarnUnsafe
7+
// RUN: %target-swift-frontend -typecheck %s -I %t
118

129
import unsafe_swift_decls
1310

‎test/Unsafe/unsafe_c_imports.swift

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
// RUN: %target-typecheck-verify-swift -enable-experimental-feature AllowUnsafeAttribute -enable-experimental-feature WarnUnsafe -I %S/Inputs
2-
3-
// REQUIRES: swift_feature_AllowUnsafeAttribute
4-
// REQUIRES: swift_feature_WarnUnsafe
1+
// RUN: %target-typecheck-verify-swift -strict-memory-safety -I %S/Inputs
52

63
import unsafe_decls
74

‎test/Unsafe/unsafe_command_line.swift

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
// RUN: %target-swift-frontend -typecheck -enable-experimental-feature WarnUnsafe -Ounchecked -disable-access-control %s 2>&1 | %FileCheck %s
2-
3-
// REQUIRES: swift_feature_WarnUnsafe
1+
// RUN: %target-swift-frontend -typecheck -strict-memory-safety -Ounchecked -disable-access-control %s 2>&1 | %FileCheck %s
42

53
// CHECK: warning: '-Ounchecked' is not memory-safe
64
// CHECK: warning: '-disable-access-control' is not memory-safe

‎test/Unsafe/unsafe_concurrency.swift

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %target-swift-frontend -emit-module-path %t/unsafe_swift_decls.swiftmodule %S/Inputs/unsafe_swift_decls.swift -enable-experimental-feature AllowUnsafeAttribute -enable-experimental-feature AllowUnsafeAttribute
2+
// RUN: %target-swift-frontend -emit-module-path %t/unsafe_swift_decls.swiftmodule %S/Inputs/unsafe_swift_decls.swift
33

4-
// RUN: %target-typecheck-verify-swift -enable-experimental-feature WarnUnsafe -enable-experimental-feature StrictConcurrency -enable-experimental-feature AllowUnsafeAttribute -I %t
4+
// RUN: %target-typecheck-verify-swift -strict-memory-safety -enable-experimental-feature StrictConcurrency -I %t
55

66
// REQUIRES: concurrency
77
// REQUIRES: swift_feature_StrictConcurrency
8-
// REQUIRES: swift_feature_WarnUnsafe
9-
// REQUIRES: swift_feature_AllowUnsafeAttribute
108

119
@preconcurrency import unsafe_swift_decls // expected-warning{{@preconcurrency import is not memory-safe because it can silently introduce data races}}
1210

‎test/Unsafe/unsafe_disallowed.swift

-5
This file was deleted.

‎test/Unsafe/unsafe_feature.swift

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
4+
// Can use @unsafe and @safe without strict memory safety being enabled.
5+
@unsafe func f() { }
6+
@safe func g(_: UnsafeRawPointer) { }
7+
8+
protocol P {
9+
func f()
10+
}
11+
12+
struct X: @unsafe P {
13+
@unsafe func f() { }
14+
}
15+
16+
// The feature flag is not enabled, though.
17+
#if hasFeature(StrictMemorySafety)
18+
#error("Strict memory safety is not enabled!")
19+
#endif

‎test/Unsafe/unsafe_imports.swift

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %target-swift-frontend -emit-module-path %t/unsafe_swift_decls.swiftmodule %S/Inputs/unsafe_swift_decls.swift -enable-experimental-feature AllowUnsafeAttribute
2+
// RUN: %target-swift-frontend -emit-module-path %t/unsafe_swift_decls.swiftmodule %S/Inputs/unsafe_swift_decls.swift
33

4-
// RUN: %target-typecheck-verify-swift -enable-experimental-feature AllowUnsafeAttribute -enable-experimental-feature WarnUnsafe -I %S/Inputs -I %t
5-
6-
// REQUIRES: swift_feature_AllowUnsafeAttribute
7-
// REQUIRES: swift_feature_WarnUnsafe
4+
// RUN: %target-typecheck-verify-swift -strict-memory-safety -I %S/Inputs -I %t
85

96
import unsafe_decls
107
import unsafe_swift_decls

‎test/Unsafe/unsafe_in_unsafe.swift

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
// RUN: %target-typecheck-verify-swift -enable-experimental-feature AllowUnsafeAttribute -print-diagnostic-groups
2-
3-
// REQUIRES: swift_feature_AllowUnsafeAttribute
4-
1+
// RUN: %target-typecheck-verify-swift -print-diagnostic-groups
52

63
protocol P { }
74

‎test/Unsafe/unsafe_stdlib.swift

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
// RUN: %target-typecheck-verify-swift -enable-experimental-feature WarnUnsafe
1+
// RUN: %target-typecheck-verify-swift -strict-memory-safety
22

33
// Make sure everything compiles without error when unsafe code is allowed.
4-
// RUN: %target-swift-frontend -typecheck -enable-experimental-feature AllowUnsafeAttribute -warnings-as-errors %s
5-
6-
// REQUIRES: swift_feature_AllowUnsafeAttribute
7-
// REQUIRES: swift_feature_WarnUnsafe
4+
// RUN: %target-swift-frontend -typecheck -warnings-as-errors %s
85

96
func test(
107
x: OpaquePointer,

‎test/lit.swift-features.cfg.inc

+1
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ def language_feature(feature_name, enabled):
3131
#define UPCOMING_FEATURE(FeatureName, SENumber, Version) language_feature(#FeatureName, True)
3232
#define EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd) language_feature(#FeatureName, #AvailableInProd == "true")
3333
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) language_feature(#FeatureName, True)
34+
#define OPTIONAL_LANGUAGE_FEATURE(FeatureName, SENumber, Description) language_feature(#FeatureName, True)
3435

3536
#include <swift/Basic/Features.def>

0 commit comments

Comments
 (0)
Please sign in to comment.