I've seen something similar recently, with this code:

----------------------------------------------------------------------
fn main() {
   // Fails.
   assert_eq!(stuff(), &[]);

   // Works
   let tmp: &'static [uint] = &[];
   assert_eq!(stuff(), tmp);
}

static simple: &'static [uint] = &[1, 2];

fn stuff() -> &'static [uint] {
   simple
}
----------------------------------------------------------------------

The failing code worked a week or so ago.  Similar issue in that I can
make it work by giving an explicit type for the literal array.

David

On Sat, Aug 30, 2014 at 04:52:00PM +0800, Kai Noda wrote:
Hi Rusters,
I'm very new to Rust, so I'd like you to check if this is really a
compiler bug or not.
fn main() {
    let s1 = &[0i];
    {
        let s2 = &[0i];
        let s3 = if true { s1 } else { s2 };
    };
}
[1]http://is.gd/NCeGpl

<anon>:5:19: 5:23 error: borrowed value does not live long enough
<anon>:5         let s2 = &[0i];
                          ^~~~
<anon>:2:11: 8:2 note: reference must be valid for the block at 2:10...
<anon>:2 fn main() {
<anon>:3     let s1 = &[0i];
<anon>:4     {
<anon>:5         let s2 = &[0i];
<anon>:6         let s3 = if true { s1 } else { s2 };
<anon>:7     };
        ...
<anon>:4:5: 7:6 note: ...but borrowed value is only valid for the block at 4:4
<anon>:4     {
<anon>:5         let s2 = &[0i];
<anon>:6         let s3 = if true { s1 } else { s2 };
<anon>:7     };
error: aborting due to previous error

Seems like s1 and s2 are inferred as &[int, ..1] and when I manually
type them as &[int], it successfully compiles.
fn main() {
    let s1: &[int] = &[0i];
    {
        let s2: &[int] = &[0i];
        let s3 = if true { s1 } else { s2 };
    };
}
Putting s1 and s2 into a single block also satisfies the compiler.
fn main() {
    let s1 = &[0i];
    let s2 = &[0i];
    let s3 = if true { s1 } else { s2 };
}
I came from C++ and I think the way I take reference of a fixed vector
literal is correct.
[2]http://doc.rust-lang.org/rust.html#pointer-types
I was originally trying to fix this line which doesn't compile with
the nightly build of Rust:
[3]https://github.com/servo/rust-url/blob/master/src/form_urlencoded.rs#L55
No similar issue is filed here:
[4]https://github.com/rust-lang/rust/labels/A-lifetimes
Best regards,
Kai
野田  開 <[5][email protected]>

References

  Visible links
  1. http://is.gd/NCeGpl
  2. http://doc.rust-lang.org/rust.html#pointer-types
  3. https://github.com/servo/rust-url/blob/master/src/form_urlencoded.rs#L55
  4. https://github.com/rust-lang/rust/labels/A-lifetimes
  5. mailto:[email protected]

_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to