auto merge of #5304 : jld/rust/const-adjustments, r=graydon
These changes make const translation use adjustments (autodereference, autoreference, bare-fn-to-closure), like normal code does, replacing some ad-hoc logic that wasn't always right. As a convenient side-effect, explicit dereference (both of pointers and of newtypes) is also supported in const expressions. There is also a “bonus fix” for a bug in the pretty-printer exposed by one of the added tests.
This commit is contained in:
commit
9b9ffd5b41
8 changed files with 194 additions and 54 deletions
17
src/test/run-pass/const-autoderef-newtype.rs
Normal file
17
src/test/run-pass/const-autoderef-newtype.rs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
// Copyright 2013 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 S(&'static [int]);
|
||||
const C0: S = S([3]);
|
||||
const C1: int = C0[0];
|
||||
|
||||
pub fn main() {
|
||||
fail_unless!(C1 == 3);
|
||||
}
|
||||
19
src/test/run-pass/const-autoderef.rs
Normal file
19
src/test/run-pass/const-autoderef.rs
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
// Copyright 2013 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.
|
||||
|
||||
const A: [u8 * 1] = ['h' as u8];
|
||||
const B: u8 = (&A)[0];
|
||||
const C: &'static &'static &'static &'static [u8 * 1] = & & & &A;
|
||||
const D: u8 = (&C)[0];
|
||||
|
||||
pub fn main() {
|
||||
fail_unless!(B == A[0]);
|
||||
fail_unless!(D == A[0]);
|
||||
}
|
||||
20
src/test/run-pass/const-deref.rs
Normal file
20
src/test/run-pass/const-deref.rs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
// Copyright 2013 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.
|
||||
|
||||
const C: &'static int = &1000;
|
||||
const D: int = *C;
|
||||
struct S(&'static int);
|
||||
const E: &'static S = &S(C);
|
||||
const F: int = ***E;
|
||||
|
||||
pub fn main() {
|
||||
fail_unless!(D == 1000);
|
||||
fail_unless!(F == 1000);
|
||||
}
|
||||
18
src/test/run-pass/const-region-ptrs-noncopy.rs
Normal file
18
src/test/run-pass/const-region-ptrs-noncopy.rs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
// Copyright 2013 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.
|
||||
|
||||
type Big = [u64 * 8];
|
||||
struct Pair { a: int, b: &'self Big }
|
||||
const x: &'static Big = &([13, 14, 10, 13, 11, 14, 14, 15]);
|
||||
const y: &'static Pair<'static> = &Pair {a: 15, b: x};
|
||||
|
||||
pub fn main() {
|
||||
fail_unless!(ptr::addr_of(x) == ptr::addr_of(y.b));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue