Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 8 pull requests #134988

Merged
merged 17 commits into from
Jan 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <asp11@sfu.ca> <apoelstra@wpsoftware.net>
Anhad Singh <andypythonappdeveloper@gmail.com>
Antoine Plaskowski <antoine.plaskowski@epitech.eu>
Anton Löfgren <anton.lofgren@gmail.com> <alofgren@op5.com>
apiraino <apiraino@users.noreply.github.com> <apiraino@protonmail.com>
Araam Borhanian <avborhanian@gmail.com>
Araam Borhanian <avborhanian@gmail.com> <dobbybabee@gmail.com>
Areski Belaid <areski@gmail.com> areski <areski@gmail.com>
Expand All @@ -62,7 +63,10 @@ Austin Seipp <mad.one@gmail.com> <as@hacks.yi.org>
Ayaz Hafiz <ayaz.hafiz.1@gmail.com>
Aydin Kim <ladinjin@hanmail.net> aydin.kim <aydin.kim@samsung.com>
Ayush Mishra <ayushmishra2005@gmail.com>
Ashley Mannix <kodraus@hey.com> <ashleymannix@live.com.au>
asrar <aszenz@gmail.com>
b-naber <bn263@gmx.de>
b-naber <bn263@gmx.de> <b_naber@gmx.de>
BaoshanPang <pangbw@gmail.com>
Barosl Lee <vcs@barosl.com> Barosl LEE <github@barosl.com>
Bastian Kersting <bastian@cmbt.de>
Expand Down Expand Up @@ -98,6 +102,8 @@ Caleb Cartwright <caleb.cartwright@outlook.com>
Caleb Jones <code@calebjones.net>
Noah Lev <camelidcamel@gmail.com>
Noah Lev <camelidcamel@gmail.com> <37223377+camelid@users.noreply.github.com>
Catherine <catherine3.flores@gmail.com>
Catherine <catherine3.flores@gmail.com> <catherine.3.flores@gmail.com>
cameron1024 <cameron.studdstreet@gmail.com>
Camille Gillot <gillot.camille@gmail.com>
Carl-Anton Ingmarsson <mail@carlanton.se> <ca.ingmarsson@gmail.com>
Expand Down Expand Up @@ -133,11 +139,13 @@ Clement Miao <clementmiao@gmail.com>
Clément Renault <renault.cle@gmail.com>
Cliff Dyer <jcd@sdf.org>
Clinton Ryan <clint.ryan3@gmail.com>
Taylor Cramer <cramertaylorj@gmail.com> <cramertj@google.com>
ember arlynx <ember@lunar.town> <corey@octayn.net>
Crazycolorz5 <Crazycolorz5@gmail.com>
csmoe <35686186+csmoe@users.noreply.github.com>
Cyryl Płotnicki <cyplo@cyplo.net>
Damien Schoof <damien.schoof@gmail.com>
Dan Gohman <dev@sunfishcode.online> <sunfish@mozilla.com>
Dan Robertson <danlrobertson89@gmail.com>
Daniel Campoverde <alx741@riseup.net>
Daniel J Rollins <drollins@financialforce.com>
Expand Down Expand Up @@ -179,10 +187,14 @@ Eduardo Bautista <me@eduardobautista.com> <=>
Eduardo Bautista <me@eduardobautista.com> <mail@eduardobautista.com>
Eduardo Broto <ebroto@tutanota.com>
Edward Shen <code@eddie.sh> <xes@meta.com>
Jacob Finkelman <eh2406@wayne.edu>
Jacob Finkelman <eh2406@wayne.edu> <YeomanYaacov@gmail.com>
Elliott Slaughter <elliottslaughter@gmail.com> <eslaughter@mozilla.com>
Elly Fong-Jones <elly@leptoquark.net>
Eric Holk <eric.holk@gmail.com> <eholk@cs.indiana.edu>
Eric Holk <eric.holk@gmail.com> <eholk@mozilla.com>
Eric Holk <eric.holk@gmail.com> <eric@theincredibleholk.org>
Eric Holk <eric.holk@gmail.com> <ericholk@microsoft.com>
Eric Holmes <eric@ejholmes.net>
Eric Reed <ecreed@cs.washington.edu> <ereed@mozilla.com>
Erick Tryzelaar <erick.tryzelaar@gmail.com> <etryzelaar@iqt.org>
Expand All @@ -206,6 +218,7 @@ Felix S. Klock II <pnkfelix@pnkfx.org> <pnkfelix@mozilla.com>
Félix Saparelli <felix@passcod.name>
Flaper Fesp <flaper87@gmail.com>
Florian Berger <fbergr@gmail.com>
Florian Gilcher <florian.gilcher@asquera.de> <flo@andersground.net>
Florian Wilkens <mrfloya_github@outlook.com> Florian Wilkens <floya@live.de>
François Mockers <mockersf@gmail.com>
Frank Steffahn <fdsteffahn@gmail.com> <frank.steffahn@stu.uni-kiel.de>
Expand Down Expand Up @@ -240,6 +253,8 @@ Herman J. Radtke III <herman@hermanradtke.com> Herman J. Radtke III <hermanradtk
Hirochika Matsumoto <git@hkmatsumoto.com> <matsujika@gmail.com>
Hrvoje Nikšić <hniksic@gmail.com>
Hsiang-Cheng Yang <rick68@users.noreply.github.com>
Huon Wilson <dbau.pp@gmail.com>
Huon Wilson <dbau.pp@gmail.com> <wilson.huon@gmail.com>
Ian Jackson <ijackson@chiark.greenend.org.uk> <ian.jackson@citrix.com>
Ian Jackson <ijackson@chiark.greenend.org.uk> <ijackson+github@slimy.greenend.org.uk>
Ian Jackson <ijackson@chiark.greenend.org.uk> <iwj@xenproject.org>
Expand All @@ -252,9 +267,13 @@ ivan tkachenko <me@ratijas.tk>
J. J. Weber <jjweber@gmail.com>
Jack Huey <jack.huey@umassmed.edu> <jackh726@gmail.com>
Jacob <jacob.macritchie@gmail.com>
Jacob Hoffman-Andrews <rust@hoffman-andrews.com> <github@hoffman-andrews.com>
Jacob Greenfield <xales@naveria.com>
Jacob Pratt <jacob@jhpratt.dev> <the.z.cuber@gmail.com>
Jacob Pratt <jacob@jhpratt.dev> <jacopratt@tesla.com>
Jake Goulding <jake.goulding@integer32.com>
Jake Goulding <jake.goulding@integer32.com> <jake.goulding@gmail.com>
Jake Goulding <jake.goulding@integer32.com> <shepmaster@mac.com>
Jake Vossen <jake@vossen.dev>
Jakob Degen <jakob.e.degen@gmail.com> <jakob@degen.com>
Jakob Lautrup Nysom <jako3047@gmail.com>
Expand Down Expand Up @@ -287,6 +306,7 @@ Jerry Hardee <hardeejj9@gmail.com>
Jesús Rubio <jesusprubio@gmail.com>
Jethro Beekman <github@jbeekman.nl>
Jian Zeng <knight42@mail.ustc.edu.cn>
Jieyou Xu <jieyouxu@outlook.com>
Jieyou Xu <jieyouxu@outlook.com> <39484203+jieyouxu@users.noreply.github.com>
Jihyun Yu <j.yu@navercorp.com> <yjh0502@gmail.com>
Jihyun Yu <j.yu@navercorp.com> jihyun <jihyun@nablecomm.com>
Expand Down Expand Up @@ -322,9 +342,12 @@ Josh Holmer <jholmer.in@gmail.com>
Josh Stone <cuviper@gmail.com> <jistone@redhat.com>
Josh Stone <cuviper@gmail.com> <jistone@fedoraproject.org>
Julia Ryan <juliaryan3.14@gmail.com> <josephryan3.14@gmail.com>
Jubilee Young <workingjubilee@gmail.com> <46493976+workingjubilee@users.noreply.github.com>
Jubilee Young <workingjubilee@gmail.com>
Julian Knodt <julianknodt@gmail.com>
jumbatm <jumbatm@gmail.com> <30644300+jumbatm@users.noreply.github.com>
Junyoung Cho <june0.cho@samsung.com>
Jynn Nelson <github@jyn.dev> <rust@jyn.dev>
Jynn Nelson <github@jyn.dev> <jyn514@gmail.com>
Jynn Nelson <github@jyn.dev> <joshua@yottadb.com>
Jynn Nelson <github@jyn.dev> <jyn.nelson@redjack.com>
Expand Down Expand Up @@ -385,12 +408,14 @@ Marcell Pardavi <marcell.pardavi@gmail.com>
Marcus Klaas de Vries <mail@marcusklaas.nl>
Margaret Meyerhofer <mmeyerho@andrew.cmu.edu> <mmeyerho@andrew>
Mark Mansi <markm@cs.wisc.edu>
Mark Mansi <markm@cs.wisc.edu> <m.mim95@gmail.com>
Mark Rousskov <mark.simulacrum@gmail.com>
Mark Sinclair <mark.edward.x@gmail.com>
Mark Sinclair <mark.edward.x@gmail.com> =Mark Sinclair <=125axel125@gmail.com>
Markus Legner <markus@legner.ch>
Markus Westerlind <marwes91@gmail.com> Markus <marwes91@gmail.com>
Martin Carton <cartonmartin+git@gmail.com>
Martin Carton <cartonmartin+git@gmail.com> <cartonmartin@gmail.com>
Martin Habovštiak <martin.habovstiak@gmail.com>
Martin Hafskjold Thoresen <martinhath@gmail.com>
Martin Nordholts <martin.nordholts@codetale.se> <enselic@gmail.com>
Expand All @@ -415,6 +440,7 @@ Melody Horn <melody@boringcactus.com> <mathphreak@gmail.com>
Mendes <pedro.mendes.26@gmail.com>
mental <m3nta1@yahoo.com>
mibac138 <5672750+mibac138@users.noreply.github.com>
Michael Howell <michael@notriddle.com> <notriddle+rust-mod@protonmail.com>
Michael Williams <m.t.williams@live.com>
Michael Woerister <michaelwoerister@posteo> <michaelwoerister@gmail>
Michael Woerister <michaelwoerister@posteo> <michaelwoerister@gmail.com>
Expand All @@ -431,6 +457,7 @@ Ms2ger <ms2ger@gmail.com> <Ms2ger@gmail.com>
msizanoen1 <qtmlabs@protonmail.com>
Mukilan Thiagarajan <mukilanthiagarajan@gmail.com>
Nadrieril Feneanar <Nadrieril@users.noreply.github.com>
Nadrieril Feneanar <Nadrieril@users.noreply.github.com> <nadrieril+rust@gmail.com>
Nadrieril Feneanar <Nadrieril@users.noreply.github.com> <nadrieril+git@gmail.com>
NAKASHIMA, Makoto <makoto.nksm+github@gmail.com> <makoto.nksm@gmail.com>
NAKASHIMA, Makoto <makoto.nksm+github@gmail.com> <makoto.nksm+github@gmail.com>
Expand All @@ -447,15 +474,23 @@ Nicholas Bishop <nbishop@nbishop.net> <nicholasbishop@gmail.com>
Nicholas Bishop <nbishop@nbishop.net> <nicholasbishop@google.com>
Nicholas Nethercote <n.nethercote@gmail.com> <nnethercote@apple.com>
Nicholas Nethercote <n.nethercote@gmail.com> <nnethercote@mozilla.com>
Nick Cameron <nrc@ncameron.org> <ncameron@mozilla.com>
Nick Fitzgerald <fitzgen@gmail.com> <nfitzgerald@mozilla.com>
Nick Platt <platt.nicholas@gmail.com>
Niclas Schwarzlose <15schnic@gmail.com>
Nicolas Abram <abramlujan@gmail.com>
Nicole Mazzuca <npmazzuca@gmail.com>
Niko Matsakis <rust@nikomatsakis.com>
Niko Matsakis <rust@nikomatsakis.com> <niko@alum.mit.edu>
Noratrieb <48135649+Noratrieb@users.noreply.github.com>
Noratrieb <48135649+Noratrieb@users.noreply.github.com> <48135649+Nilstrieb@users.noreply.github.com>
Noratrieb <48135649+Noratrieb@users.noreply.github.com> <nilstrieb@gmail.com>
Noratrieb <48135649+Noratrieb@users.noreply.github.com> <rust@noratrieb.dev>
Noratrieb <48135649+Noratrieb@users.noreply.github.com> <nora@noratrieb.dev>
Nif Ward <nif.ward@gmail.com>
Nika Layzell <nika@thelayzells.com> <michael@thelayzells.com>
Nikita Popov <nikita.ppv@gmail.com>
Nikita Popov <nikita.ppv@gmail.com> <npopov@redhat.com>
NODA Kai <nodakai@gmail.com>
Oğuz Ağcayazı <oguz.agcayazi@gmail.com> <oguz.agcayazi@gmail.com>
Oğuz Ağcayazı <oguz.agcayazi@gmail.com> <ouz.agz@gmail.com>
Expand Down Expand Up @@ -516,6 +551,7 @@ Ricky Hosfelt <ricky@hosfelt.io>
Ritiek Malhotra <ritiekmalhotra123@gmail.com>
Rob Arnold <robarnold@cs.cmu.edu>
Rob Arnold <robarnold@cs.cmu.edu> Rob Arnold <robarnold@68-26-94-7.pools.spcsdns.net>
Robert Collins <robertc@robertcollins.net> <robertc+rust@robertcollins.net>
Robert Foss <dev@robertfoss.se> robertfoss <dev@robertfoss.se>
Robert Gawdzik <rgawdzik@hotmail.com> Robert Gawdzik ☢ <rgawdzik@hotmail.com>
Robert Habermeier <rphmeier@gmail.com>
Expand Down Expand Up @@ -553,7 +589,15 @@ Simon Sapin <simon@exyr.org> <simon.sapin@exyr.org>
Simonas Kazlauskas <git@kazlauskas.me> Simonas Kazlauskas <github@kazlauskas.me>
Simonas Kazlauskas <git@kazlauskas.me> <simonas+t-compiler@kazlauskas.me>
Siva Prasad <sivaauturic@gmail.com>
Skgland <3877590+Skgland@users.noreply.github.com>
Skgland <3877590+Skgland@users.noreply.github.com> <bb-github@t-online.de>
Skgland <3877590+Skgland@users.noreply.github.com> <bennet.blessmann+github@googlemail.com>
Smittyvb <me@smitop.com>
Sophia June Turner <547158+sophiajt@users.noreply.github.com>
Sophia June Turner <547158+sophiajt@users.noreply.github.com> <547158+jntrnr@users.noreply.github.com>
Sophia June Turner <547158+sophiajt@users.noreply.github.com> <jonathandturner@users.noreply.github.com>
Sophia June Turner <547158+sophiajt@users.noreply.github.com> <jturner@mozilla.com>
Sophia June Turner <547158+sophiajt@users.noreply.github.com> <jonathan.d.turner@gmail.com>
Srinivas Reddy Thatiparthy <thatiparthysreenivas@gmail.com>
Stanislav Tkach <stanislav.tkach@gmail.com>
startling <tdixon51793@gmail.com>
Expand Down Expand Up @@ -586,8 +630,10 @@ Tim Diekmann <t.diekmann.3dv@gmail.com>
Tim Hutt <tdhutt@gmail.com>
Tim JIANG <p90eri@gmail.com>
Tim Joseph Dumol <tim@timdumol.com>
Tim Neumann <mail@timnn.me> <timnn@google.com>
Timothy Maloney <tmaloney@pdx.edu>
Tomas Koutsky <tomas@stepnivlk.net>
Tomasz Miąsko <tomasz.miasko@gmail.com>
Torsten Weber <TorstenWeber12@gmail.com>
Torsten Weber <TorstenWeber12@gmail.com> <torstenweber12@gmail.com>
Trevor Gross <tmgross@umich.edu> <t.gross35@gmail.com>
Expand All @@ -607,7 +653,7 @@ Valerii Lashmanov <vflashm@gmail.com>
Vitali Haravy <HumaneProgrammer@gmail.com> Vitali Haravy <humaneprogrammer@gmail.com>
Vitaly Shukela <vi0oss@gmail.com>
Waffle Lapkin <waffle.lapkin@gmail.com>
Waffle Lapkin <waffle.lapkin@tasking.com>
Waffle Lapkin <waffle.lapkin@gmail.com> <waffle.lapkin@tasking.com>
Wesley Wiser <wwiser@gmail.com> <wesleywiser@microsoft.com>
whitequark <whitequark@whitequark.org>
William Ting <io@williamting.com> <william.h.ting@gmail.com>
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
Loading