Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions cpp-overload-test/examples/stdcpp-unordered-map.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//! Test C++ [std::unordered_map][unordered_map] overloads that can't be represented in Rust, and
//! record the macro diagnostics. Error output is in `stdcpp-unordered-map.stderr`.
//! Working overloads are in `src/stdcpp/unordered_map.rs`.
//!
//! [unordered_map]: https://en.cppreference.com/cpp/container/unordered_map
#![feature(splat, tuple_trait)]
#![allow(incomplete_features)]
#![allow(unused_braces)]

use splat_overload::overload;

/// Workaround for missing generic support in the `overload!` macro: the map key type.
type K = std::ffi::c_int;

/// Workaround for missing generic support in the `overload!` macro: the mapped value type.
type V = std::ffi::c_int;

/// A fake key-value pair, matching the working `KeyValue` type.
#[expect(dead_code, reason = "Incompatible overloads can't be used")]
#[derive(Copy, Clone)]
struct KeyValue {
key: K,
value: V,
}

/// A wrapper struct to hold the returned C++ pointer.
/// We fake the C++ map pointer with a Rust vector to help with type checking.
#[expect(dead_code, reason = "Incompatible overloads can't be used")]
struct StdUnorderedMap(Vec<KeyValue>);

// Accessors: `max_load_factor(...)`
overload! {
impl StdUnorderedMap {
/// Gets the maximum load factor, a `const` method in C++.
///
/// ### Incompatibilities
///
/// The `overload!` macro requires all overloads in a group to have matching
/// receiver kinds, but `max_load_factor` uses both `&self` and `&mut self`.
///
/// ### Workarounds
///
/// Use associated functions with explicit `&StdUnorderedMap` and
/// `&mut StdUnorderedMap` parameters, as in `src/stdcpp/unordered_map.rs`.
fn max_load_factor(&self) -> f32 {
todo!()
}

/// Set the maximum load factor, a non-`const` method in C++.
fn max_load_factor(&mut self, _ml: f32) {
todo!()
}
}
}

// Accessors: `contains(...)`
overload! {
impl StdUnorderedMap {
/// Check whether the map contains `key`.
fn contains(&self, _key: &K) -> bool {
todo!()
}

/// Checks whether the map contains a key equal to `key`, even if it has a
/// different type from the map's key type.
///
/// ### Incompatibilities
///
/// The `overload!` macro does not preserve generic parameters, leaving `Q`
/// unresolved in the generated code.
///
/// ### Limitations
///
/// C++ only allows this overload when both `Hash` and `KeyEqual` support
/// transparent lookup through `is_transparent`. Rust has no direct equivalent.
///
/// ### Workaround
///
/// Use `HashMap::get` with the `K: Borrow<Q>` trait bound instead.
fn contains<Q>(&self, _key: &Q) -> bool {
todo!()
}
}
}

pub fn main() {}
32 changes: 32 additions & 0 deletions cpp-overload-test/examples/stdcpp-unordered-map.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
error: proc macro panicked
Comment thread
EDSONZ-WASSWA marked this conversation as resolved.
Resolved
--> cpp-overload-test/examples/stdcpp-unordered-map.rs:32:1
|
32 | / overload! {
33 | | impl StdUnorderedMap {
34 | | /// Gets the maximum load factor, a `const` method in C++.
35 | | ///
... |
54 | | }
| |_^
|
= help: message: all overloads must have the same receiver kind (&self, &mut self, self, or no receiver), got max_load_factor: &self and max_load_factor: &mut self

error[E0425]: cannot find type `Q` in this scope
--> cpp-overload-test/examples/stdcpp-unordered-map.rs:80:38
|
80 | fn contains<Q>(&self, _key: &Q) -> bool {
| ^ not found in this scope
|
note: similarly named type alias `K` defined here
--> cpp-overload-test/examples/stdcpp-unordered-map.rs:13:1
|
13 | type K = std::ffi::c_int;
| ^^^^^^^^^^^^^^^^^^^^^^^^^
help: a type alias with a similar name exists
|
80 - fn contains<Q>(&self, _key: &Q) -> bool {
80 + fn contains<Q>(&self, _key: &K) -> bool {
|

For more information about this error, try `rustc --explain E0425`.
error: could not compile `cpp-overload-test` (example "stdcpp-unordered-map") due to 2 previous errors
1 change: 1 addition & 0 deletions cpp-overload-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub mod stdcpp {
// Add new modules for each type here, in alphabetical order.
pub mod forward_list;
pub mod hash;
pub mod unordered_map;
pub mod unordered_multiset;
}

Expand Down
Loading
Loading