Skip to content

Commit 1dbc746

Browse files
committed
Lint instead of warn
1 parent 48975e3 commit 1dbc746

File tree

4 files changed

+59
-8
lines changed

4 files changed

+59
-8
lines changed

compiler/rustc_codegen_ssa/src/mir/mod.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::iter;
22

3+
use rustc_hir::CRATE_HIR_ID;
34
use rustc_index::IndexVec;
45
use rustc_index::bit_set::BitSet;
56
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
@@ -8,10 +9,11 @@ use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt, TyAndLayout};
89
use rustc_middle::ty::{self, Instance, Ty, TyCtxt, TypeFoldable, TypeVisitableExt};
910
use rustc_middle::{bug, mir, span_bug};
1011
use rustc_target::abi::call::{FnAbi, PassMode};
12+
use rustc_session::lint;
1113
use tracing::{debug, instrument};
1214

1315
use crate::traits::*;
14-
use crate::{base, errors};
16+
use crate::base;
1517

1618
mod analyze;
1719
mod block;
@@ -238,12 +240,16 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
238240

239241
if layout.size.bytes() >= MIN_DANGEROUS_SIZE {
240242
let (size_quantity, size_unit) = human_readable_bytes(layout.size.bytes());
241-
cx.tcx().dcx().emit_warn(errors::DangerousStackAllocation {
242-
span: decl.source_info.span,
243-
output: format!("{:.2} {}", size_quantity, size_unit),
244-
});
243+
cx.tcx().node_span_lint(
244+
lint::builtin::DANGEROUS_STACK_ALLOCATION,
245+
CRATE_HIR_ID,
246+
decl.source_info.span,
247+
|lint| {
248+
lint.primary_message(format!("allocation of size: {:.2} {} exceeds most system architecture limits", size_quantity, size_unit));
249+
},
250+
);
245251
}
246-
252+
247253
if local == mir::RETURN_PLACE {
248254
match fx.fn_abi.ret.mode {
249255
PassMode::Indirect { .. } => {

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ declare_lint_pass! {
3131
CONFLICTING_REPR_HINTS,
3232
CONST_EVALUATABLE_UNCHECKED,
3333
CONST_ITEM_MUTATION,
34+
DANGEROUS_STACK_ALLOCATION,
3435
DEAD_CODE,
3536
DEPENDENCY_ON_UNIT_NEVER_TYPE_FALLBACK,
3637
DEPRECATED,
@@ -709,6 +710,47 @@ declare_lint! {
709710
"detect assignments that will never be read"
710711
}
711712

713+
declare_lint! {
714+
/// The `dangerous_stack_allocation` lint detects stack allocations that are 1 GB or more.
715+
///
716+
/// ### Example
717+
///
718+
/// ``` fn func() {
719+
/// const CAP: usize = std::u32::MAX as usize;
720+
/// let mut x: [u8; CAP>>1] = [0; CAP>>1];
721+
/// x[2] = 123;
722+
/// println!("{}", x[2]);
723+
/// }
724+
///
725+
/// fn main() {
726+
/// std::thread::Builder::new()
727+
/// .stack_size(3 * 1024 * 1024 * 1024)
728+
/// .spawn(func)
729+
/// .unwrap()
730+
/// .join()
731+
/// .unwrap();
732+
/// }
733+
/// ```
734+
///
735+
/// {{produces}}
736+
/// ```
737+
/// warning: allocation of size: 1 GiB exceeds most system architecture limits
738+
/// --> $DIR/large-stack-size-issue-83060.rs:7:9
739+
/// |
740+
/// LL | let mut x: [u8; CAP>>1] = [0; CAP>>1];
741+
/// | ^^^^^
742+
/// |
743+
/// = note: `#[warn(dangerous_stack_allocation)]` on by default
744+
/// ```
745+
/// ### Explanation
746+
///
747+
/// Large arras may cause stack overflow due to the limited size of the
748+
/// stack on most platforms.
749+
pub DANGEROUS_STACK_ALLOCATION,
750+
Warn,
751+
"Detects dangerous stack allocations at the limit of most architectures"
752+
}
753+
712754
declare_lint! {
713755
/// The `dead_code` lint detects unused, unexported items.
714756
///

tests/ui/codegen/large-stack-size-issue-83060.rs renamed to tests/ui/sanitizer/large-stack-size-issue-83060.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
fn func() {
66
const CAP: usize = std::u32::MAX as usize;
77
let mut x: [u8; CAP>>1] = [0; CAP>>1];
8-
//~^ warning: dangerous stack allocation of size: 1 GiB exceeds most system architecture limits
8+
//~^ warning: allocation of size: 1 GiB exceeds most system architecture limits
9+
//~| NOTE on by default
910
x[2] = 123;
1011
println!("{}", x[2]);
1112
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
warning: dangerous stack allocation of size: 1 GiB exceeds most system architecture limits
1+
warning: allocation of size: 1 GiB exceeds most system architecture limits
22
--> $DIR/large-stack-size-issue-83060.rs:7:9
33
|
44
LL | let mut x: [u8; CAP>>1] = [0; CAP>>1];
55
| ^^^^^
6+
|
7+
= note: `#[warn(dangerous_stack_allocation)]` on by default
68

79
warning: 1 warning emitted
810

0 commit comments

Comments
 (0)