Skip to content

Commit 142bb27

Browse files
committed
Auto merge of #53147 - ashtneoi:dont-suggest-ref, r=estebank
For move errors, suggest match ergonomics instead of `ref` Partially fixes issue #52423. Also makes errors and suggestions more consistent between move-from-place and move-from-value errors. Limitations: - Only the first pattern in a match arm can have a "consider removing this borrow operator" suggestion. - Suggestions don't always compile as-is (see the TODOs in the test for details). Sorry for the really long test. I wanted to make sure I handled every case I could think of, and it turned out there were a lot of them. Questions: - Is there any particular applicability I should set on those suggestions? - Are the notes about the `Copy` trait excessive?
2 parents 996e26c + 0023dd9 commit 142bb27

File tree

49 files changed

+3104
-279
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+3104
-279
lines changed

src/librustc/mir/mod.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,8 @@ pub enum BorrowKind {
429429

430430
/// Data must be immutable but not aliasable. This kind of borrow
431431
/// cannot currently be expressed by the user and is used only in
432-
/// implicit closure bindings. It is needed when you the closure
433-
/// is borrowing or mutating a mutable referent, e.g.:
432+
/// implicit closure bindings. It is needed when the closure is
433+
/// borrowing or mutating a mutable referent, e.g.:
434434
///
435435
/// let x: &mut isize = ...;
436436
/// let y = || *x += 5;
@@ -443,7 +443,7 @@ pub enum BorrowKind {
443443
/// let y = (&mut Env { &x }, fn_ptr); // Closure is pair of env and fn
444444
/// fn fn_ptr(env: &mut Env) { **env.x += 5; }
445445
///
446-
/// This is then illegal because you cannot mutate a `&mut` found
446+
/// This is then illegal because you cannot mutate an `&mut` found
447447
/// in an aliasable location. To solve, you'd have to translate with
448448
/// an `&mut` borrow:
449449
///
@@ -523,6 +523,8 @@ pub struct VarBindingForm<'tcx> {
523523
/// (b) it gives a way to separate this case from the remaining cases
524524
/// for diagnostics.
525525
pub opt_match_place: Option<(Option<Place<'tcx>>, Span)>,
526+
/// Span of the pattern in which this variable was bound.
527+
pub pat_span: Span,
526528
}
527529

528530
#[derive(Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
@@ -540,7 +542,8 @@ CloneTypeFoldableAndLiftImpls! { BindingForm<'tcx>, }
540542
impl_stable_hash_for!(struct self::VarBindingForm<'tcx> {
541543
binding_mode,
542544
opt_ty_info,
543-
opt_match_place
545+
opt_match_place,
546+
pat_span
544547
});
545548

546549
mod binding_form_impl {
@@ -673,7 +676,7 @@ pub struct LocalDecl<'tcx> {
673676
/// ROOT SCOPE
674677
/// │{ argument x: &str }
675678
/// │
676-
/// │ │{ #[allow(unused_mut] } // this is actually split into 2 scopes
679+
/// │ │{ #[allow(unused_mut)] } // this is actually split into 2 scopes
677680
/// │ │ // in practice because I'm lazy.
678681
/// │ │
679682
/// │ │← x.source_info.scope
@@ -710,6 +713,7 @@ impl<'tcx> LocalDecl<'tcx> {
710713
binding_mode: ty::BindingMode::BindByValue(_),
711714
opt_ty_info: _,
712715
opt_match_place: _,
716+
pat_span: _,
713717
}))) => true,
714718

715719
// FIXME: might be able to thread the distinction between
@@ -729,6 +733,7 @@ impl<'tcx> LocalDecl<'tcx> {
729733
binding_mode: ty::BindingMode::BindByValue(_),
730734
opt_ty_info: _,
731735
opt_match_place: _,
736+
pat_span: _,
732737
}))) => true,
733738

734739
Some(ClearCrossCrate::Set(BindingForm::ImplicitSelf)) => true,
@@ -906,7 +911,7 @@ pub enum TerminatorKind<'tcx> {
906911

907912
/// Drop the Place and assign the new value over it. This ensures
908913
/// that the assignment to `P` occurs *even if* the destructor for
909-
/// place unwinds. Its semantics are best explained by by the
914+
/// place unwinds. Its semantics are best explained by the
910915
/// elaboration:
911916
///
912917
/// ```

src/librustc_borrowck/borrowck/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1235,7 +1235,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
12351235
ty::BindByReference(..) => {
12361236
let let_span = self.tcx.hir.span(node_id);
12371237
let suggestion = suggest_ref_mut(self.tcx, let_span);
1238-
if let Some((let_span, replace_str)) = suggestion {
1238+
if let Some(replace_str) = suggestion {
12391239
db.span_suggestion(
12401240
let_span,
12411241
"use a mutable reference instead",

0 commit comments

Comments
 (0)