librustc: Stop desugaring for expressions and translate them directly.

This makes edge cases in which the `Iterator` trait was not in scope
and/or `Option` or its variants were not in scope work properly.

This breaks code that looks like:

    struct MyStruct { ... }

    impl MyStruct {
        fn next(&mut self) -> Option<int> { ... }
    }

    for x in MyStruct { ... } { ... }

Change ad-hoc `next` methods like the above to implementations of the
`Iterator` trait. For example:

    impl Iterator<int> for MyStruct {
        fn next(&mut self) -> Option<int> { ... }
    }

Closes #15392.

[breaking-change]
This commit is contained in:
Patrick Walton 2014-07-21 20:54:28 -07:00
parent 7f2e63ec3f
commit caa564bea3
42 changed files with 614 additions and 163 deletions

View file

@ -297,10 +297,10 @@ fn search(
// for every unused piece
for id in range(0u, 10).filter(|id| board & (1 << (id + 50)) == 0) {
// for each mask that fits on the board
for &m in masks_at.get(id).iter().filter(|&m| board & *m == 0) {
for m in masks_at.get(id).iter().filter(|&m| board & *m == 0) {
// This check is too costy.
//if is_board_unfeasible(board | m, masks) {continue;}
search(masks, board | m, i + 1, Cons(m, &cur), data);
search(masks, board | *m, i + 1, Cons(*m, &cur), data);
}
}
}
@ -311,9 +311,10 @@ fn par_search(masks: Vec<Vec<Vec<u64>>>) -> Data {
// launching the search in parallel on every masks at minimum
// coordinate (0,0)
for &m in masks.get(0).iter().flat_map(|masks_pos| masks_pos.iter()) {
for m in masks.get(0).iter().flat_map(|masks_pos| masks_pos.iter()) {
let masks = masks.clone();
let tx = tx.clone();
let m = *m;
spawn(proc() {
let mut data = Data::new();
search(&*masks, m, 1, Cons(m, &Nil), &mut data);

View file

@ -0,0 +1,31 @@
// Copyright 2014 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.
struct MyStruct {
x: int,
y: int,
}
impl MyStruct {
fn next(&mut self) -> Option<int> {
Some(self.x)
}
}
pub fn main() {
let mut bogus = MyStruct {
x: 1,
y: 2,
};
for x in bogus { //~ ERROR does not implement the `Iterator` trait
drop(x);
}
}

View file

@ -9,13 +9,18 @@
// except according to those terms.
// macro f should not be able to inject a reference to 'n'.
//
// Ignored because `for` loops are not hygienic yet; they will require special
// handling since they introduce a new pattern binding position.
// ignore-test
#![feature(macro_rules)]
macro_rules! f(() => (n))
fn main() -> (){
for n in range(0, 1) {
for n in range(0i, 1) {
println!("{}", f!()); //~ ERROR unresolved name `n`
}
}

View file

@ -9,9 +9,9 @@
// except according to those terms.
fn main() {
let mut xs = vec!(1i, 2, 3, 4);
let mut xs: Vec<int> = vec!();
for x in xs.mut_iter() {
xs.push(1) //~ ERROR cannot borrow `xs`
xs.push(1i) //~ ERROR cannot borrow `xs`
}
}

View file

@ -9,6 +9,7 @@
// except according to those terms.
// ignore-android: FIXME(#10381)
// ignore-test: Not sure what is going on here --pcwalton
// compile-flags:-g

View file

@ -0,0 +1,24 @@
// Copyright 2014 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.
enum BogusOption<T> {
None,
Some(T),
}
type Iterator = int;
pub fn main() {
let x = [ 3i, 3, 3 ];
for i in x.iter() {
assert_eq!(*i, 3);
}
}