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

include/swift/AST/Expr.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2173,6 +2173,36 @@ class AwaitExpr final : public IdentityExpr {
21732173
}
21742174
};
21752175

2176+
/// UnsafeExpr - An 'unsafe' surrounding an expression, marking that the
2177+
/// expression contains uses of unsafe declarations.
2178+
///
2179+
/// getSemanticsProvidingExpr() looks through this because it doesn't
2180+
/// provide the value and only very specific clients care where the
2181+
/// 'unsafe' was written.
2182+
class UnsafeExpr final : public IdentityExpr {
2183+
SourceLoc UnsafeLoc;
2184+
public:
2185+
UnsafeExpr(SourceLoc unsafeLoc, Expr *sub, Type type = Type(),
2186+
bool implicit = false)
2187+
: IdentityExpr(ExprKind::Unsafe, sub, type, implicit),
2188+
UnsafeLoc(unsafeLoc) {
2189+
}
2190+
2191+
static UnsafeExpr *createImplicit(ASTContext &ctx, SourceLoc unsafeLoc, Expr *sub, Type type = Type()) {
2192+
return new (ctx) UnsafeExpr(unsafeLoc, sub, type, /*implicit=*/true);
2193+
}
2194+
2195+
SourceLoc getLoc() const { return UnsafeLoc; }
2196+
2197+
SourceLoc getUnsafeLoc() const { return UnsafeLoc; }
2198+
SourceLoc getStartLoc() const { return UnsafeLoc; }
2199+
SourceLoc getEndLoc() const { return getSubExpr()->getEndLoc(); }
2200+
2201+
static bool classof(const Expr *e) {
2202+
return e->getKind() == ExprKind::Unsafe;
2203+
}
2204+
};
2205+
21762206
/// ConsumeExpr - A 'consume' surrounding an lvalue expression marking the
21772207
/// lvalue as needing to be moved.
21782208
class ConsumeExpr final : public Expr {

include/swift/AST/ExprNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ ABSTRACT_EXPR(Identity, Expr)
107107
EXPR(Paren, IdentityExpr)
108108
EXPR(DotSelf, IdentityExpr)
109109
EXPR(Await, IdentityExpr)
110+
EXPR(Unsafe, IdentityExpr)
110111
EXPR(Borrow, IdentityExpr)
111112
EXPR(UnresolvedMemberChainResult, IdentityExpr)
112113
EXPR_RANGE(Identity, Paren, UnresolvedMemberChainResult)

include/swift/AST/NameLookup.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -612,21 +612,17 @@ struct InheritedNominalEntry : Located<NominalTypeDecl *> {
612612
/// The location of the "unsafe" attribute if present.
613613
SourceLoc unsafeLoc;
614614

615-
/// The range of the "safe(unchecked)" attribute if present.
616-
SourceRange safeRange;
617-
618615
/// Whether this inherited entry was suppressed via "~".
619616
bool isSuppressed;
620617

621618
InheritedNominalEntry() { }
622619

623620
InheritedNominalEntry(NominalTypeDecl *item, SourceLoc loc,
624621
SourceLoc uncheckedLoc, SourceLoc preconcurrencyLoc,
625-
SourceLoc unsafeLoc, SourceRange safeRange,
626-
bool isSuppressed)
622+
SourceLoc unsafeLoc, bool isSuppressed)
627623
: Located(item, loc), uncheckedLoc(uncheckedLoc),
628624
preconcurrencyLoc(preconcurrencyLoc), unsafeLoc(unsafeLoc),
629-
safeRange(safeRange), isSuppressed(isSuppressed) {}
625+
isSuppressed(isSuppressed) {}
630626
};
631627

632628
/// Retrieve the set of nominal type declarations that are directly

include/swift/AST/ProtocolConformance.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -659,11 +659,6 @@ class NormalProtocolConformance : public RootProtocolConformance,
659659
return getOptions().contains(ProtocolConformanceFlags::Unsafe);
660660
}
661661

662-
/// Whether this is an "safe(unchecked)" conformance.
663-
bool isSafe() const {
664-
return getOptions().contains(ProtocolConformanceFlags::Safe);
665-
}
666-
667662
/// Determine whether we've lazily computed the associated conformance array
668663
/// already.
669664
bool hasComputedAssociatedConformances() const {

include/swift/AST/ProtocolConformanceOptions.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,8 @@ enum class ProtocolConformanceFlags {
3131
/// @unsafe conformance
3232
Unsafe = 0x04,
3333

34-
/// @safe(unchecked) conformance
35-
Safe = 0x08,
36-
3734
/// @retroactive conformance
38-
Retroactive = 0x10,
35+
Retroactive = 0x08,
3936

4037
// Note: whenever you add a bit here, update
4138
// NumProtocolConformanceOptions below.
@@ -52,7 +49,7 @@ inline ProtocolConformanceOptions operator|(
5249
}
5350

5451
enum : unsigned {
55-
NumProtocolConformanceOptions = 5
52+
NumProtocolConformanceOptions = 4
5653
};
5754

5855
} // end namespace swift

include/swift/AST/SourceFileExtras.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#ifndef SWIFT_AST_SOURCEFILEEXTRAS_H
1414
#define SWIFT_AST_SOURCEFILEEXTRAS_H
1515

16-
#include "swift/AST/UnsafeUse.h"
1716
#include "llvm/ADT/DenseMap.h"
1817
#include <vector>
1918

@@ -24,11 +23,6 @@ class Decl;
2423
/// Extra information associated with a source file that is lazily created and
2524
/// stored in a separately-allocated side structure.
2625
struct SourceFileExtras {
27-
/// Captures all of the unsafe uses associated with a given declaration.
28-
///
29-
/// The declaration is the entity that can be annotated (e.g., with @unsafe)
30-
/// to suppress all of the unsafe-related diagnostics listed here.
31-
llvm::DenseMap<const Decl *, std::vector<UnsafeUse>> unsafeUses;
3226
};
3327

3428
}

include/swift/AST/TypeAttr.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ SIMPLE_TYPE_ATTR(retroactive, Retroactive)
6161
SIMPLE_TYPE_ATTR(unchecked, Unchecked)
6262
SIMPLE_TYPE_ATTR(preconcurrency, Preconcurrency)
6363
SIMPLE_TYPE_ATTR(unsafe, Unsafe)
64-
SIMPLE_TYPE_ATTR(safe, Safe)
6564
SIMPLE_TYPE_ATTR(_local, Local)
6665
SIMPLE_TYPE_ATTR(_noMetadata, NoMetadata)
6766
TYPE_ATTR(_opaqueReturnTypeOf, OpaqueReturnTypeOf)

0 commit comments

Comments
 (0)