Skip to content

Commit ede7bfe

Browse files
authored
Merge pull request #78554 from DougGregor/unsafe-effect
2 parents 03e0775 + 6ec22c2 commit ede7bfe

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+840
-872
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,6 +1505,11 @@ BridgedUnresolvedSpecializeExpr BridgedUnresolvedSpecializeExpr_createParsed(
15051505
BridgedSourceLoc cLAngleLoc, BridgedArrayRef cArguments,
15061506
BridgedSourceLoc cRAngleLoc);
15071507

1508+
SWIFT_NAME("BridgedUnsafeExpr.createParsed(_:unsafeLoc:subExpr:)")
1509+
BridgedUnsafeExpr BridgedUnsafeExpr_createParsed(BridgedASTContext cContext,
1510+
BridgedSourceLoc cUnsafeLoc,
1511+
BridgedExpr cSubExpr);
1512+
15081513
SWIFT_NAME("BridgedInOutExpr.createParsed(_:loc:subExpr:)")
15091514
BridgedInOutExpr BridgedInOutExpr_createParsed(BridgedASTContext cContext,
15101515
BridgedSourceLoc cLoc,

include/swift/AST/Attr.h

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2830,26 +2830,6 @@ class RawLayoutAttr final : public DeclAttribute {
28302830
UNIMPLEMENTED_CLONE(RawLayoutAttr)
28312831
};
28322832

2833-
class SafeAttr final : public DeclAttribute {
2834-
public:
2835-
/// The optional message.
2836-
const StringRef message;
2837-
2838-
SafeAttr(SourceLoc atLoc, SourceRange range, StringRef message,
2839-
bool isImplicit = false)
2840-
: DeclAttribute(DeclAttrKind::Safe, atLoc, range, isImplicit),
2841-
message(message) { }
2842-
2843-
static bool classof(const DeclAttribute *DA) {
2844-
return DA->getKind() == DeclAttrKind::Safe;
2845-
}
2846-
2847-
/// Create a copy of this attribute.
2848-
SafeAttr *clone(ASTContext &ctx) const {
2849-
return new (ctx) SafeAttr(AtLoc, Range, message, isImplicit());
2850-
}
2851-
};
2852-
28532833
class LifetimeAttr final : public DeclAttribute {
28542834
LifetimeEntry *entry;
28552835

include/swift/AST/AvailabilityContext.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,6 @@ class AvailabilityContext {
8787
/// Returns true if this context is `@_unavailableInEmbedded`.
8888
bool isUnavailableInEmbedded() const;
8989

90-
/// Returns true if this context allows the use of unsafe constructs inside
91-
/// it.
92-
bool allowsUnsafe() const;
93-
9490
/// Constrain with another `AvailabilityContext`.
9591
void constrainWithContext(const AvailabilityContext &other, ASTContext &ctx);
9692

@@ -107,9 +103,6 @@ class AvailabilityContext {
107103
constrainWithDeclAndPlatformRange(const Decl *decl,
108104
const AvailabilityRange &platformRange);
109105

110-
/// Constrain to allow unsafe code.
111-
void constrainWithAllowsUnsafe(ASTContext &ctx);
112-
113106
/// Returns true if `other` is as available or is more available.
114107
bool isContainedIn(const AvailabilityContext other) const;
115108

include/swift/AST/AvailabilityContextStorage.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ struct AvailabilityContext::PlatformInfo {
4343
/// platform.
4444
unsigned IsDeprecated : 1;
4545

46-
/// Whether or not the context allows unsafe code within it, e.g., via the
47-
/// `@unsafe` attribute.
48-
unsigned AllowsUnsafe: 1;
49-
5046
/// Sets each field to the value of the corresponding field in `other` if the
5147
/// other is more restrictive. Returns true if any field changed as a result
5248
/// of adding this constraint.
@@ -67,7 +63,6 @@ struct AvailabilityContext::PlatformInfo {
6763
ID.AddBoolean(IsUnavailableInEmbedded);
6864
ID.AddInteger(static_cast<uint8_t>(UnavailablePlatform));
6965
ID.AddBoolean(IsDeprecated);
70-
ID.AddBoolean(AllowsUnsafe);
7166
}
7267
};
7368

include/swift/AST/Decl.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,10 +1207,6 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl>, public Swi
12071207
/// used in a "safe" dialect.
12081208
bool isUnsafe() const;
12091209

1210-
/// Whether this declaration explicitly states that it is allowed to contain
1211-
/// unsafe code.
1212-
bool allowsUnsafe() const;
1213-
12141210
private:
12151211
bool isUnsafeComputed() const {
12161212
return Bits.Decl.IsUnsafeComputed;
@@ -1824,10 +1820,6 @@ struct InheritedEntry : public TypeLoc {
18241820
return getOptions().contains(ProtocolConformanceFlags::Unsafe);
18251821
}
18261822

1827-
bool isSafe() const {
1828-
return getOptions().contains(ProtocolConformanceFlags::Safe);
1829-
}
1830-
18311823
bool isSuppressed() const { return IsSuppressed; }
18321824

18331825
void setOption(ProtocolConformanceFlags flag) {
@@ -1957,7 +1949,6 @@ class ExtensionDecl final : public GenericContext, public Decl,
19571949
friend class Decl;
19581950
public:
19591951
using Decl::getASTContext;
1960-
using Decl::allowsUnsafe;
19611952

19621953
/// Create a new extension declaration.
19631954
static ExtensionDecl *create(ASTContext &ctx, SourceLoc extensionLoc,
@@ -3435,7 +3426,6 @@ class GenericTypeDecl : public GenericContext, public TypeDecl {
34353426
using DeclContext::operator new;
34363427
using DeclContext::operator delete;
34373428
using TypeDecl::getDeclaredInterfaceType;
3438-
using Decl::allowsUnsafe;
34393429

34403430
static bool classof(const DeclContext *C) {
34413431
if (auto D = C->getAsDecl())

include/swift/AST/DeclAttr.def

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -531,11 +531,7 @@ SIMPLE_DECL_ATTR(_addressableForDependencies, AddressableForDependencies,
531531
OnNominalType | ABIBreakingToAdd | ABIBreakingToRemove | APIBreakingToAdd | APIBreakingToRemove | UserInaccessible,
532532
163)
533533

534-
DECL_ATTR(safe, Safe,
535-
OnAbstractFunction | OnSubscript | OnVar | OnMacro | OnNominalType |
536-
OnExtension | OnTypeAlias | OnImport | UserInaccessible |
537-
ABIStableToAdd | ABIStableToRemove | APIBreakingToAdd | APIStableToRemove,
538-
164)
534+
// 164 was the never-shipped @safe attribute and can be reused
539535

540536
DECL_ATTR(abi, ABI,
541537
OnAbstractFunction | OnVar /* will eventually add types */ | LongAttribute | ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,

include/swift/AST/DeclContext.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -718,10 +718,6 @@ class alignas(1 << DeclContextAlignInBits) DeclContext
718718
/// target. Used for conformance lookup disambiguation.
719719
bool isAlwaysAvailableConformanceContext() const;
720720

721-
/// Determines whether this context is explicitly allowed to use unsafe
722-
/// constructs.
723-
bool allowsUnsafe() const;
724-
725721
/// \returns true if traversal was aborted, false otherwise.
726722
bool walkContext(ASTWalker &Walker);
727723

include/swift/AST/DiagnosticsParse.def

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,8 @@ ERROR(expected_expr_after_ternary_colon,none,
13711371
"expected expression after '? ... :' in ternary expression", ())
13721372
ERROR(expected_expr_after_await, none,
13731373
"expected expression after 'await'", ())
1374+
ERROR(expected_expr_after_unsafe, none,
1375+
"expected expression after 'unsafe'", ())
13741376
ERROR(expected_expr_after_move, none,
13751377
"expected expression after 'consume'", ())
13761378
ERROR(expected_expr_after_copy, none,
@@ -2109,9 +2111,6 @@ ERROR(parser_new_parser_errors,none,
21092111
"new Swift parser generated errors for code that C++ parser accepted",
21102112
())
21112113

2112-
ERROR(safe_attr_unchecked,none,
2113-
"'@safe' attribute must be written as '@safe(unchecked)'", ())
2114-
21152114
// MARK: Reference Binding Diagnostics
21162115
ERROR(sil_markuncheckedreferencebinding_requires_attribute,none,
21172116
"mark_unchecked_reference_binding requires an attribute like [inout]", ())

include/swift/AST/DiagnosticsSema.def

Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4523,7 +4523,7 @@ WARNING(nan_comparison_both_nan, none,
45234523
(StringRef, bool))
45244524

45254525
// If you change this, also change enum TryKindForDiagnostics.
4526-
#define TRY_KIND_SELECT(SUB) "%select{try|try!|try?|await}" #SUB
4526+
#define TRY_KIND_SELECT(SUB) "%select{try|try!|try?|await|unsafe}" #SUB
45274527

45284528
ERROR(try_rhs,none,
45294529
"'" TRY_KIND_SELECT(0) "' cannot appear to the right of a "
@@ -8079,11 +8079,6 @@ NOTE(sending_function_result_with_sending_param_note, none,
80798079
ERROR(unsafe_attr_disabled,none,
80808080
"attribute requires '-enable-experimental-feature AllowUnsafeAttribute'", ())
80818081

8082-
GROUPED_WARNING(decl_involves_unsafe,Unsafe,none,
8083-
"%kindbase0 involves unsafe code; "
8084-
"use %select{'@unsafe' to indicate that its use is not memory-safe|"
8085-
"'@safe(unchecked)' to assert that the code is memory-safe}1",
8086-
(const Decl *, bool))
80878082
NOTE(note_reference_to_unsafe_decl,none,
80888083
"%select{reference|call}0 to unsafe %kind1",
80898084
(bool, const ValueDecl *))
@@ -8103,6 +8098,12 @@ NOTE(note_reference_exclusivity_unchecked,none,
81038098
NOTE(note_use_of_unsafe_conformance_is_unsafe,none,
81048099
"@unsafe conformance of %0 to %kind1 involves unsafe code",
81058100
(Type, const ValueDecl *))
8101+
8102+
GROUPED_WARNING(decl_signature_involves_unsafe,Unsafe,none,
8103+
"%kindbase0 has an interface that is not memory-safe; "
8104+
"use '@unsafe' to indicate that its use is unsafe",
8105+
(const Decl *))
8106+
81068107
GROUPED_WARNING(conformance_involves_unsafe,Unsafe,none,
81078108
"conformance of %0 to %kind1 involves unsafe code; use '@unsafe' to "
81088109
"indicate that the conformance is not memory-safe",
@@ -8114,44 +8115,19 @@ NOTE(note_type_witness_unsafe,none,
81148115
"unsafe type %0 cannot satisfy safe associated type %1",
81158116
(Type, DeclName))
81168117

8117-
GROUPED_WARNING(override_safe_withunsafe,Unsafe,none,
8118+
GROUPED_WARNING(override_safe_with_unsafe,Unsafe,none,
81188119
"override of safe %0 with unsafe %0", (DescriptiveDeclKind))
8119-
GROUPED_WARNING(use_of_unsafe_conformance_is_unsafe,Unsafe,none,
8120-
"@unsafe conformance of %0 to %kind1 involves unsafe code",
8121-
(Type, const ValueDecl *))
8122-
GROUPED_WARNING(reference_unowned_unsafe,Unsafe,none,
8123-
"reference to unowned(unsafe) %kind0 is unsafe", (const ValueDecl *))
8124-
GROUPED_WARNING(reference_exclusivity_unchecked,Unsafe,none,
8125-
"reference to @exclusivity(unchecked) %kind0 is unsafe", (const ValueDecl *))
8126-
8127-
GROUPED_WARNING(reference_to_nonisolated_unsafe,Unsafe,none,
8128-
"reference to nonisolated(unsafe) %kind0 is unsafe in concurrently-executing code",
8129-
(const ValueDecl *))
8130-
GROUPED_WARNING(reference_to_unsafe_decl,Unsafe,none,
8131-
"%select{reference|call}0 to unsafe %kindbase1",
8132-
(bool, const ValueDecl *))
8133-
GROUPED_WARNING(reference_to_unsafe_typed_decl,Unsafe,none,
8134-
"%select{reference|call}0 to %kindbase1 involves unsafe type %2",
8135-
(bool, const ValueDecl *, Type))
8136-
GROUPED_WARNING(reference_to_unsafe_through_typealias,Unsafe,none,
8137-
"reference to %kind0 whose underlying type involves unsafe type %1",
8138-
(const ValueDecl *, Type))
8120+
81398121
GROUPED_WARNING(preconcurrency_import_unsafe,Unsafe,none,
81408122
"@preconcurrency import is not memory-safe because it can silently "
8141-
"introduce data races; use '@safe(unchecked)' to assert that the "
8142-
"code is memory-safe", ())
8143-
NOTE(encapsulate_unsafe_in_enclosing_context,none,
8144-
"make %kindbase0 @safe(unchecked) to allow it to use unsafe constructs in its definition",
8145-
(const Decl *))
8146-
NOTE(make_enclosing_context_unsafe,none,
8147-
"make %kindbase0 @unsafe to indicate that its use is not memory-safe",
8148-
(const Decl *))
8123+
"introduce data races", ())
8124+
GROUPED_WARNING(unsafe_without_unsafe,Unsafe,none,
8125+
"expression uses unsafe constructs but is not marked with 'unsafe'", ())
8126+
WARNING(no_unsafe_in_unsafe,none,
8127+
"no unsafe operations occur within 'unsafe' expression", ())
81498128
NOTE(make_subclass_unsafe,none,
81508129
"make class %0 @unsafe to allow unsafe overrides of safe superclass methods",
81518130
(DeclName))
8152-
NOTE(make_conforming_context_unsafe,none,
8153-
"make the enclosing %0 @unsafe to allow unsafe conformance to protocol %1",
8154-
(DescriptiveDeclKind, DeclName))
81558131

81568132
//===----------------------------------------------------------------------===//
81578133
// MARK: Value Generics

include/swift/AST/Effects.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ class ProtocolDecl;
4040

4141
enum class EffectKind : uint8_t {
4242
Throws = 1 << 0,
43-
Async = 1 << 1
43+
Async = 1 << 1,
44+
Unsafe = 1 << 2,
4445
};
4546
using PossibleEffects = OptionSet<EffectKind>;
4647

0 commit comments

Comments
 (0)