Skip to content
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

@unsafe attribute is not propagated through type alias declarations #78220

Open
Xazax-hun opened this issue Dec 16, 2024 · 0 comments
Open

@unsafe attribute is not propagated through type alias declarations #78220

Xazax-hun opened this issue Dec 16, 2024 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels

Comments

@Xazax-hun
Copy link
Contributor

Xazax-hun commented Dec 16, 2024

Description

When a typealias is created in a non-strictly-safe module to a @unsafe construct, the alias can be used without triggering a warning in a strictly-safe module.

Reproduction

Apply the following diff and run tests:

diff --git a/test/Unsafe/Inputs/unsafe_swift_decls.swift b/test/Unsafe/Inputs/unsafe_swift_decls.swift
index 7c92bd6203f..c0996e6a183 100644
--- a/test/Unsafe/Inputs/unsafe_swift_decls.swift
+++ b/test/Unsafe/Inputs/unsafe_swift_decls.swift
@@ -1,5 +1,7 @@
 @unsafe public struct PointerType { } // expected-note{{'PointerType' declared here}}

+public typealias UnsafeTypeAlias = PointerType
+
 public func getPointers() -> [PointerType] { [] }

 public struct HasAPointerType {
diff --git a/test/Unsafe/unsafe_imports.swift b/test/Unsafe/unsafe_imports.swift
index 0b80943f303..fabc78c5554 100644
--- a/test/Unsafe/unsafe_imports.swift
+++ b/test/Unsafe/unsafe_imports.swift
@@ -1,9 +1,13 @@
-// RUN: %target-typecheck-verify-swift -enable-experimental-feature AllowUnsafeAttribute -enable-experimental-feature WarnUnsafe -I %S/Inputs
+// 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 %S/Inputs  -I %t

 // REQUIRES: swift_feature_AllowUnsafeAttribute
 // REQUIRES: swift_feature_WarnUnsafe

 import unsafe_decls
+import unsafe_swift_decls

 func testUnsafe(_ ut: UnsafeType) { // expected-warning{{reference to unsafe struct 'UnsafeType'}}
   unsafe_c_function() // expected-warning{{call to unsafe global function 'unsafe_c_function'}}
@@ -12,3 +16,7 @@ func testUnsafe(_ ut: UnsafeType) { // expected-warning{{reference to unsafe str
   print_ints(&array, CInt(array.count))
   // expected-warning@-1{{call to global function 'print_ints' involves unsafe type 'UnsafeMutablePointer<Int32>'}}
 }
+
+
+func testUnsafe2(_ ut: UnsafeTypeAlias) { // expected-warning{{reference to unsafe type alias 'UnsafeTypeAlias'}}
+}

Expected behavior

Warning is produced for func testUnsafe2(_ ut: UnsafeTypeAlias)

Environment

Swift version 6.2-dev (LLVM be8c96d78337932, Swift 7867657)
Target: arm64-apple-macosx15.0

Additional information

No response

@Xazax-hun Xazax-hun added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Dec 16, 2024
DougGregor added a commit to DougGregor/swift that referenced this issue Dec 24, 2024
As we do when referencing other kinds of declarations, if a
typealias isn't `@unsafe`, but it involves unsafe types,
diagnose the non-safety at the point of reference.
Fixes swiftlang#78220
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels
Projects
None yet
Development

No branches or pull requests

1 participant