Skip to content

Commit 6c19ebe

Browse files
committed
Added tests for RFC 2008.
1 parent d51ea53 commit 6c19ebe

File tree

17 files changed

+480
-0
lines changed

17 files changed

+480
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
//#![feature(non_exhaustive)]
12+
13+
#[non_exhaustive] //~ERROR non exhaustive is an experimental feature (see issue #44109)
14+
pub enum NonExhaustiveEnum {
15+
Unit,
16+
Tuple(u32),
17+
Struct { field: u32 }
18+
}
19+
20+
fn main() { }
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![crate_type = "rlib"]
12+
#![feature(non_exhaustive)]
13+
14+
#[non_exhaustive]
15+
pub enum NonExhaustiveEnum {
16+
Unit,
17+
Tuple(u32),
18+
Struct { field: u32 }
19+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(non_exhaustive)]
12+
13+
#[non_exhaustive]
14+
pub struct NormalStruct {
15+
pub first_field: u16,
16+
pub second_field: u16,
17+
}
18+
19+
#[non_exhaustive]
20+
pub struct UnitStruct;
21+
22+
#[non_exhaustive]
23+
pub struct TupleStruct(pub u16, pub u16);
24+
25+
#[derive(Debug)]
26+
#[non_exhaustive]
27+
pub struct FunctionalRecord {
28+
pub first_field: u16,
29+
pub second_field: u16,
30+
pub third_field: bool
31+
}
32+
33+
impl Default for FunctionalRecord {
34+
fn default() -> FunctionalRecord {
35+
FunctionalRecord { first_field: 640, second_field: 480, third_field: false }
36+
}
37+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![crate_type = "rlib"]
12+
#![feature(non_exhaustive)]
13+
14+
pub enum NonExhaustiveVariants {
15+
#[non_exhaustive] Unit,
16+
#[non_exhaustive] Tuple(u32),
17+
#[non_exhaustive] Struct { field: u32 }
18+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// aux-build:enums.rs
12+
extern crate enums;
13+
14+
use enums::NonExhaustiveEnum;
15+
16+
fn main() {
17+
let enum_unit = NonExhaustiveEnum::Unit;
18+
19+
match enum_unit {
20+
//~^ ERROR non-exhaustive patterns: `_` not covered [E0004]
21+
NonExhaustiveEnum::Unit => "first",
22+
NonExhaustiveEnum::Tuple(_) => "second",
23+
NonExhaustiveEnum::Struct { .. } => "third"
24+
};
25+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// aux-build:structs.rs
12+
extern crate structs;
13+
14+
use structs::{NormalStruct, UnitStruct, TupleStruct, FunctionalRecord};
15+
16+
fn main() {
17+
let fr = FunctionalRecord {
18+
//~^ ERROR cannot create non-exhaustive struct
19+
first_field: 1920,
20+
second_field: 1080,
21+
..FunctionalRecord::default()
22+
};
23+
24+
let ns = NormalStruct { first_field: 640, second_field: 480 };
25+
//~^ ERROR cannot create non-exhaustive struct
26+
27+
let NormalStruct { first_field, second_field } = ns;
28+
//~^ ERROR `..` required with struct marked as non-exhaustive
29+
30+
let ts = TupleStruct(640, 480);
31+
//~^ ERROR expected function, found struct `TupleStruct` [E0423]
32+
33+
let ts_explicit = structs::TupleStruct(640, 480);
34+
//~^ ERROR tuple struct `TupleStruct` is private [E0603]
35+
36+
let TupleStruct { 0: first_field, 1: second_field } = ts;
37+
//~^ ERROR `..` required with struct marked as non-exhaustive
38+
39+
let us = UnitStruct;
40+
//~^ ERROR expected value, found struct `UnitStruct` [E0423]
41+
42+
let us_explicit = structs::UnitStruct;
43+
//~^ ERROR unit struct `UnitStruct` is private [E0603]
44+
45+
let UnitStruct { } = us;
46+
//~^ ERROR `..` required with struct marked as non-exhaustive
47+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// aux-build:variants.rs
12+
extern crate variants;
13+
14+
use variants::NonExhaustiveVariants;
15+
16+
/*
17+
* The initial implementation of #[non_exhaustive] (RFC 2008) does not include support for
18+
* variants. See issue #44109 and PR 45394.
19+
*/
20+
// ignore-test
21+
22+
fn main() {
23+
let variant_struct = NonExhaustiveVariants::Struct { field: 640 };
24+
//~^ ERROR cannot create non-exhaustive variant
25+
26+
let variant_tuple = NonExhaustiveVariants::Tuple { 0: 640 };
27+
//~^ ERROR cannot create non-exhaustive variant
28+
29+
match variant_struct {
30+
NonExhaustiveVariants::Unit => "",
31+
NonExhaustiveVariants::Tuple(fe_tpl) => "",
32+
//~^ ERROR `..` required with variant marked as non-exhaustive
33+
NonExhaustiveVariants::Struct { field } => ""
34+
//~^ ERROR `..` required with variant marked as non-exhaustive
35+
};
36+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(non_exhaustive)]
12+
13+
/*
14+
* The initial implementation of #[non_exhaustive] (RFC 2008) does not include support for
15+
* variants. See issue #44109 and PR 45394.
16+
*/
17+
18+
pub enum NonExhaustiveVariants {
19+
#[non_exhaustive] Unit,
20+
//~^ ERROR #[non_exhaustive] is not yet supported on variants
21+
#[non_exhaustive] Tuple(u32),
22+
//~^ ERROR #[non_exhaustive] is not yet supported on variants
23+
#[non_exhaustive] Struct { field: u32 }
24+
//~^ ERROR #[non_exhaustive] is not yet supported on variants
25+
}
26+
27+
fn main() { }
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![crate_type = "rlib"]
12+
#![feature(non_exhaustive)]
13+
14+
#[non_exhaustive]
15+
pub enum NonExhaustiveEnum {
16+
Unit,
17+
Tuple(u32),
18+
Struct { field: u32 }
19+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(non_exhaustive)]
12+
13+
#[non_exhaustive]
14+
pub struct NormalStruct {
15+
pub first_field: u16,
16+
pub second_field: u16,
17+
}
18+
19+
#[non_exhaustive]
20+
pub struct UnitStruct;
21+
22+
#[non_exhaustive]
23+
pub struct TupleStruct (pub u16, pub u16);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![crate_type = "rlib"]
12+
#![feature(non_exhaustive)]
13+
14+
pub enum NonExhaustiveVariants {
15+
#[non_exhaustive] Unit,
16+
#[non_exhaustive] Tuple(u32),
17+
#[non_exhaustive] Struct { field: u32 }
18+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// aux-build:enums.rs
12+
extern crate enums;
13+
14+
use enums::NonExhaustiveEnum;
15+
16+
fn main() {
17+
let enum_unit = NonExhaustiveEnum::Unit;
18+
19+
match enum_unit {
20+
NonExhaustiveEnum::Unit => 1,
21+
NonExhaustiveEnum::Tuple(_) => 2,
22+
// This particular arm tests that a enum marked as non-exhaustive
23+
// will not error if its variants are matched exhaustively.
24+
NonExhaustiveEnum::Struct { field } => field,
25+
_ => 0 // no error with wildcard
26+
};
27+
28+
match enum_unit {
29+
_ => "no error with only wildcard"
30+
};
31+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(non_exhaustive)]
12+
13+
#[non_exhaustive]
14+
pub enum NonExhaustiveEnum {
15+
Unit,
16+
Tuple(u32),
17+
Struct { field: u32 }
18+
}
19+
20+
fn main() {
21+
let enum_unit = NonExhaustiveEnum::Unit;
22+
23+
match enum_unit {
24+
NonExhaustiveEnum::Unit => "first",
25+
NonExhaustiveEnum::Tuple(_) => "second",
26+
NonExhaustiveEnum::Struct { .. } => "third",
27+
};
28+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// aux-build:structs.rs
12+
extern crate structs;
13+
14+
use structs::{NormalStruct, UnitStruct, TupleStruct};
15+
16+
// We only test matching here as we cannot create non-exhaustive
17+
// structs from another crate. ie. they'll never pass in run-pass tests.
18+
19+
fn match_structs(ns: NormalStruct, ts: TupleStruct, us: UnitStruct) {
20+
let NormalStruct { first_field, second_field, .. } = ns;
21+
22+
let TupleStruct { 0: first, 1: second, .. } = ts;
23+
24+
let UnitStruct { .. } = us;
25+
}
26+
27+
fn main() { }

0 commit comments

Comments
 (0)