From df6254f7a204b383bd8346880247a68dc32cab32 Mon Sep 17 00:00:00 2001 From: Jonathan Gruner Date: Tue, 8 Apr 2025 20:49:50 +0200 Subject: [PATCH 1/2] report call site of inlined scopes for large assignment lints --- .../src/mono_checks/move_check.rs | 20 +++++++++++++--- tests/ui/lint/large_assignments/inline_mir.rs | 24 +++++++++++++++++++ .../lint/large_assignments/inline_mir.stderr | 15 ++++++++++++ 3 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 tests/ui/lint/large_assignments/inline_mir.rs create mode 100644 tests/ui/lint/large_assignments/inline_mir.stderr diff --git a/compiler/rustc_monomorphize/src/mono_checks/move_check.rs b/compiler/rustc_monomorphize/src/mono_checks/move_check.rs index 838bfdab1ea59..946714e3e48a1 100644 --- a/compiler/rustc_monomorphize/src/mono_checks/move_check.rs +++ b/compiler/rustc_monomorphize/src/mono_checks/move_check.rs @@ -162,13 +162,27 @@ impl<'tcx> MoveCheckVisitor<'tcx> { // but correct span? This would make the lint at least accept crate-level lint attributes. return; }; + + // If the source scope is inlined by the MIR inliner, report the lint on the call site. + let reported_span = self + .body + .source_scopes + .get(source_info.scope) + .and_then(|source_scope_data| source_scope_data.inlined) + .map(|(_, call_site)| call_site) + .unwrap_or(span); + self.tcx.emit_node_span_lint( LARGE_ASSIGNMENTS, lint_root, - span, - LargeAssignmentsLint { span, size: too_large_size.bytes(), limit: limit as u64 }, + reported_span, + LargeAssignmentsLint { + span: reported_span, + size: too_large_size.bytes(), + limit: limit as u64, + }, ); - self.move_size_spans.push(span); + self.move_size_spans.push(reported_span); } } diff --git a/tests/ui/lint/large_assignments/inline_mir.rs b/tests/ui/lint/large_assignments/inline_mir.rs new file mode 100644 index 0000000000000..fc27b8ff244d1 --- /dev/null +++ b/tests/ui/lint/large_assignments/inline_mir.rs @@ -0,0 +1,24 @@ +#![feature(large_assignments)] +#![deny(large_assignments)] +#![move_size_limit = "1000"] + +//! Tests that with `-Zinline-mir`, we do NOT get an error that points to the +//! implementation of `UnsafeCell` since that is not actionable by the user: +//! +//! ```text +//! error: moving 9999 bytes +//! --> /rustc/FAKE_PREFIX/library/core/src/cell.rs:2054:9 +//! | +//! = note: value moved from here +//! ``` +//! +//! We want the diagnostics to point to the relevant user code. + +//@ build-fail +//@ compile-flags: -Zmir-opt-level=1 -Zinline-mir + +pub fn main() { + let data = [10u8; 9999]; + let cell = std::cell::UnsafeCell::new(data); //~ ERROR large_assignments + std::hint::black_box(cell); +} diff --git a/tests/ui/lint/large_assignments/inline_mir.stderr b/tests/ui/lint/large_assignments/inline_mir.stderr new file mode 100644 index 0000000000000..d9010e24d03be --- /dev/null +++ b/tests/ui/lint/large_assignments/inline_mir.stderr @@ -0,0 +1,15 @@ +error: moving 9999 bytes + --> $DIR/inline_mir.rs:22:16 + | +LL | let cell = std::cell::UnsafeCell::new(data); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ value moved from here + | + = note: The current maximum size is 1000, but it can be customized with the move_size_limit attribute: `#![move_size_limit = "..."]` +note: the lint level is defined here + --> $DIR/inline_mir.rs:2:9 + | +LL | #![deny(large_assignments)] + | ^^^^^^^^^^^^^^^^^ + +error: aborting due to 1 previous error + From 6d71fc15d8983960745bf2f61ab74ca272f0585f Mon Sep 17 00:00:00 2001 From: Jonathan Gruner Date: Tue, 8 Apr 2025 21:22:20 +0200 Subject: [PATCH 2/2] for large assignment lint, use the correct span for checking for duplicate lints --- .../src/mono_checks/move_check.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_monomorphize/src/mono_checks/move_check.rs b/compiler/rustc_monomorphize/src/mono_checks/move_check.rs index 946714e3e48a1..a484573f0d860 100644 --- a/compiler/rustc_monomorphize/src/mono_checks/move_check.rs +++ b/compiler/rustc_monomorphize/src/mono_checks/move_check.rs @@ -148,11 +148,7 @@ impl<'tcx> MoveCheckVisitor<'tcx> { span: Span, ) { let source_info = self.body.source_info(location); - for reported_span in &self.move_size_spans { - if reported_span.overlaps(span) { - return; - } - } + let lint_root = source_info.scope.lint_root(&self.body.source_scopes); let Some(lint_root) = lint_root else { // This happens when the issue is in a function from a foreign crate that @@ -172,6 +168,12 @@ impl<'tcx> MoveCheckVisitor<'tcx> { .map(|(_, call_site)| call_site) .unwrap_or(span); + for previously_reported_span in &self.move_size_spans { + if previously_reported_span.overlaps(reported_span) { + return; + } + } + self.tcx.emit_node_span_lint( LARGE_ASSIGNMENTS, lint_root, @@ -182,6 +184,7 @@ impl<'tcx> MoveCheckVisitor<'tcx> { limit: limit as u64, }, ); + self.move_size_spans.push(reported_span); } }