|
17 | 17 | #include "swift/AST/ASTContext.h"
|
18 | 18 | #include "swift/AST/UnsafeUse.h"
|
19 | 19 | #include "swift/AST/DiagnosticsSema.h"
|
| 20 | +#include "swift/AST/SourceFile.h" |
| 21 | +#include "swift/AST/SourceFileExtras.h" |
20 | 22 | #include "TypeCheckAvailability.h"
|
21 | 23 | #include "TypeCheckType.h"
|
22 | 24 |
|
23 | 25 | using namespace swift;
|
24 | 26 |
|
| 27 | +static std::pair<const Decl *, bool /*inDefinition*/> |
| 28 | +enclosingContextForUnsafe(const UnsafeUse &use) { |
| 29 | + return swift::enclosingContextForUnsafe(use.getLocation(), |
| 30 | + use.getDeclContext()); |
| 31 | +} |
| 32 | + |
| 33 | +/// Whether this particular unsafe use occurs within the definition of an |
| 34 | +/// entity, but not in its signature, meaning that the unsafety could be |
| 35 | +/// encapsulated. |
| 36 | +static bool isUnsafeUseInDefinition(const UnsafeUse &use) { |
| 37 | + switch (use.getKind()) { |
| 38 | + case UnsafeUse::Override: |
| 39 | + case UnsafeUse::Witness: |
| 40 | + case UnsafeUse::TypeWitness: |
| 41 | + case UnsafeUse::UnsafeConformance: |
| 42 | + case UnsafeUse::UnownedUnsafe: |
| 43 | + case UnsafeUse::NonisolatedUnsafe: |
| 44 | + // Never part of the definition. These are always part of the interface. |
| 45 | + return false; |
| 46 | + |
| 47 | + case UnsafeUse::ReferenceToUnsafe: |
| 48 | + case UnsafeUse::CallToUnsafe: |
| 49 | + return enclosingContextForUnsafe(use).second; |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | + |
25 | 54 | static void suggestUnsafeOnEnclosingDecl(
|
26 | 55 | SourceLoc referenceLoc, const DeclContext *referenceDC) {
|
27 | 56 | const Decl *decl = nullptr;
|
@@ -58,7 +87,39 @@ static void suggestUnsafeMarkerOnConformance(
|
58 | 87 | ).fixItInsert(decl->getAttributeInsertionLoc(false), "@unsafe ");
|
59 | 88 | }
|
60 | 89 |
|
61 |
| -void swift::diagnoseUnsafeUse(const UnsafeUse &use) { |
| 90 | +static bool shouldDeferUnsafeUseIn(const Decl *decl) { |
| 91 | + if (auto func = dyn_cast_or_null<AbstractFunctionDecl>(decl)) { |
| 92 | + if (func->hasBody()) { |
| 93 | + return true; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return false; |
| 98 | +} |
| 99 | + |
| 100 | +/// Retrieve the extra information |
| 101 | +static SourceFileExtras &getSourceFileExtrasFor(const Decl *decl) { |
| 102 | + auto dc = decl->getDeclContext(); |
| 103 | + return dc->getOutermostParentSourceFile()->getExtras(); |
| 104 | +} |
| 105 | + |
| 106 | +void swift::diagnoseUnsafeUse(const UnsafeUse &use, bool asNote) { |
| 107 | + if (!asNote) { |
| 108 | + // If we can associate this unsafe use within a particular declaration, do so. |
| 109 | + // It will be diagnosed later, along with all other unsafe uses within this |
| 110 | + // same declaration. |
| 111 | + if (use.getKind() == UnsafeUse::ReferenceToUnsafe || |
| 112 | + use.getKind() == UnsafeUse::CallToUnsafe) { |
| 113 | + auto [enclosingDecl, _] = enclosingContextForUnsafe( |
| 114 | + use.getLocation(), use.getDeclContext()); |
| 115 | + if (enclosingDecl && shouldDeferUnsafeUseIn(enclosingDecl)) { |
| 116 | + getSourceFileExtrasFor(enclosingDecl).unsafeUses[enclosingDecl] |
| 117 | + .push_back(use); |
| 118 | + return; |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
62 | 123 | switch (use.getKind()) {
|
63 | 124 | case UnsafeUse::Override: {
|
64 | 125 | auto override = use.getDecl();
|
@@ -142,18 +203,52 @@ void swift::diagnoseUnsafeUse(const UnsafeUse &use) {
|
142 | 203 | use.getDeclContext(),
|
143 | 204 | [&](Type specificType) {
|
144 | 205 | ctx.Diags.diagnose(
|
145 |
| - loc, diag::reference_to_unsafe_typed_decl, isCall, decl, |
146 |
| - specificType); |
| 206 | + loc, |
| 207 | + asNote ? diag::note_reference_to_unsafe_typed_decl |
| 208 | + : diag::reference_to_unsafe_typed_decl, |
| 209 | + isCall, decl, specificType); |
147 | 210 | });
|
148 |
| - suggestUnsafeOnEnclosingDecl(loc, use.getDeclContext()); |
149 |
| - decl->diagnose(diag::unsafe_decl_here, decl); |
150 | 211 | } else {
|
151 |
| - ctx.Diags |
152 |
| - .diagnose(loc, diag::reference_to_unsafe_decl, isCall, decl); |
| 212 | + ctx.Diags.diagnose( |
| 213 | + loc, |
| 214 | + asNote ? diag::note_reference_to_unsafe_decl |
| 215 | + : diag::reference_to_unsafe_decl, |
| 216 | + isCall, decl); |
| 217 | + } |
| 218 | + |
| 219 | + if (!asNote) { |
153 | 220 | suggestUnsafeOnEnclosingDecl(loc, use.getDeclContext());
|
154 | 221 | decl->diagnose(diag::unsafe_decl_here, decl);
|
155 | 222 | }
|
| 223 | + |
156 | 224 | return;
|
157 | 225 | }
|
158 | 226 | }
|
159 | 227 | }
|
| 228 | + |
| 229 | +void swift::diagnoseUnsafeUsesIn(const Decl *decl) { |
| 230 | + auto &extras = getSourceFileExtrasFor(decl); |
| 231 | + auto known = extras.unsafeUses.find(decl); |
| 232 | + if (known == extras.unsafeUses.end()) |
| 233 | + return; |
| 234 | + |
| 235 | + // Take the unsafe uses. |
| 236 | + auto unsafeUses = std::move(known->second); |
| 237 | + extras.unsafeUses.erase(known); |
| 238 | + if (unsafeUses.empty()) |
| 239 | + return; |
| 240 | + |
| 241 | + // Determine whether all of the uses are in the definition. |
| 242 | + bool canEncapsulateUnsafety = std::all_of( |
| 243 | + unsafeUses.begin(), unsafeUses.end(), isUnsafeUseInDefinition); |
| 244 | + StringRef fixItStr = canEncapsulateUnsafety |
| 245 | + ? "@safe(unchecked) " |
| 246 | + : "@unsafe "; |
| 247 | + decl->diagnose(diag::decl_involves_unsafe, decl, canEncapsulateUnsafety) |
| 248 | + .fixItInsert(decl->getAttributeInsertionLoc(/*forModifier=*/false), |
| 249 | + fixItStr); |
| 250 | + |
| 251 | + for (const auto &unsafeUse : unsafeUses) { |
| 252 | + diagnoseUnsafeUse(unsafeUse, /*asNote=*/true); |
| 253 | + } |
| 254 | +} |
0 commit comments