cleaned up some tests

This commit is contained in:
Kivooeo 2025-06-29 21:05:19 +05:00
parent 6ca9b43ea9
commit f12120d2bd
9 changed files with 154 additions and 63 deletions

View File

@ -1,4 +1,18 @@
//! Test that nested `cfg_attr` attributes work correctly for conditional compilation.
//! This checks that `cfg_attr` can be arbitrarily deeply nested and that the
//! expansion works from outside to inside, eventually applying the innermost
//! conditional compilation directive.
//!
//! In this test, `cfg_attr(all(), cfg_attr(all(), cfg(false)))` should expand to:
//! 1. `cfg_attr(all(), cfg(false))` (outer cfg_attr applied)
//! 2. `cfg(false)` (inner cfg_attr applied)
//! 3. Function `f` is excluded from compilation
//!
//! Added in <https://github.com/rust-lang/rust/pull/34216>.
#[cfg_attr(all(), cfg_attr(all(), cfg(false)))]
fn f() {}
fn main() { f() } //~ ERROR cannot find function `f` in this scope
fn main() {
f() //~ ERROR cannot find function `f` in this scope
}

View File

@ -1,8 +1,8 @@
error[E0425]: cannot find function `f` in this scope
--> $DIR/nested-cfg-attrs.rs:4:13
--> $DIR/nested-cfg-attr-conditional-compilation.rs:17:5
|
LL | fn main() { f() }
| ^ not found in this scope
LL | f()
| ^ not found in this scope
error: aborting due to 1 previous error

View File

@ -1,13 +1,17 @@
//! Test that the compiler can handle code bases with a high number of closures.
//! This is particularly important for the MinGW toolchain which has a limit of
//! 2^15 weak symbols per binary. This test creates 2^12 closures (256 functions
//! with 16 closures each) to check the compiler handles this correctly.
//!
//! Regression test for <https://github.com/rust-lang/rust/issues/34793>.
//! See also <https://github.com/rust-lang/rust/pull/34830>.
//@ run-pass
// This test case tests whether we can handle code bases that contain a high
// number of closures, something that needs special handling in the MingGW
// toolchain.
// See https://github.com/rust-lang/rust/issues/34793 for more information.
// Make sure we don't optimize anything away:
//@ compile-flags: -C no-prepopulate-passes -Cpasses=name-anon-globals
// Expand something exponentially
/// Macro for exponential expansion - creates 2^n copies of the given macro call
macro_rules! go_bacterial {
($mac:ident) => ($mac!());
($mac:ident 1 $($t:tt)*) => (
@ -16,24 +20,28 @@ macro_rules! go_bacterial {
)
}
macro_rules! mk_closure {
() => ((move || {})())
/// Creates and immediately calls a closure
macro_rules! create_closure {
() => {
(move || {})()
};
}
macro_rules! mk_fn {
/// Creates a function containing 16 closures (2^4)
macro_rules! create_function_with_closures {
() => {
{
fn function() {
// Make 16 closures
go_bacterial!(mk_closure 1 1 1 1);
fn function_with_closures() {
// Create 16 closures using exponential expansion: 2^4 = 16
go_bacterial!(create_closure 1 1 1 1);
}
let _ = function();
let _ = function_with_closures();
}
}
}
fn main() {
// Make 2^8 functions, each containing 16 closures,
// resulting in 2^12 closures overall.
go_bacterial!(mk_fn 1 1 1 1 1 1 1 1);
// Create 2^8 = 256 functions, each containing 16 closures,
// resulting in 2^12 = 4096 closures total.
go_bacterial!(create_function_with_closures 1 1 1 1 1 1 1 1);
}

View File

@ -1,3 +1,6 @@
//! Test that function and closure parameters marked as `mut` can be mutated
//! within the function body.
//@ run-pass
fn f(mut y: Box<isize>) {
@ -6,10 +9,12 @@ fn f(mut y: Box<isize>) {
}
fn g() {
let frob = |mut q: Box<isize>| { *q = 2; assert_eq!(*q, 2); };
let frob = |mut q: Box<isize>| {
*q = 2;
assert_eq!(*q, 2);
};
let w = Box::new(37);
frob(w);
}
pub fn main() {

View File

@ -1,9 +1,14 @@
fn hd<U>(v: Vec<U> ) -> U {
fn hd1(w: [U]) -> U { return w[0]; }
//~^ ERROR can't use generic parameters from outer item
//~| ERROR can't use generic parameters from outer item
//! Test that generic parameters from an outer function are not accessible
//! in nested functions.
return hd1(v);
fn foo<U>(v: Vec<U>) -> U {
fn bar(w: [U]) -> U {
//~^ ERROR can't use generic parameters from outer item
//~| ERROR can't use generic parameters from outer item
return w[0];
}
return bar(v);
}
fn main() {}

View File

@ -1,19 +1,19 @@
error[E0401]: can't use generic parameters from outer item
--> $DIR/nested-ty-params.rs:2:16
--> $DIR/generic-params-nested-fn-scope-error.rs:5:16
|
LL | fn hd<U>(v: Vec<U> ) -> U {
| - type parameter from outer item
LL | fn hd1(w: [U]) -> U { return w[0]; }
LL | fn foo<U>(v: Vec<U>) -> U {
| - type parameter from outer item
LL | fn bar(w: [U]) -> U {
| - ^ use of generic parameter from outer item
| |
| help: try introducing a local generic parameter here: `<U>`
error[E0401]: can't use generic parameters from outer item
--> $DIR/nested-ty-params.rs:2:23
--> $DIR/generic-params-nested-fn-scope-error.rs:5:23
|
LL | fn hd<U>(v: Vec<U> ) -> U {
| - type parameter from outer item
LL | fn hd1(w: [U]) -> U { return w[0]; }
LL | fn foo<U>(v: Vec<U>) -> U {
| - type parameter from outer item
LL | fn bar(w: [U]) -> U {
| - ^ use of generic parameter from outer item
| |
| help: try introducing a local generic parameter here: `<U>`

View File

@ -1,11 +1,34 @@
//! Test that nested block comments are properly supported by the parser.
//!
//! See <https://github.com/rust-lang/rust/issues/66>.
//@ run-pass
/* This test checks that nested comments are supported
/*
This should not panic
/* This is a nested comment
/* And this is even more deeply nested */
Back to the first level of nesting
*/
/* Another nested comment at the same level */
*/
/* Additional test cases for nested comments */
#[rustfmt::skip]
/*
/* Level 1
/* Level 2
/* Level 3 */
*/
*/
*/
pub fn main() {
// Check that code after nested comments works correctly
let _x = 42;
/* Even inline /* nested */ comments work */
let _y = /* nested /* comment */ test */ 100;
}

View File

@ -1,25 +1,29 @@
//! Test that name resolution works correctly when a struct and its constructor
//! function have the same name within a nested scope. This checks that the
//! compiler can distinguish between type names and value names in the same
//! namespace.
//@ run-pass
#![allow(non_camel_case_types)]
struct Point {
i: isize,
}
impl Point {
fn get_value(&self) -> isize {
return 37;
}
}
// Constructor function with the same name as the struct
#[allow(non_snake_case)]
fn Point(i: isize) -> Point {
Point { i }
}
pub fn main() {
struct b {
i: isize,
}
impl b {
fn do_stuff(&self) -> isize { return 37; }
}
fn b(i:isize) -> b {
b {
i: i
}
}
// fn b(x:isize) -> isize { panic!(); }
let z = b(42);
assert_eq!(z.i, 42);
assert_eq!(z.do_stuff(), 37);
// Test that we can use the constructor function
let point = Point(42);
assert_eq!(point.i, 42);
assert_eq!(point.get_value(), 37);
}

View File

@ -1,15 +1,47 @@
//! Test that mutually recursive type definitions are properly handled by the compiler.
//! This checks that types can reference each other in their definitions through
//! `Box` indirection, creating cycles in the type dependency graph.
//@ run-pass
#![allow(non_camel_case_types)]
#![allow(dead_code)]
#[derive(Debug, PartialEq)]
enum Colour {
Red,
Green,
Blue,
}
#[derive(Debug, PartialEq)]
enum Tree {
Children(Box<List>),
Leaf(Colour),
}
enum colour { red, green, blue, }
#[derive(Debug, PartialEq)]
enum List {
Cons(Box<Tree>, Box<List>),
Nil,
}
enum tree { children(Box<list>), leaf(colour), }
#[derive(Debug, PartialEq)]
enum SmallList {
Kons(isize, Box<SmallList>),
Neel,
}
enum list { cons(Box<tree>, Box<list>), nil, }
pub fn main() {
// Construct and test all variants of Colour
let _ = Tree::Leaf(Colour::Red);
enum small_list { kons(isize, Box<small_list>), neel, }
let _ = Tree::Leaf(Colour::Green);
pub fn main() { }
let _ = Tree::Leaf(Colour::Blue);
let _ = List::Nil;
let _ = Tree::Children(Box::new(List::Nil));
let _ = List::Cons(Box::new(Tree::Leaf(Colour::Blue)), Box::new(List::Nil));
let _ = SmallList::Kons(42, Box::new(SmallList::Neel));
}