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

UniquePtr of pure virtual opaque extern C type doesn't compile #93

Closed
dtolnay opened this issue Apr 10, 2020 · 0 comments · Fixed by #94
Closed

UniquePtr of pure virtual opaque extern C type doesn't compile #93

dtolnay opened this issue Apr 10, 2020 · 0 comments · Fixed by #94
Labels

Comments

@dtolnay
Copy link
Owner

dtolnay commented Apr 10, 2020

Repro:

// lib.rs

#[cxx::bridge]
mod ffi {
    extern "C" {
        include!("repro.h");

        type Virtual;
        fn make() -> UniquePtr<Virtual>;
    }
}
// repro.h

#pragma once

struct Virtual {
  virtual void function() = 0;
};

std::unique_ptr<Virtual> make();

This fails to compile with:

repro.rs.cc: In function ‘void cxxbridge02$unique_ptr$Virtual$new(std::unique_ptr<Virtual>*, Virtual*)’:
repro.rs.cc:599:85: error: invalid new-expression of abstract class type ‘Virtual’
  |   new (ptr) ::std::unique_ptr<Virtual>(new Virtual(::std::move(*value)));
  |                                                                       ^
repro.h:11:8: note:   because the following virtual functions are pure within ‘Virtual’:
  | struct Virtual {
  |        ^~~~~~~
repro.h:12:16: note: 	‘virtual void Virtual::function()’
  |   virtual void function() = 0;
  |                ^~~~~~~~

The error is inside of the shim we emit for UniquePtr::new:

void cxxbridge02$unique_ptr$Virtual$new(::std::unique_ptr<Virtual> *ptr, Virtual *value) noexcept {
  new (ptr) ::std::unique_ptr<Virtual>(new Virtual(::std::move(*value)));
}

But we should just not emit that shim for opaque C types. The signature of UniquePtr::new is fn(T) -> UniquePtr<T> which is uncallable when T is an opaque C type because those can never exist by value on the Rust side of the bridge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant