[SILGen] Map the @_extern(c) C function name over to the asmname of a SIL function

`@_extern(c)` is meant for referencing C functions defined outside of
this Swift file. Instead of using the C function name as the SIL
function name, which is prone to collisions across different Swift
modules, place make the C function name the "asmname" of the
corresponding SIL function. Show that this prevents deserialization
errors when there are conflicting Swift-level types for the same
`@_extern(c)`-named symbol across modules.

Part of rdar://137014448.
Instead of using the C name as the mangled name of a SIL function
This commit is contained in:
Doug Gregor
2025-10-15 23:19:12 -07:00
parent 081b5cd1e8
commit 4b745170c3
7 changed files with 55 additions and 19 deletions

View File

@@ -0,0 +1,8 @@
@_extern(c, "takes_a_void_pointer")
public func takes_a_void_pointer(_ pointer: UnsafeRawPointer)
@_alwaysEmitIntoClient
public func callWithNonNull() {
let pointer = UnsafeMutablePointer<Int>.allocate(capacity: 1)
takes_a_void_pointer(UnsafeRawPointer(pointer))
}

View File

@@ -0,0 +1,7 @@
@_extern(c, "takes_a_void_pointer")
public func takes_a_void_pointer(_ pointer: UnsafeRawPointer?)
@_alwaysEmitIntoClient
public func callWithNullable() {
takes_a_void_pointer(nil)
}

View File

@@ -0,0 +1,18 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -emit-module -enable-experimental-feature Extern -o %t %S/Inputs/extern_with_nullable.swift
// RUN: %target-swift-frontend -emit-module -enable-experimental-feature Extern -o %t %S/Inputs/extern_with_nonnull.swift
// RUN: %target-swift-frontend -emit-sil -o %t -I %t -primary-file %s -module-name main -O
// REQUIRES: swift_feature_Extern
// Don't crash or otherwise fail when inlining multiple functions that reference
// @_extern(c) declarations of the same name but different types at the SIL
// level.
import extern_with_nullable
import extern_with_nonnull
public func main() {
callWithNullable()
callWithNonNull()
}