Skip to content

Commit 6fd1a9f

Browse files
committed
Don't try to suggest ref mut for implicit ref
1 parent 52d6ae8 commit 6fd1a9f

File tree

3 files changed

+26
-20
lines changed

3 files changed

+26
-20
lines changed

src/librustc_mir/borrow_check/mod.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,9 +1841,9 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
18411841
elem: ProjectionElem::Deref,
18421842
}) if self.mir.local_decls[*local].is_user_variable.is_some() => {
18431843
let local_decl = &self.mir.local_decls[*local];
1844-
let (err_help_span, suggested_code) = match local_decl.is_user_variable {
1844+
let suggestion = match local_decl.is_user_variable {
18451845
Some(ClearCrossCrate::Set(mir::BindingForm::ImplicitSelf)) => {
1846-
suggest_ampmut_self(local_decl)
1846+
Some(suggest_ampmut_self(local_decl))
18471847
},
18481848

18491849
Some(ClearCrossCrate::Set(mir::BindingForm::Var(mir::VarBindingForm {
@@ -1854,9 +1854,9 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
18541854
if let Some(x) = try_suggest_ampmut_rhs(
18551855
self.tcx, self.mir, *local,
18561856
) {
1857-
x
1857+
Some(x)
18581858
} else {
1859-
suggest_ampmut_type(local_decl, opt_ty_info)
1859+
Some(suggest_ampmut_type(local_decl, opt_ty_info))
18601860
}
18611861
},
18621862

@@ -1872,11 +1872,13 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
18721872
None => bug!(),
18731873
};
18741874

1875-
err.span_suggestion(
1876-
err_help_span,
1877-
"consider changing this to be a mutable reference",
1878-
suggested_code,
1879-
);
1875+
if let Some((err_help_span, suggested_code)) = suggestion {
1876+
err.span_suggestion(
1877+
err_help_span,
1878+
"consider changing this to be a mutable reference",
1879+
suggested_code,
1880+
);
1881+
}
18801882

18811883
if let Some(name) = local_decl.name {
18821884
err.span_label(
@@ -1967,13 +1969,17 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
19671969
fn suggest_ref_mut<'cx, 'gcx, 'tcx>(
19681970
tcx: TyCtxt<'cx, 'gcx, 'tcx>,
19691971
local_decl: &mir::LocalDecl<'tcx>,
1970-
) -> (Span, String) {
1972+
) -> Option<(Span, String)> {
19711973
let hi_span = local_decl.source_info.span;
19721974
let hi_src = tcx.sess.codemap().span_to_snippet(hi_span).unwrap();
1973-
assert!(hi_src.starts_with("ref"));
1974-
assert!(hi_src["ref".len()..].starts_with(Pattern_White_Space));
1975-
let suggestion = format!("ref mut{}", &hi_src["ref".len()..]);
1976-
(hi_span, suggestion)
1975+
if hi_src.starts_with("ref")
1976+
&& hi_src["ref".len()..].starts_with(Pattern_White_Space)
1977+
{
1978+
let suggestion = format!("ref mut{}", &hi_src["ref".len()..]);
1979+
Some((hi_span, suggestion))
1980+
} else {
1981+
None
1982+
}
19771983
}
19781984
}
19791985

src/test/ui/rfc-2005-default-binding-mode/enum.nll.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ error[E0594]: cannot assign to `*x` which is behind a `&` reference
22
--> $DIR/enum.rs:19:5
33
|
44
LL | *x += 1; //~ ERROR cannot assign to immutable
5-
| ^^^^^^^ cannot assign
5+
| ^^^^^^^ `x` is a `&` reference, so the data it refers to cannot be written
66

77
error[E0594]: cannot assign to `*x` which is behind a `&` reference
88
--> $DIR/enum.rs:23:9
99
|
1010
LL | *x += 1; //~ ERROR cannot assign to immutable
11-
| ^^^^^^^ cannot assign
11+
| ^^^^^^^ `x` is a `&` reference, so the data it refers to cannot be written
1212

1313
error[E0594]: cannot assign to `*x` which is behind a `&` reference
1414
--> $DIR/enum.rs:29:9
1515
|
1616
LL | *x += 1; //~ ERROR cannot assign to immutable
17-
| ^^^^^^^ cannot assign
17+
| ^^^^^^^ `x` is a `&` reference, so the data it refers to cannot be written
1818

1919
error: aborting due to 3 previous errors
2020

src/test/ui/rfc-2005-default-binding-mode/explicit-mut.nll.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ error[E0594]: cannot assign to `*n` which is behind a `&` reference
22
--> $DIR/explicit-mut.rs:17:13
33
|
44
LL | *n += 1; //~ ERROR cannot assign to immutable
5-
| ^^^^^^^ cannot assign
5+
| ^^^^^^^ `n` is a `&` reference, so the data it refers to cannot be written
66

77
error[E0594]: cannot assign to `*n` which is behind a `&` reference
88
--> $DIR/explicit-mut.rs:25:13
99
|
1010
LL | *n += 1; //~ ERROR cannot assign to immutable
11-
| ^^^^^^^ cannot assign
11+
| ^^^^^^^ `n` is a `&` reference, so the data it refers to cannot be written
1212

1313
error[E0594]: cannot assign to `*n` which is behind a `&` reference
1414
--> $DIR/explicit-mut.rs:33:13
1515
|
1616
LL | *n += 1; //~ ERROR cannot assign to immutable
17-
| ^^^^^^^ cannot assign
17+
| ^^^^^^^ `n` is a `&` reference, so the data it refers to cannot be written
1818

1919
error: aborting due to 3 previous errors
2020

0 commit comments

Comments
 (0)