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

Suggest dereferencing on assignment to mutable borrow #61054

Merged
merged 2 commits into from
May 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
34 changes: 29 additions & 5 deletions src/librustc_typeck/check/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,11 +306,12 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
/// In addition of this check, it also checks between references mutability state. If the
/// expected is mutable but the provided isn't, maybe we could just say "Hey, try with
/// `&mut`!".
pub fn check_ref(&self,
expr: &hir::Expr,
checked_ty: Ty<'tcx>,
expected: Ty<'tcx>)
-> Option<(Span, &'static str, String)> {
pub fn check_ref(
&self,
expr: &hir::Expr,
checked_ty: Ty<'tcx>,
expected: Ty<'tcx>,
) -> Option<(Span, &'static str, String)> {
let cm = self.sess().source_map();
let sp = expr.span;
if !cm.span_to_filename(sp).is_real() {
Expand Down Expand Up @@ -397,6 +398,29 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
} else {
String::new()
};
if let Some(hir::Node::Expr(hir::Expr {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this addition the function is ~200+ LOC -- can we avoid making it bigger? =P

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm focusing on slimming/splitting Parser now. I'll move to other parts of the compiler once in done with that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, just don't forget about it... =)

node: hir::ExprKind::Assign(left_expr, _),
..
})) = self.tcx.hir().find_by_hir_id(
self.tcx.hir().get_parent_node_by_hir_id(expr.hir_id),
) {
if mutability == hir::Mutability::MutMutable {
// Found the following case:
// fn foo(opt: &mut Option<String>){ opt = None }
// --- ^^^^
// | |
// consider dereferencing here: `*opt` |
// expected mutable reference, found enum `Option`
if let Ok(src) = cm.span_to_snippet(left_expr.span) {
return Some((
left_expr.span,
"consider dereferencing here to assign to the mutable \
borrowed piece of memory",
format!("*{}", src),
));
}
}
}
return Some(match mutability {
hir::Mutability::MutMutable => (
sp,
Expand Down
17 changes: 17 additions & 0 deletions src/test/ui/suggestions/mut-ref-reassignment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
fn suggestion(opt: &mut Option<String>) {
opt = None; //~ ERROR mismatched types
}

fn no_suggestion(opt: &mut Result<String, ()>) {
opt = None //~ ERROR mismatched types
}

fn suggestion2(opt: &mut Option<String>) {
opt = Some(String::new())//~ ERROR mismatched types
}

fn no_suggestion2(opt: &mut Option<String>) {
opt = Some(42)//~ ERROR mismatched types
}

fn main() {}
47 changes: 47 additions & 0 deletions src/test/ui/suggestions/mut-ref-reassignment.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
error[E0308]: mismatched types
--> $DIR/mut-ref-reassignment.rs:2:11
|
LL | opt = None;
| ^^^^ expected mutable reference, found enum `std::option::Option`
|
= note: expected type `&mut std::option::Option<std::string::String>`
found type `std::option::Option<_>`
help: consider dereferencing here to assign to the mutable borrowed piece of memory
|
LL | *opt = None;
| ^^^^

error[E0308]: mismatched types
--> $DIR/mut-ref-reassignment.rs:6:11
|
LL | opt = None
| ^^^^ expected mutable reference, found enum `std::option::Option`
|
= note: expected type `&mut std::result::Result<std::string::String, ()>`
found type `std::option::Option<_>`

error[E0308]: mismatched types
--> $DIR/mut-ref-reassignment.rs:10:11
|
LL | opt = Some(String::new())
| ^^^^^^^^^^^^^^^^^^^ expected mutable reference, found enum `std::option::Option`
|
= note: expected type `&mut std::option::Option<std::string::String>`
found type `std::option::Option<std::string::String>`
help: consider dereferencing here to assign to the mutable borrowed piece of memory
|
LL | *opt = Some(String::new())
| ^^^^

error[E0308]: mismatched types
--> $DIR/mut-ref-reassignment.rs:14:11
|
LL | opt = Some(42)
| ^^^^^^^^ expected mutable reference, found enum `std::option::Option`
|
= note: expected type `&mut std::option::Option<std::string::String>`
found type `std::option::Option<{integer}>`

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0308`.