Skip to content

Commit f40f996

Browse files
committed
compiler: Lower fn call arg spans down to MIR
To enable improved accuracy of diagnostics in upcoming commits.
1 parent 18e12dc commit f40f996

File tree

6 files changed

+77
-72
lines changed

6 files changed

+77
-72
lines changed

src/abi/mod.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use cranelift_module::ModuleError;
1111
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
1212
use rustc_middle::ty::layout::FnAbiOf;
1313
use rustc_session::Session;
14+
use rustc_span::source_map::Spanned;
1415
use rustc_target::abi::call::{Conv, FnAbi};
1516
use rustc_target::spec::abi::Abi;
1617

@@ -360,7 +361,7 @@ pub(crate) fn codegen_terminator_call<'tcx>(
360361
fx: &mut FunctionCx<'_, '_, 'tcx>,
361362
source_info: mir::SourceInfo,
362363
func: &Operand<'tcx>,
363-
args: &[Operand<'tcx>],
364+
args: &[Spanned<Operand<'tcx>>],
364365
destination: Place<'tcx>,
365366
target: Option<BasicBlock>,
366367
) {
@@ -415,7 +416,7 @@ pub(crate) fn codegen_terminator_call<'tcx>(
415416

416417
let extra_args = &args[fn_sig.inputs().skip_binder().len()..];
417418
let extra_args = fx.tcx.mk_type_list_from_iter(
418-
extra_args.iter().map(|op_arg| fx.monomorphize(op_arg.ty(fx.mir, fx.tcx))),
419+
extra_args.iter().map(|op_arg| fx.monomorphize(op_arg.node.ty(fx.mir, fx.tcx))),
419420
);
420421
let fn_abi = if let Some(instance) = instance {
421422
RevealAllLayoutCx(fx.tcx).fn_abi_of_instance(instance, extra_args)
@@ -440,10 +441,10 @@ pub(crate) fn codegen_terminator_call<'tcx>(
440441
// Unpack arguments tuple for closures
441442
let mut args = if fn_sig.abi() == Abi::RustCall {
442443
let (self_arg, pack_arg) = match args {
443-
[pack_arg] => (None, codegen_call_argument_operand(fx, pack_arg)),
444+
[pack_arg] => (None, codegen_call_argument_operand(fx, &pack_arg.node)),
444445
[self_arg, pack_arg] => (
445-
Some(codegen_call_argument_operand(fx, self_arg)),
446-
codegen_call_argument_operand(fx, pack_arg),
446+
Some(codegen_call_argument_operand(fx, &self_arg.node)),
447+
codegen_call_argument_operand(fx, &pack_arg.node),
447448
),
448449
_ => panic!("rust-call abi requires one or two arguments"),
449450
};
@@ -463,7 +464,7 @@ pub(crate) fn codegen_terminator_call<'tcx>(
463464
}
464465
args
465466
} else {
466-
args.iter().map(|arg| codegen_call_argument_operand(fx, arg)).collect::<Vec<_>>()
467+
args.iter().map(|arg| codegen_call_argument_operand(fx, &arg.node)).collect::<Vec<_>>()
467468
};
468469

469470
// Pass the caller location for `#[track_caller]`.

src/intrinsics/llvm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub(crate) fn codegen_llvm_intrinsic_call<'tcx>(
77
fx: &mut FunctionCx<'_, '_, 'tcx>,
88
intrinsic: &str,
99
generic_args: GenericArgsRef<'tcx>,
10-
args: &[mir::Operand<'tcx>],
10+
args: &[Spanned<mir::Operand<'tcx>>],
1111
ret: CPlace<'tcx>,
1212
target: Option<BasicBlock>,
1313
span: Span,

src/intrinsics/llvm_aarch64.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub(crate) fn codegen_aarch64_llvm_intrinsic_call<'tcx>(
77
fx: &mut FunctionCx<'_, '_, 'tcx>,
88
intrinsic: &str,
99
_args: GenericArgsRef<'tcx>,
10-
args: &[mir::Operand<'tcx>],
10+
args: &[Spanned<mir::Operand<'tcx>>],
1111
ret: CPlace<'tcx>,
1212
target: Option<BasicBlock>,
1313
) {

src/intrinsics/llvm_x86.rs

+43-43
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>(
1111
fx: &mut FunctionCx<'_, '_, 'tcx>,
1212
intrinsic: &str,
1313
_args: GenericArgsRef<'tcx>,
14-
args: &[mir::Operand<'tcx>],
14+
args: &[Spanned<mir::Operand<'tcx>>],
1515
ret: CPlace<'tcx>,
1616
target: Option<BasicBlock>,
1717
span: Span,
@@ -175,9 +175,9 @@ pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>(
175175
[x, y, kind] => (x, y, kind),
176176
_ => bug!("wrong number of args for intrinsic {intrinsic}"),
177177
};
178-
let x = codegen_operand(fx, x);
179-
let y = codegen_operand(fx, y);
180-
let kind = match kind {
178+
let x = codegen_operand(fx, &x.node);
179+
let y = codegen_operand(fx, &y.node);
180+
let kind = match &kind.node {
181181
Operand::Constant(const_) => crate::constant::eval_mir_constant(fx, const_).0,
182182
Operand::Copy(_) | Operand::Move(_) => unreachable!("{kind:?}"),
183183
};
@@ -287,8 +287,8 @@ pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>(
287287
[a, b] => (a, b),
288288
_ => bug!("wrong number of args for intrinsic {intrinsic}"),
289289
};
290-
let a = codegen_operand(fx, a);
291-
let b = codegen_operand(fx, b);
290+
let a = codegen_operand(fx, &a.node);
291+
let b = codegen_operand(fx, &b.node);
292292

293293
// Based on the pseudocode at https://github.com/rust-lang/stdarch/blob/1cfbca8b38fd9b4282b2f054f61c6ca69fc7ce29/crates/core_arch/src/x86/avx2.rs#L2319-L2332
294294
let zero = fx.bcx.ins().iconst(types::I8, 0);
@@ -325,9 +325,9 @@ pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>(
325325
[a, b, imm8] => (a, b, imm8),
326326
_ => bug!("wrong number of args for intrinsic {intrinsic}"),
327327
};
328-
let a = codegen_operand(fx, a);
329-
let b = codegen_operand(fx, b);
330-
let imm8 = codegen_operand(fx, imm8).load_scalar(fx);
328+
let a = codegen_operand(fx, &a.node);
329+
let b = codegen_operand(fx, &b.node);
330+
let imm8 = codegen_operand(fx, &imm8.node).load_scalar(fx);
331331

332332
let a_low = a.value_typed_lane(fx, fx.tcx.types.u128, 0).load_scalar(fx);
333333
let a_high = a.value_typed_lane(fx, fx.tcx.types.u128, 1).load_scalar(fx);
@@ -956,14 +956,14 @@ pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>(
956956
let b = b.load_scalar(fx);
957957
let lb = lb.load_scalar(fx);
958958

959-
let imm8 = if let Some(imm8) = crate::constant::mir_operand_get_const_val(fx, &args[4])
960-
{
961-
imm8
962-
} else {
963-
fx.tcx
964-
.dcx()
965-
.span_fatal(span, "Index argument for `_mm_cmpestri` is not a constant");
966-
};
959+
let imm8 =
960+
if let Some(imm8) = crate::constant::mir_operand_get_const_val(fx, &args[4].node) {
961+
imm8
962+
} else {
963+
fx.tcx
964+
.dcx()
965+
.span_fatal(span, "Index argument for `_mm_cmpestri` is not a constant");
966+
};
967967

968968
let imm8 = imm8.try_to_u8().unwrap_or_else(|_| panic!("kind not scalar: {:?}", imm8));
969969

@@ -1009,14 +1009,14 @@ pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>(
10091009
let b = b.load_scalar(fx);
10101010
let lb = lb.load_scalar(fx);
10111011

1012-
let imm8 = if let Some(imm8) = crate::constant::mir_operand_get_const_val(fx, &args[4])
1013-
{
1014-
imm8
1015-
} else {
1016-
fx.tcx
1017-
.dcx()
1018-
.span_fatal(span, "Index argument for `_mm_cmpestrm` is not a constant");
1019-
};
1012+
let imm8 =
1013+
if let Some(imm8) = crate::constant::mir_operand_get_const_val(fx, &args[4].node) {
1014+
imm8
1015+
} else {
1016+
fx.tcx
1017+
.dcx()
1018+
.span_fatal(span, "Index argument for `_mm_cmpestrm` is not a constant");
1019+
};
10201020

10211021
let imm8 = imm8.try_to_u8().unwrap_or_else(|_| panic!("kind not scalar: {:?}", imm8));
10221022

@@ -1056,15 +1056,15 @@ pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>(
10561056
let a = a.load_scalar(fx);
10571057
let b = b.load_scalar(fx);
10581058

1059-
let imm8 = if let Some(imm8) = crate::constant::mir_operand_get_const_val(fx, &args[2])
1060-
{
1061-
imm8
1062-
} else {
1063-
fx.tcx.dcx().span_fatal(
1064-
span,
1065-
"Index argument for `_mm_clmulepi64_si128` is not a constant",
1066-
);
1067-
};
1059+
let imm8 =
1060+
if let Some(imm8) = crate::constant::mir_operand_get_const_val(fx, &args[2].node) {
1061+
imm8
1062+
} else {
1063+
fx.tcx.dcx().span_fatal(
1064+
span,
1065+
"Index argument for `_mm_clmulepi64_si128` is not a constant",
1066+
);
1067+
};
10681068

10691069
let imm8 = imm8.try_to_u8().unwrap_or_else(|_| panic!("kind not scalar: {:?}", imm8));
10701070

@@ -1093,15 +1093,15 @@ pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>(
10931093

10941094
let a = a.load_scalar(fx);
10951095

1096-
let imm8 = if let Some(imm8) = crate::constant::mir_operand_get_const_val(fx, &args[1])
1097-
{
1098-
imm8
1099-
} else {
1100-
fx.tcx.dcx().span_fatal(
1101-
span,
1102-
"Index argument for `_mm_aeskeygenassist_si128` is not a constant",
1103-
);
1104-
};
1096+
let imm8 =
1097+
if let Some(imm8) = crate::constant::mir_operand_get_const_val(fx, &args[1].node) {
1098+
imm8
1099+
} else {
1100+
fx.tcx.dcx().span_fatal(
1101+
span,
1102+
"Index argument for `_mm_aeskeygenassist_si128` is not a constant",
1103+
);
1104+
};
11051105

11061106
let imm8 = imm8.try_to_u8().unwrap_or_else(|_| panic!("kind not scalar: {:?}", imm8));
11071107

src/intrinsics/mod.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ macro_rules! intrinsic_args {
55
($fx:expr, $args:expr => ($($arg:tt),*); $intrinsic:expr) => {
66
#[allow(unused_parens)]
77
let ($($arg),*) = if let [$($arg),*] = $args {
8-
($(codegen_operand($fx, $arg)),*)
8+
($(codegen_operand($fx, &($arg).node)),*)
99
} else {
1010
$crate::intrinsics::bug_on_incorrect_arg_count($intrinsic);
1111
};
@@ -22,6 +22,7 @@ use rustc_middle::ty;
2222
use rustc_middle::ty::layout::{HasParamEnv, ValidityRequirement};
2323
use rustc_middle::ty::print::{with_no_trimmed_paths, with_no_visible_paths};
2424
use rustc_middle::ty::GenericArgsRef;
25+
use rustc_span::source_map::Spanned;
2526
use rustc_span::symbol::{kw, sym, Symbol};
2627

2728
pub(crate) use self::llvm::codegen_llvm_intrinsic_call;
@@ -263,7 +264,7 @@ fn bool_to_zero_or_max_uint<'tcx>(
263264
pub(crate) fn codegen_intrinsic_call<'tcx>(
264265
fx: &mut FunctionCx<'_, '_, 'tcx>,
265266
instance: Instance<'tcx>,
266-
args: &[mir::Operand<'tcx>],
267+
args: &[Spanned<mir::Operand<'tcx>>],
267268
destination: CPlace<'tcx>,
268269
target: Option<BasicBlock>,
269270
source_info: mir::SourceInfo,
@@ -301,7 +302,7 @@ pub(crate) fn codegen_intrinsic_call<'tcx>(
301302
fn codegen_float_intrinsic_call<'tcx>(
302303
fx: &mut FunctionCx<'_, '_, 'tcx>,
303304
intrinsic: Symbol,
304-
args: &[mir::Operand<'tcx>],
305+
args: &[Spanned<mir::Operand<'tcx>>],
305306
ret: CPlace<'tcx>,
306307
) -> bool {
307308
let (name, arg_count, ty, clif_ty) = match intrinsic {
@@ -353,18 +354,21 @@ fn codegen_float_intrinsic_call<'tcx>(
353354
let (a, b, c);
354355
let args = match args {
355356
[x] => {
356-
a = [codegen_operand(fx, x).load_scalar(fx)];
357+
a = [codegen_operand(fx, &x.node).load_scalar(fx)];
357358
&a as &[_]
358359
}
359360
[x, y] => {
360-
b = [codegen_operand(fx, x).load_scalar(fx), codegen_operand(fx, y).load_scalar(fx)];
361+
b = [
362+
codegen_operand(fx, &x.node).load_scalar(fx),
363+
codegen_operand(fx, &y.node).load_scalar(fx),
364+
];
361365
&b
362366
}
363367
[x, y, z] => {
364368
c = [
365-
codegen_operand(fx, x).load_scalar(fx),
366-
codegen_operand(fx, y).load_scalar(fx),
367-
codegen_operand(fx, z).load_scalar(fx),
369+
codegen_operand(fx, &x.node).load_scalar(fx),
370+
codegen_operand(fx, &y.node).load_scalar(fx),
371+
codegen_operand(fx, &z.node).load_scalar(fx),
368372
];
369373
&c
370374
}
@@ -422,7 +426,7 @@ fn codegen_regular_intrinsic_call<'tcx>(
422426
instance: Instance<'tcx>,
423427
intrinsic: Symbol,
424428
generic_args: GenericArgsRef<'tcx>,
425-
args: &[mir::Operand<'tcx>],
429+
args: &[Spanned<mir::Operand<'tcx>>],
426430
ret: CPlace<'tcx>,
427431
destination: Option<BasicBlock>,
428432
source_info: mir::SourceInfo,

src/intrinsics/simd.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>(
2121
fx: &mut FunctionCx<'_, '_, 'tcx>,
2222
intrinsic: Symbol,
2323
generic_args: GenericArgsRef<'tcx>,
24-
args: &[mir::Operand<'tcx>],
24+
args: &[Spanned<mir::Operand<'tcx>>],
2525
ret: CPlace<'tcx>,
2626
target: BasicBlock,
2727
span: Span,
@@ -121,8 +121,8 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>(
121121
let [x, y] = args else {
122122
bug!("wrong number of args for intrinsic {intrinsic}");
123123
};
124-
let x = codegen_operand(fx, x);
125-
let y = codegen_operand(fx, y);
124+
let x = codegen_operand(fx, &x.node);
125+
let y = codegen_operand(fx, &y.node);
126126

127127
if !x.layout().ty.is_simd() {
128128
report_simd_type_validation_error(fx, intrinsic, span, x.layout().ty);
@@ -172,8 +172,8 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>(
172172
bug!("wrong number of args for intrinsic {intrinsic}");
173173
}
174174
};
175-
let x = codegen_operand(fx, x);
176-
let y = codegen_operand(fx, y);
175+
let x = codegen_operand(fx, &x.node);
176+
let y = codegen_operand(fx, &y.node);
177177

178178
if !x.layout().ty.is_simd() {
179179
report_simd_type_validation_error(fx, intrinsic, span, x.layout().ty);
@@ -182,7 +182,7 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>(
182182

183183
// Make sure this is actually an array, since typeck only checks the length-suffixed
184184
// version of this intrinsic.
185-
let idx_ty = fx.monomorphize(idx.ty(fx.mir, fx.tcx));
185+
let idx_ty = fx.monomorphize(idx.node.ty(fx.mir, fx.tcx));
186186
let n: u16 = match idx_ty.kind() {
187187
ty::Array(ty, len) if matches!(ty.kind(), ty::Uint(ty::UintTy::U32)) => len
188188
.try_eval_target_usize(fx.tcx, ty::ParamEnv::reveal_all())
@@ -215,7 +215,7 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>(
215215

216216
let indexes = {
217217
use rustc_middle::mir::interpret::*;
218-
let idx_const = match idx {
218+
let idx_const = match &idx.node {
219219
Operand::Constant(const_) => crate::constant::eval_mir_constant(fx, const_).0,
220220
Operand::Copy(_) | Operand::Move(_) => unreachable!("{idx:?}"),
221221
};
@@ -269,12 +269,12 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>(
269269
bug!("wrong number of args for intrinsic {intrinsic}");
270270
}
271271
};
272-
let base = codegen_operand(fx, base);
273-
let val = codegen_operand(fx, val);
272+
let base = codegen_operand(fx, &base.node);
273+
let val = codegen_operand(fx, &val.node);
274274

275275
// FIXME validate
276276
let idx_const = if let Some(idx_const) =
277-
crate::constant::mir_operand_get_const_val(fx, idx)
277+
crate::constant::mir_operand_get_const_val(fx, &idx.node)
278278
{
279279
idx_const
280280
} else {
@@ -304,15 +304,15 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>(
304304
bug!("wrong number of args for intrinsic {intrinsic}");
305305
}
306306
};
307-
let v = codegen_operand(fx, v);
307+
let v = codegen_operand(fx, &v.node);
308308

309309
if !v.layout().ty.is_simd() {
310310
report_simd_type_validation_error(fx, intrinsic, span, v.layout().ty);
311311
return;
312312
}
313313

314314
let idx_const = if let Some(idx_const) =
315-
crate::constant::mir_operand_get_const_val(fx, idx)
315+
crate::constant::mir_operand_get_const_val(fx, &idx.node)
316316
{
317317
idx_const
318318
} else {

0 commit comments

Comments
 (0)