Uploaded image for project: 'Swift'
  1. Swift
  2. SR-9589

Clang crash when calling external C library function with UnsafePointer to C struct as argument (was Can't pass an imported C extern const struct to a C function which expects a struct pointer)

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Reopened
    • Priority: Medium
    • Resolution: Unresolved
    • Component/s: Compiler
    • Labels:
      None

      Description

      I'm trying to build a Swift app on Linux (Fedora 29) using a Wayland desktop. I need to pass a C struct declared externally as

       extern const struct wl_interface wl_compositor_interface;
      

      (from https://people.freedesktop.org/~whot/wayland-doxygen/wayland/Server/wayland-server-protocol_8h_source.html)
      to a C function declared as:

      static void* wl_registry_bind(
          struct wl_registry *wl_registry,
          uint32_t name,
          const struct wl_interface *interface,
          uint32_t version 
      )
      

      However if I try to call the function as:

      window._compositor = wl_registry_bind(registry, name, &wl_compositor_interface, 1)
      

      I get the error:

      error: cannot pass immutable value as inout argument: 'wl_compositor_interface' is a 'let' constant
      

      Removing the & gives the (expected) error:

      error: cannot convert value of type 'wl_interface' to expected argument type 'UnsafePointer<wl_interface>?'
      

      If I try to create an UnsafePointer to the struct to pass to the function as follows:

      var compositorInterfacePointer: wl_interface = wl_compositor_interface
      window._compositor = wl_registry_bind(registry, name, &compositorInterfacePointer, 1)
      

      I get a compiler crash (on both the Fedora-built Swift 4.2.1 and my own build from dev HEAD):
      https://gist.github.com/tokyovigilante/89a1209eb7802b8a035d6f03b63adeae

      Is there any way to pass these externally defined structs to a function which expects a const struct pointer?

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              ryanw Ryan
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated: