Skip to content

Commit 14dda38

Browse files
committed
cleaned up some tests
1 parent 5b8081f commit 14dda38

9 files changed

+168
-64
lines changed
Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1+
//! Test that nested `cfg_attr` attributes work correctly for conditional compilation.
2+
//! This checks that `cfg_attr` can be arbitrarily deeply nested and that the
3+
//! expansion works from outside to inside, eventually applying the innermost
4+
//! conditional compilation directive.
5+
//!
6+
//! In this test, `cfg_attr(all(), cfg_attr(all(), cfg(false)))` should expand to:
7+
//! 1. `cfg_attr(all(), cfg(false))` (outer cfg_attr applied)
8+
//! 2. `cfg(false)` (inner cfg_attr applied)
9+
//! 3. Function `f` is excluded from compilation
10+
//!
11+
//! Added in <https://github.com/rust-lang/rust/pull/34216>.
12+
113
#[cfg_attr(all(), cfg_attr(all(), cfg(false)))]
214
fn f() {}
315

4-
fn main() { f() } //~ ERROR cannot find function `f` in this scope
16+
fn main() {
17+
f() //~ ERROR cannot find function `f` in this scope
18+
}

tests/ui/cfg/nested-cfg-attr-conditional-compilation.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0425]: cannot find function `f` in this scope
2-
--> $DIR/nested-cfg-attrs.rs:4:13
2+
--> $DIR/nested-cfg-attr-conditional-compilation.rs:17:5
33
|
4-
LL | fn main() { f() }
5-
| ^ not found in this scope
4+
LL | f()
5+
| ^ not found in this scope
66

77
error: aborting due to 1 previous error
88

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,47 @@
1+
//! Test that the compiler can handle code bases with a high number of closures.
2+
//! This is particularly important for the MinGW toolchain which has a limit of
3+
//! 2^15 weak symbols per binary. This test creates 2^12 closures (256 functions
4+
//! with 16 closures each) to check the compiler handles this correctly.
5+
//!
6+
//! Regression test for <https://github.com/rust-lang/rust/issues/34793>.
7+
//! See also <https://github.com/rust-lang/rust/pull/34830>.
8+
19
//@ run-pass
2-
// This test case tests whether we can handle code bases that contain a high
3-
// number of closures, something that needs special handling in the MingGW
4-
// toolchain.
5-
// See https://github.com/rust-lang/rust/issues/34793 for more information.
610

711
// Make sure we don't optimize anything away:
812
//@ compile-flags: -C no-prepopulate-passes -Cpasses=name-anon-globals
913

10-
// Expand something exponentially
11-
macro_rules! go_bacterial {
14+
/// Macro for exponential expansion - creates 2^n copies of the given macro call
15+
macro_rules! expand_exponentially {
1216
($mac:ident) => ($mac!());
1317
($mac:ident 1 $($t:tt)*) => (
14-
go_bacterial!($mac $($t)*);
15-
go_bacterial!($mac $($t)*);
18+
expand_exponentially!($mac $($t)*);
19+
expand_exponentially!($mac $($t)*);
1620
)
1721
}
1822

19-
macro_rules! mk_closure {
20-
() => ((move || {})())
23+
/// Creates and immediately calls a closure
24+
macro_rules! create_closure {
25+
() => {
26+
(move || {})()
27+
};
2128
}
2229

23-
macro_rules! mk_fn {
30+
/// Creates a function containing 16 closures (2^4)
31+
macro_rules! create_function_with_closures {
2432
() => {
2533
{
26-
fn function() {
27-
// Make 16 closures
28-
go_bacterial!(mk_closure 1 1 1 1);
34+
fn function_with_closures() {
35+
// Create 16 closures using exponential expansion: 2^4 = 16
36+
expand_exponentially!(create_closure 1 1 1 1);
2937
}
30-
let _ = function();
38+
let _ = function_with_closures();
3139
}
3240
}
3341
}
3442

3543
fn main() {
36-
// Make 2^8 functions, each containing 16 closures,
37-
// resulting in 2^12 closures overall.
38-
go_bacterial!(mk_fn 1 1 1 1 1 1 1 1);
44+
// Create 2^8 = 256 functions, each containing 16 closures,
45+
// resulting in 2^12 = 4096 closures total.
46+
expand_exponentially!(create_function_with_closures 1 1 1 1 1 1 1 1);
3947
}

tests/ui/fn/mutable-function-parameters.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//! Test that function and closure parameters marked as `mut` can be mutated
2+
//! within the function body.
3+
14
//@ run-pass
25

36
fn f(mut y: Box<isize>) {
@@ -6,10 +9,12 @@ fn f(mut y: Box<isize>) {
69
}
710

811
fn g() {
9-
let frob = |mut q: Box<isize>| { *q = 2; assert_eq!(*q, 2); };
12+
let frob = |mut q: Box<isize>| {
13+
*q = 2;
14+
assert_eq!(*q, 2);
15+
};
1016
let w = Box::new(37);
1117
frob(w);
12-
1318
}
1419

1520
pub fn main() {
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
fn hd<U>(v: Vec<U> ) -> U {
2-
fn hd1(w: [U]) -> U { return w[0]; }
3-
//~^ ERROR can't use generic parameters from outer item
4-
//~| ERROR can't use generic parameters from outer item
1+
//! Test that generic parameters from an outer function are not accessible
2+
//! in nested functions.
53
6-
return hd1(v);
4+
fn foo<U>(v: Vec<U>) -> U {
5+
fn bar(w: [U]) -> U {
6+
//~^ ERROR can't use generic parameters from outer item
7+
//~| ERROR can't use generic parameters from outer item
8+
return w[0];
9+
}
10+
11+
return bar(v);
712
}
813

914
fn main() {}

tests/ui/generics/generic-params-nested-fn-scope-error.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
error[E0401]: can't use generic parameters from outer item
2-
--> $DIR/nested-ty-params.rs:2:16
2+
--> $DIR/generic-params-nested-fn-scope-error.rs:5:16
33
|
4-
LL | fn hd<U>(v: Vec<U> ) -> U {
5-
| - type parameter from outer item
6-
LL | fn hd1(w: [U]) -> U { return w[0]; }
4+
LL | fn foo<U>(v: Vec<U>) -> U {
5+
| - type parameter from outer item
6+
LL | fn bar(w: [U]) -> U {
77
| - ^ use of generic parameter from outer item
88
| |
99
| help: try introducing a local generic parameter here: `<U>`
1010

1111
error[E0401]: can't use generic parameters from outer item
12-
--> $DIR/nested-ty-params.rs:2:23
12+
--> $DIR/generic-params-nested-fn-scope-error.rs:5:23
1313
|
14-
LL | fn hd<U>(v: Vec<U> ) -> U {
15-
| - type parameter from outer item
16-
LL | fn hd1(w: [U]) -> U { return w[0]; }
14+
LL | fn foo<U>(v: Vec<U>) -> U {
15+
| - type parameter from outer item
16+
LL | fn bar(w: [U]) -> U {
1717
| - ^ use of generic parameter from outer item
1818
| |
1919
| help: try introducing a local generic parameter here: `<U>`
Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,36 @@
1+
//! Test that nested block comments are properly supported by the parser.
2+
//!
3+
//! This is a historical test from the early days of Rust development when
4+
//! multi-line comment support was first implemented.
5+
//!
6+
//! See <https://github.com/rust-lang/rust/issues/66>.
7+
18
//@ run-pass
29

310
/* This test checks that nested comments are supported
411
5-
/*
6-
This should not panic
12+
/* This is a nested comment
13+
/* And this is even more deeply nested */
14+
Back to the first level of nesting
715
*/
16+
17+
/* Another nested comment at the same level */
18+
*/
19+
20+
/* Additional test cases for nested comments */
21+
22+
/*
23+
/* Level 1
24+
/* Level 2
25+
/* Level 3 */
26+
*/
27+
*/
828
*/
929

1030
pub fn main() {
31+
// Check that code after nested comments works correctly
32+
let _x = 42;
33+
34+
/* Even inline /* nested */ comments work */
35+
let _y = /* nested /* comment */ test */ 100;
1136
}
Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
1-
//@ run-pass
2-
3-
#![allow(non_camel_case_types)]
1+
//! Test that name resolution works correctly when a struct and its constructor
2+
//! function have the same name within a nested scope. This checks that the
3+
//! compiler can distinguish between type names and value names in the same
4+
//! namespace.
5+
//!
6+
//! This is a historical test from early Rust development (circa 2012) when
7+
//! there was a bug that prevented referring to constructors for structs
8+
//! nested inside functions from the struct's outer scope.
49
5-
pub fn main() {
6-
struct b {
7-
i: isize,
8-
}
10+
//@ run-pass
911

10-
impl b {
11-
fn do_stuff(&self) -> isize { return 37; }
12-
}
12+
struct Point {
13+
i: isize,
14+
}
1315

14-
fn b(i:isize) -> b {
15-
b {
16-
i: i
17-
}
16+
impl Point {
17+
fn get_value(&self) -> isize {
18+
return 37;
1819
}
20+
}
1921

20-
// fn b(x:isize) -> isize { panic!(); }
22+
// Constructor function with the same name as the struct
23+
#[allow(non_snake_case)]
24+
fn Point(i: isize) -> Point {
25+
Point { i }
26+
}
2127

22-
let z = b(42);
23-
assert_eq!(z.i, 42);
24-
assert_eq!(z.do_stuff(), 37);
28+
pub fn main() {
29+
// Test that we can use the constructor function
30+
let point = Point(42);
31+
assert_eq!(point.i, 42);
32+
assert_eq!(point.get_value(), 37);
2533
}
Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,54 @@
1+
//! Test that mutually recursive type definitions are properly handled by the compiler.
2+
//! This checks that types can reference each other in their definitions through
3+
//! `Box` indirection, creating cycles in the type dependency graph.
4+
15
//@ run-pass
26

3-
#![allow(non_camel_case_types)]
4-
#![allow(dead_code)]
7+
#[derive(Debug, PartialEq)]
8+
enum Colour {
9+
Red,
10+
Green,
11+
Blue,
12+
}
13+
14+
#[derive(Debug, PartialEq)]
15+
enum Tree {
16+
Children(Box<List>),
17+
Leaf(Colour),
18+
}
19+
20+
#[derive(Debug, PartialEq)]
21+
enum List {
22+
Cons(Box<Tree>, Box<List>),
23+
Nil,
24+
}
25+
26+
#[derive(Debug, PartialEq)]
27+
enum SmallList {
28+
Kons(isize, Box<SmallList>),
29+
Neel,
30+
}
31+
32+
pub fn main() {
33+
// Construct and test all variants of Colour
34+
let leaf_red = Tree::Leaf(Colour::Red);
35+
assert_eq!(leaf_red, Tree::Leaf(Colour::Red));
536

37+
let leaf_green = Tree::Leaf(Colour::Green);
38+
assert_eq!(leaf_green, Tree::Leaf(Colour::Green));
639

7-
enum colour { red, green, blue, }
40+
let leaf_blue = Tree::Leaf(Colour::Blue);
41+
assert_eq!(leaf_blue, Tree::Leaf(Colour::Blue));
842

9-
enum tree { children(Box<list>), leaf(colour), }
43+
let empty_list = List::Nil;
44+
assert_eq!(empty_list, List::Nil);
1045

11-
enum list { cons(Box<tree>, Box<list>), nil, }
46+
let tree_with_children = Tree::Children(Box::new(List::Nil));
47+
assert_eq!(tree_with_children, Tree::Children(Box::new(List::Nil)));
1248

13-
enum small_list { kons(isize, Box<small_list>), neel, }
49+
let list_with_tree = List::Cons(Box::new(Tree::Leaf(Colour::Blue)), Box::new(List::Nil));
50+
assert_eq!(list_with_tree, List::Cons(Box::new(Tree::Leaf(Colour::Blue)), Box::new(List::Nil)));
1451

15-
pub fn main() { }
52+
let small_list = SmallList::Kons(42, Box::new(SmallList::Neel));
53+
assert_eq!(small_list, SmallList::Kons(42, Box::new(SmallList::Neel)));
54+
}

0 commit comments

Comments
 (0)