Skip to content

Commit e962e52

Browse files
authored
Rollup merge of rust-lang#139551 - jogru0:121672, r=oli-obk
report call site of inlined scopes for large assignment lints Addressed issue: rust-lang#121672 Tracking issue: rust-lang#83518 r? `@oli-obk` I tried to follow your comment about what to do [here](rust-lang#121672 (comment)). However, I'm totally unfamiliar with the code so far (this is my first contribution touching compiler code), so I apologize in advance if I did something stupid 😅 In particular, I'm not sure I use the _correct_ source scope to look for inline data, as there is a whole `IndexVec` of them. My changes definitely did something, as can be seen by the added ui test. However, the result is not as anticipated in the issue: ``` LL | let cell = std::cell::UnsafeCell::new(data); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ value moved from here ``` instead of ``` LL | let cell = std::cell::UnsafeCell::new(data); | ^^^^ value moved from here ``` raising my suspicion that maybe I got the wrong source scope.
2 parents a8b0d56 + 6d71fc1 commit e962e52

File tree

3 files changed

+64
-8
lines changed

3 files changed

+64
-8
lines changed

Diff for: compiler/rustc_monomorphize/src/mono_checks/move_check.rs

+25-8
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,7 @@ impl<'tcx> MoveCheckVisitor<'tcx> {
148148
span: Span,
149149
) {
150150
let source_info = self.body.source_info(location);
151-
for reported_span in &self.move_size_spans {
152-
if reported_span.overlaps(span) {
153-
return;
154-
}
155-
}
151+
156152
let lint_root = source_info.scope.lint_root(&self.body.source_scopes);
157153
let Some(lint_root) = lint_root else {
158154
// This happens when the issue is in a function from a foreign crate that
@@ -162,13 +158,34 @@ impl<'tcx> MoveCheckVisitor<'tcx> {
162158
// but correct span? This would make the lint at least accept crate-level lint attributes.
163159
return;
164160
};
161+
162+
// If the source scope is inlined by the MIR inliner, report the lint on the call site.
163+
let reported_span = self
164+
.body
165+
.source_scopes
166+
.get(source_info.scope)
167+
.and_then(|source_scope_data| source_scope_data.inlined)
168+
.map(|(_, call_site)| call_site)
169+
.unwrap_or(span);
170+
171+
for previously_reported_span in &self.move_size_spans {
172+
if previously_reported_span.overlaps(reported_span) {
173+
return;
174+
}
175+
}
176+
165177
self.tcx.emit_node_span_lint(
166178
LARGE_ASSIGNMENTS,
167179
lint_root,
168-
span,
169-
LargeAssignmentsLint { span, size: too_large_size.bytes(), limit: limit as u64 },
180+
reported_span,
181+
LargeAssignmentsLint {
182+
span: reported_span,
183+
size: too_large_size.bytes(),
184+
limit: limit as u64,
185+
},
170186
);
171-
self.move_size_spans.push(span);
187+
188+
self.move_size_spans.push(reported_span);
172189
}
173190
}
174191

Diff for: tests/ui/lint/large_assignments/inline_mir.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#![feature(large_assignments)]
2+
#![deny(large_assignments)]
3+
#![move_size_limit = "1000"]
4+
5+
//! Tests that with `-Zinline-mir`, we do NOT get an error that points to the
6+
//! implementation of `UnsafeCell` since that is not actionable by the user:
7+
//!
8+
//! ```text
9+
//! error: moving 9999 bytes
10+
//! --> /rustc/FAKE_PREFIX/library/core/src/cell.rs:2054:9
11+
//! |
12+
//! = note: value moved from here
13+
//! ```
14+
//!
15+
//! We want the diagnostics to point to the relevant user code.
16+
17+
//@ build-fail
18+
//@ compile-flags: -Zmir-opt-level=1 -Zinline-mir
19+
20+
pub fn main() {
21+
let data = [10u8; 9999];
22+
let cell = std::cell::UnsafeCell::new(data); //~ ERROR large_assignments
23+
std::hint::black_box(cell);
24+
}

Diff for: tests/ui/lint/large_assignments/inline_mir.stderr

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: moving 9999 bytes
2+
--> $DIR/inline_mir.rs:22:16
3+
|
4+
LL | let cell = std::cell::UnsafeCell::new(data);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ value moved from here
6+
|
7+
= note: The current maximum size is 1000, but it can be customized with the move_size_limit attribute: `#![move_size_limit = "..."]`
8+
note: the lint level is defined here
9+
--> $DIR/inline_mir.rs:2:9
10+
|
11+
LL | #![deny(large_assignments)]
12+
| ^^^^^^^^^^^^^^^^^
13+
14+
error: aborting due to 1 previous error
15+

0 commit comments

Comments
 (0)