Closed
Description
I don't know the version, but play.rust-lang.org nightly (and stable, for that matter, as well -- the error printed there is thread 'rustc' panicked at 'capacity overflow'
).
This is a weird one:
extern {
fn write(fildes: i32, buf: *const i8, nbyte: u64) -> i64;
}
#[inline(always)]
fn size_of<T>(_: T) -> usize {
::std::mem::size_of::<T>()
}
macro_rules! write {
($arr:expr) => {{
#[allow(non_upper_case_globals)]
const stdout: i32 = 1;
unsafe {
write(stdout, $arr.as_ptr() as *const i8,
$arr.len() * size_of($arr[0]));
}
}}
}
fn main() {
let hello = ['H', 'e', 'y'];
write!(hello);
}
errors with
<anon>:23:12: 16:48 error: mismatched types:
expected `u64`,
found `usize`
(expected u64,
found usize) [E0308]
(internal compiler error: unprintable span) # << Notice the ICE
<anon>:10:1: 19:2 note: in expansion of write!
<anon>:23:5: 23:19 note: expansion site
<anon>:23:12: 16:48 help: see the detailed explanation for E0308
error: aborting due to previous error
playpen: application terminated with error code 101
But, if you add parentheses around $arr.len() * size_of($arr[0])
, the ICE disappears, and
<anon>:16:19: 16:50 error: mismatched types:
expected `u64`,
found `usize`
(expected u64,
found usize) [E0308]
<anon>:16 ($arr.len() * size_of($arr[0])));
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:10:1: 19:2 note: in expansion of write!
<anon>:23:5: 23:19 note: expansion site
<anon>:16:19: 16:50 help: see the detailed explanation for E0308
error: aborting due to previous error
playpen: application terminated with error code 101
is printed instead.
Also, on a completely unrelated note, a C-style sizeof(x)
would be very useful, instead of having to use size_of::<T>()
.