Skip to content

Commit 2085bce

Browse files
committed
Auto merge of rust-lang#134988 - tgross35:rollup-s59bx7c, r=tgross35
Rollup of 8 pull requests Successful merges: - rust-lang#132474 (Add more mailmap entries) - rust-lang#133486 (borrowck diagnostics: make `add_move_error_suggestions` use the HIR rather than `SourceMap`) - rust-lang#134861 (Add GUI test for item info elements color) - rust-lang#134968 (Print how to rebless Python formatting in tidy) - rust-lang#134971 (chore: fix typos) - rust-lang#134972 (add .mailmap entry for myself) - rust-lang#134974 (Revert rust-lang#119515 single line where clause style guide) - rust-lang#134975 (Revert style guide rhs break) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 4e59b1d + 9472d32 commit 2085bce

22 files changed

+331
-165
lines changed

.mailmap

+47-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Andrew Poelstra <[email protected]> <[email protected]>
4848
Anhad Singh <[email protected]>
4949
Antoine Plaskowski <[email protected]>
5050
51+
5152
Araam Borhanian <[email protected]>
5253
5354
Areski Belaid <[email protected]> areski <[email protected]>
@@ -62,7 +63,10 @@ Austin Seipp <[email protected]> <[email protected]>
6263
Ayaz Hafiz <[email protected]>
6364
Aydin Kim <[email protected]> aydin.kim <[email protected]>
6465
Ayush Mishra <[email protected]>
66+
6567
68+
69+
6670
BaoshanPang <[email protected]>
6771
Barosl Lee <[email protected]> Barosl LEE <[email protected]>
6872
Bastian Kersting <[email protected]>
@@ -98,6 +102,8 @@ Caleb Cartwright <[email protected]>
98102
Caleb Jones <[email protected]>
99103
100104
105+
Catherine <[email protected]>
106+
101107
cameron1024 <[email protected]>
102108
Camille Gillot <[email protected]>
103109
Carl-Anton Ingmarsson <[email protected]> <[email protected]>
@@ -133,11 +139,13 @@ Clement Miao <[email protected]>
133139
Clément Renault <[email protected]>
134140
Cliff Dyer <[email protected]>
135141
Clinton Ryan <[email protected]>
142+
136143
137144
Crazycolorz5 <[email protected]>
138145
139146
Cyryl Płotnicki <[email protected]>
140147
Damien Schoof <[email protected]>
148+
141149
Dan Robertson <[email protected]>
142150
Daniel Campoverde <[email protected]>
143151
Daniel J Rollins <[email protected]>
@@ -179,10 +187,14 @@ Eduardo Bautista <[email protected]> <=>
179187
180188
Eduardo Broto <[email protected]>
181189
190+
Jacob Finkelman <[email protected]>
191+
182192
183193
Elly Fong-Jones <[email protected]>
184194
185195
196+
197+
186198
Eric Holmes <[email protected]>
187199
188200
@@ -206,6 +218,7 @@ Felix S. Klock II <[email protected]> <[email protected]>
206218
Félix Saparelli <[email protected]>
207219
Flaper Fesp <[email protected]>
208220
Florian Berger <[email protected]>
221+
209222
Florian Wilkens <[email protected]> Florian Wilkens <[email protected]>
210223
François Mockers <[email protected]>
211224
@@ -240,6 +253,8 @@ Herman J. Radtke III <[email protected]> Herman J. Radtke III <hermanradtk
240253
Hirochika Matsumoto <[email protected]> <[email protected]>
241254
Hrvoje Nikšić <[email protected]>
242255
Hsiang-Cheng Yang <[email protected]>
256+
Huon Wilson <[email protected]>
257+
243258
244259
245260
@@ -252,9 +267,13 @@ ivan tkachenko <[email protected]>
252267
J. J. Weber <[email protected]>
253268
254269
270+
Jacob Hoffman-Andrews <[email protected]> <[email protected]>
255271
Jacob Greenfield <[email protected]>
256272
257273
274+
Jake Goulding <[email protected]>
275+
276+
258277
Jake Vossen <[email protected]>
259278
260279
Jakob Lautrup Nysom <[email protected]>
@@ -287,6 +306,7 @@ Jerry Hardee <[email protected]>
287306
Jesús Rubio <[email protected]>
288307
Jethro Beekman <[email protected]>
289308
Jian Zeng <[email protected]>
309+
Jieyou Xu <[email protected]>
290310
291311
292312
@@ -322,9 +342,12 @@ Josh Holmer <[email protected]>
322342
323343
324344
345+
346+
Jubilee Young <[email protected]>
325347
Julian Knodt <[email protected]>
326348
327349
Junyoung Cho <[email protected]>
350+
328351
329352
330353
@@ -385,12 +408,14 @@ Marcell Pardavi <[email protected]>
385408
Marcus Klaas de Vries <[email protected]>
386409
Margaret Meyerhofer <[email protected]> <mmeyerho@andrew>
387410
Mark Mansi <[email protected]>
411+
388412
Mark Rousskov <[email protected]>
389413
Mark Sinclair <[email protected]>
390414
Mark Sinclair <[email protected]> =Mark Sinclair <[email protected]>
391415
Markus Legner <[email protected]>
392416
Markus Westerlind <[email protected]> Markus <[email protected]>
393417
Martin Carton <[email protected]>
418+
394419
Martin Habovštiak <[email protected]>
395420
Martin Hafskjold Thoresen <[email protected]>
396421
@@ -415,6 +440,7 @@ Melody Horn <[email protected]> <[email protected]>
415440
416441
417442
443+
418444
Michael Williams <[email protected]>
419445
Michael Woerister <michaelwoerister@posteo> <michaelwoerister@gmail>
420446
Michael Woerister <michaelwoerister@posteo> <[email protected]>
@@ -431,6 +457,7 @@ Ms2ger <[email protected]> <[email protected]>
431457
msizanoen1 <[email protected]>
432458
Mukilan Thiagarajan <[email protected]>
433459
Nadrieril Feneanar <[email protected]>
460+
Nadrieril Feneanar <[email protected]> <[email protected]>
434461
Nadrieril Feneanar <[email protected]> <[email protected]>
435462
436463
@@ -447,15 +474,23 @@ Nicholas Bishop <[email protected]> <[email protected]>
447474
448475
Nicholas Nethercote <[email protected]> <[email protected]>
449476
Nicholas Nethercote <[email protected]> <[email protected]>
477+
478+
450479
Nick Platt <[email protected]>
451480
Niclas Schwarzlose <[email protected]>
452481
Nicolas Abram <[email protected]>
453482
Nicole Mazzuca <[email protected]>
483+
Niko Matsakis <[email protected]>
484+
485+
Noratrieb <[email protected]>
454486
455487
488+
456489
457490
458491
492+
Nikita Popov <[email protected]>
493+
459494
460495
461496
@@ -516,6 +551,7 @@ Ricky Hosfelt <[email protected]>
516551
Ritiek Malhotra <[email protected]>
517552
Rob Arnold <[email protected]>
518553
Rob Arnold <[email protected]> Rob Arnold <[email protected]>
554+
519555
Robert Foss <[email protected]> robertfoss <[email protected]>
520556
Robert Gawdzik <[email protected]> Robert Gawdzik ☢ <[email protected]>
521557
Robert Habermeier <[email protected]>
@@ -553,7 +589,15 @@ Simon Sapin <[email protected]> <[email protected]>
553589
Simonas Kazlauskas <[email protected]> Simonas Kazlauskas <[email protected]>
554590
Simonas Kazlauskas <[email protected]> <[email protected]>
555591
Siva Prasad <[email protected]>
592+
593+
594+
556595
596+
Sophia June Turner <[email protected]>
597+
Sophia June Turner <[email protected]> <[email protected]>
598+
Sophia June Turner <[email protected]> <[email protected]>
599+
Sophia June Turner <[email protected]> <[email protected]>
600+
Sophia June Turner <[email protected]> <[email protected]>
557601
Srinivas Reddy Thatiparthy <[email protected]>
558602
Stanislav Tkach <[email protected]>
559603
startling <[email protected]>
@@ -586,8 +630,10 @@ Tim Diekmann <[email protected]>
586630
587631
Tim JIANG <[email protected]>
588632
Tim Joseph Dumol <[email protected]>
633+
589634
Timothy Maloney <[email protected]>
590635
Tomas Koutsky <[email protected]>
636+
Tomasz Miąsko <[email protected]>
591637
Torsten Weber <[email protected]>
592638
593639
@@ -607,7 +653,7 @@ Valerii Lashmanov <[email protected]>
607653
Vitali Haravy <[email protected]> Vitali Haravy <[email protected]>
608654
Vitaly Shukela <[email protected]>
609655
Waffle Lapkin <[email protected]>
610-
Waffle Lapkin <[email protected]>
656+
Waffle Lapkin <waffle.lapkin@gmail.com> <waffle.lapkin@tasking.com>
611657
612658
whitequark <[email protected]>
613659

compiler/rustc_borrowck/src/diagnostics/move_errors.rs

+118-39
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#![allow(rustc::diagnostic_outside_of_impl)]
22
#![allow(rustc::untranslatable_diagnostic)]
33

4+
use rustc_data_structures::fx::FxHashSet;
45
use rustc_errors::{Applicability, Diag};
56
use rustc_hir::intravisit::Visitor;
6-
use rustc_hir::{CaptureBy, ExprKind, HirId, Node};
7+
use rustc_hir::{self as hir, CaptureBy, ExprKind, HirId, Node};
78
use rustc_middle::bug;
89
use rustc_middle::mir::*;
910
use rustc_middle::ty::{self, Ty};
@@ -683,48 +684,126 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
683684
}
684685

685686
fn add_move_error_suggestions(&self, err: &mut Diag<'_>, binds_to: &[Local]) {
686-
let mut suggestions: Vec<(Span, String, String)> = Vec::new();
687+
/// A HIR visitor to associate each binding with a `&` or `&mut` that could be removed to
688+
/// make it bind by reference instead (if possible)
689+
struct BindingFinder<'tcx> {
690+
typeck_results: &'tcx ty::TypeckResults<'tcx>,
691+
hir: rustc_middle::hir::map::Map<'tcx>,
692+
/// Input: the span of the pattern we're finding bindings in
693+
pat_span: Span,
694+
/// Input: the spans of the bindings we're providing suggestions for
695+
binding_spans: Vec<Span>,
696+
/// Internal state: have we reached the pattern we're finding bindings in?
697+
found_pat: bool,
698+
/// Internal state: the innermost `&` or `&mut` "above" the visitor
699+
ref_pat: Option<&'tcx hir::Pat<'tcx>>,
700+
/// Internal state: could removing a `&` give bindings unexpected types?
701+
has_adjustments: bool,
702+
/// Output: for each input binding, the `&` or `&mut` to remove to make it by-ref
703+
ref_pat_for_binding: Vec<(Span, Option<&'tcx hir::Pat<'tcx>>)>,
704+
/// Output: ref patterns that can't be removed straightforwardly
705+
cannot_remove: FxHashSet<HirId>,
706+
}
707+
impl<'tcx> Visitor<'tcx> for BindingFinder<'tcx> {
708+
type NestedFilter = rustc_middle::hir::nested_filter::OnlyBodies;
709+
710+
fn nested_visit_map(&mut self) -> Self::Map {
711+
self.hir
712+
}
713+
714+
fn visit_expr(&mut self, ex: &'tcx hir::Expr<'tcx>) -> Self::Result {
715+
// Don't walk into const patterns or anything else that might confuse this
716+
if !self.found_pat {
717+
hir::intravisit::walk_expr(self, ex)
718+
}
719+
}
720+
721+
fn visit_pat(&mut self, p: &'tcx hir::Pat<'tcx>) {
722+
if p.span == self.pat_span {
723+
self.found_pat = true;
724+
}
725+
726+
let parent_has_adjustments = self.has_adjustments;
727+
self.has_adjustments |=
728+
self.typeck_results.pat_adjustments().contains_key(p.hir_id);
729+
730+
// Track the innermost `&` or `&mut` enclosing bindings, to suggest removing it.
731+
let parent_ref_pat = self.ref_pat;
732+
if let hir::PatKind::Ref(..) = p.kind {
733+
self.ref_pat = Some(p);
734+
// To avoid edition-dependent logic to figure out how many refs this `&` can
735+
// peel off, simply don't remove the "parent" `&`.
736+
self.cannot_remove.extend(parent_ref_pat.map(|r| r.hir_id));
737+
if self.has_adjustments {
738+
// Removing this `&` could give child bindings unexpected types, so don't.
739+
self.cannot_remove.insert(p.hir_id);
740+
// As long the `&` stays, child patterns' types should be as expected.
741+
self.has_adjustments = false;
742+
}
743+
}
744+
745+
if let hir::PatKind::Binding(_, _, ident, _) = p.kind {
746+
// the spans in `binding_spans` encompass both the ident and binding mode
747+
if let Some(&bind_sp) =
748+
self.binding_spans.iter().find(|bind_sp| bind_sp.contains(ident.span))
749+
{
750+
self.ref_pat_for_binding.push((bind_sp, self.ref_pat));
751+
} else {
752+
// we've encountered a binding that we're not reporting a move error for.
753+
// we don't want to change its type, so don't remove the surrounding `&`.
754+
if let Some(ref_pat) = self.ref_pat {
755+
self.cannot_remove.insert(ref_pat.hir_id);
756+
}
757+
}
758+
}
759+
760+
hir::intravisit::walk_pat(self, p);
761+
self.ref_pat = parent_ref_pat;
762+
self.has_adjustments = parent_has_adjustments;
763+
}
764+
}
765+
let mut pat_span = None;
766+
let mut binding_spans = Vec::new();
687767
for local in binds_to {
688768
let bind_to = &self.body.local_decls[*local];
689-
if let LocalInfo::User(BindingForm::Var(VarBindingForm { pat_span, .. })) =
769+
if let LocalInfo::User(BindingForm::Var(VarBindingForm { pat_span: pat_sp, .. })) =
690770
*bind_to.local_info()
691771
{
692-
let Ok(pat_snippet) = self.infcx.tcx.sess.source_map().span_to_snippet(pat_span)
693-
else {
694-
continue;
695-
};
696-
let Some(stripped) = pat_snippet.strip_prefix('&') else {
697-
suggestions.push((
698-
bind_to.source_info.span.shrink_to_lo(),
699-
"consider borrowing the pattern binding".to_string(),
700-
"ref ".to_string(),
701-
));
702-
continue;
703-
};
704-
let inner_pat_snippet = stripped.trim_start();
705-
let (pat_span, suggestion, to_remove) = if inner_pat_snippet.starts_with("mut")
706-
&& inner_pat_snippet["mut".len()..].starts_with(rustc_lexer::is_whitespace)
707-
{
708-
let inner_pat_snippet = inner_pat_snippet["mut".len()..].trim_start();
709-
let pat_span = pat_span.with_hi(
710-
pat_span.lo()
711-
+ BytePos((pat_snippet.len() - inner_pat_snippet.len()) as u32),
712-
);
713-
(pat_span, String::new(), "mutable borrow")
714-
} else {
715-
let pat_span = pat_span.with_hi(
716-
pat_span.lo()
717-
+ BytePos(
718-
(pat_snippet.len() - inner_pat_snippet.trim_start().len()) as u32,
719-
),
720-
);
721-
(pat_span, String::new(), "borrow")
722-
};
723-
suggestions.push((
724-
pat_span,
725-
format!("consider removing the {to_remove}"),
726-
suggestion,
727-
));
772+
pat_span = Some(pat_sp);
773+
binding_spans.push(bind_to.source_info.span);
774+
}
775+
}
776+
let Some(pat_span) = pat_span else { return };
777+
778+
let hir = self.infcx.tcx.hir();
779+
let Some(body) = hir.maybe_body_owned_by(self.mir_def_id()) else { return };
780+
let typeck_results = self.infcx.tcx.typeck(self.mir_def_id());
781+
let mut finder = BindingFinder {
782+
typeck_results,
783+
hir,
784+
pat_span,
785+
binding_spans,
786+
found_pat: false,
787+
ref_pat: None,
788+
has_adjustments: false,
789+
ref_pat_for_binding: Vec::new(),
790+
cannot_remove: FxHashSet::default(),
791+
};
792+
finder.visit_body(body);
793+
794+
let mut suggestions = Vec::new();
795+
for (binding_span, opt_ref_pat) in finder.ref_pat_for_binding {
796+
if let Some(ref_pat) = opt_ref_pat
797+
&& !finder.cannot_remove.contains(&ref_pat.hir_id)
798+
&& let hir::PatKind::Ref(subpat, mutbl) = ref_pat.kind
799+
&& let Some(ref_span) = ref_pat.span.trim_end(subpat.span)
800+
{
801+
let mutable_str = if mutbl.is_mut() { "mutable " } else { "" };
802+
let msg = format!("consider removing the {mutable_str}borrow");
803+
suggestions.push((ref_span, msg, "".to_string()));
804+
} else {
805+
let msg = "consider borrowing the pattern binding".to_string();
806+
suggestions.push((binding_span.shrink_to_lo(), msg, "ref ".to_string()));
728807
}
729808
}
730809
suggestions.sort_unstable_by_key(|&(span, _, _)| span);

0 commit comments

Comments
 (0)