-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Made ref pattern bindings correctly pick Deref or DerefMut #26058
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// Test that we choose Deref or DerefMut appropriately based on mutability of ref bindings (#15609). | ||
|
||
use std::ops::{Deref, DerefMut}; | ||
|
||
struct DerefOk<T>(T); | ||
struct DerefMutOk<T>(T); | ||
|
||
impl<T> Deref for DerefOk<T> { | ||
type Target = T; | ||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl<T> DerefMut for DerefOk<T> { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
panic!() | ||
} | ||
} | ||
|
||
impl<T> Deref for DerefMutOk<T> { | ||
type Target = T; | ||
fn deref(&self) -> &Self::Target { | ||
panic!() | ||
} | ||
} | ||
|
||
impl<T> DerefMut for DerefMutOk<T> { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
&mut self.0 | ||
} | ||
} | ||
|
||
fn main() { | ||
// Check that mutable ref binding in match picks DerefMut | ||
let mut b = DerefMutOk(0); | ||
match *b { | ||
ref mut n => n, | ||
}; | ||
|
||
// Check that mutable ref binding in let picks DerefMut | ||
let mut y = DerefMutOk(1); | ||
let ref mut z = *y; | ||
|
||
// Check that immutable ref binding in match picks Deref | ||
let mut b = DerefOk(2); | ||
match *b { | ||
ref n => n, | ||
}; | ||
|
||
// Check that immutable ref binding in let picks Deref | ||
let mut y = DerefOk(3); | ||
let ref z = *y; | ||
|
||
// Check that mixed mutable/immutable ref binding in match picks DerefMut | ||
let mut b = DerefMutOk((0, 9)); | ||
match *b { | ||
(ref mut n, ref m) => (n, m), | ||
}; | ||
|
||
let mut b = DerefMutOk((0, 9)); | ||
match *b { | ||
(ref n, ref mut m) => (n, m), | ||
}; | ||
|
||
// Check that mixed mutable/immutable ref binding in let picks DerefMut | ||
let mut y = DerefMutOk((1, 8)); | ||
let (ref mut z, ref a) = *y; | ||
|
||
let mut y = DerefMutOk((1, 8)); | ||
let (ref z, ref mut a) = *y; | ||
|
||
// Check that multiple immutable ref bindings in match picks Deref | ||
let mut b = DerefOk((2, 7)); | ||
match *b { | ||
(ref n, ref m) => (n, m), | ||
}; | ||
|
||
// Check that multiple immutable ref bindings in let picks Deref | ||
let mut y = DerefOk((3, 6)); | ||
let (ref z, ref a) = *y; | ||
|
||
// Check that multiple mutable ref bindings in match picks DerefMut | ||
let mut b = DerefMutOk((4, 5)); | ||
match *b { | ||
(ref mut n, ref mut m) => (n, m), | ||
}; | ||
|
||
// Check that multiple mutable ref bindings in let picks DerefMut | ||
let mut y = DerefMutOk((5, 4)); | ||
let (ref mut z, ref mut a) = *y; | ||
} |
44 changes: 44 additions & 0 deletions
44
src/test/run-pass/overloaded_deref_with_ref_pattern_issue15609.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// Test that we choose Deref or DerefMut appropriately based on mutability of ref bindings (#15609). | ||
|
||
fn main() { | ||
use std::cell::RefCell; | ||
|
||
struct S { | ||
node: E, | ||
} | ||
|
||
enum E { | ||
Foo(u32), | ||
Bar, | ||
} | ||
|
||
// Check match | ||
let x = RefCell::new(S { node: E::Foo(0) }); | ||
|
||
let mut b = x.borrow_mut(); | ||
match b.node { | ||
E::Foo(ref mut n) => *n += 1, | ||
_ => (), | ||
} | ||
|
||
// Check let | ||
let x = RefCell::new(0); | ||
let mut y = x.borrow_mut(); | ||
let ref mut z = *y; | ||
|
||
fn foo(a: &mut RefCell<Option<String>>) { | ||
if let Some(ref mut s) = *a.borrow_mut() { | ||
s.push('a') | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Naming issue: the name of the function still implies it's a predicate function but now it returns an Option. Same below.