Auto merge of #13620 - GnomedDev:large-const-array-compute-const, r=Manishearth

Fire large_const_arrays for computed array lengths

changelog: [`large_const_arrays`]: Lint now fires when the length is determined by an expression
This commit is contained in:
bors 2024-10-28 20:15:54 +00:00
commit 528dcc3025
4 changed files with 21 additions and 9 deletions

View file

@ -4,7 +4,7 @@ use rustc_errors::Applicability;
use rustc_hir::{Item, ItemKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::layout::LayoutOf;
use rustc_middle::ty::{self, ConstKind};
use rustc_middle::ty::{self, ParamEnv};
use rustc_session::impl_lint_pass;
use rustc_span::{BytePos, Pos, Span};
@ -56,7 +56,7 @@ impl<'tcx> LateLintPass<'tcx> for LargeConstArrays {
&& !item.span.from_expansion()
&& let ty = cx.tcx.type_of(item.owner_id).instantiate_identity()
&& let ty::Array(element_type, cst) = ty.kind()
&& let ConstKind::Value(_, ty::ValTree::Leaf(element_count)) = cst.kind()
&& let Ok((_, ty::ValTree::Leaf(element_count))) = cst.eval(cx.tcx, ParamEnv::empty(), item.span)
&& let element_count = element_count.to_target_usize(cx.tcx)
&& let Ok(element_size) = cx.layout_of(*element_type).map(|l| l.size.bytes())
&& u128::from(self.maximum_allowed_size) < u128::from(element_count) * u128::from(element_size)

View file

@ -9,11 +9,13 @@ pub struct S {
// Should lint
pub(crate) static FOO_PUB_CRATE: [u32; 1_000_000] = [0u32; 1_000_000];
pub static FOO_PUB: [u32; 1_000_000] = [0u32; 1_000_000];
static FOO_COMPUTED: [u32; 1_000 * 100] = [0u32; 1_000 * 100];
static FOO: [u32; 1_000_000] = [0u32; 1_000_000];
// Good
pub(crate) const G_FOO_PUB_CRATE: [u32; 250] = [0u32; 250];
pub const G_FOO_PUB: [u32; 250] = [0u32; 250];
const G_FOO_COMPUTED: [u32; 25 * 10] = [0u32; 25 * 10];
const G_FOO: [u32; 250] = [0u32; 250];
fn main() {

View file

@ -9,11 +9,13 @@ pub struct S {
// Should lint
pub(crate) const FOO_PUB_CRATE: [u32; 1_000_000] = [0u32; 1_000_000];
pub const FOO_PUB: [u32; 1_000_000] = [0u32; 1_000_000];
const FOO_COMPUTED: [u32; 1_000 * 100] = [0u32; 1_000 * 100];
const FOO: [u32; 1_000_000] = [0u32; 1_000_000];
// Good
pub(crate) const G_FOO_PUB_CRATE: [u32; 250] = [0u32; 250];
pub const G_FOO_PUB: [u32; 250] = [0u32; 250];
const G_FOO_COMPUTED: [u32; 25 * 10] = [0u32; 25 * 10];
const G_FOO: [u32; 250] = [0u32; 250];
fn main() {

View file

@ -20,13 +20,21 @@ LL | pub const FOO_PUB: [u32; 1_000_000] = [0u32; 1_000_000];
error: large array defined as const
--> tests/ui/large_const_arrays.rs:12:1
|
LL | const FOO_COMPUTED: [u32; 1_000 * 100] = [0u32; 1_000 * 100];
| -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| help: make this a static item: `static`
error: large array defined as const
--> tests/ui/large_const_arrays.rs:13:1
|
LL | const FOO: [u32; 1_000_000] = [0u32; 1_000_000];
| -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| help: make this a static item: `static`
error: large array defined as const
--> tests/ui/large_const_arrays.rs:21:5
--> tests/ui/large_const_arrays.rs:23:5
|
LL | pub const BAR_PUB: [u32; 1_000_000] = [0u32; 1_000_000];
| ^^^^-----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -34,7 +42,7 @@ LL | pub const BAR_PUB: [u32; 1_000_000] = [0u32; 1_000_000];
| help: make this a static item: `static`
error: large array defined as const
--> tests/ui/large_const_arrays.rs:22:5
--> tests/ui/large_const_arrays.rs:24:5
|
LL | const BAR: [u32; 1_000_000] = [0u32; 1_000_000];
| -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -42,7 +50,7 @@ LL | const BAR: [u32; 1_000_000] = [0u32; 1_000_000];
| help: make this a static item: `static`
error: large array defined as const
--> tests/ui/large_const_arrays.rs:23:5
--> tests/ui/large_const_arrays.rs:25:5
|
LL | pub const BAR_STRUCT_PUB: [S; 5_000] = [S { data: [0; 32] }; 5_000];
| ^^^^-----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -50,7 +58,7 @@ LL | pub const BAR_STRUCT_PUB: [S; 5_000] = [S { data: [0; 32] }; 5_000];
| help: make this a static item: `static`
error: large array defined as const
--> tests/ui/large_const_arrays.rs:24:5
--> tests/ui/large_const_arrays.rs:26:5
|
LL | const BAR_STRUCT: [S; 5_000] = [S { data: [0; 32] }; 5_000];
| -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -58,7 +66,7 @@ LL | const BAR_STRUCT: [S; 5_000] = [S { data: [0; 32] }; 5_000];
| help: make this a static item: `static`
error: large array defined as const
--> tests/ui/large_const_arrays.rs:25:5
--> tests/ui/large_const_arrays.rs:27:5
|
LL | pub const BAR_S_PUB: [Option<&str>; 200_000] = [Some("str"); 200_000];
| ^^^^-----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -66,12 +74,12 @@ LL | pub const BAR_S_PUB: [Option<&str>; 200_000] = [Some("str"); 200_000];
| help: make this a static item: `static`
error: large array defined as const
--> tests/ui/large_const_arrays.rs:26:5
--> tests/ui/large_const_arrays.rs:28:5
|
LL | const BAR_S: [Option<&str>; 200_000] = [Some("str"); 200_000];
| -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| help: make this a static item: `static`
error: aborting due to 9 previous errors
error: aborting due to 10 previous errors