Skip to content

Put warnings about unsafe constructs into a new diagnostic group Unsafe #77547

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 1 commit into from
Nov 12, 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
1 change: 1 addition & 0 deletions include/swift/AST/DiagnosticGroups.def
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
GROUP(no_group, "")

GROUP(DeprecatedDeclaration, "DeprecatedDeclaration.md")
GROUP(Unsafe, "Unsafe.md")
GROUP(UnknownWarningGroup, "UnknownWarningGroup.md")

#define UNDEFINE_DIAGNOSTIC_GROUPS_MACROS
Expand Down
16 changes: 8 additions & 8 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -8053,24 +8053,24 @@ NOTE(sending_function_result_with_sending_param_note, none,
//------------------------------------------------------------------------------
ERROR(unsafe_attr_disabled,none,
"attribute requires '-enable-experimental-feature AllowUnsafeAttribute'", ())
WARNING(override_safe_withunsafe,none,
GROUPED_WARNING(override_safe_withunsafe,Unsafe,none,
"override of safe %0 with unsafe %0", (DescriptiveDeclKind))
WARNING(witness_unsafe,none,
GROUPED_WARNING(witness_unsafe,Unsafe,none,
"unsafe %0 %1 cannot satisfy safe requirement",
(DescriptiveDeclKind, DeclName))
WARNING(type_witness_unsafe,none,
GROUPED_WARNING(type_witness_unsafe,Unsafe,none,
"unsafe type %0 cannot satisfy safe associated type %1",
(Type, DeclName))
WARNING(unchecked_conformance_is_unsafe,none,
GROUPED_WARNING(unchecked_conformance_is_unsafe,Unsafe,none,
"@unchecked conformance involves unsafe code", ())
WARNING(unowned_unsafe_is_unsafe,none,
GROUPED_WARNING(unowned_unsafe_is_unsafe,Unsafe,none,
"unowned(unsafe) involves unsafe code", ())
WARNING(nonisolated_unsafe_is_unsafe,none,
GROUPED_WARNING(nonisolated_unsafe_is_unsafe,Unsafe,none,
"nonisolated(unsafe) involves unsafe code", ())
WARNING(reference_to_unsafe_decl,none,
GROUPED_WARNING(reference_to_unsafe_decl,Unsafe,none,
"%select{reference|call}0 to unsafe %kindbase1",
(bool, const ValueDecl *))
WARNING(reference_to_unsafe_typed_decl,none,
GROUPED_WARNING(reference_to_unsafe_typed_decl,Unsafe,none,
"%select{reference|call}0 to %kindbase1 involves unsafe type %2",
(bool, const ValueDecl *, Type))
NOTE(unsafe_decl_here,none,
Expand Down
4 changes: 2 additions & 2 deletions test/Unsafe/unsafe.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -emit-module-path %t/unsafe_swift_decls.swiftmodule %S/Inputs/unsafe_swift_decls.swift -enable-experimental-feature AllowUnsafeAttribute

// RUN: %target-typecheck-verify-swift -enable-experimental-feature AllowUnsafeAttribute -enable-experimental-feature WarnUnsafe -I %t
// RUN: %target-typecheck-verify-swift -enable-experimental-feature AllowUnsafeAttribute -enable-experimental-feature WarnUnsafe -I %t -print-diagnostic-groups

// Make sure everything compiles without error when unsafe code is allowed.
// RUN: %target-swift-frontend -typecheck -enable-experimental-feature AllowUnsafeAttribute %s -I %t
Expand All @@ -20,7 +20,7 @@ protocol P {
}

struct XP: P {
@unsafe func f() { } // expected-warning{{unsafe instance method 'f()' cannot satisfy safe requirement}}
@unsafe func f() { } // expected-warning{{unsafe instance method 'f()' cannot satisfy safe requirement [Unsafe]}}
@unsafe func g() { }
}

Expand Down
25 changes: 25 additions & 0 deletions userdocs/diagnostic_groups/Unsafe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Unsafe code wanings (`Unsafe`)


This diagnostic group includes warnings that identify the use of unsafe language constructs and library APIs. Example warnings include:

- Use of an unsafe language feature:
```swift
// warning: unowned(unsafe) involves unsafe code
unowned(unsafe) var parentNode: TreeNode<T>
```
- Use of a function or type annotated with `@unsafe`:
```swift
// warning: reference to unsafe generic struct 'UnsafeMutablePointer'
func getPointee<T>(_ pointer: UnsafeMutablePointer<Int>, as type: T.Type) -> T {
// warning: call to unsafe global function 'unsafeBitCast'
return unsafeBitCast(pointer.pointee, to: type)
}
```
- Use of a function involving an `@unsafe` type:
```swift
func evilMalloc(size: Int) -> Int {
// warning: call to global function 'malloc' involves unsafe type 'UnsafeMutableRawPointer'
return Int(bitPattern: malloc(size))
}
```