Skip to content

Commit fd19350

Browse files
committed
fix ICE in const_c_variadic when passing ZSTs
1 parent d2218f5 commit fd19350

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

compiler/rustc_const_eval/src/interpret/stack.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,9 @@ impl<'a, 'tcx: 'a, M: Machine<'tcx>> InterpCx<'tcx, M> {
638638
I: Iterator<Item = (&'a FnArg<'tcx, M::Provenance>, &'a ArgAbi<'tcx, Ty<'tcx>>)>,
639639
J: Iterator<Item = (usize, &'a ArgAbi<'tcx, Ty<'tcx>>)>,
640640
{
641+
// Arguments that are ignored (ZSTs) are not passed by the caller.
642+
let mut callee_abis = callee_abis.filter(|(_, abi)| !abi.is_ignore());
643+
641644
// Consume the remaining arguments and store them in fresh allocations.
642645
let mut varargs = Vec::new();
643646
for (fn_arg, caller_abi) in caller_args {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//@ compile-flags: --emit=obj
2+
#![feature(c_variadic)]
3+
#![feature(const_c_variadic)]
4+
#![feature(const_destruct)]
5+
#![crate_type = "lib"]
6+
7+
// Regression test for when a c-variadic argument is `mode: Ignore`. The caller won't pass the
8+
// argument, but the callee ABI does have the argument. Ensure that const-eval is able to handle
9+
// this case, even though it only comes up in programs that would error anyway because ZSTs do not
10+
// implement VaArgSafe.
11+
12+
const unsafe extern "C" fn read_n<const N: usize>(_: ...) {}
13+
14+
unsafe fn read_too_many() {
15+
const { read_n::<0>(read_as::<u32>, 1i32) }
16+
//~^ ERROR can't pass a function item to a variadic function
17+
}
18+
19+
fn read_as<T>() -> () {}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0617]: can't pass a function item to a variadic function
2+
--> $DIR/c-variadic-ignored-argument.rs:15:25
3+
|
4+
LL | const { read_n::<0>(read_as::<u32>, 1i32) }
5+
| ^^^^^^^^^^^^^^
6+
|
7+
= help: a function item is zero-sized and needs to be cast into a function pointer to be used in FFI
8+
= note: for more information on function items, visit https://doc.rust-lang.org/reference/types/function-item.html
9+
help: use a function pointer instead
10+
|
11+
LL | const { read_n::<0>(read_as::<u32> as fn(), 1i32) }
12+
| +++++++
13+
14+
error: aborting due to 1 previous error
15+
16+
For more information about this error, try `rustc --explain E0617`.

0 commit comments

Comments
 (0)