diff options
| author | Nick Cameron <ncameron@mozilla.com> | 2014-02-24 20:17:02 +1300 | 
|---|---|---|
| committer | Nick Cameron <ncameron@mozilla.com> | 2014-04-20 13:41:18 +1200 | 
| commit | ff04aa8e385f343f66c5bed6a34d1ebf6c971e4d (patch) | |
| tree | 9c0845b6a369ab071138f6c542e7aef46ff5a2a0 /src/test | |
| parent | 3757f01c9bed7b2fe0d726ac45c754312e2917ea (diff) | |
| download | rust-ff04aa8e385f343f66c5bed6a34d1ebf6c971e4d.tar.gz rust-ff04aa8e385f343f66c5bed6a34d1ebf6c971e4d.zip  | |
Allow inheritance between structs.
No subtyping, no interaction with traits. Partially addresses #9912.
Diffstat (limited to 'src/test')
| -rw-r--r-- | src/test/auxiliary/inherit_struct_lib.rs | 27 | ||||
| -rw-r--r-- | src/test/compile-fail/inherit-struct1.rs (renamed from src/test/compile-fail/removed-syntax-class-traits.rs) | 11 | ||||
| -rw-r--r-- | src/test/compile-fail/inherit-struct2.rs | 25 | ||||
| -rw-r--r-- | src/test/compile-fail/inherit-struct3.rs | 25 | ||||
| -rw-r--r-- | src/test/compile-fail/inherit-struct4.rs | 24 | ||||
| -rw-r--r-- | src/test/compile-fail/inherit-struct5.rs | 20 | ||||
| -rw-r--r-- | src/test/compile-fail/inherit-struct6.rs | 42 | ||||
| -rw-r--r-- | src/test/compile-fail/inherit-struct7.rs | 19 | ||||
| -rw-r--r-- | src/test/compile-fail/inherit-struct8.rs | 31 | ||||
| -rw-r--r-- | src/test/compile-fail/inherit-struct9.rs | 18 | ||||
| -rw-r--r-- | src/test/debug-info/simple-struct.rs | 18 | ||||
| -rw-r--r-- | src/test/run-pass/inherit-struct1.rs | 61 | ||||
| -rw-r--r-- | src/test/run-pass/inherit-struct2.rs | 25 | 
13 files changed, 339 insertions, 7 deletions
diff --git a/src/test/auxiliary/inherit_struct_lib.rs b/src/test/auxiliary/inherit_struct_lib.rs new file mode 100644 index 00000000000..fd049a25a0c --- /dev/null +++ b/src/test/auxiliary/inherit_struct_lib.rs @@ -0,0 +1,27 @@ +// 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. + +// Test struct inheritance on structs from another crate. +#![feature(struct_inherit)] + +pub virtual struct S1 { + pub f1: int, +} + +pub struct S2 : S1 { + pub f2: int, +} + +pub fn test_s2(s2: S2) { + assert!(s2.f1 == 115); + assert!(s2.f2 == 113); +} + +pub static glob_s: S2 = S2 { f1: 32, f2: -45 }; diff --git a/src/test/compile-fail/removed-syntax-class-traits.rs b/src/test/compile-fail/inherit-struct1.rs index 45b648560da..00ea4b7783b 100644 --- a/src/test/compile-fail/removed-syntax-class-traits.rs +++ b/src/test/compile-fail/inherit-struct1.rs @@ -1,4 +1,4 @@ -// Copyright 2013 The Rust Project Developers. See the COPYRIGHT +// 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. // @@ -8,7 +8,10 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -struct q : r { - //~^ ERROR expected `{`, `(`, or `;` after struct name - foo: int +// Test struct inheritance. +#![feature(struct_inherit)] + +struct S6 : ~S2; //~ ERROR not a struct + +pub fn main() { } diff --git a/src/test/compile-fail/inherit-struct2.rs b/src/test/compile-fail/inherit-struct2.rs new file mode 100644 index 00000000000..99fd2d2f69d --- /dev/null +++ b/src/test/compile-fail/inherit-struct2.rs @@ -0,0 +1,25 @@ +// 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. + +// Test struct inheritance. +#![feature(struct_inherit)] + +struct S2 : S0 { //~ ERROR super-struct could not be resolved + f2: int, +} + +trait T {} + +struct S3 : T { //~ ERROR super-struct is not a struct type + f3: int, +} + +pub fn main() { +} diff --git a/src/test/compile-fail/inherit-struct3.rs b/src/test/compile-fail/inherit-struct3.rs new file mode 100644 index 00000000000..88329033df7 --- /dev/null +++ b/src/test/compile-fail/inherit-struct3.rs @@ -0,0 +1,25 @@ +// 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. + +// Test struct inheritance. +#![feature(struct_inherit)] + +virtual struct S1 { + f1: int, +} + +struct S6 : S1 { + f2: int, +} + +pub fn main() { + let s = S6{f2: 3}; //~ ERROR missing field: `f1` + let s = S6{f1: 3}; //~ ERROR missing field: `f2` +} diff --git a/src/test/compile-fail/inherit-struct4.rs b/src/test/compile-fail/inherit-struct4.rs new file mode 100644 index 00000000000..e01ec2904a6 --- /dev/null +++ b/src/test/compile-fail/inherit-struct4.rs @@ -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. + +// Test struct inheritance. +#![feature(struct_inherit)] + +// With lifetime parameters. +struct S5<'a> : S4 { //~ ERROR wrong number of lifetime parameters: expected 1 but found 0 + f4: int, +} + +virtual struct S4<'a> { + f3: &'a int, +} + +pub fn main() { +} diff --git a/src/test/compile-fail/inherit-struct5.rs b/src/test/compile-fail/inherit-struct5.rs new file mode 100644 index 00000000000..c40d27c3b6b --- /dev/null +++ b/src/test/compile-fail/inherit-struct5.rs @@ -0,0 +1,20 @@ +// 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. + +// Test struct inheritance on structs from another crate. +#![feature(struct_inherit)] + +// aux-build:inherit_struct_lib.rs +extern crate inherit_struct_lib; + +struct S3 : inherit_struct_lib::S1; //~ ERROR super-struct is defined in a different crate + +pub fn main() { +} diff --git a/src/test/compile-fail/inherit-struct6.rs b/src/test/compile-fail/inherit-struct6.rs new file mode 100644 index 00000000000..e8c86dcb316 --- /dev/null +++ b/src/test/compile-fail/inherit-struct6.rs @@ -0,0 +1,42 @@ +// 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. + +// Test privacy and struct inheritance. +#![feature(struct_inherit)] + +mod Foo { + pub virtual struct S1 { + pub f1: int, + f2: int, + } +} + +struct S2 : Foo::S1 { + pub f3: int, +} + +impl S2 { + fn new() -> S2 { + S2{f1: 3, f2: 4, f3: 5} //~ ERROR field `f2` of struct `S2` is private + } + + fn bar(&self) { + self.f3; + self.f1; + self.f2; //~ ERROR field `f2` of struct `S2` is private + } +} + +pub fn main() { + let s = S2{f1: 3, f2: 4, f3: 5}; //~ ERROR field `f2` of struct `S2` is private + s.f3; + s.f1; + s.f2; //~ ERROR field `f2` of struct `S2` is private +} diff --git a/src/test/compile-fail/inherit-struct7.rs b/src/test/compile-fail/inherit-struct7.rs new file mode 100644 index 00000000000..fb0c9175d15 --- /dev/null +++ b/src/test/compile-fail/inherit-struct7.rs @@ -0,0 +1,19 @@ +// 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. + +// Test struct inheritance. +#![feature(struct_inherit)] + +virtual trait Foo {} //~ ERROR `virtual` keyword may only be used with `struct` +virtual enum Bar {} //~ ERROR `virtual` keyword may only be used with `struct` +virtual fn baz() {} //~ ERROR `virtual` keyword may only be used with `struct` + +pub fn main() { +} diff --git a/src/test/compile-fail/inherit-struct8.rs b/src/test/compile-fail/inherit-struct8.rs new file mode 100644 index 00000000000..d1108349db1 --- /dev/null +++ b/src/test/compile-fail/inherit-struct8.rs @@ -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. + +// Test struct inheritance. +#![feature(struct_inherit)] + +virtual struct S1 { + f1: int, +} + +virtual struct S6 : S1 { + f2: int, +} + +struct S7 : S1 { + f1: int, //~ ERROR field `f1` hides field declared in super-struct +} + +struct S8 : S6 { + f1: int, //~ ERROR field `f1` hides field declared in super-struct +} + +pub fn main() { +} diff --git a/src/test/compile-fail/inherit-struct9.rs b/src/test/compile-fail/inherit-struct9.rs new file mode 100644 index 00000000000..70e341d589c --- /dev/null +++ b/src/test/compile-fail/inherit-struct9.rs @@ -0,0 +1,18 @@ +// 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. + +// Test struct inheritance. +#![feature(struct_inherit)] + +struct s9; +struct s10 : s9; //~ ERROR struct inheritance is only allowed from virtual structs + +pub fn main() { +} diff --git a/src/test/debug-info/simple-struct.rs b/src/test/debug-info/simple-struct.rs index 736ab76f7db..2ac3b48f682 100644 --- a/src/test/debug-info/simple-struct.rs +++ b/src/test/debug-info/simple-struct.rs @@ -72,15 +72,20 @@ // debugger:print 'simple-struct::PADDING_AT_END' // check:$18 = {x = -27, y = 28} -#![allow(unused_variable)] -#![allow(dead_code)] +// debugger:print inheriting +// check:$19 = {a = 10019, b = -10020, x = -10016, y = -10017.5, z = 10018} + + +#![feature(struct_inherit)]; +#![allow(unused_variable)]; +#![allow(dead_code)]; struct NoPadding16 { x: u16, y: i16 } -struct NoPadding32 { +virtual struct NoPadding32 { x: i32, y: f32, z: u32 @@ -143,6 +148,11 @@ static mut PADDING_AT_END: PaddingAtEnd = PaddingAtEnd { y: 14 }; +struct Inheriting : NoPadding32 { + a: u16, + b: i16 +} + fn main() { let no_padding16 = NoPadding16 { x: 10000, y: -10001 }; let no_padding32 = NoPadding32 { x: -10002, y: -10003.5, z: 10004 }; @@ -152,6 +162,8 @@ fn main() { let internal_padding = InternalPadding { x: 10012, y: -10013 }; let padding_at_end = PaddingAtEnd { x: -10014, y: 10015 }; + let inheriting = Inheriting { a: 10019, b: -10020, x: -10016, y: -10017.5, z: 10018 }; + unsafe { NO_PADDING_16.x = 100; NO_PADDING_16.y = -101; diff --git a/src/test/run-pass/inherit-struct1.rs b/src/test/run-pass/inherit-struct1.rs new file mode 100644 index 00000000000..4602f13fef0 --- /dev/null +++ b/src/test/run-pass/inherit-struct1.rs @@ -0,0 +1,61 @@ +// 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. + +// Test struct inheritance. +#![feature(struct_inherit)] + +virtual struct S1 { + f1: int, +} + +virtual struct S2 : S1 { + f2: int, +} + +struct S3 : S2 { + f3: int, +} + +// With lifetime parameters. +struct S5<'a> : S4<'a> { + f4: int, +} + +virtual struct S4<'a> { + f3: &'a int, +} + +// With type parameters. +struct S7<T> : S6<T> { + f4: int, +} + +virtual struct S6<T> { + f3: T, +} + +pub fn main() { + let s = S2{f1: 115, f2: 113}; + assert!(s.f1 == 115); + assert!(s.f2 == 113); + + let s = S3{f1: 15, f2: 13, f3: 17}; + assert!(s.f1 == 15); + assert!(s.f2 == 13); + assert!(s.f3 == 17); + + let s = S5{f3: &5, f4: 3}; + assert!(*s.f3 == 5); + assert!(s.f4 == 3); + + let s = S7{f3: 5u, f4: 3}; + assert!(s.f3 == 5u); + assert!(s.f4 == 3); +} diff --git a/src/test/run-pass/inherit-struct2.rs b/src/test/run-pass/inherit-struct2.rs new file mode 100644 index 00000000000..bbcba0af680 --- /dev/null +++ b/src/test/run-pass/inherit-struct2.rs @@ -0,0 +1,25 @@ +// 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. + +// Test struct inheritance on structs from another crate. + +// aux-build:inherit_struct_lib.rs +extern crate inherit_struct_lib; + +pub fn main() { + let s = inherit_struct_lib::S2{f1: 115, f2: 113}; + assert!(s.f1 == 115); + assert!(s.f2 == 113); + + assert!(inherit_struct_lib::glob_s.f1 == 32); + assert!(inherit_struct_lib::glob_s.f2 == -45); + + inherit_struct_lib::test_s2(s); +}  | 
