From dd2d6b222b7a7ad615e612b55fcf3933d4036be3 Mon Sep 17 00:00:00 2001 From: reddevilmidzy Date: Fri, 18 Apr 2025 00:44:11 +0900 Subject: [PATCH] Cleaned up 5 tests in `tests/ui` --- .../writing-to-immutable-vec.rs | 0 .../writing-to-immutable-vec.stderr | 0 .../{ => deref-patterns}/deref-non-pointer.rs | 0 .../deref-non-pointer.stderr | 0 tests/ui/lazy-and-or.rs | 12 --------- tests/ui/list.rs | 9 ------- tests/ui/minus-string.rs | 1 - tests/ui/or-patterns/lazy-and-or.rs | 25 +++++++++++++++++++ tests/ui/recursion/recursive-enum-box.rs | 21 ++++++++++++++++ tests/ui/typeck/minus-string.rs | 7 ++++++ tests/ui/{ => typeck}/minus-string.stderr | 6 ++--- 11 files changed, 56 insertions(+), 25 deletions(-) rename tests/ui/{ => borrowck}/writing-to-immutable-vec.rs (100%) rename tests/ui/{ => borrowck}/writing-to-immutable-vec.stderr (100%) rename tests/ui/{ => deref-patterns}/deref-non-pointer.rs (100%) rename tests/ui/{ => deref-patterns}/deref-non-pointer.stderr (100%) delete mode 100644 tests/ui/lazy-and-or.rs delete mode 100644 tests/ui/list.rs delete mode 100644 tests/ui/minus-string.rs create mode 100644 tests/ui/or-patterns/lazy-and-or.rs create mode 100644 tests/ui/recursion/recursive-enum-box.rs create mode 100644 tests/ui/typeck/minus-string.rs rename tests/ui/{ => typeck}/minus-string.stderr (69%) diff --git a/tests/ui/writing-to-immutable-vec.rs b/tests/ui/borrowck/writing-to-immutable-vec.rs similarity index 100% rename from tests/ui/writing-to-immutable-vec.rs rename to tests/ui/borrowck/writing-to-immutable-vec.rs diff --git a/tests/ui/writing-to-immutable-vec.stderr b/tests/ui/borrowck/writing-to-immutable-vec.stderr similarity index 100% rename from tests/ui/writing-to-immutable-vec.stderr rename to tests/ui/borrowck/writing-to-immutable-vec.stderr diff --git a/tests/ui/deref-non-pointer.rs b/tests/ui/deref-patterns/deref-non-pointer.rs similarity index 100% rename from tests/ui/deref-non-pointer.rs rename to tests/ui/deref-patterns/deref-non-pointer.rs diff --git a/tests/ui/deref-non-pointer.stderr b/tests/ui/deref-patterns/deref-non-pointer.stderr similarity index 100% rename from tests/ui/deref-non-pointer.stderr rename to tests/ui/deref-patterns/deref-non-pointer.stderr diff --git a/tests/ui/lazy-and-or.rs b/tests/ui/lazy-and-or.rs deleted file mode 100644 index f9dbeb68959a3..0000000000000 --- a/tests/ui/lazy-and-or.rs +++ /dev/null @@ -1,12 +0,0 @@ -//@ run-pass - -fn incr(x: &mut isize) -> bool { *x += 1; assert!((false)); return false; } - -pub fn main() { - let x = 1 == 2 || 3 == 3; - assert!((x)); - let mut y: isize = 10; - println!("{}", x || incr(&mut y)); - assert_eq!(y, 10); - if true && x { assert!((true)); } else { assert!((false)); } -} diff --git a/tests/ui/list.rs b/tests/ui/list.rs deleted file mode 100644 index 443c4c9f28f88..0000000000000 --- a/tests/ui/list.rs +++ /dev/null @@ -1,9 +0,0 @@ -//@ run-pass - -#![allow(non_camel_case_types)] - -enum list { #[allow(dead_code)] cons(isize, Box), nil, } - -pub fn main() { - list::cons(10, Box::new(list::cons(11, Box::new(list::cons(12, Box::new(list::nil)))))); -} diff --git a/tests/ui/minus-string.rs b/tests/ui/minus-string.rs deleted file mode 100644 index b83347b937edf..0000000000000 --- a/tests/ui/minus-string.rs +++ /dev/null @@ -1 +0,0 @@ -fn main() { -"foo".to_string(); } //~ ERROR cannot apply unary operator `-` to type `String` diff --git a/tests/ui/or-patterns/lazy-and-or.rs b/tests/ui/or-patterns/lazy-and-or.rs new file mode 100644 index 0000000000000..3d69553132b98 --- /dev/null +++ b/tests/ui/or-patterns/lazy-and-or.rs @@ -0,0 +1,25 @@ +//@ run-pass +// This test verifies the short-circuiting behavior of logical operators `||` and `&&`. +// It ensures that the right-hand expression is not evaluated when the left-hand +// expression is sufficient to determine the result. + +fn would_panic_if_called(x: &mut isize) -> bool { + *x += 1; + assert!(false, "This function should never be called due to short-circuiting"); + false +} + +fn main() { + let x = 1 == 2 || 3 == 3; + assert!(x); + + let mut y: isize = 10; + println!("Result of short-circuit: {}", x || would_panic_if_called(&mut y)); + assert_eq!(y, 10, "y should remain 10 if short-circuiting works correctly"); + + if true && x { + assert!(true); + } else { + assert!(false, "This branch should not be reached"); + } +} diff --git a/tests/ui/recursion/recursive-enum-box.rs b/tests/ui/recursion/recursive-enum-box.rs new file mode 100644 index 0000000000000..540b0c553603c --- /dev/null +++ b/tests/ui/recursion/recursive-enum-box.rs @@ -0,0 +1,21 @@ +//@ run-pass +// A smoke test for recursive enum structures using Box. +// This test constructs a linked list-like structure to exercise memory allocation and ownership. +// Originally introduced in 2010, this is one of Rust’s earliest test cases. + +#![allow(dead_code)] + +enum List { + Cons(isize, Box), + Nil, +} + +fn main() { + List::Cons( + 10, + Box::new(List::Cons( + 11, + Box::new(List::Cons(12, Box::new(List::Nil))), + )), + ); +} diff --git a/tests/ui/typeck/minus-string.rs b/tests/ui/typeck/minus-string.rs new file mode 100644 index 0000000000000..1c0f73a37132c --- /dev/null +++ b/tests/ui/typeck/minus-string.rs @@ -0,0 +1,7 @@ +// Regression test for issue #813. +// This ensures that the unary negation operator `-` cannot be applied to an owned `String`. +// Previously, due to a type-checking bug, this was mistakenly accepted by the compiler. + +fn main() { + -"foo".to_string(); //~ ERROR cannot apply unary operator `-` to type `String` +} diff --git a/tests/ui/minus-string.stderr b/tests/ui/typeck/minus-string.stderr similarity index 69% rename from tests/ui/minus-string.stderr rename to tests/ui/typeck/minus-string.stderr index 153965c810ea7..d2ebcd01ff9c1 100644 --- a/tests/ui/minus-string.stderr +++ b/tests/ui/typeck/minus-string.stderr @@ -1,8 +1,8 @@ error[E0600]: cannot apply unary operator `-` to type `String` - --> $DIR/minus-string.rs:1:13 + --> $DIR/minus-string.rs:6:5 | -LL | fn main() { -"foo".to_string(); } - | ^^^^^^^^^^^^^^^^^^ cannot apply unary operator `-` +LL | -"foo".to_string(); + | ^^^^^^^^^^^^^^^^^^ cannot apply unary operator `-` | note: the foreign item type `String` doesn't implement `Neg` --> $SRC_DIR/alloc/src/string.rs:LL:COL