Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 296586f

Browse files
committed
miri: for ABI mismatch errors, say which argument is the problem
1 parent 86ef320 commit 296586f

25 files changed

+45
-35
lines changed

‎compiler/rustc_const_eval/messages.ftl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,15 @@ const_eval_frame_note_inner = inside {$where_ ->
128128
129129
const_eval_frame_note_last = the failure occurred here
130130
131+
const_eval_incompatible_arg_types =
132+
calling a function whose parameter #{$arg_idx} has type {$callee_ty} passing argument of type {$caller_ty}
133+
131134
const_eval_incompatible_calling_conventions =
132135
calling a function with calling convention "{$callee_conv}" using calling convention "{$caller_conv}"
133136
134137
const_eval_incompatible_return_types =
135138
calling a function with return type {$callee_ty} passing return place of type {$caller_ty}
136139
137-
const_eval_incompatible_types =
138-
calling a function with argument of type {$callee_ty} passing data of type {$caller_ty}
139-
140140
const_eval_interior_mutable_borrow_escaping =
141141
interior mutable shared borrows of temporaries that have their lifetime extended until the end of the program are not allowed
142142
.label = this borrow of an interior mutable value refers to such a temporary

‎compiler/rustc_const_eval/src/errors.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ impl<'a> ReportErrorExt for UndefinedBehaviorInfo<'a> {
500500
InvalidNichedEnumVariantWritten { .. } => {
501501
const_eval_invalid_niched_enum_variant_written
502502
}
503-
AbiMismatchArgument { .. } => const_eval_incompatible_types,
503+
AbiMismatchArgument { .. } => const_eval_incompatible_arg_types,
504504
AbiMismatchReturn { .. } => const_eval_incompatible_return_types,
505505
}
506506
}
@@ -625,12 +625,16 @@ impl<'a> ReportErrorExt for UndefinedBehaviorInfo<'a> {
625625
diag.arg("data_size", info.data_size);
626626
}
627627
InvalidNichedEnumVariantWritten { enum_ty } => {
628-
diag.arg("ty", enum_ty.to_string());
628+
diag.arg("ty", enum_ty);
629629
}
630-
AbiMismatchArgument { caller_ty, callee_ty }
631-
| AbiMismatchReturn { caller_ty, callee_ty } => {
632-
diag.arg("caller_ty", caller_ty.to_string());
633-
diag.arg("callee_ty", callee_ty.to_string());
630+
AbiMismatchArgument { arg_idx, caller_ty, callee_ty } => {
631+
diag.arg("arg_idx", arg_idx + 1); // adjust for 1-indexed lists in output
632+
diag.arg("caller_ty", caller_ty);
633+
diag.arg("callee_ty", callee_ty);
634+
}
635+
AbiMismatchReturn { caller_ty, callee_ty } => {
636+
diag.arg("caller_ty", caller_ty);
637+
diag.arg("callee_ty", callee_ty);
634638
}
635639
}
636640
}

‎compiler/rustc_const_eval/src/interpret/call.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
270270
Item = (&'x FnArg<'tcx, M::Provenance>, &'y ArgAbi<'tcx, Ty<'tcx>>),
271271
>,
272272
callee_abi: &ArgAbi<'tcx, Ty<'tcx>>,
273+
callee_arg_idx: usize,
273274
callee_arg: &mir::Place<'tcx>,
274275
callee_ty: Ty<'tcx>,
275276
already_live: bool,
@@ -298,6 +299,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
298299
// Check compatibility
299300
if !self.check_argument_compat(caller_abi, callee_abi)? {
300301
throw_ub!(AbiMismatchArgument {
302+
arg_idx: callee_arg_idx,
301303
caller_ty: caller_abi.layout.ty,
302304
callee_ty: callee_abi.layout.ty
303305
});
@@ -424,7 +426,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
424426
// this is a single iterator (that handles `spread_arg`), then
425427
// `pass_argument` would be the loop body. It takes care to
426428
// not advance `caller_iter` for ignored arguments.
427-
let mut callee_args_abis = callee_fn_abi.args.iter();
429+
let mut callee_args_abis = callee_fn_abi.args.iter().enumerate();
428430
for local in body.args_iter() {
429431
// Construct the destination place for this argument. At this point all
430432
// locals are still dead, so we cannot construct a `PlaceTy`.
@@ -445,21 +447,23 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
445447
&[mir::ProjectionElem::Field(FieldIdx::from_usize(i), field_ty)],
446448
*self.tcx,
447449
);
448-
let callee_abi = callee_args_abis.next().unwrap();
450+
let (idx,callee_abi) = callee_args_abis.next().unwrap();
449451
self.pass_argument(
450452
&mut caller_args,
451453
callee_abi,
454+
idx,
452455
&dest,
453456
field_ty,
454457
/* already_live */ true,
455458
)?;
456459
}
457460
} else {
458461
// Normal argument. Cannot mark it as live yet, it might be unsized!
459-
let callee_abi = callee_args_abis.next().unwrap();
462+
let (idx,callee_abi) = callee_args_abis.next().unwrap();
460463
self.pass_argument(
461464
&mut caller_args,
462465
callee_abi,
466+
idx,
463467
&dest,
464468
ty,
465469
/* already_live */ false,

‎compiler/rustc_middle/src/mir/interpret/error.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,12 @@ pub enum UndefinedBehaviorInfo<'tcx> {
426426
/// Trying to set discriminant to the niched variant, but the value does not match.
427427
InvalidNichedEnumVariantWritten { enum_ty: Ty<'tcx> },
428428
/// ABI-incompatible argument types.
429-
AbiMismatchArgument { caller_ty: Ty<'tcx>, callee_ty: Ty<'tcx> },
429+
AbiMismatchArgument {
430+
/// The index of the argument whose type is wrong.
431+
arg_idx: usize,
432+
caller_ty: Ty<'tcx>,
433+
callee_ty: Ty<'tcx>,
434+
},
430435
/// ABI-incompatible return types.
431436
AbiMismatchReturn { caller_ty: Ty<'tcx>, callee_ty: Ty<'tcx> },
432437
}

‎src/tools/miri/src/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ pub fn report_error<'tcx>(
382382
helps.push(note_span!(span, "{:?} was deallocated here:", alloc_id));
383383
}
384384
}
385-
AbiMismatchArgument { .. } | AbiMismatchReturn{ .. }=> {
385+
AbiMismatchArgument { .. } => {
386386
helps.push(note!("this means these two types are not *guaranteed* to be ABI-compatible across all targets"));
387387
helps.push(note!("if you think this code should be accepted anyway, please report an issue with Miri"));
388388
}

‎src/tools/miri/src/helpers.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
10791079
.position(|b| !b)
10801080
{
10811081
throw_ub!(AbiMismatchArgument {
1082+
arg_idx: index,
10821083
caller_ty: caller_fn_abi.args[index].layout.ty,
10831084
callee_ty: callee_fn_abi.args[index].layout.ty
10841085
});

‎src/tools/miri/tests/fail/function_pointers/abi_mismatch_array_vs_struct.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ fn main() {
1717
// These two types have the same size but are still not compatible.
1818
let g = unsafe { std::mem::transmute::<fn(S), fn(A)>(f) };
1919

20-
g(Default::default()) //~ ERROR: calling a function with argument of type S passing data of type [i32; 4]
20+
g(Default::default()) //~ ERROR: type S passing argument of type [i32; 4]
2121
}

‎src/tools/miri/tests/fail/function_pointers/abi_mismatch_array_vs_struct.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: Undefined Behavior: calling a function with argument of type S passing data of type [i32; 4]
1+
error: Undefined Behavior: calling a function whose parameter #1 has type S passing argument of type [i32; 4]
22
--> tests/fail/function_pointers/abi_mismatch_array_vs_struct.rs:LL:CC
33
|
44
LL | g(Default::default())

‎src/tools/miri/tests/fail/function_pointers/abi_mismatch_int_vs_float.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ fn main() {
33

44
let g = unsafe { std::mem::transmute::<fn(f32), fn(i32)>(f) };
55

6-
g(42) //~ ERROR: calling a function with argument of type f32 passing data of type i32
6+
g(42) //~ ERROR: type f32 passing argument of type i32
77
}

‎src/tools/miri/tests/fail/function_pointers/abi_mismatch_int_vs_float.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: Undefined Behavior: calling a function with argument of type f32 passing data of type i32
1+
error: Undefined Behavior: calling a function whose parameter #1 has type f32 passing argument of type i32
22
--> tests/fail/function_pointers/abi_mismatch_int_vs_float.rs:LL:CC
33
|
44
LL | g(42)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /