mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
[SE-0458] Enable unsafe expressions / attributes / for..in effects by default
With the acceptance of SE-0458, allow the use of unsafe expressions, the @safe and @unsafe attributes, and the `unsafe` effect on the for..in loop in all Swift code. Introduce the `-strict-memory-safety` flag detailed in the proposal to enable strict memory safety checking. This enables a new class of feature, an optional feature (that is *not* upcoming or experimental), and which can be detected via `hasFeature(StrictMemorySafety)`.
This commit is contained in:
@@ -1,10 +1,8 @@
|
||||
// RUN: %empty-directory(%t)
|
||||
|
||||
// RUN: %target-swift-frontend -swift-version 5 -enable-library-evolution -module-name unsafe -emit-module -o %t/unsafe.swiftmodule -emit-module-interface-path - %s -enable-experimental-feature AllowUnsafeAttribute | %FileCheck %s
|
||||
// RUN: %target-swift-frontend -swift-version 5 -enable-library-evolution -module-name unsafe -emit-module -o %t/unsafe.swiftmodule -emit-module-interface-path - %s | %FileCheck %s
|
||||
|
||||
// REQUIRES: swift_feature_AllowUnsafeAttribute
|
||||
|
||||
// CHECK: #if compiler(>=5.3) && $AllowUnsafeAttribute
|
||||
// CHECK: #if compiler(>=5.3) && $MemorySafetyAttributes
|
||||
// CHECK: @unsafe public func testFunction()
|
||||
// CHECK: #else
|
||||
// CHECK: public func testFunction()
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
// RUN: %target-swift-emit-module-interface(%t.swiftinterface) %s -module-name UserModule -enable-experimental-feature AllowUnsafeAttribute -enable-experimental-feature WarnUnsafe
|
||||
// RUN: %target-swift-emit-module-interface(%t.swiftinterface) %s -module-name UserModule -strict-memory-safety
|
||||
// RUN: %target-swift-typecheck-module-from-interface(%t.swiftinterface) -module-name UserModule
|
||||
// RUN: %FileCheck %s < %t.swiftinterface
|
||||
|
||||
// REQUIRES: swift_feature_AllowUnsafeAttribute
|
||||
// REQUIRES: swift_feature_WarnUnsafe
|
||||
|
||||
// CHECK: #if compiler(>=5.3) && $AllowUnsafeAttribute
|
||||
// CHECK: #if compiler(>=5.3) && $MemorySafetyAttributes
|
||||
// CHECK: @unsafe public func getIntUnsafely() -> Swift.Int
|
||||
// CHECK: #else
|
||||
// CHECK: public func getIntUnsafely() -> Swift.Int
|
||||
@@ -25,7 +22,7 @@ public protocol P {
|
||||
|
||||
// CHECK: public struct X : @unsafe UserModule.P
|
||||
public struct X: @unsafe P {
|
||||
// CHECK: #if compiler(>=5.3) && $AllowUnsafeAttribute
|
||||
// CHECK: #if compiler(>=5.3) && $MemorySafetyAttributes
|
||||
// CHECK: @unsafe public func f()
|
||||
// CHECK: #else
|
||||
// CHECK: public func f()
|
||||
|
||||
@@ -1,14 +1,11 @@
|
||||
// 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-swift-frontend -emit-module-path %t/safe_swift_decls.swiftmodule %S/Inputs/safe_swift_decls.swift -enable-experimental-feature AllowUnsafeAttribute -enable-experimental-feature WarnUnsafe
|
||||
// RUN: %target-swift-frontend -emit-module-path %t/unsafe_swift_decls.swiftmodule %S/Inputs/unsafe_swift_decls.swift
|
||||
// RUN: %target-swift-frontend -emit-module-path %t/safe_swift_decls.swiftmodule %S/Inputs/safe_swift_decls.swift -strict-memory-safety
|
||||
|
||||
// RUN: %target-typecheck-verify-swift -enable-experimental-feature AllowUnsafeAttribute -enable-experimental-feature WarnUnsafe -I %S/Inputs -I %t -emit-loaded-module-trace-path %t/unsafe.trace
|
||||
// RUN: %target-typecheck-verify-swift -strict-memory-safety -I %S/Inputs -I %t -emit-loaded-module-trace-path %t/unsafe.trace
|
||||
|
||||
// RUN: %FileCheck -check-prefix TRACE %s < %t/unsafe.trace
|
||||
|
||||
// REQUIRES: swift_feature_AllowUnsafeAttribute
|
||||
// REQUIRES: swift_feature_WarnUnsafe
|
||||
|
||||
import unsafe_decls
|
||||
import unsafe_swift_decls
|
||||
import safe_swift_decls
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
// RUN: %target-typecheck-verify-swift -enable-experimental-feature AllowUnsafeAttribute -enable-experimental-feature WarnUnsafe -print-diagnostic-groups
|
||||
// RUN: %target-typecheck-verify-swift -strict-memory-safety -print-diagnostic-groups
|
||||
|
||||
// The feature flag should be enabled.
|
||||
#if !hasFeature(StrictMemorySafety)
|
||||
#error("Strict memory safety is not enabled!")
|
||||
#endif
|
||||
|
||||
// REQUIRES: swift_feature_AllowUnsafeAttribute
|
||||
// REQUIRES: swift_feature_WarnUnsafe
|
||||
|
||||
@unsafe
|
||||
func unsafeFunction() { }
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
// RUN: %target-typecheck-verify-swift -enable-experimental-feature AllowUnsafeAttribute -enable-experimental-feature WarnUnsafe -print-diagnostic-groups
|
||||
|
||||
// REQUIRES: swift_feature_AllowUnsafeAttribute
|
||||
// REQUIRES: swift_feature_WarnUnsafe
|
||||
// RUN: %target-typecheck-verify-swift -strict-memory-safety -print-diagnostic-groups
|
||||
|
||||
@unsafe
|
||||
class NotSafe {
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
// RUN: %target-typecheck-verify-swift -enable-experimental-feature AllowUnsafeAttribute -enable-experimental-feature WarnUnsafe -print-diagnostic-groups
|
||||
|
||||
// REQUIRES: swift_feature_AllowUnsafeAttribute
|
||||
// REQUIRES: swift_feature_WarnUnsafe
|
||||
// RUN: %target-typecheck-verify-swift -strict-memory-safety -print-diagnostic-groups
|
||||
|
||||
@unsafe
|
||||
func iAmUnsafe() { }
|
||||
|
||||
@@ -1,13 +1,10 @@
|
||||
// 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-swift-frontend -emit-module-path %t/unsafe_swift_decls.swiftmodule %S/Inputs/unsafe_swift_decls.swift
|
||||
|
||||
// RUN: %target-typecheck-verify-swift -enable-experimental-feature AllowUnsafeAttribute -enable-experimental-feature WarnUnsafe -I %t -print-diagnostic-groups
|
||||
// RUN: %target-typecheck-verify-swift -strict-memory-safety -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
|
||||
|
||||
// REQUIRES: swift_feature_AllowUnsafeAttribute
|
||||
// REQUIRES: swift_feature_WarnUnsafe
|
||||
// RUN: %target-swift-frontend -typecheck %s -I %t
|
||||
|
||||
import unsafe_swift_decls
|
||||
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
// RUN: %target-typecheck-verify-swift -enable-experimental-feature AllowUnsafeAttribute -enable-experimental-feature WarnUnsafe -I %S/Inputs
|
||||
|
||||
// REQUIRES: swift_feature_AllowUnsafeAttribute
|
||||
// REQUIRES: swift_feature_WarnUnsafe
|
||||
// RUN: %target-typecheck-verify-swift -strict-memory-safety -I %S/Inputs
|
||||
|
||||
import unsafe_decls
|
||||
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
// RUN: %target-swift-frontend -typecheck -enable-experimental-feature WarnUnsafe -Ounchecked -disable-access-control %s 2>&1 | %FileCheck %s
|
||||
|
||||
// REQUIRES: swift_feature_WarnUnsafe
|
||||
// RUN: %target-swift-frontend -typecheck -strict-memory-safety -Ounchecked -disable-access-control %s 2>&1 | %FileCheck %s
|
||||
|
||||
// CHECK: warning: '-Ounchecked' is not memory-safe
|
||||
// CHECK: warning: '-disable-access-control' is not memory-safe
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
// 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 -enable-experimental-feature AllowUnsafeAttribute
|
||||
// RUN: %target-swift-frontend -emit-module-path %t/unsafe_swift_decls.swiftmodule %S/Inputs/unsafe_swift_decls.swift
|
||||
|
||||
// RUN: %target-typecheck-verify-swift -enable-experimental-feature WarnUnsafe -enable-experimental-feature StrictConcurrency -enable-experimental-feature AllowUnsafeAttribute -I %t
|
||||
// RUN: %target-typecheck-verify-swift -strict-memory-safety -enable-experimental-feature StrictConcurrency -I %t
|
||||
|
||||
// REQUIRES: concurrency
|
||||
// REQUIRES: swift_feature_StrictConcurrency
|
||||
// REQUIRES: swift_feature_WarnUnsafe
|
||||
// REQUIRES: swift_feature_AllowUnsafeAttribute
|
||||
|
||||
@preconcurrency import unsafe_swift_decls // expected-warning{{@preconcurrency import is not memory-safe because it can silently introduce data races}}
|
||||
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
// RUN: %target-typecheck-verify-swift
|
||||
|
||||
@unsafe func f() { }
|
||||
// expected-error@-1{{attribute requires '-enable-experimental-feature AllowUnsafeAttribute'}}
|
||||
|
||||
19
test/Unsafe/unsafe_feature.swift
Normal file
19
test/Unsafe/unsafe_feature.swift
Normal file
@@ -0,0 +1,19 @@
|
||||
// RUN: %target-typecheck-verify-swift
|
||||
|
||||
|
||||
// Can use @unsafe and @safe without strict memory safety being enabled.
|
||||
@unsafe func f() { }
|
||||
@safe func g(_: UnsafeRawPointer) { }
|
||||
|
||||
protocol P {
|
||||
func f()
|
||||
}
|
||||
|
||||
struct X: @unsafe P {
|
||||
@unsafe func f() { }
|
||||
}
|
||||
|
||||
// The feature flag is not enabled, though.
|
||||
#if hasFeature(StrictMemorySafety)
|
||||
#error("Strict memory safety is not enabled!")
|
||||
#endif
|
||||
@@ -1,10 +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-swift-frontend -emit-module-path %t/unsafe_swift_decls.swiftmodule %S/Inputs/unsafe_swift_decls.swift
|
||||
|
||||
// RUN: %target-typecheck-verify-swift -enable-experimental-feature AllowUnsafeAttribute -enable-experimental-feature WarnUnsafe -I %S/Inputs -I %t
|
||||
|
||||
// REQUIRES: swift_feature_AllowUnsafeAttribute
|
||||
// REQUIRES: swift_feature_WarnUnsafe
|
||||
// RUN: %target-typecheck-verify-swift -strict-memory-safety -I %S/Inputs -I %t
|
||||
|
||||
import unsafe_decls
|
||||
import unsafe_swift_decls
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
// RUN: %target-typecheck-verify-swift -enable-experimental-feature AllowUnsafeAttribute -print-diagnostic-groups
|
||||
|
||||
// REQUIRES: swift_feature_AllowUnsafeAttribute
|
||||
|
||||
// RUN: %target-typecheck-verify-swift -print-diagnostic-groups
|
||||
|
||||
protocol P { }
|
||||
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
// RUN: %target-typecheck-verify-swift -enable-experimental-feature WarnUnsafe
|
||||
// RUN: %target-typecheck-verify-swift -strict-memory-safety
|
||||
|
||||
// Make sure everything compiles without error when unsafe code is allowed.
|
||||
// RUN: %target-swift-frontend -typecheck -enable-experimental-feature AllowUnsafeAttribute -warnings-as-errors %s
|
||||
|
||||
// REQUIRES: swift_feature_AllowUnsafeAttribute
|
||||
// REQUIRES: swift_feature_WarnUnsafe
|
||||
// RUN: %target-swift-frontend -typecheck -warnings-as-errors %s
|
||||
|
||||
func test(
|
||||
x: OpaquePointer,
|
||||
|
||||
Reference in New Issue
Block a user