Skip to content

Desugar fn(&mut self) to fn(mut self: &mut self) #50014

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

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 2 additions & 1 deletion src/librustc_borrowck/borrowck/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ impl<'a, 'tcx> UnusedMutCx<'a, 'tcx> {
None => span_bug!(span, "missing binding mode"),
};
match bm {
ty::BindByValue(hir::MutMutable) => {}
// allow `fn(mut self: &mut self)` so we can desugar `fn(&mut self)` to that
ty::BindByValue(hir::MutMutable) if name.as_str() != "self" => {}
_ => return,
}

Expand Down
3 changes: 3 additions & 0 deletions src/librustc_passes/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ impl<'a> AstValidator<'a> {
match arg.pat.node {
PatKind::Ident(BindingMode::ByValue(Mutability::Immutable), _, None) |
PatKind::Wild => {}
// allow `fn(mut self: &mut self)` so we can desugar `fn(&mut self)` to that
PatKind::Ident(BindingMode::ByValue(Mutability::Mutable), ref ident, None)
if ident.name.as_str() == "self" => {}
PatKind::Ident(BindingMode::ByValue(Mutability::Mutable), _, None) =>
report_err(arg.pat.span, true),
_ => report_err(arg.pat.span, false),
Expand Down
8 changes: 5 additions & 3 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1597,7 +1597,7 @@ pub enum TyKind {
Infer,
/// Inferred type of a `self` or `&self` argument in a method.
ImplicitSelf,
// A macro in the type position.
/// A macro in the type position.
Mac(Mac),
/// Placeholder for a kind that has failed to be defined.
Err,
Expand Down Expand Up @@ -1677,7 +1677,9 @@ impl Arg {
if ident.name == keywords::SelfValue.name() {
return match self.ty.node {
TyKind::ImplicitSelf => Some(respan(self.pat.span, SelfKind::Value(mutbl))),
TyKind::Rptr(lt, MutTy{ref ty, mutbl}) if ty.node == TyKind::ImplicitSelf => {
TyKind::Rptr(lt, MutTy { ref ty, mutbl })
if ty.node == TyKind::ImplicitSelf =>
{
Some(respan(self.pat.span, SelfKind::Region(lt, mutbl)))
}
_ => Some(respan(self.pat.span.to(self.ty.span),
Expand Down Expand Up @@ -1715,7 +1717,7 @@ impl Arg {
match eself.node {
SelfKind::Explicit(ty, mutbl) => arg(mutbl, ty),
SelfKind::Value(mutbl) => arg(mutbl, infer_ty),
SelfKind::Region(lt, mutbl) => arg(Mutability::Immutable, P(Ty {
SelfKind::Region(lt, mutbl) => arg(mutbl, P(Ty {
id: DUMMY_NODE_ID,
node: TyKind::Rptr(lt, MutTy { ty: infer_ty, mutbl: mutbl }),
span,
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/did_you_mean/issue-31424.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct Struct;

impl Struct {
fn foo(&mut self) {
(&mut self).bar(); //~ ERROR cannot borrow
(&mut self).bar();
}

// In this case we could keep the suggestion, but to distinguish the
Expand Down
11 changes: 1 addition & 10 deletions src/test/ui/did_you_mean/issue-31424.stderr
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
error[E0596]: cannot borrow immutable argument `self` as mutable
--> $DIR/issue-31424.rs:17:15
|
LL | (&mut self).bar(); //~ ERROR cannot borrow
| ^^^^
| |
| cannot reborrow mutably
| try removing `&mut` here

error[E0596]: cannot borrow immutable argument `self` as mutable
--> $DIR/issue-31424.rs:23:15
|
Expand All @@ -15,6 +6,6 @@ LL | fn bar(self: &mut Self) {
LL | (&mut self).bar(); //~ ERROR cannot borrow
| ^^^^ cannot borrow mutably

error: aborting due to 2 previous errors
error: aborting due to previous error

For more information about this error, try `rustc --explain E0596`.
2 changes: 1 addition & 1 deletion src/test/ui/did_you_mean/issue-34126.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
struct Z { }

impl Z {
fn run(&self, z: &mut Z) { }
fn run(&self, _z: &mut Z) { }
fn start(&mut self) {
self.run(&mut self); //~ ERROR cannot borrow
}
Expand Down
12 changes: 6 additions & 6 deletions src/test/ui/did_you_mean/issue-34126.stderr
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
error[E0596]: cannot borrow immutable argument `self` as mutable
error[E0502]: cannot borrow `self` as mutable because `*self` is also borrowed as immutable
--> $DIR/issue-34126.rs:16:23
|
LL | self.run(&mut self); //~ ERROR cannot borrow
| ^^^^
| |
| cannot reborrow mutably
| try removing `&mut` here
| ---- ^^^^- immutable borrow ends here
| | |
| | mutable borrow occurs here
| immutable borrow occurs here

error: aborting due to previous error

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