Skip to content

Commit 21ca3e8

Browse files
CoAlloc: Workaround invariant detection defect rust-lang/rust rust-lang#108751.
1 parent aca7905 commit 21ca3e8

File tree

1 file changed

+40
-3
lines changed
  • library/alloc/src/collections/btree

1 file changed

+40
-3
lines changed

library/alloc/src/collections/btree/node.rs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,20 @@ const KV_IDX_CENTER: usize = B - 1;
4646
const EDGE_IDX_LEFT_OF_CENTER: usize = B - 1;
4747
const EDGE_IDX_RIGHT_OF_CENTER: usize = B;
4848

49+
/// Workaround https://github.com/rust-lang/rust/issues/108751
50+
macro_rules! leaf_node_capacity {
51+
() => {
52+
11
53+
}; // instead of: CAPACITY
54+
}
55+
56+
/// Workaround https://github.com/rust-lang/rust/issues/108751
57+
macro_rules! internal_node_capacity {
58+
() => {
59+
12
60+
}; // instead of: 2 * B
61+
}
62+
4963
/// The underlying representation of leaf nodes and part of the representation of internal nodes.
5064
struct LeafNode<K, V> {
5165
/// We want to be covariant in `K` and `V`.
@@ -61,8 +75,20 @@ struct LeafNode<K, V> {
6175

6276
/// The arrays storing the actual data of the node. Only the first `len` elements of each
6377
/// array are initialized and valid.
64-
keys: [MaybeUninit<K>; CAPACITY],
65-
vals: [MaybeUninit<V>; CAPACITY],
78+
keys: [MaybeUninit<K>; leaf_node_capacity!()], // @FIXME leaf_node_capacity!() workaround for https://github.com/rust-lang/rust/issues/108751
79+
vals: [MaybeUninit<V>; leaf_node_capacity!()],
80+
}
81+
82+
/// @FIXME Remove once we remove leaf_node_capacity!() workaround for https://github.com/rust-lang/rust/issues/108751
83+
#[test]
84+
#[allow(dead_code)]
85+
fn assert_leaf_node_capacity() {
86+
fn leaf_node() -> LeafNode<char, bool> {
87+
loop {}
88+
}
89+
let node = leaf_node();
90+
let _keys: [MaybeUninit<char>; CAPACITY] = node.keys;
91+
let _vals: [MaybeUninit<bool>; CAPACITY] = node.vals;
6692
}
6793

6894
impl<K, V> LeafNode<K, V> {
@@ -100,7 +126,18 @@ struct InternalNode<K, V> {
100126
/// The pointers to the children of this node. `len + 1` of these are considered
101127
/// initialized and valid, except that near the end, while the tree is held
102128
/// through borrow type `Dying`, some of these pointers are dangling.
103-
edges: [MaybeUninit<BoxedNode<K, V>>; 2 * B],
129+
edges: [MaybeUninit<BoxedNode<K, V>>; internal_node_capacity!()], // @FIXME internal_node_capacity!() workaround for https://github.com/rust-lang/rust/issues/108751
130+
}
131+
132+
/// @FIXME Remove once we remove internal_node_capacity!() workaround for https://github.com/rust-lang/rust/issues/108751
133+
#[test]
134+
#[allow(dead_code)]
135+
fn assert_internal_node_capacity() {
136+
fn internal_node() -> InternalNode<char, bool> {
137+
loop {}
138+
}
139+
let node = internal_node();
140+
let _edges: [MaybeUninit<BoxedNode<char, bool>>; 2 * B] = node.edges;
104141
}
105142

106143
impl<K, V> InternalNode<K, V> {

0 commit comments

Comments
 (0)