Skip to content

Commit 1dfb596

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

File tree

46 files changed

+231
-135
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+231
-135
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use rustc_middle::util::CallKind;
2323
use rustc_mir_dataflow::move_paths::{InitKind, MoveOutIndex, MovePathIndex};
2424
use rustc_span::def_id::LocalDefId;
2525
use rustc_span::hygiene::DesugaringKind;
26+
use rustc_span::source_map::Spanned;
2627
use rustc_span::symbol::{kw, sym, Ident};
2728
use rustc_span::{BytePos, Span, Symbol};
2829
use rustc_trait_selection::infer::InferCtxtExt;
@@ -1247,7 +1248,10 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
12471248
return None;
12481249
};
12491250
debug!("checking call args for uses of inner_param: {:?}", args);
1250-
args.contains(&Operand::Move(inner_param)).then_some((loc, term))
1251+
args.iter()
1252+
.map(|a| &a.node)
1253+
.any(|a| a == &Operand::Move(inner_param))
1254+
.then_some((loc, term))
12511255
})
12521256
else {
12531257
debug!("no uses of inner_param found as a by-move call arg");
@@ -3172,7 +3176,10 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
31723176
assigned_to, args
31733177
);
31743178
for operand in args {
3175-
let (Operand::Copy(assigned_from) | Operand::Move(assigned_from)) = operand
3179+
let Spanned {
3180+
node: Operand::Copy(assigned_from) | Operand::Move(assigned_from),
3181+
..
3182+
} = operand
31763183
else {
31773184
continue;
31783185
};

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
635635
);
636636
// Check if one of the arguments to this function is the target place.
637637
let found_target = args.iter().any(|arg| {
638-
if let Operand::Move(place) = arg {
638+
if let Operand::Move(place) = arg.node {
639639
if let Some(potential) = place.as_local() {
640640
potential == target
641641
} else {

compiler/rustc_borrowck/src/diagnostics/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
2222
use rustc_middle::util::{call_kind, CallDesugaringKind};
2323
use rustc_mir_dataflow::move_paths::{InitLocation, LookupResult};
2424
use rustc_span::def_id::LocalDefId;
25+
use rustc_span::source_map::Spanned;
2526
use rustc_span::{symbol::sym, Span, Symbol, DUMMY_SP};
2627
use rustc_target::abi::{FieldIdx, VariantIdx};
2728
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
@@ -110,9 +111,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
110111
debug!("add_moved_or_invoked_closure_note: id={:?}", id);
111112
if Some(self.infcx.tcx.parent(id)) == self.infcx.tcx.lang_items().fn_once_trait() {
112113
let closure = match args.first() {
113-
Some(Operand::Copy(place) | Operand::Move(place))
114-
if target == place.local_or_deref_local() =>
115-
{
114+
Some(Spanned {
115+
node: Operand::Copy(place) | Operand::Move(place), ..
116+
}) if target == place.local_or_deref_local() => {
116117
place.local_or_deref_local().unwrap()
117118
}
118119
_ => return false,

compiler/rustc_borrowck/src/invalidation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ impl<'cx, 'tcx> Visitor<'tcx> for InvalidationGenerator<'cx, 'tcx> {
133133
} => {
134134
self.consume_operand(location, func);
135135
for arg in args {
136-
self.consume_operand(location, arg);
136+
self.consume_operand(location, &arg.node);
137137
}
138138
self.mutate_place(location, *destination, Deep);
139139
}

compiler/rustc_borrowck/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ impl<'cx, 'tcx, R> rustc_mir_dataflow::ResultsVisitor<'cx, 'tcx, R> for MirBorro
715715
} => {
716716
self.consume_operand(loc, (func, span), flow_state);
717717
for arg in args {
718-
self.consume_operand(loc, (arg, span), flow_state);
718+
self.consume_operand(loc, (&arg.node, arg.span), flow_state);
719719
}
720720
self.mutate_place(loc, (*destination, span), Deep, flow_state);
721721
}

compiler/rustc_borrowck/src/type_check/mod.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use rustc_middle::ty::{
3838
};
3939
use rustc_middle::ty::{GenericArgsRef, UserArgs};
4040
use rustc_span::def_id::CRATE_DEF_ID;
41+
use rustc_span::source_map::Spanned;
4142
use rustc_span::symbol::sym;
4243
use rustc_span::{Span, DUMMY_SP};
4344
use rustc_target::abi::{FieldIdx, FIRST_VARIANT};
@@ -1372,7 +1373,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
13721373
TerminatorKind::Call { func, args, destination, call_source, target, .. } => {
13731374
self.check_operand(func, term_location);
13741375
for arg in args {
1375-
self.check_operand(arg, term_location);
1376+
self.check_operand(&arg.node, term_location);
13761377
}
13771378

13781379
let func_ty = func.ty(body, tcx);
@@ -1571,7 +1572,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
15711572
term: &Terminator<'tcx>,
15721573
func: &Operand<'tcx>,
15731574
sig: &ty::FnSig<'tcx>,
1574-
args: &[Operand<'tcx>],
1575+
args: &[Spanned<Operand<'tcx>>],
15751576
term_location: Location,
15761577
call_source: CallSource,
15771578
) {
@@ -1584,7 +1585,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
15841585
if self.tcx().is_intrinsic(def_id) {
15851586
match self.tcx().item_name(def_id) {
15861587
sym::simd_shuffle => {
1587-
if !matches!(args[2], Operand::Constant(_)) {
1588+
if !matches!(args[2], Spanned { node: Operand::Constant(_), .. }) {
15881589
self.tcx()
15891590
.sess
15901591
.emit_err(SimdShuffleLastConst { span: term.source_info.span });
@@ -1597,7 +1598,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
15971598
debug!(?func_ty);
15981599

15991600
for (n, (fn_arg, op_arg)) in iter::zip(sig.inputs(), args).enumerate() {
1600-
let op_arg_ty = op_arg.ty(body, self.tcx());
1601+
let op_arg_ty = op_arg.node.ty(body, self.tcx());
16011602

16021603
let op_arg_ty = self.normalize(op_arg_ty, term_location);
16031604
let category = if call_source.from_hir_call() {

compiler/rustc_codegen_cranelift/src/abi/mod.rs

+6-5
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

@@ -355,19 +356,19 @@ struct CallArgument<'tcx> {
355356
// FIXME avoid intermediate `CValue` before calling `adjust_arg_for_abi`
356357
fn codegen_call_argument_operand<'tcx>(
357358
fx: &mut FunctionCx<'_, '_, 'tcx>,
358-
operand: &Operand<'tcx>,
359+
operand: &Spanned<Operand<'tcx>>,
359360
) -> CallArgument<'tcx> {
360361
CallArgument {
361-
value: codegen_operand(fx, operand),
362-
is_owned: matches!(operand, Operand::Move(_)),
362+
value: codegen_operand(fx, &operand.node),
363+
is_owned: matches!(operand.node, Operand::Move(_)),
363364
}
364365
}
365366

366367
pub(crate) fn codegen_terminator_call<'tcx>(
367368
fx: &mut FunctionCx<'_, '_, 'tcx>,
368369
source_info: mir::SourceInfo,
369370
func: &Operand<'tcx>,
370-
args: &[Operand<'tcx>],
371+
args: &[Spanned<Operand<'tcx>>],
371372
destination: Place<'tcx>,
372373
target: Option<BasicBlock>,
373374
) {
@@ -421,7 +422,7 @@ pub(crate) fn codegen_terminator_call<'tcx>(
421422

422423
let extra_args = &args[fn_sig.inputs().skip_binder().len()..];
423424
let extra_args = fx.tcx.mk_type_list_from_iter(
424-
extra_args.iter().map(|op_arg| fx.monomorphize(op_arg.ty(fx.mir, fx.tcx))),
425+
extra_args.iter().map(|op_arg| fx.monomorphize(op_arg.node.ty(fx.mir, fx.tcx))),
425426
);
426427
let fn_abi = if let Some(instance) = instance {
427428
RevealAllLayoutCx(fx.tcx).fn_abi_of_instance(instance, extra_args)

compiler/rustc_codegen_cranelift/src/intrinsics/llvm.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Emulate LLVM intrinsics
22
33
use rustc_middle::ty::GenericArgsRef;
4+
use rustc_span::source_map::Spanned;
45

56
use crate::intrinsics::*;
67
use crate::prelude::*;
@@ -9,7 +10,7 @@ pub(crate) fn codegen_llvm_intrinsic_call<'tcx>(
910
fx: &mut FunctionCx<'_, '_, 'tcx>,
1011
intrinsic: &str,
1112
generic_args: GenericArgsRef<'tcx>,
12-
args: &[mir::Operand<'tcx>],
13+
args: &[Spanned<mir::Operand<'tcx>>],
1314
ret: CPlace<'tcx>,
1415
target: Option<BasicBlock>,
1516
) {

compiler/rustc_codegen_cranelift/src/intrinsics/llvm_aarch64.rs

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

compiler/rustc_codegen_cranelift/src/intrinsics/llvm_x86.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>(
99
fx: &mut FunctionCx<'_, '_, 'tcx>,
1010
intrinsic: &str,
1111
_args: GenericArgsRef<'tcx>,
12-
args: &[mir::Operand<'tcx>],
12+
args: &[Spanned<mir::Operand<'tcx>>],
1313
ret: CPlace<'tcx>,
1414
target: Option<BasicBlock>,
1515
) {
@@ -72,9 +72,9 @@ pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>(
7272
[x, y, kind] => (x, y, kind),
7373
_ => bug!("wrong number of args for intrinsic {intrinsic}"),
7474
};
75-
let x = codegen_operand(fx, x);
76-
let y = codegen_operand(fx, y);
77-
let kind = match kind {
75+
let x = codegen_operand(fx, &x.node);
76+
let y = codegen_operand(fx, &y.node);
77+
let kind = match &kind.node {
7878
Operand::Constant(const_) => crate::constant::eval_mir_constant(fx, const_).0,
7979
Operand::Copy(_) | Operand::Move(_) => unreachable!("{kind:?}"),
8080
};
@@ -184,8 +184,8 @@ pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>(
184184
[a, b] => (a, b),
185185
_ => bug!("wrong number of args for intrinsic {intrinsic}"),
186186
};
187-
let a = codegen_operand(fx, a);
188-
let b = codegen_operand(fx, b);
187+
let a = codegen_operand(fx, &a.node);
188+
let b = codegen_operand(fx, &b.node);
189189

190190
// Based on the pseudocode at https://github.com/rust-lang/stdarch/blob/1cfbca8b38fd9b4282b2f054f61c6ca69fc7ce29/crates/core_arch/src/x86/avx2.rs#L2319-L2332
191191
let zero = fx.bcx.ins().iconst(types::I8, 0);
@@ -218,9 +218,9 @@ pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>(
218218
[a, b, imm8] => (a, b, imm8),
219219
_ => bug!("wrong number of args for intrinsic {intrinsic}"),
220220
};
221-
let a = codegen_operand(fx, a);
222-
let b = codegen_operand(fx, b);
223-
let imm8 = codegen_operand(fx, imm8).load_scalar(fx);
221+
let a = codegen_operand(fx, &a.node);
222+
let b = codegen_operand(fx, &b.node);
223+
let imm8 = codegen_operand(fx, &imm8.node).load_scalar(fx);
224224

225225
let a_0 = a.value_lane(fx, 0).load_scalar(fx);
226226
let a_1 = a.value_lane(fx, 1).load_scalar(fx);
@@ -275,7 +275,7 @@ pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>(
275275
[a] => a,
276276
_ => bug!("wrong number of args for intrinsic {intrinsic}"),
277277
};
278-
let a = codegen_operand(fx, a);
278+
let a = codegen_operand(fx, &a.node);
279279

280280
simd_for_each_lane(fx, a, ret, &|fx, _lane_ty, _res_lane_ty, lane| {
281281
fx.bcx.ins().iabs(lane)

compiler/rustc_codegen_cranelift/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
};
@@ -23,6 +23,7 @@ use rustc_middle::ty;
2323
use rustc_middle::ty::layout::{HasParamEnv, ValidityRequirement};
2424
use rustc_middle::ty::print::{with_no_trimmed_paths, with_no_visible_paths};
2525
use rustc_middle::ty::GenericArgsRef;
26+
use rustc_span::source_map::Spanned;
2627
use rustc_span::symbol::{kw, sym, Symbol};
2728

2829
pub(crate) use self::cpuid::codegen_cpuid_call;
@@ -206,7 +207,7 @@ fn bool_to_zero_or_max_uint<'tcx>(
206207
pub(crate) fn codegen_intrinsic_call<'tcx>(
207208
fx: &mut FunctionCx<'_, '_, 'tcx>,
208209
instance: Instance<'tcx>,
209-
args: &[mir::Operand<'tcx>],
210+
args: &[Spanned<mir::Operand<'tcx>>],
210211
destination: CPlace<'tcx>,
211212
target: Option<BasicBlock>,
212213
source_info: mir::SourceInfo,
@@ -244,7 +245,7 @@ pub(crate) fn codegen_intrinsic_call<'tcx>(
244245
fn codegen_float_intrinsic_call<'tcx>(
245246
fx: &mut FunctionCx<'_, '_, 'tcx>,
246247
intrinsic: Symbol,
247-
args: &[mir::Operand<'tcx>],
248+
args: &[Spanned<mir::Operand<'tcx>>],
248249
ret: CPlace<'tcx>,
249250
) -> bool {
250251
let (name, arg_count, ty, clif_ty) = match intrinsic {
@@ -296,18 +297,21 @@ fn codegen_float_intrinsic_call<'tcx>(
296297
let (a, b, c);
297298
let args = match args {
298299
[x] => {
299-
a = [codegen_operand(fx, x).load_scalar(fx)];
300+
a = [codegen_operand(fx, &x.node).load_scalar(fx)];
300301
&a as &[_]
301302
}
302303
[x, y] => {
303-
b = [codegen_operand(fx, x).load_scalar(fx), codegen_operand(fx, y).load_scalar(fx)];
304+
b = [
305+
codegen_operand(fx, &x.node).load_scalar(fx),
306+
codegen_operand(fx, &y.node).load_scalar(fx),
307+
];
304308
&b
305309
}
306310
[x, y, z] => {
307311
c = [
308-
codegen_operand(fx, x).load_scalar(fx),
309-
codegen_operand(fx, y).load_scalar(fx),
310-
codegen_operand(fx, z).load_scalar(fx),
312+
codegen_operand(fx, &x.node).load_scalar(fx),
313+
codegen_operand(fx, &y.node).load_scalar(fx),
314+
codegen_operand(fx, &z.node).load_scalar(fx),
311315
];
312316
&c
313317
}
@@ -365,7 +369,7 @@ fn codegen_regular_intrinsic_call<'tcx>(
365369
instance: Instance<'tcx>,
366370
intrinsic: Symbol,
367371
generic_args: GenericArgsRef<'tcx>,
368-
args: &[mir::Operand<'tcx>],
372+
args: &[Spanned<mir::Operand<'tcx>>],
369373
ret: CPlace<'tcx>,
370374
destination: Option<BasicBlock>,
371375
source_info: mir::SourceInfo,

compiler/rustc_codegen_cranelift/src/intrinsics/simd.rs

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

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

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

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

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

276276
// FIXME validate
277277
let idx_const = if let Some(idx_const) =
278-
crate::constant::mir_operand_get_const_val(fx, idx)
278+
crate::constant::mir_operand_get_const_val(fx, &idx.node)
279279
{
280280
idx_const
281281
} else {
@@ -305,15 +305,15 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>(
305305
bug!("wrong number of args for intrinsic {intrinsic}");
306306
}
307307
};
308-
let v = codegen_operand(fx, v);
308+
let v = codegen_operand(fx, &v.node);
309309

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

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

0 commit comments

Comments
 (0)