Skip to content

Commit

Permalink
Auto merge of #134988 - tgross35:rollup-s59bx7c, r=tgross35
Browse files Browse the repository at this point in the history
Rollup of 8 pull requests

Successful merges:

 - #132474 (Add more mailmap entries)
 - #133486 (borrowck diagnostics: make `add_move_error_suggestions` use the HIR rather than `SourceMap`)
 - #134861 (Add GUI test for item info elements color)
 - #134968 (Print how to rebless Python formatting in tidy)
 - #134971 (chore: fix typos)
 - #134972 (add .mailmap entry for myself)
 - #134974 (Revert #119515 single line where clause style guide)
 - #134975 (Revert style guide rhs break)

r? `@ghost`
`@rustbot` modify labels: rollup
  • Loading branch information
bors committed Jan 1, 2025
2 parents 4e59b1d + 9472d32 commit 2085bce
Show file tree
Hide file tree
Showing 22 changed files with 331 additions and 165 deletions.
48 changes: 47 additions & 1 deletion .mailmap
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Andrew Poelstra <[email protected]> <[email protected]>
Anhad Singh <[email protected]>
Antoine Plaskowski <[email protected]>
Anton Löfgren <[email protected]> <[email protected]>
apiraino <[email protected]> <[email protected]>
Araam Borhanian <[email protected]>
Araam Borhanian <[email protected]> <[email protected]>
Areski Belaid <[email protected]> areski <[email protected]>
Expand All @@ -62,7 +63,10 @@ Austin Seipp <[email protected]> <[email protected]>
Ayaz Hafiz <[email protected]>
Aydin Kim <[email protected]> aydin.kim <[email protected]>
Ayush Mishra <[email protected]>
Ashley Mannix <[email protected]> <[email protected]>
asrar <[email protected]>
b-naber <[email protected]>
b-naber <[email protected]> <[email protected]>
BaoshanPang <[email protected]>
Barosl Lee <[email protected]> Barosl LEE <[email protected]>
Bastian Kersting <[email protected]>
Expand Down Expand Up @@ -98,6 +102,8 @@ Caleb Cartwright <[email protected]>
Caleb Jones <[email protected]>
Noah Lev <[email protected]>
Noah Lev <[email protected]> <[email protected]>
Catherine <[email protected]>
Catherine <[email protected]> <[email protected]>
cameron1024 <[email protected]>
Camille Gillot <[email protected]>
Carl-Anton Ingmarsson <[email protected]> <[email protected]>
Expand Down Expand Up @@ -133,11 +139,13 @@ Clement Miao <[email protected]>
Clément Renault <[email protected]>
Cliff Dyer <[email protected]>
Clinton Ryan <[email protected]>
Taylor Cramer <[email protected]> <[email protected]>
ember arlynx <[email protected]> <[email protected]>
Crazycolorz5 <[email protected]>
csmoe <[email protected]>
Cyryl Płotnicki <[email protected]>
Damien Schoof <[email protected]>
Dan Gohman <[email protected]> <[email protected]>
Dan Robertson <[email protected]>
Daniel Campoverde <[email protected]>
Daniel J Rollins <[email protected]>
Expand Down Expand Up @@ -179,10 +187,14 @@ Eduardo Bautista <[email protected]> <=>
Eduardo Bautista <[email protected]> <[email protected]>
Eduardo Broto <[email protected]>
Edward Shen <[email protected]> <[email protected]>
Jacob Finkelman <[email protected]>
Jacob Finkelman <[email protected]> <[email protected]>
Elliott Slaughter <[email protected]> <[email protected]>
Elly Fong-Jones <[email protected]>
Eric Holk <[email protected]> <[email protected]>
Eric Holk <[email protected]> <[email protected]>
Eric Holk <[email protected]> <[email protected]>
Eric Holk <[email protected]> <[email protected]>
Eric Holmes <[email protected]>
Eric Reed <[email protected]> <[email protected]>
Erick Tryzelaar <[email protected]> <[email protected]>
Expand All @@ -206,6 +218,7 @@ Felix S. Klock II <[email protected]> <[email protected]>
Félix Saparelli <[email protected]>
Flaper Fesp <[email protected]>
Florian Berger <[email protected]>
Florian Gilcher <[email protected]> <[email protected]>
Florian Wilkens <[email protected]> Florian Wilkens <[email protected]>
François Mockers <[email protected]>
Frank Steffahn <[email protected]> <[email protected]>
Expand Down Expand Up @@ -240,6 +253,8 @@ Herman J. Radtke III <[email protected]> Herman J. Radtke III <hermanradtk
Hirochika Matsumoto <[email protected]> <[email protected]>
Hrvoje Nikšić <[email protected]>
Hsiang-Cheng Yang <[email protected]>
Huon Wilson <[email protected]>
Huon Wilson <[email protected]> <[email protected]>
Ian Jackson <[email protected]> <[email protected]>
Ian Jackson <[email protected]> <[email protected]>
Ian Jackson <[email protected]> <[email protected]>
Expand All @@ -252,9 +267,13 @@ ivan tkachenko <[email protected]>
J. J. Weber <[email protected]>
Jack Huey <[email protected]> <[email protected]>
Jacob <[email protected]>
Jacob Hoffman-Andrews <[email protected]> <[email protected]>
Jacob Greenfield <[email protected]>
Jacob Pratt <[email protected]> <[email protected]>
Jacob Pratt <[email protected]> <[email protected]>
Jake Goulding <[email protected]>
Jake Goulding <[email protected]> <[email protected]>
Jake Goulding <[email protected]> <[email protected]>
Jake Vossen <[email protected]>
Jakob Degen <[email protected]> <[email protected]>
Jakob Lautrup Nysom <[email protected]>
Expand Down Expand Up @@ -287,6 +306,7 @@ Jerry Hardee <[email protected]>
Jesús Rubio <[email protected]>
Jethro Beekman <[email protected]>
Jian Zeng <[email protected]>
Jieyou Xu <[email protected]>
Jieyou Xu <[email protected]> <[email protected]>
Jihyun Yu <[email protected]> <[email protected]>
Jihyun Yu <[email protected]> jihyun <[email protected]>
Expand Down Expand Up @@ -322,9 +342,12 @@ Josh Holmer <[email protected]>
Josh Stone <[email protected]> <[email protected]>
Josh Stone <[email protected]> <[email protected]>
Julia Ryan <[email protected]> <[email protected]>
Jubilee Young <[email protected]> <[email protected]>
Jubilee Young <[email protected]>
Julian Knodt <[email protected]>
jumbatm <[email protected]> <[email protected]>
Junyoung Cho <[email protected]>
Jynn Nelson <[email protected]> <[email protected]>
Jynn Nelson <[email protected]> <[email protected]>
Jynn Nelson <[email protected]> <[email protected]>
Jynn Nelson <[email protected]> <[email protected]>
Expand Down Expand Up @@ -385,12 +408,14 @@ Marcell Pardavi <[email protected]>
Marcus Klaas de Vries <[email protected]>
Margaret Meyerhofer <[email protected]> <mmeyerho@andrew>
Mark Mansi <[email protected]>
Mark Mansi <[email protected]> <[email protected]>
Mark Rousskov <[email protected]>
Mark Sinclair <[email protected]>
Mark Sinclair <[email protected]> =Mark Sinclair <[email protected]>
Markus Legner <[email protected]>
Markus Westerlind <[email protected]> Markus <[email protected]>
Martin Carton <[email protected]>
Martin Carton <[email protected]> <[email protected]>
Martin Habovštiak <[email protected]>
Martin Hafskjold Thoresen <[email protected]>
Martin Nordholts <[email protected]> <[email protected]>
Expand All @@ -415,6 +440,7 @@ Melody Horn <[email protected]> <[email protected]>
Mendes <[email protected]>
mental <[email protected]>
mibac138 <[email protected]>
Michael Howell <[email protected]> <[email protected]>
Michael Williams <[email protected]>
Michael Woerister <michaelwoerister@posteo> <michaelwoerister@gmail>
Michael Woerister <michaelwoerister@posteo> <[email protected]>
Expand All @@ -431,6 +457,7 @@ Ms2ger <[email protected]> <[email protected]>
msizanoen1 <[email protected]>
Mukilan Thiagarajan <[email protected]>
Nadrieril Feneanar <[email protected]>
Nadrieril Feneanar <[email protected]> <[email protected]>
Nadrieril Feneanar <[email protected]> <[email protected]>
NAKASHIMA, Makoto <[email protected]> <[email protected]>
NAKASHIMA, Makoto <[email protected]> <[email protected]>
Expand All @@ -447,15 +474,23 @@ Nicholas Bishop <[email protected]> <[email protected]>
Nicholas Bishop <[email protected]> <[email protected]>
Nicholas Nethercote <[email protected]> <[email protected]>
Nicholas Nethercote <[email protected]> <[email protected]>
Nick Cameron <[email protected]> <[email protected]>
Nick Fitzgerald <[email protected]> <[email protected]>
Nick Platt <[email protected]>
Niclas Schwarzlose <[email protected]>
Nicolas Abram <[email protected]>
Nicole Mazzuca <[email protected]>
Niko Matsakis <[email protected]>
Niko Matsakis <[email protected]> <[email protected]>
Noratrieb <[email protected]>
Noratrieb <[email protected]> <[email protected]>
Noratrieb <[email protected]> <[email protected]>
Noratrieb <[email protected]> <[email protected]>
Noratrieb <[email protected]> <[email protected]>
Nif Ward <[email protected]>
Nika Layzell <[email protected]> <[email protected]>
Nikita Popov <[email protected]>
Nikita Popov <[email protected]> <[email protected]>
NODA Kai <[email protected]>
Oğuz Ağcayazı <[email protected]> <[email protected]>
Oğuz Ağcayazı <[email protected]> <[email protected]>
Expand Down Expand Up @@ -516,6 +551,7 @@ Ricky Hosfelt <[email protected]>
Ritiek Malhotra <[email protected]>
Rob Arnold <[email protected]>
Rob Arnold <[email protected]> Rob Arnold <[email protected]>
Robert Collins <[email protected]> <[email protected]>
Robert Foss <[email protected]> robertfoss <[email protected]>
Robert Gawdzik <[email protected]> Robert Gawdzik ☢ <[email protected]>
Robert Habermeier <[email protected]>
Expand Down Expand Up @@ -553,7 +589,15 @@ Simon Sapin <[email protected]> <[email protected]>
Simonas Kazlauskas <[email protected]> Simonas Kazlauskas <[email protected]>
Simonas Kazlauskas <[email protected]> <[email protected]>
Siva Prasad <[email protected]>
Skgland <[email protected]>
Skgland <[email protected]> <[email protected]>
Skgland <[email protected]> <[email protected]>
Smittyvb <[email protected]>
Sophia June Turner <[email protected]>
Sophia June Turner <[email protected]> <[email protected]>
Sophia June Turner <[email protected]> <[email protected]>
Sophia June Turner <[email protected]> <[email protected]>
Sophia June Turner <[email protected]> <[email protected]>
Srinivas Reddy Thatiparthy <[email protected]>
Stanislav Tkach <[email protected]>
startling <[email protected]>
Expand Down Expand Up @@ -586,8 +630,10 @@ Tim Diekmann <[email protected]>
Tim Hutt <[email protected]>
Tim JIANG <[email protected]>
Tim Joseph Dumol <[email protected]>
Tim Neumann <[email protected]> <[email protected]>
Timothy Maloney <[email protected]>
Tomas Koutsky <[email protected]>
Tomasz Miąsko <[email protected]>
Torsten Weber <[email protected]>
Torsten Weber <[email protected]> <[email protected]>
Trevor Gross <[email protected]> <[email protected]>
Expand All @@ -607,7 +653,7 @@ Valerii Lashmanov <[email protected]>
Vitali Haravy <[email protected]> Vitali Haravy <[email protected]>
Vitaly Shukela <[email protected]>
Waffle Lapkin <[email protected]>
Waffle Lapkin <[email protected]>
Waffle Lapkin <waffle.lapkin@gmail.com> <waffle.lapkin@tasking.com>
Wesley Wiser <[email protected]> <[email protected]>
whitequark <[email protected]>
William Ting <[email protected]> <[email protected]>
Expand Down
157 changes: 118 additions & 39 deletions compiler/rustc_borrowck/src/diagnostics/move_errors.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#![allow(rustc::diagnostic_outside_of_impl)]
#![allow(rustc::untranslatable_diagnostic)]

use rustc_data_structures::fx::FxHashSet;
use rustc_errors::{Applicability, Diag};
use rustc_hir::intravisit::Visitor;
use rustc_hir::{CaptureBy, ExprKind, HirId, Node};
use rustc_hir::{self as hir, CaptureBy, ExprKind, HirId, Node};
use rustc_middle::bug;
use rustc_middle::mir::*;
use rustc_middle::ty::{self, Ty};
Expand Down Expand Up @@ -683,48 +684,126 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
}

fn add_move_error_suggestions(&self, err: &mut Diag<'_>, binds_to: &[Local]) {
let mut suggestions: Vec<(Span, String, String)> = Vec::new();
/// A HIR visitor to associate each binding with a `&` or `&mut` that could be removed to
/// make it bind by reference instead (if possible)
struct BindingFinder<'tcx> {
typeck_results: &'tcx ty::TypeckResults<'tcx>,
hir: rustc_middle::hir::map::Map<'tcx>,
/// Input: the span of the pattern we're finding bindings in
pat_span: Span,
/// Input: the spans of the bindings we're providing suggestions for
binding_spans: Vec<Span>,
/// Internal state: have we reached the pattern we're finding bindings in?
found_pat: bool,
/// Internal state: the innermost `&` or `&mut` "above" the visitor
ref_pat: Option<&'tcx hir::Pat<'tcx>>,
/// Internal state: could removing a `&` give bindings unexpected types?
has_adjustments: bool,
/// Output: for each input binding, the `&` or `&mut` to remove to make it by-ref
ref_pat_for_binding: Vec<(Span, Option<&'tcx hir::Pat<'tcx>>)>,
/// Output: ref patterns that can't be removed straightforwardly
cannot_remove: FxHashSet<HirId>,
}
impl<'tcx> Visitor<'tcx> for BindingFinder<'tcx> {
type NestedFilter = rustc_middle::hir::nested_filter::OnlyBodies;

fn nested_visit_map(&mut self) -> Self::Map {
self.hir
}

fn visit_expr(&mut self, ex: &'tcx hir::Expr<'tcx>) -> Self::Result {
// Don't walk into const patterns or anything else that might confuse this
if !self.found_pat {
hir::intravisit::walk_expr(self, ex)
}
}

fn visit_pat(&mut self, p: &'tcx hir::Pat<'tcx>) {
if p.span == self.pat_span {
self.found_pat = true;
}

let parent_has_adjustments = self.has_adjustments;
self.has_adjustments |=
self.typeck_results.pat_adjustments().contains_key(p.hir_id);

// Track the innermost `&` or `&mut` enclosing bindings, to suggest removing it.
let parent_ref_pat = self.ref_pat;
if let hir::PatKind::Ref(..) = p.kind {
self.ref_pat = Some(p);
// To avoid edition-dependent logic to figure out how many refs this `&` can
// peel off, simply don't remove the "parent" `&`.
self.cannot_remove.extend(parent_ref_pat.map(|r| r.hir_id));
if self.has_adjustments {
// Removing this `&` could give child bindings unexpected types, so don't.
self.cannot_remove.insert(p.hir_id);
// As long the `&` stays, child patterns' types should be as expected.
self.has_adjustments = false;
}
}

if let hir::PatKind::Binding(_, _, ident, _) = p.kind {
// the spans in `binding_spans` encompass both the ident and binding mode
if let Some(&bind_sp) =
self.binding_spans.iter().find(|bind_sp| bind_sp.contains(ident.span))
{
self.ref_pat_for_binding.push((bind_sp, self.ref_pat));
} else {
// we've encountered a binding that we're not reporting a move error for.
// we don't want to change its type, so don't remove the surrounding `&`.
if let Some(ref_pat) = self.ref_pat {
self.cannot_remove.insert(ref_pat.hir_id);
}
}
}

hir::intravisit::walk_pat(self, p);
self.ref_pat = parent_ref_pat;
self.has_adjustments = parent_has_adjustments;
}
}
let mut pat_span = None;
let mut binding_spans = Vec::new();
for local in binds_to {
let bind_to = &self.body.local_decls[*local];
if let LocalInfo::User(BindingForm::Var(VarBindingForm { pat_span, .. })) =
if let LocalInfo::User(BindingForm::Var(VarBindingForm { pat_span: pat_sp, .. })) =
*bind_to.local_info()
{
let Ok(pat_snippet) = self.infcx.tcx.sess.source_map().span_to_snippet(pat_span)
else {
continue;
};
let Some(stripped) = pat_snippet.strip_prefix('&') else {
suggestions.push((
bind_to.source_info.span.shrink_to_lo(),
"consider borrowing the pattern binding".to_string(),
"ref ".to_string(),
));
continue;
};
let inner_pat_snippet = stripped.trim_start();
let (pat_span, suggestion, to_remove) = if inner_pat_snippet.starts_with("mut")
&& inner_pat_snippet["mut".len()..].starts_with(rustc_lexer::is_whitespace)
{
let inner_pat_snippet = inner_pat_snippet["mut".len()..].trim_start();
let pat_span = pat_span.with_hi(
pat_span.lo()
+ BytePos((pat_snippet.len() - inner_pat_snippet.len()) as u32),
);
(pat_span, String::new(), "mutable borrow")
} else {
let pat_span = pat_span.with_hi(
pat_span.lo()
+ BytePos(
(pat_snippet.len() - inner_pat_snippet.trim_start().len()) as u32,
),
);
(pat_span, String::new(), "borrow")
};
suggestions.push((
pat_span,
format!("consider removing the {to_remove}"),
suggestion,
));
pat_span = Some(pat_sp);
binding_spans.push(bind_to.source_info.span);
}
}
let Some(pat_span) = pat_span else { return };

let hir = self.infcx.tcx.hir();
let Some(body) = hir.maybe_body_owned_by(self.mir_def_id()) else { return };
let typeck_results = self.infcx.tcx.typeck(self.mir_def_id());
let mut finder = BindingFinder {
typeck_results,
hir,
pat_span,
binding_spans,
found_pat: false,
ref_pat: None,
has_adjustments: false,
ref_pat_for_binding: Vec::new(),
cannot_remove: FxHashSet::default(),
};
finder.visit_body(body);

let mut suggestions = Vec::new();
for (binding_span, opt_ref_pat) in finder.ref_pat_for_binding {
if let Some(ref_pat) = opt_ref_pat
&& !finder.cannot_remove.contains(&ref_pat.hir_id)
&& let hir::PatKind::Ref(subpat, mutbl) = ref_pat.kind
&& let Some(ref_span) = ref_pat.span.trim_end(subpat.span)
{
let mutable_str = if mutbl.is_mut() { "mutable " } else { "" };
let msg = format!("consider removing the {mutable_str}borrow");
suggestions.push((ref_span, msg, "".to_string()));
} else {
let msg = "consider borrowing the pattern binding".to_string();
suggestions.push((binding_span.shrink_to_lo(), msg, "ref ".to_string()));
}
}
suggestions.sort_unstable_by_key(|&(span, _, _)| span);
Expand Down
Loading

0 comments on commit 2085bce

Please sign in to comment.