Skip to content

Improve and collate diagnostics for uses of unsafe constructs in declarations #78307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -8072,6 +8072,24 @@ NOTE(sending_function_result_with_sending_param_note, none,
//------------------------------------------------------------------------------
ERROR(unsafe_attr_disabled,none,
"attribute requires '-enable-experimental-feature AllowUnsafeAttribute'", ())

GROUPED_WARNING(decl_involves_unsafe,Unsafe,none,
"%kindbase0 involves unsafe code; "
"use %select{'@unsafe' to indicate that its use is not memory-safe|"
"'@safe(unchecked)' to assert that the code is memory-safe}1",
(const Decl *, bool))
NOTE(note_reference_to_unsafe_decl,none,
"%select{reference|call}0 to unsafe %kind1",
(bool, const ValueDecl *))
NOTE(note_reference_to_unsafe_typed_decl,none,
"%select{reference|call}0 to %kind1 involves unsafe type %2",
(bool, const ValueDecl *, Type))
NOTE(note_reference_to_nonisolated_unsafe,none,
"reference to nonisolated(unsafe) %kind0 is unsafe in concurrently-executing code",
(const ValueDecl *))
NOTE(note_reference_unowned_unsafe,none,
"reference to unowned(unsafe) %kind0 is unsafe", (const ValueDecl *))

GROUPED_WARNING(override_safe_withunsafe,Unsafe,none,
"override of safe %0 with unsafe %0", (DescriptiveDeclKind))
GROUPED_WARNING(witness_unsafe,Unsafe,none,
Expand All @@ -8082,10 +8100,11 @@ GROUPED_WARNING(type_witness_unsafe,Unsafe,none,
(Type, DeclName))
GROUPED_WARNING(unchecked_conformance_is_unsafe,Unsafe,none,
"@unchecked conformance involves unsafe code", ())
GROUPED_WARNING(unowned_unsafe_is_unsafe,Unsafe,none,
"unowned(unsafe) involves unsafe code", ())
GROUPED_WARNING(nonisolated_unsafe_is_unsafe,Unsafe,none,
"nonisolated(unsafe) involves unsafe code", ())
GROUPED_WARNING(reference_unowned_unsafe,Unsafe,none,
"reference to unowned(unsafe) %kind0 is unsafe", (const ValueDecl *))
GROUPED_WARNING(reference_to_nonisolated_unsafe,Unsafe,none,
"reference to nonisolated(unsafe) %kind0 is unsafe in concurrently-executing code",
(const ValueDecl *))
GROUPED_WARNING(reference_to_unsafe_decl,Unsafe,none,
"%select{reference|call}0 to unsafe %kindbase1",
(bool, const ValueDecl *))
Expand Down
9 changes: 9 additions & 0 deletions include/swift/AST/SourceFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
namespace swift {

class PersistentParserState;
struct SourceFileExtras;

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

/// Extra information for the source file, allocated as needed.
SourceFileExtras *extras = nullptr;

friend ASTContext;

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

/// Retrieve "extra" information associated with this source file, which is
/// lazily and separately constructed. Use this for scratch information
/// that isn't needed for all source files.
SourceFileExtras &getExtras() const;

SourceFile(ModuleDecl &M, SourceFileKind K, unsigned bufferID,
ParsingOptions parsingOpts = {}, bool isPrimary = false);

Expand Down
36 changes: 36 additions & 0 deletions include/swift/AST/SourceFileExtras.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//===--- SourceFileExtras.h - Extra data for a source file ------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_AST_SOURCEFILEEXTRAS_H
#define SWIFT_AST_SOURCEFILEEXTRAS_H

#include "swift/AST/UnsafeUse.h"
#include "llvm/ADT/DenseMap.h"
#include <vector>

namespace swift {

class Decl;

/// Extra information associated with a source file that is lazily created and
/// stored in a separately-allocated side structure.
struct SourceFileExtras {
/// Captures all of the unsafe uses associated with a given declaration.
///
/// The declaration is the entity that can be annotated (e.g., with @unsafe)
/// to suppress all of the unsafe-related diagnostics listed here.
llvm::DenseMap<const Decl *, std::vector<UnsafeUse>> unsafeUses;
};

}

#endif // SWIFT_AST_SOURCEFILEEXTRAS_H
Loading