large_assignments: Allow moves into functions

Moves into functions are typically implemented with pointer passing
rather than memcpy's at the llvm-ir level, so allow moves into
functions.
This commit is contained in:
Martin Nordholts 2024-02-08 09:55:34 +01:00
parent 1a0200ebc0
commit e2979a8b8c
7 changed files with 66 additions and 17 deletions

View File

@ -666,7 +666,15 @@ impl<'a, 'tcx> MirUsedCollector<'a, 'tcx> {
debug!(?def_id, ?fn_span);
for arg in args {
if let Some(too_large_size) = self.operand_size_if_too_large(limit, &arg.node) {
// Moving args into functions is typically implemented with pointer
// passing at the llvm-ir level and not by memcpy's. So always allow
// moving args into functions.
let operand: &mir::Operand<'tcx> = &arg.node;
if let mir::Operand::Move(_) = operand {
continue;
}
if let Some(too_large_size) = self.operand_size_if_too_large(limit, operand) {
self.lint_large_assignment(limit.0, too_large_size, location, arg.span);
};
}

View File

@ -17,6 +17,9 @@ fn main() {
let _ = Arc::new(data); // OK!
let _ = Box::new(data); // OK!
let _ = Rc::new(data); // OK!
// Looking at --emit llvm-ir, we can see that a memcpy is involved in the
// parameter passing. So we want the lint to trigger here.
let _ = NotBox::new(data); //~ ERROR large_assignments
}
@ -26,6 +29,8 @@ struct NotBox {
impl NotBox {
fn new(data: [u8; 9999]) -> Self {
// Looking at --emit llvm-ir, we can see that a memcpy is involved.
// So we want the lint to trigger here.
Self { //~ ERROR large_assignments
data,
}

View File

@ -1,5 +1,5 @@
error: moving 9999 bytes
--> $DIR/copy_into_box_rc_arc.rs:20:25
--> $DIR/copy_into_box_rc_arc.rs:23:25
|
LL | let _ = NotBox::new(data);
| ^^^^ value moved from here
@ -12,7 +12,7 @@ LL | #![deny(large_assignments)]
| ^^^^^^^^^^^^^^^^^
error: moving 9999 bytes
--> $DIR/copy_into_box_rc_arc.rs:29:9
--> $DIR/copy_into_box_rc_arc.rs:34:9
|
LL | / Self {
LL | | data,

View File

@ -11,11 +11,16 @@ use std::{sync::Arc, rc::Rc};
fn main() {
// Looking at --emit mir, we can see that all parameters below are passed
// with by move.
// by move.
let _ = Arc::new([0; 9999]); // OK!
let _ = Box::new([0; 9999]); // OK!
let _ = Rc::new([0; 9999]); // OK!
let _ = NotBox::new([0; 9999]); //~ ERROR large_assignments
// Looking at --emit llvm-ir, we can see that no memcpy is involved in the
// parameter passing. Instead, a pointer is passed. This is typically what
// we get when moving parameter into functions. So we don't want the lint to
// trigger here.
let _ = NotBox::new([0; 9999]); // OK (compare with copy_into_box_rc_arc.rs)
}
struct NotBox {
@ -25,6 +30,8 @@ struct NotBox {
impl NotBox {
fn new(data: [u8; 9999]) -> Self {
Self {
// Looking at --emit llvm-ir, we can see that a memcpy is involved.
// So we want the lint to trigger here.
data, //~ ERROR large_assignments
}
}

View File

@ -1,8 +1,8 @@
error: moving 9999 bytes
--> $DIR/move_into_box_rc_arc.rs:18:25
--> $DIR/move_into_box_rc_arc.rs:35:13
|
LL | let _ = NotBox::new([0; 9999]);
| ^^^^^^^^^ value moved from here
LL | data,
| ^^^^ value moved from here
|
= note: The current maximum size is 1000, but it can be customized with the move_size_limit attribute: `#![move_size_limit = "..."]`
note: the lint level is defined here
@ -11,13 +11,5 @@ note: the lint level is defined here
LL | #![deny(large_assignments)]
| ^^^^^^^^^^^^^^^^^
error: moving 9999 bytes
--> $DIR/move_into_box_rc_arc.rs:28:13
|
LL | data,
| ^^^^ value moved from here
|
= note: The current maximum size is 1000, but it can be customized with the move_size_limit attribute: `#![move_size_limit = "..."]`
error: aborting due to 2 previous errors
error: aborting due to 1 previous error

View File

@ -0,0 +1,22 @@
// build-fail
#![feature(large_assignments)]
#![move_size_limit = "1000"]
#![deny(large_assignments)]
#![allow(unused)]
// Note: This type does not implement Copy.
struct Data([u8; 9999]);
fn main() {
// Looking at llvm-ir output, we can see a memcpy'd into Data, so we want
// the lint to trigger here.
let data = Data([100; 9999]); //~ ERROR large_assignments
// Looking at llvm-ir output, we can see that there is no memcpy involved in
// this function call. Instead, just a pointer is passed to the function. So
// the lint shall not trigger here.
take_data(data);
}
fn take_data(data: Data) {}

View File

@ -0,0 +1,15 @@
error: moving 9999 bytes
--> $DIR/move_into_fn.rs:14:16
|
LL | let data = Data([100; 9999]);
| ^^^^^^^^^^^^^^^^^ value moved from here
|
= note: The current maximum size is 1000, but it can be customized with the move_size_limit attribute: `#![move_size_limit = "..."]`
note: the lint level is defined here
--> $DIR/move_into_fn.rs:5:9
|
LL | #![deny(large_assignments)]
| ^^^^^^^^^^^^^^^^^
error: aborting due to 1 previous error