Skip to content

Commit a2f2179

Browse files
oli-obkpnkfelix
authored andcommitted
Add an attribute to be able to configure the limit
1 parent e9696c8 commit a2f2179

File tree

11 files changed

+65
-11
lines changed

11 files changed

+65
-11
lines changed

compiler/rustc_feature/src/active.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,9 @@ declare_features! (
633633
/// Allows associated types in inherent impls.
634634
(active, inherent_associated_types, "1.52.0", Some(8995), None),
635635

636+
// Allows setting the threshold for the `large_assignments` lint.
637+
(active, large_assignments, "1.52.0", Some(83518), None),
638+
636639
/// Allows `extern "C-unwind" fn` to enable unwinding across ABI boundaries.
637640
(active, c_unwind, "1.52.0", Some(74990), None),
638641

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
241241
const_eval_limit, CrateLevel, template!(NameValueStr: "N"), const_eval_limit,
242242
experimental!(const_eval_limit)
243243
),
244+
gated!(
245+
move_size_limit, CrateLevel, template!(NameValueStr: "N"), large_assignments,
246+
experimental!(move_size_limit)
247+
),
244248

245249
// Entry point:
246250
ungated!(main, Normal, template!(Word)),

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2906,7 +2906,7 @@ declare_lint! {
29062906
/// This lint will trigger on all sites of large moves and thus allow the
29072907
/// user to resolve them in code.
29082908
pub LARGE_ASSIGNMENTS,
2909-
Allow,
2909+
Warn,
29102910
"detects large moves or copies",
29112911
}
29122912

compiler/rustc_middle/src/middle/limits.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
//! Registering limits, recursion_limit, type_length_limit and const_eval_limit
1+
//! Registering limits:
2+
//! * recursion_limit,
3+
//! * move_size_limit,
4+
//! * type_length_limit, and
5+
//! * const_eval_limit
26
//!
37
//! There are various parts of the compiler that must impose arbitrary limits
48
//! on how deeply they recurse to prevent stack overflow. Users can override
@@ -8,21 +12,22 @@
812
use crate::bug;
913
use rustc_ast as ast;
1014
use rustc_data_structures::sync::OnceCell;
11-
use rustc_session::{Limit, Session};
15+
use rustc_session::Session;
1216
use rustc_span::symbol::{sym, Symbol};
1317

1418
use std::num::IntErrorKind;
1519

1620
pub fn update_limits(sess: &Session, krate: &ast::Crate) {
1721
update_limit(sess, krate, &sess.recursion_limit, sym::recursion_limit, 128);
22+
update_limit(sess, krate, &sess.move_size_limit, sym::move_size_limit, 0);
1823
update_limit(sess, krate, &sess.type_length_limit, sym::type_length_limit, 1048576);
1924
update_limit(sess, krate, &sess.const_eval_limit, sym::const_eval_limit, 1_000_000);
2025
}
2126

2227
fn update_limit(
2328
sess: &Session,
2429
krate: &ast::Crate,
25-
limit: &OnceCell<Limit>,
30+
limit: &OnceCell<impl From<usize> + std::fmt::Debug>,
2631
name: Symbol,
2732
default: usize,
2833
) {
@@ -34,7 +39,7 @@ fn update_limit(
3439
if let Some(s) = attr.value_str() {
3540
match s.as_str().parse() {
3641
Ok(n) => {
37-
limit.set(Limit::new(n)).unwrap();
42+
limit.set(From::from(n)).unwrap();
3843
return;
3944
}
4045
Err(e) => {
@@ -63,5 +68,5 @@ fn update_limit(
6368
}
6469
}
6570
}
66-
limit.set(Limit::new(default)).unwrap();
71+
limit.set(From::from(default)).unwrap();
6772
}

compiler/rustc_mir/src/monomorphize/collector.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -757,11 +757,16 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
757757

758758
fn visit_operand(&mut self, operand: &mir::Operand<'tcx>, location: Location) {
759759
self.super_operand(operand, location);
760+
let limit = self.tcx.sess.move_size_limit();
761+
if limit == 0 {
762+
return;
763+
}
764+
let limit = Size::from_bytes(limit);
760765
let ty = operand.ty(self.body, self.tcx);
761766
let ty = self.monomorphize(ty);
762767
let layout = self.tcx.layout_of(ty::ParamEnv::reveal_all().and(ty));
763768
if let Ok(layout) = layout {
764-
if layout.size > Size::from_bytes(1000) {
769+
if layout.size > limit {
765770
debug!(?layout);
766771
let source_info = self.body.source_info(location);
767772
debug!(?source_info);

compiler/rustc_session/src/session.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ impl Limit {
8383
}
8484
}
8585

86+
impl From<usize> for Limit {
87+
fn from(value: usize) -> Self {
88+
Self::new(value)
89+
}
90+
}
91+
8692
impl fmt::Display for Limit {
8793
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
8894
write!(f, "{}", self.0)
@@ -143,6 +149,10 @@ pub struct Session {
143149
/// operations such as auto-dereference and monomorphization.
144150
pub recursion_limit: OnceCell<Limit>,
145151

152+
/// The size at which the `large_assignments` lint starts
153+
/// being emitted.
154+
pub move_size_limit: OnceCell<usize>,
155+
146156
/// The maximum length of types during monomorphization.
147157
pub type_length_limit: OnceCell<Limit>,
148158

@@ -352,6 +362,11 @@ impl Session {
352362
self.recursion_limit.get().copied().unwrap()
353363
}
354364

365+
#[inline]
366+
pub fn move_size_limit(&self) -> usize {
367+
self.move_size_limit.get().copied().unwrap()
368+
}
369+
355370
#[inline]
356371
pub fn type_length_limit(&self) -> Limit {
357372
self.type_length_limit.get().copied().unwrap()
@@ -1414,6 +1429,7 @@ pub fn build_session(
14141429
features: OnceCell::new(),
14151430
lint_store: OnceCell::new(),
14161431
recursion_limit: OnceCell::new(),
1432+
move_size_limit: OnceCell::new(),
14171433
type_length_limit: OnceCell::new(),
14181434
const_eval_limit: OnceCell::new(),
14191435
incr_comp_session: OneThread::new(RefCell::new(IncrCompSession::NotInitialized)),

compiler/rustc_span/src/symbol.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,7 @@ symbols! {
669669
label_break_value,
670670
lang,
671671
lang_items,
672+
large_assignments,
672673
lateout,
673674
lazy_normalization_consts,
674675
le,
@@ -749,6 +750,7 @@ symbols! {
749750
more_struct_aliases,
750751
movbe_target_feature,
751752
move_ref_pattern,
753+
move_size_limit,
752754
mul,
753755
mul_assign,
754756
mul_with_overflow,

src/test/ui/async-await/large_moves.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#![deny(large_assignments)]
2+
#![feature(large_assignments)]
3+
#![move_size_limit = "1000"]
24
// build-fail
35

46
// edition:2018

src/test/ui/async-await/large_moves.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: moving 10024 bytes
2-
--> $DIR/large_moves.rs:7:13
2+
--> $DIR/large_moves.rs:9:13
33
|
44
LL | let x = async {
55
| _____________^
@@ -17,19 +17,19 @@ LL | #![deny(large_assignments)]
1717
| ^^^^^^^^^^^^^^^^^
1818

1919
error: moving 10024 bytes
20-
--> $DIR/large_moves.rs:13:14
20+
--> $DIR/large_moves.rs:15:14
2121
|
2222
LL | let z = (x, 42);
2323
| ^ value moved from here
2424

2525
error: moving 10024 bytes
26-
--> $DIR/large_moves.rs:13:13
26+
--> $DIR/large_moves.rs:15:13
2727
|
2828
LL | let z = (x, 42);
2929
| ^^^^^^^ value moved from here
3030

3131
error: moving 10024 bytes
32-
--> $DIR/large_moves.rs:15:13
32+
--> $DIR/large_moves.rs:17:13
3333
|
3434
LL | let a = z.0;
3535
| ^^^ value moved from here
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// check that `move_size_limit is feature-gated
2+
3+
#![move_size_limit = "42"] //~ ERROR the `#[move_size_limit]` attribute is an experimental feature
4+
5+
fn main() {}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0658]: the `#[move_size_limit]` attribute is an experimental feature
2+
--> $DIR/feature-gate-large-assignments.rs:3:1
3+
|
4+
LL | #![move_size_limit = "42"]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #83518 <https://github.com/rust-lang/rust/issues/83518> for more information
8+
= help: add `#![feature(large_assignments)]` to the crate attributes to enable
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)