Skip to content

Commit 263bbe3

Browse files
committed
Lint instead of warn
1 parent 90a3f14 commit 263bbe3

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, HasTypingEnv, TyAndLayout};
89
use rustc_middle::ty::{self, Instance, Ty, TyCtxt, TypeFoldable, TypeVisitableExt};
910
use rustc_middle::{bug, mir, span_bug};
1011
use rustc_target::callconv::{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;
@@ -249,12 +251,16 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
249251

250252
if layout.size.bytes() >= MIN_DANGEROUS_SIZE {
251253
let (size_quantity, size_unit) = human_readable_bytes(layout.size.bytes());
252-
cx.tcx().dcx().emit_warn(errors::DangerousStackAllocation {
253-
span: decl.source_info.span,
254-
output: format!("{:.2} {}", size_quantity, size_unit),
255-
});
254+
cx.tcx().node_span_lint(
255+
lint::builtin::DANGEROUS_STACK_ALLOCATION,
256+
CRATE_HIR_ID,
257+
decl.source_info.span,
258+
|lint| {
259+
lint.primary_message(format!("allocation of size: {:.2} {} exceeds most system architecture limits", size_quantity, size_unit));
260+
},
261+
);
256262
}
257-
263+
258264
if local == mir::RETURN_PLACE {
259265
match fx.fn_abi.ret.mode {
260266
PassMode::Indirect { .. } => {

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ declare_lint_pass! {
3232
CONFLICTING_REPR_HINTS,
3333
CONST_EVALUATABLE_UNCHECKED,
3434
CONST_ITEM_MUTATION,
35+
DANGEROUS_STACK_ALLOCATION,
3536
DEAD_CODE,
3637
DEPENDENCY_ON_UNIT_NEVER_TYPE_FALLBACK,
3738
DEPRECATED,
@@ -711,6 +712,47 @@ declare_lint! {
711712
"detect assignments that will never be read"
712713
}
713714

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

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)