Skip to content

Commit f57217f

Browse files
authored
Merge pull request #78307 from DougGregor/strict-safety-diags
Improve and collate diagnostics for uses of unsafe constructs in declarations
2 parents b31a2c6 + 70adcad commit f57217f

26 files changed

+853
-192
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8072,6 +8072,24 @@ NOTE(sending_function_result_with_sending_param_note, none,
80728072
//------------------------------------------------------------------------------
80738073
ERROR(unsafe_attr_disabled,none,
80748074
"attribute requires '-enable-experimental-feature AllowUnsafeAttribute'", ())
8075+
8076+
GROUPED_WARNING(decl_involves_unsafe,Unsafe,none,
8077+
"%kindbase0 involves unsafe code; "
8078+
"use %select{'@unsafe' to indicate that its use is not memory-safe|"
8079+
"'@safe(unchecked)' to assert that the code is memory-safe}1",
8080+
(const Decl *, bool))
8081+
NOTE(note_reference_to_unsafe_decl,none,
8082+
"%select{reference|call}0 to unsafe %kind1",
8083+
(bool, const ValueDecl *))
8084+
NOTE(note_reference_to_unsafe_typed_decl,none,
8085+
"%select{reference|call}0 to %kind1 involves unsafe type %2",
8086+
(bool, const ValueDecl *, Type))
8087+
NOTE(note_reference_to_nonisolated_unsafe,none,
8088+
"reference to nonisolated(unsafe) %kind0 is unsafe in concurrently-executing code",
8089+
(const ValueDecl *))
8090+
NOTE(note_reference_unowned_unsafe,none,
8091+
"reference to unowned(unsafe) %kind0 is unsafe", (const ValueDecl *))
8092+
80758093
GROUPED_WARNING(override_safe_withunsafe,Unsafe,none,
80768094
"override of safe %0 with unsafe %0", (DescriptiveDeclKind))
80778095
GROUPED_WARNING(witness_unsafe,Unsafe,none,
@@ -8082,10 +8100,11 @@ GROUPED_WARNING(type_witness_unsafe,Unsafe,none,
80828100
(Type, DeclName))
80838101
GROUPED_WARNING(unchecked_conformance_is_unsafe,Unsafe,none,
80848102
"@unchecked conformance involves unsafe code", ())
8085-
GROUPED_WARNING(unowned_unsafe_is_unsafe,Unsafe,none,
8086-
"unowned(unsafe) involves unsafe code", ())
8087-
GROUPED_WARNING(nonisolated_unsafe_is_unsafe,Unsafe,none,
8088-
"nonisolated(unsafe) involves unsafe code", ())
8103+
GROUPED_WARNING(reference_unowned_unsafe,Unsafe,none,
8104+
"reference to unowned(unsafe) %kind0 is unsafe", (const ValueDecl *))
8105+
GROUPED_WARNING(reference_to_nonisolated_unsafe,Unsafe,none,
8106+
"reference to nonisolated(unsafe) %kind0 is unsafe in concurrently-executing code",
8107+
(const ValueDecl *))
80898108
GROUPED_WARNING(reference_to_unsafe_decl,Unsafe,none,
80908109
"%select{reference|call}0 to unsafe %kindbase1",
80918110
(bool, const ValueDecl *))

include/swift/AST/SourceFile.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
namespace swift {
2828

2929
class PersistentParserState;
30+
struct SourceFileExtras;
3031

3132
/// Kind of import affecting how a decl can be reexported.
3233
///
@@ -243,6 +244,9 @@ class SourceFile final : public FileUnit {
243244
/// Storage for \c HasImportsMatchingFlagRequest.
244245
ImportOptions cachedImportOptions;
245246

247+
/// Extra information for the source file, allocated as needed.
248+
SourceFileExtras *extras = nullptr;
249+
246250
friend ASTContext;
247251

248252
public:
@@ -368,6 +372,11 @@ class SourceFile final : public FileUnit {
368372
/// \c #sourceLocation(file:) declarations.
369373
llvm::StringMap<SourceFilePathInfo> getInfoForUsedFilePaths() const;
370374

375+
/// Retrieve "extra" information associated with this source file, which is
376+
/// lazily and separately constructed. Use this for scratch information
377+
/// that isn't needed for all source files.
378+
SourceFileExtras &getExtras() const;
379+
371380
SourceFile(ModuleDecl &M, SourceFileKind K, unsigned bufferID,
372381
ParsingOptions parsingOpts = {}, bool isPrimary = false);
373382

include/swift/AST/SourceFileExtras.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//===--- SourceFileExtras.h - Extra data for a source file ------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SWIFT_AST_SOURCEFILEEXTRAS_H
14+
#define SWIFT_AST_SOURCEFILEEXTRAS_H
15+
16+
#include "swift/AST/UnsafeUse.h"
17+
#include "llvm/ADT/DenseMap.h"
18+
#include <vector>
19+
20+
namespace swift {
21+
22+
class Decl;
23+
24+
/// Extra information associated with a source file that is lazily created and
25+
/// stored in a separately-allocated side structure.
26+
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;
32+
};
33+
34+
}
35+
36+
#endif // SWIFT_AST_SOURCEFILEEXTRAS_H

0 commit comments

Comments
 (0)