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

How to solve relocatable semantics between Rust and C++? #2

Closed
jclinton opened this issue Jan 8, 2020 · 1 comment
Closed

How to solve relocatable semantics between Rust and C++? #2

jclinton opened this issue Jan 8, 2020 · 1 comment

Comments

@jclinton
Copy link

jclinton commented Jan 8, 2020

Semantically, C++ types cannot be relocated because they may contain pointers to themselves or chains of objects that eventually point back to themselves. Rust assumes that all types can be relocated (e.g. by memcpy). Pin<T> can help with this problem when working with such FFI, but is there any way in which this crate can assist bridging these semantic differences?

@dtolnay
Copy link
Owner

dtolnay commented Jan 8, 2020

In the CXX type system the only types that could contain internal pointers are the opaque C++ types. We don't allow opaque types to be passed across the FFI by value, only by reference or smart pointer. I think I would prefer to keep it that way; adding relocation behavior to Rust would need to be a separate project.

@dtolnay dtolnay closed this as completed Feb 23, 2020
dtolnay added a commit that referenced this issue Dec 31, 2020
Previously:

    error[E0034]: multiple applicable items in scope
      --> src/main.rs:39:20
       |
    39 |     let _ = vector.get_mut(0);
       |                    ^^^^^^^ multiple `get_mut` found
       |
       = note: candidate #1 is defined in an impl for the type `Pin<&'a mut T>`
       = note: candidate #2 is defined in an impl for the type `CxxVector<T>`
dtolnay added a commit that referenced this issue Jun 4, 2022
This test is affected by an ICE in nightly-2022-06-04.
rust-lang/rust#97698

Minimized repro:

    trait Ambiguous<A> {
        fn method() {}
    }

    struct One;
    struct Two;
    struct Struct;

    impl Ambiguous<One> for Struct {}
    impl Ambiguous<Two> for Struct {}

    fn main() {
        <Struct as Ambiguous<_>>::method();
    }

Correct error in nightly-2022-06-03:

    error[E0282]: type annotations needed
      --> src/main.rs:13:26
       |
    13 |     <Struct as Ambiguous<_>>::method();
       |                          ^ cannot infer type

    error[E0283]: type annotations needed
      --> src/main.rs:13:5
       |
    13 |     <Struct as Ambiguous<_>>::method();
       |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type
       |
    note: multiple `impl`s satisfying `Struct: Ambiguous<_>` found
      --> src/main.rs:9:1
       |
    9  | impl Ambiguous<One> for Struct {}
       | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    10 | impl Ambiguous<Two> for Struct {}
       | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

ICE in nightly-2022-06-04:

    thread 'rustc' panicked at 'range end index 2 out of range for slice of length 1', library/core/src/slice/index.rs:73:5
    stack backtrace:
      16:     0x7f4cccefd816 - core::slice::index::slice_end_index_len_fail_rt::h04ca4b0b6ddf070e
      17:     0x7f4ccceef707 - core::ops::function::FnOnce::call_once::hb4cbec441e0e0d97
      18:     0x7f4cccef6516 - core::intrinsics::const_eval_select::h2fab5eaa67d7a905
      19:     0x7f4ccce67556 - core::slice::index::slice_end_index_len_fail::ha1557d304be1c61e
      20:     0x7f4cce5ea20f - <rustc_infer[9b42245fee805938]::infer::error_reporting::need_type_info::FindInferSourceVisitor as rustc_hir[5dbb21eaf9590c92]::intravisit::Visitor>::visit_expr
      21:     0x7f4cce5e8edc - <rustc_infer[9b42245fee805938]::infer::error_reporting::need_type_info::FindInferSourceVisitor as rustc_hir[5dbb21eaf9590c92]::intravisit::Visitor>::visit_expr
      22:     0x7f4cce5fbe37 - rustc_hir[5dbb21eaf9590c92]::intravisit::walk_block::<rustc_infer[9b42245fee805938]::infer::error_reporting::need_type_info::FindInferSourceVisitor>
      23:     0x7f4cce5e8eee - <rustc_infer[9b42245fee805938]::infer::error_reporting::need_type_info::FindInferSourceVisitor as rustc_hir[5dbb21eaf9590c92]::intravisit::Visitor>::visit_expr
      24:     0x7f4cce5a1714 - <rustc_infer[9b42245fee805938]::infer::InferCtxt>::emit_inference_failure_err
      25:     0x7f4cce4f32bb - <rustc_infer[9b42245fee805938]::infer::InferCtxt as rustc_trait_selection[af2ed82902f8af73]::traits::error_reporting::InferCtxtPrivExt>::maybe_report_ambiguity
      26:     0x7f4cce4e6f87 - <rustc_infer[9b42245fee805938]::infer::InferCtxt as rustc_trait_selection[af2ed82902f8af73]::traits::error_reporting::InferCtxtExt>::report_fulfillment_errors
      27:     0x7f4ccec3fcb4 - <rustc_infer[9b42245fee805938]::infer::InferCtxtBuilder>::enter::<&rustc_middle[c4bb946b42085c78]::ty::context::TypeckResults, <rustc_typeck[4b1a1a7c06e44333]::check::inherited::InheritedBuilder>::enter<rustc_typeck[4b1a1a7c06e44333]::check::typeck_with_fallback<rustc_typeck[4b1a1a7c06e44333]::check::typeck::{closure#0}>::{closure#1}, &rustc_middle[c4bb946b42085c78]::ty::context::TypeckResults>::{closure#0}>
      28:     0x7f4ccebd170a - rustc_typeck[4b1a1a7c06e44333]::check::typeck
      29:     0x7f4ccfccf5f0 - <rustc_query_system[a1cbf340ec6d6f93]::dep_graph::graph::DepGraph<rustc_middle[c4bb946b42085c78]::dep_graph::dep_node::DepKind>>::with_task::<rustc_middle[c4bb946b42085c78]::ty::context::TyCtxt, rustc_span[c774ced8c61a5d44]::def_id::LocalDefId, &rustc_middle[c4bb946b42085c78]::ty::context::TypeckResults>
      30:     0x7f4ccf1f8380 - rustc_query_system[a1cbf340ec6d6f93]::query::plumbing::try_execute_query::<rustc_query_impl[f03dd36fb3d62396]::plumbing::QueryCtxt, rustc_query_system[a1cbf340ec6d6f93]::query::caches::DefaultCache<rustc_span[c774ced8c61a5d44]::def_id::LocalDefId, &rustc_middle[c4bb946b42085c78]::ty::context::TypeckResults>>
      31:     0x7f4ccf123c0e - <rustc_query_impl[f03dd36fb3d62396]::Queries as rustc_middle[c4bb946b42085c78]::ty::query::QueryEngine>::typeck
      32:     0x7f4ccec91708 - <rustc_middle[c4bb946b42085c78]::hir::map::Map>::par_body_owners::<rustc_typeck[4b1a1a7c06e44333]::check::typeck_item_bodies::{closure#0}>
      33:     0x7f4ccfa8e18c - rustc_typeck[4b1a1a7c06e44333]::check::typeck_item_bodies
      34:     0x7f4ccfcee823 - <rustc_query_system[a1cbf340ec6d6f93]::dep_graph::graph::DepGraph<rustc_middle[c4bb946b42085c78]::dep_graph::dep_node::DepKind>>::with_task::<rustc_middle[c4bb946b42085c78]::ty::context::TyCtxt, (), ()>
      35:     0x7f4ccfd991d5 - rustc_query_system[a1cbf340ec6d6f93]::query::plumbing::try_execute_query::<rustc_query_impl[f03dd36fb3d62396]::plumbing::QueryCtxt, rustc_query_system[a1cbf340ec6d6f93]::query::caches::DefaultCache<(), ()>>
      36:     0x7f4ccfdc3331 - rustc_query_system[a1cbf340ec6d6f93]::query::plumbing::get_query::<rustc_query_impl[f03dd36fb3d62396]::queries::typeck_item_bodies, rustc_query_impl[f03dd36fb3d62396]::plumbing::QueryCtxt>
      37:     0x7f4ccfac4473 - <rustc_session[d1b76e352c1a3b5b]::session::Session>::time::<(), rustc_typeck[4b1a1a7c06e44333]::check_crate::{closure#7}>
      38:     0x7f4ccfab109b - rustc_typeck[4b1a1a7c06e44333]::check_crate
      39:     0x7f4ccf86b017 - rustc_interface[94703aee012e7483]::passes::analysis
      40:     0x7f4ccfcea595 - <rustc_query_system[a1cbf340ec6d6f93]::dep_graph::graph::DepGraph<rustc_middle[c4bb946b42085c78]::dep_graph::dep_node::DepKind>>::with_task::<rustc_middle[c4bb946b42085c78]::ty::context::TyCtxt, (), core[c8091fc9dea0c6cf]::result::Result<(), rustc_errors[ae611370a546922c]::ErrorGuaranteed>>
      41:     0x7f4ccfd8f1fd - rustc_query_system[a1cbf340ec6d6f93]::query::plumbing::try_execute_query::<rustc_query_impl[f03dd36fb3d62396]::plumbing::QueryCtxt, rustc_query_system[a1cbf340ec6d6f93]::query::caches::DefaultCache<(), core[c8091fc9dea0c6cf]::result::Result<(), rustc_errors[ae611370a546922c]::ErrorGuaranteed>>>
      42:     0x7f4ccfdd58ee - rustc_query_system[a1cbf340ec6d6f93]::query::plumbing::get_query::<rustc_query_impl[f03dd36fb3d62396]::queries::analysis, rustc_query_impl[f03dd36fb3d62396]::plumbing::QueryCtxt>
      43:     0x7f4ccf8282a7 - <rustc_interface[94703aee012e7483]::passes::QueryContext>::enter::<rustc_driver[7b89ea061d8aedc7]::run_compiler::{closure#1}::{closure#2}::{closure#3}, core[c8091fc9dea0c6cf]::result::Result<(), rustc_errors[ae611370a546922c]::ErrorGuaranteed>>
      44:     0x7f4ccf81234f - <rustc_interface[94703aee012e7483]::interface::Compiler>::enter::<rustc_driver[7b89ea061d8aedc7]::run_compiler::{closure#1}::{closure#2}, core[c8091fc9dea0c6cf]::result::Result<core[c8091fc9dea0c6cf]::option::Option<rustc_interface[94703aee012e7483]::queries::Linker>, rustc_errors[ae611370a546922c]::ErrorGuaranteed>>
      45:     0x7f4ccf83bd3f - rustc_span[c774ced8c61a5d44]::with_source_map::<core[c8091fc9dea0c6cf]::result::Result<(), rustc_errors[ae611370a546922c]::ErrorGuaranteed>, rustc_interface[94703aee012e7483]::interface::create_compiler_and_run<core[c8091fc9dea0c6cf]::result::Result<(), rustc_errors[ae611370a546922c]::ErrorGuaranteed>, rustc_driver[7b89ea061d8aedc7]::run_compiler::{closure#1}>::{closure#1}>
      46:     0x7f4ccf8131e2 - <scoped_tls[a35f89285f520bf3]::ScopedKey<rustc_span[c774ced8c61a5d44]::SessionGlobals>>::set::<rustc_interface[94703aee012e7483]::interface::run_compiler<core[c8091fc9dea0c6cf]::result::Result<(), rustc_errors[ae611370a546922c]::ErrorGuaranteed>, rustc_driver[7b89ea061d8aedc7]::run_compiler::{closure#1}>::{closure#0}, core[c8091fc9dea0c6cf]::result::Result<(), rustc_errors[ae611370a546922c]::ErrorGuaranteed>>
      47:     0x7f4ccf82893f - std[f19dd7bb03296d5c]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[94703aee012e7483]::util::run_in_thread_pool_with_globals<rustc_interface[94703aee012e7483]::interface::run_compiler<core[c8091fc9dea0c6cf]::result::Result<(), rustc_errors[ae611370a546922c]::ErrorGuaranteed>, rustc_driver[7b89ea061d8aedc7]::run_compiler::{closure#1}>::{closure#0}, core[c8091fc9dea0c6cf]::result::Result<(), rustc_errors[ae611370a546922c]::ErrorGuaranteed>>::{closure#0}, core[c8091fc9dea0c6cf]::result::Result<(), rustc_errors[ae611370a546922c]::ErrorGuaranteed>>
      48:     0x7f4ccf828a99 - <<std[f19dd7bb03296d5c]::thread::Builder>::spawn_unchecked_<rustc_interface[94703aee012e7483]::util::run_in_thread_pool_with_globals<rustc_interface[94703aee012e7483]::interface::run_compiler<core[c8091fc9dea0c6cf]::result::Result<(), rustc_errors[ae611370a546922c]::ErrorGuaranteed>, rustc_driver[7b89ea061d8aedc7]::run_compiler::{closure#1}>::{closure#0}, core[c8091fc9dea0c6cf]::result::Result<(), rustc_errors[ae611370a546922c]::ErrorGuaranteed>>::{closure#0}, core[c8091fc9dea0c6cf]::result::Result<(), rustc_errors[ae611370a546922c]::ErrorGuaranteed>>::{closure#1} as core[c8091fc9dea0c6cf]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}

    error: internal compiler error: unexpected panic

    note: the compiler unexpectedly panicked. this is a bug.

    note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

    note: rustc 1.63.0-nightly (a6b8c6954 2022-06-03) running on x86_64-unknown-linux-gnu

    note: compiler flags: --crate-type bin -C embed-bitcode=no -C debuginfo=2 -C incremental

    note: some of the compiler flags provided by cargo are hidden

    query stack during panic:
    #0 [typeck] type-checking `main`
    #1 [typeck_item_bodies] type-checking all item bodies
    #2 [analysis] running analysis passes on this crate
    end of query stack
Repository owner locked and limited conversation to collaborators Nov 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants