Skip to content

Commit 92f2e0a

Browse files
committed
Auto merge of rust-lang#116520 - Enselic:large-copy-into-fn, r=oli-obk
large_assignments: Lint on specific large args passed to functions Requires lowering function call arg spans down to MIR, which is done in the second commit. Part of rust-lang#83518 Also see * https://rust-lang.zulipchat.com/#narrow/stream/182449-t-compiler.2Fhelp/topic/arg.20Spans.20for.20TerminatorKind.3A.3ACall.3F * https://rust-lang.zulipchat.com/#narrow/stream/122651-general/topic/move_size_limit.20lint r? `@oli-obk` (E-mentor)
2 parents e64f849 + 8f440f0 commit 92f2e0a

File tree

52 files changed

+379
-210
lines changed

Some content is hidden

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

52 files changed

+379
-210
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1257,7 +1257,10 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
12571257
return None;
12581258
};
12591259
debug!("checking call args for uses of inner_param: {:?}", args);
1260-
args.contains(&Operand::Move(inner_param)).then_some((loc, term))
1260+
args.iter()
1261+
.map(|a| &a.node)
1262+
.any(|a| a == &Operand::Move(inner_param))
1263+
.then_some((loc, term))
12611264
})
12621265
else {
12631266
debug!("no uses of inner_param found as a by-move call arg");
@@ -3242,7 +3245,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
32423245
assigned_to, args
32433246
);
32443247
for operand in args {
3245-
let (Operand::Copy(assigned_from) | Operand::Move(assigned_from)) = operand
3248+
let (Operand::Copy(assigned_from) | Operand::Move(assigned_from)) =
3249+
&operand.node
32463250
else {
32473251
continue;
32483252
};

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
691691
);
692692
// Check if one of the arguments to this function is the target place.
693693
let found_target = args.iter().any(|arg| {
694-
if let Operand::Move(place) = arg {
694+
if let Operand::Move(place) = arg.node {
695695
if let Some(potential) = place.as_local() {
696696
potential == target
697697
} else {

compiler/rustc_borrowck/src/diagnostics/mod.rs

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

compiler/rustc_borrowck/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ impl<'cx, 'tcx, R> rustc_mir_dataflow::ResultsVisitor<'cx, 'tcx, R> for MirBorro
703703
} => {
704704
self.consume_operand(loc, (func, span), flow_state);
705705
for arg in args {
706-
self.consume_operand(loc, (arg, span), flow_state);
706+
self.consume_operand(loc, (&arg.node, arg.span), flow_state);
707707
}
708708
self.mutate_place(loc, (*destination, span), Deep, flow_state);
709709
}

compiler/rustc_borrowck/src/polonius/loan_invalidations.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ impl<'cx, 'tcx> Visitor<'tcx> for LoanInvalidationsGenerator<'cx, 'tcx> {
120120
} => {
121121
self.consume_operand(location, func);
122122
for arg in args {
123-
self.consume_operand(location, arg);
123+
self.consume_operand(location, &arg.node);
124124
}
125125
self.mutate_place(location, *destination, Deep);
126126
}

compiler/rustc_borrowck/src/type_check/mod.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use rustc_middle::ty::{
3636
};
3737
use rustc_middle::ty::{GenericArgsRef, UserArgs};
3838
use rustc_span::def_id::CRATE_DEF_ID;
39+
use rustc_span::source_map::Spanned;
3940
use rustc_span::symbol::sym;
4041
use rustc_span::{Span, DUMMY_SP};
4142
use rustc_target::abi::{FieldIdx, FIRST_VARIANT};
@@ -1359,7 +1360,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
13591360
TerminatorKind::Call { func, args, destination, call_source, target, .. } => {
13601361
self.check_operand(func, term_location);
13611362
for arg in args {
1362-
self.check_operand(arg, term_location);
1363+
self.check_operand(&arg.node, term_location);
13631364
}
13641365

13651366
let func_ty = func.ty(body, tcx);
@@ -1580,7 +1581,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
15801581
term: &Terminator<'tcx>,
15811582
func: &Operand<'tcx>,
15821583
sig: &ty::FnSig<'tcx>,
1583-
args: &[Operand<'tcx>],
1584+
args: &[Spanned<Operand<'tcx>>],
15841585
term_location: Location,
15851586
call_source: CallSource,
15861587
) {
@@ -1593,7 +1594,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
15931594
if self.tcx().is_intrinsic(def_id) {
15941595
match self.tcx().item_name(def_id) {
15951596
sym::simd_shuffle => {
1596-
if !matches!(args[2], Operand::Constant(_)) {
1597+
if !matches!(args[2], Spanned { node: Operand::Constant(_), .. }) {
15971598
self.tcx()
15981599
.dcx()
15991600
.emit_err(SimdShuffleLastConst { span: term.source_info.span });
@@ -1606,7 +1607,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
16061607
debug!(?func_ty);
16071608

16081609
for (n, (fn_arg, op_arg)) in iter::zip(sig.inputs(), args).enumerate() {
1609-
let op_arg_ty = op_arg.ty(body, self.tcx());
1610+
let op_arg_ty = op_arg.node.ty(body, self.tcx());
16101611

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

compiler/rustc_codegen_cranelift/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]`.

compiler/rustc_codegen_cranelift/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,

compiler/rustc_codegen_cranelift/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
) {

compiler/rustc_codegen_cranelift/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

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
};
@@ -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,

0 commit comments

Comments
 (0)