Add an auto-slice-and-ref step to method lookup. Allows ~[T] to work with explicit self

This commit is contained in:
Brian Anderson 2012-12-06 16:29:17 -08:00
parent e71081ec03
commit 2fcf562d16
6 changed files with 111 additions and 7 deletions

View file

@ -0,0 +1,18 @@
fn main() {
// Testing that method lookup does not automatically borrow
// vectors to slices then automatically create a &mut self
// reference. That would allow creating a mutable pointer to a
// temporary, which would be a source of confusion
let mut a = @[0];
a.test_mut(); //~ ERROR type `@[int]` does not implement any method in scope named `test_mut`
}
trait MyIter {
pure fn test_mut(&mut self);
}
impl &[int]: MyIter {
pure fn test_mut(&mut self) { }
}

View file

@ -0,0 +1,48 @@
// Testing that method lookup automatically both borrows vectors to slices
// and also references them to create the &self pointer
trait MyIter {
pure fn test_imm(&self);
pure fn test_const(&const self);
}
impl &[int]: MyIter {
pure fn test_imm(&self) { assert self[0] == 1 }
pure fn test_const(&const self) { assert self[0] == 1 }
}
impl &str: MyIter {
pure fn test_imm(&self) { assert *self == "test" }
pure fn test_const(&const self) { assert *self == "test" }
}
fn main() {
// NB: Associativity of ~, etc. in this context is surprising. These must be parenthesized
([1]).test_imm();
(~[1]).test_imm();
(@[1]).test_imm();
(&[1]).test_imm();
("test").test_imm();
(~"test").test_imm();
(@"test").test_imm();
(&"test").test_imm();
// XXX: Other types of mutable vecs don't currently exist
(@mut [1]).test_imm();
([1]).test_const();
(~[1]).test_const();
(@[1]).test_const();
(&[1]).test_const();
("test").test_const();
(~"test").test_const();
(@"test").test_const();
(&"test").test_const();
(@mut [1]).test_const();
// NB: We don't do this double autoreffing for &mut self because that would
// allow creating a mutable pointer to a temporary, which would be a source
// of confusion
}