Skip to content

Commit 931614c

Browse files
authored
Merge pull request #77547 from DougGregor/unsafe-diagnostic-group
Put warnings about unsafe constructs into a new diagnostic group `Unsafe`
2 parents 525713a + 7ffa5c3 commit 931614c

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

include/swift/AST/DiagnosticGroups.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
GROUP(no_group, "")
2424

2525
GROUP(DeprecatedDeclaration, "DeprecatedDeclaration.md")
26+
GROUP(Unsafe, "Unsafe.md")
2627
GROUP(UnknownWarningGroup, "UnknownWarningGroup.md")
2728

2829
#define UNDEFINE_DIAGNOSTIC_GROUPS_MACROS

include/swift/AST/DiagnosticsSema.def

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8053,24 +8053,24 @@ NOTE(sending_function_result_with_sending_param_note, none,
80538053
//------------------------------------------------------------------------------
80548054
ERROR(unsafe_attr_disabled,none,
80558055
"attribute requires '-enable-experimental-feature AllowUnsafeAttribute'", ())
8056-
WARNING(override_safe_withunsafe,none,
8056+
GROUPED_WARNING(override_safe_withunsafe,Unsafe,none,
80578057
"override of safe %0 with unsafe %0", (DescriptiveDeclKind))
8058-
WARNING(witness_unsafe,none,
8058+
GROUPED_WARNING(witness_unsafe,Unsafe,none,
80598059
"unsafe %0 %1 cannot satisfy safe requirement",
80608060
(DescriptiveDeclKind, DeclName))
8061-
WARNING(type_witness_unsafe,none,
8061+
GROUPED_WARNING(type_witness_unsafe,Unsafe,none,
80628062
"unsafe type %0 cannot satisfy safe associated type %1",
80638063
(Type, DeclName))
8064-
WARNING(unchecked_conformance_is_unsafe,none,
8064+
GROUPED_WARNING(unchecked_conformance_is_unsafe,Unsafe,none,
80658065
"@unchecked conformance involves unsafe code", ())
8066-
WARNING(unowned_unsafe_is_unsafe,none,
8066+
GROUPED_WARNING(unowned_unsafe_is_unsafe,Unsafe,none,
80678067
"unowned(unsafe) involves unsafe code", ())
8068-
WARNING(nonisolated_unsafe_is_unsafe,none,
8068+
GROUPED_WARNING(nonisolated_unsafe_is_unsafe,Unsafe,none,
80698069
"nonisolated(unsafe) involves unsafe code", ())
8070-
WARNING(reference_to_unsafe_decl,none,
8070+
GROUPED_WARNING(reference_to_unsafe_decl,Unsafe,none,
80718071
"%select{reference|call}0 to unsafe %kindbase1",
80728072
(bool, const ValueDecl *))
8073-
WARNING(reference_to_unsafe_typed_decl,none,
8073+
GROUPED_WARNING(reference_to_unsafe_typed_decl,Unsafe,none,
80748074
"%select{reference|call}0 to %kindbase1 involves unsafe type %2",
80758075
(bool, const ValueDecl *, Type))
80768076
NOTE(unsafe_decl_here,none,

test/Unsafe/unsafe.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %empty-directory(%t)
22
// RUN: %target-swift-frontend -emit-module-path %t/unsafe_swift_decls.swiftmodule %S/Inputs/unsafe_swift_decls.swift -enable-experimental-feature AllowUnsafeAttribute
33

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

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

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

userdocs/diagnostic_groups/Unsafe.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Unsafe code wanings (`Unsafe`)
2+
3+
4+
This diagnostic group includes warnings that identify the use of unsafe language constructs and library APIs. Example warnings include:
5+
6+
- Use of an unsafe language feature:
7+
```swift
8+
// warning: unowned(unsafe) involves unsafe code
9+
unowned(unsafe) var parentNode: TreeNode<T>
10+
```
11+
- Use of a function or type annotated with `@unsafe`:
12+
```swift
13+
// warning: reference to unsafe generic struct 'UnsafeMutablePointer'
14+
func getPointee<T>(_ pointer: UnsafeMutablePointer<Int>, as type: T.Type) -> T {
15+
// warning: call to unsafe global function 'unsafeBitCast'
16+
return unsafeBitCast(pointer.pointee, to: type)
17+
}
18+
```
19+
- Use of a function involving an `@unsafe` type:
20+
```swift
21+
func evilMalloc(size: Int) -> Int {
22+
// warning: call to global function 'malloc' involves unsafe type 'UnsafeMutableRawPointer'
23+
return Int(bitPattern: malloc(size))
24+
}
25+
```

0 commit comments

Comments
 (0)