Skip to content

Commit d2a5893

Browse files
Limited working around rust-lang/rust issue rust-lang#106473.
1 parent 76b2237 commit d2a5893

File tree

1 file changed

+59
-18
lines changed

1 file changed

+59
-18
lines changed

library/alloc/src/macros.rs

+59-18
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,32 @@ macro_rules! __rust_force_expr {
125125
};
126126
}
127127

128-
// ----- CoAlloc constant-like macros:
128+
// ----- CoAlloc ICE workaround macro:
129+
/// This "validates" type of a given `const` expression, and it casts it. That helps to prevent mix ups with macros/integer constant values.
130+
#[doc(hidden)]
131+
#[macro_export]
132+
#[unstable(feature = "global_co_alloc_meta", issue = "none")]macro_rules! check_type_and_cast {
133+
// Use the following for compile-time/build check only. And use it
134+
// with a hard-coded `0` version of `meta_num_slots` - otherwise you get an ICE.
135+
//
136+
/*($e:expr, $t_check:ty, $t_cast:ty) => {
137+
($e + 0 as $t_check) as $t_cast
138+
}*/
139+
// Use the following to build for testing/using, while rustc causes an ICE with the above and
140+
// with a full version of `meta_num_slots`.
141+
($e:expr, $t_check:ty, $t_cast:ty) => {
142+
$e
143+
}
144+
}
129145

146+
// ----- CoAlloc constant-like macros:
130147
/// Coallocation option/parameter about using metadata that does prefer to use meta data. This is of type [::alloc::co_alloc::CoAllocMetaNumSlotsPref] (but not a whole []::alloc::co_alloc::CoAllocPref]).
148+
#[doc(hidden)]
131149
#[unstable(feature = "global_co_alloc_meta", issue = "none")]
132150
#[macro_export]
133151
macro_rules! CO_ALLOC_PREF_NUM_META_SLOTS_ONE {
134152
() => {
135-
(1 as $crate::co_alloc::CoAllocMetaNumSlotsPref)
153+
$crate::check_type_and_cast!(1, i32, $crate::co_alloc::CoAllocMetaNumSlotsPref)
136154
};
137155
}
138156

@@ -141,7 +159,7 @@ macro_rules! CO_ALLOC_PREF_NUM_META_SLOTS_ONE {
141159
#[macro_export]
142160
macro_rules! CO_ALLOC_PREF_NUM_META_SLOTS_ZERO {
143161
() => {
144-
(0 as $crate::co_alloc::CoAllocMetaNumSlotsPref)
162+
$crate::check_type_and_cast!(0, i32, $crate::co_alloc::CoAllocMetaNumSlotsPref)
145163
};
146164
}
147165

@@ -150,7 +168,7 @@ macro_rules! CO_ALLOC_PREF_NUM_META_SLOTS_ZERO {
150168
#[macro_export]
151169
macro_rules! CO_ALLOC_PREF_NUM_META_SLOTS_DEFAULT {
152170
() => {
153-
(0 as $crate::co_alloc::CoAllocMetaNumSlotsPref)
171+
$crate::check_type_and_cast!(0, i32, $crate::co_alloc::CoAllocMetaNumSlotsPref)
154172
};
155173
}
156174

@@ -167,6 +185,7 @@ macro_rules! CO_ALLOC_PREF_NUM_META_SLOTS_DEFAULT {
167185
#[macro_export]
168186
macro_rules! CO_ALLOC_PREF_META_YES {
169187
() => {
188+
//1usize
170189
$crate::co_alloc_pref!($crate::CO_ALLOC_PREF_NUM_META_SLOTS_ONE!())
171190
};
172191
}
@@ -184,6 +203,7 @@ macro_rules! CO_ALLOC_PREF_META_YES {
184203
#[macro_export]
185204
macro_rules! CO_ALLOC_PREF_META_NO {
186205
() => {
206+
//0usize
187207
$crate::co_alloc_pref!($crate::CO_ALLOC_PREF_NUM_META_SLOTS_ZERO!())
188208
};
189209
}
@@ -202,6 +222,7 @@ macro_rules! CO_ALLOC_PREF_META_NO {
202222
#[macro_export]
203223
macro_rules! CO_ALLOC_PREF_META_DEFAULT {
204224
() => {
225+
//0usize
205226
$crate::co_alloc_pref!($crate::CO_ALLOC_PREF_NUM_META_SLOTS_DEFAULT!())
206227
};
207228
}
@@ -211,6 +232,7 @@ macro_rules! CO_ALLOC_PREF_META_DEFAULT {
211232
#[macro_export]
212233
macro_rules! CO_ALLOC_PREF_DEFAULT {
213234
() => {
235+
//0usize
214236
$crate::CO_ALLOC_PREF_META_DEFAULT!()
215237
};
216238
}
@@ -221,6 +243,7 @@ macro_rules! CO_ALLOC_PREF_DEFAULT {
221243
#[macro_export]
222244
macro_rules! SHORT_TERM_VEC_CO_ALLOC_PREF {
223245
() => {
246+
//0usize
224247
$crate::CO_ALLOC_PREF_META_NO!()
225248
};
226249
}
@@ -242,8 +265,8 @@ macro_rules! co_alloc_pref {
242265
// report the incorrect type of $meta_pref (if $meta_pref were some other integer, casting would
243266
// compile, and we would not be notified).
244267
($meta_pref:expr) => {
245-
(($meta_pref + (0 as $crate::co_alloc::CoAllocMetaNumSlotsPref))
246-
as $crate::co_alloc::CoAllocPref)
268+
$crate::check_type_and_cast!($meta_pref,$crate::co_alloc::CoAllocMetaNumSlotsPref,
269+
$crate::co_alloc::CoAllocPref)
247270
};
248271
}
249272

@@ -267,18 +290,29 @@ macro_rules! co_alloc_pref {
267290
#[unstable(feature = "global_co_alloc", issue = "none")]
268291
#[macro_export]
269292
macro_rules! meta_num_slots {
270-
// Generating, for example, (0 as usize), here, triggers an ICE.
271-
272-
// This "validates" types of both params - to prevent mix ups.
273-
// @FIXME remove this comment line: Removing/commenting out the part: <$alloc as ::core::alloc::Allocator>::CO_ALLOC_META_NUM_SLOTS +
274-
// does NOT fix the ICE (unless there are multiple ICE's).
293+
// @FIXME Use this only
294+
// - once the ICE gets fixed, or
295+
// - (until the ICE is fixed) with a related change in `check_type_and_cast` that makes it pass
296+
// the given expression (parameter) unchecked & uncast.
297+
/*($alloc:ty, $co_alloc_pref:expr) => {
298+
$crate::check_type_and_cast!(<$alloc as ::core::alloc::Allocator>::CO_ALLOC_META_NUM_SLOTS,::core::alloc::CoAllocatorMetaNumSlots,
299+
usize) *
300+
$crate::check_type_and_cast!($co_alloc_pref, $crate::co_alloc::CoAllocPref, usize)
301+
};*/
302+
// Use for testing & production, until ICE gets fixed. (Regardless of $co_alloc_pref.)
303+
//
304+
// Why still ICE?!
275305
($alloc:ty, $co_alloc_pref:expr) => {
276-
/*(
277-
((<$alloc as ::core::alloc::Allocator>::CO_ALLOC_META_NUM_SLOTS + (0 as ::core::alloc::CoAllocatorMetaNumSlots))0
278-
as usize)
279-
* ($co_alloc_pref + (0 as $crate::co_alloc::CoAllocPref))
280-
as usize)*/0usize
281-
};
306+
// The following fails here - even if not used from meta_num_slots_default nor from meta_num_slots_global!
307+
//<$alloc as ::core::alloc::Allocator>::CO_ALLOC_META_NUM_SLOTS
308+
//<$crate::alloc::Global as ::core::alloc::Allocator>::CO_ALLOC_META_NUM_SLOTS
309+
//1usize
310+
$co_alloc_pref
311+
}
312+
// Use for testing & production as enforcing no meta.
313+
/*($alloc:ty, $co_alloc_pref:expr) => {
314+
0usize // compiles
315+
}*/
282316
}
283317
// -\---> replace with something like:
284318
/*
@@ -304,7 +338,10 @@ macro_rules! meta_num_slots_default {
304338
// Can't generate if ... {1} else {0}
305339
// because it's "overly complex generic constant".
306340
($alloc:ty) => {
341+
// EITHER of the following are OK here
307342
$crate::meta_num_slots!($alloc, $crate::CO_ALLOC_PREF_DEFAULT!())
343+
//<$alloc as ::core::alloc::Allocator>::CO_ALLOC_META_NUM_SLOTS
344+
//<$crate::alloc::Global as ::core::alloc::Allocator>::CO_ALLOC_META_NUM_SLOTS
308345
};
309346
}
310347

@@ -320,10 +357,13 @@ macro_rules! meta_num_slots_default {
320357
#[macro_export]
321358
macro_rules! meta_num_slots_global {
322359
($co_alloc_pref:expr) => {
360+
// EITHER of the following are OK here
323361
$crate::meta_num_slots!($crate::alloc::Global, $co_alloc_pref)
362+
// The following is OK here:
363+
//<$crate::alloc::Global as ::core::alloc::Allocator>::CO_ALLOC_META_NUM_SLOTS
324364
};
325365
}
326-
366+
/*
327367
/// Like `meta_num_slots`, but for `Global allocator and default coallocation preference
328368
/// (`CO_ALLOC_PREF_DEFAULT`).
329369
///
@@ -336,3 +376,4 @@ macro_rules! meta_num_slots_default_global {
336376
$crate::meta_num_slots!($crate::alloc::Global, $crate::CO_ALLOC_PREF_DEFAULT!())
337377
};
338378
}
379+
*/

0 commit comments

Comments
 (0)