2021-03-25 19:29:11 +01:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_then;
|
2020-02-29 18:41:18 +01:00
|
|
|
use if_chain::if_chain;
|
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir::{Item, ItemKind};
|
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2021-08-30 17:38:27 +03:00
|
|
|
use rustc_middle::ty::layout::LayoutOf;
|
2020-03-23 22:07:46 +01:00
|
|
|
use rustc_middle::ty::{self, ConstKind};
|
2020-02-29 18:41:18 +01:00
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
|
|
|
use rustc_span::{BytePos, Pos, Span};
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 12:16:06 +02:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for large `const` arrays that should
|
2020-02-29 18:41:18 +01:00
|
|
|
/// be defined as `static` instead.
|
|
|
|
///
|
2021-07-29 12:16:06 +02:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Performance: const variables are inlined upon use.
|
2020-02-29 18:41:18 +01:00
|
|
|
/// Static items result in only one instance and has a fixed location in memory.
|
|
|
|
///
|
2021-07-29 12:16:06 +02:00
|
|
|
/// ### Example
|
2020-02-29 18:41:18 +01:00
|
|
|
/// ```rust,ignore
|
|
|
|
/// pub const a = [0u32; 1_000_000];
|
2022-06-16 17:39:06 +02:00
|
|
|
/// ```
|
2020-02-29 18:41:18 +01:00
|
|
|
///
|
2022-06-16 17:39:06 +02:00
|
|
|
/// Use instead:
|
2022-06-30 10:50:09 +02:00
|
|
|
/// ```rust,ignore
|
2020-02-29 18:41:18 +01:00
|
|
|
/// pub static a = [0u32; 1_000_000];
|
|
|
|
/// ```
|
2021-12-06 12:33:31 +01:00
|
|
|
#[clippy::version = "1.44.0"]
|
2020-02-29 18:41:18 +01:00
|
|
|
pub LARGE_CONST_ARRAYS,
|
|
|
|
perf,
|
|
|
|
"large non-scalar const array may cause performance overhead"
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct LargeConstArrays {
|
2022-12-29 14:28:34 +01:00
|
|
|
maximum_allowed_size: u128,
|
2020-02-29 18:41:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LargeConstArrays {
|
|
|
|
#[must_use]
|
2022-12-29 14:28:34 +01:00
|
|
|
pub fn new(maximum_allowed_size: u128) -> Self {
|
2020-02-29 18:41:18 +01:00
|
|
|
Self { maximum_allowed_size }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_lint_pass!(LargeConstArrays => [LARGE_CONST_ARRAYS]);
|
|
|
|
|
2020-06-25 23:41:36 +03:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for LargeConstArrays {
|
|
|
|
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
|
2020-02-29 18:41:18 +01:00
|
|
|
if_chain! {
|
|
|
|
if !item.span.from_expansion();
|
2023-09-20 13:41:20 +00:00
|
|
|
if let ItemKind::Const(_, generics, _) = &item.kind;
|
2023-07-02 23:21:36 +02:00
|
|
|
// Since static items may not have generics, skip generic const items.
|
|
|
|
// FIXME(generic_const_items): I don't think checking `generics.hwcp` suffices as it
|
|
|
|
// doesn't account for empty where-clauses that only consist of keyword `where` IINM.
|
|
|
|
if generics.params.is_empty() && !generics.has_where_clause_predicates;
|
2023-09-20 13:41:20 +00:00
|
|
|
let ty = cx.tcx.type_of(item.owner_id).instantiate_identity();
|
2020-08-04 00:18:29 +02:00
|
|
|
if let ty::Array(element_type, cst) = ty.kind();
|
2022-02-16 10:56:01 +01:00
|
|
|
if let ConstKind::Value(ty::ValTree::Leaf(element_count)) = cst.kind();
|
2023-02-14 14:31:26 +00:00
|
|
|
if let Ok(element_count) = element_count.try_to_target_usize(cx.tcx);
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-25 14:13:38 +11:00
|
|
|
if let Ok(element_size) = cx.layout_of(*element_type).map(|l| l.size.bytes());
|
2022-12-29 14:28:34 +01:00
|
|
|
if self.maximum_allowed_size < u128::from(element_count) * u128::from(element_size);
|
2020-02-29 18:41:18 +01:00
|
|
|
|
|
|
|
then {
|
|
|
|
let hi_pos = item.ident.span.lo() - BytePos::from_usize(1);
|
|
|
|
let sugg_span = Span::new(
|
|
|
|
hi_pos - BytePos::from_usize("const".len()),
|
|
|
|
hi_pos,
|
|
|
|
item.span.ctxt(),
|
2021-04-18 14:27:04 +02:00
|
|
|
item.span.parent(),
|
2020-02-29 18:41:18 +01:00
|
|
|
);
|
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
|
|
|
LARGE_CONST_ARRAYS,
|
|
|
|
item.span,
|
|
|
|
"large array defined as const",
|
2020-04-17 08:08:00 +02:00
|
|
|
|diag| {
|
|
|
|
diag.span_suggestion(
|
2020-02-29 18:41:18 +01:00
|
|
|
sugg_span,
|
|
|
|
"make this a static item",
|
2022-06-13 15:48:40 +09:00
|
|
|
"static",
|
2020-02-29 18:41:18 +01:00
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|