Rollup merge of #154131 - cyrgani:structs-enums, r=Kivooeo

begin `tests/ui/structs-enums` cleanup

Nearly all tests in this directory are heavily outdated, poorly formatted and have a lot of duplication. This PR is the first of a planned series of PRs to combinine this, `ui/structs` and `ui/enum` into a better structure (`ui/adt` maybe?).
This commit is contained in:
Jonathan Brouwer
2026-03-25 19:52:58 +01:00
committed by GitHub
44 changed files with 180 additions and 810 deletions
@@ -1,14 +0,0 @@
pub mod kitties {
pub struct cat {
meows : usize,
pub how_hungry : isize,
}
pub fn cat(in_x : usize, in_y : isize) -> cat {
cat {
meows: in_x,
how_hungry: in_y
}
}
}
@@ -1,19 +0,0 @@
pub mod kitties {
pub struct cat {
meows : usize,
pub how_hungry : isize,
}
impl cat {
pub fn speak(&self) {}
}
pub fn cat(in_x : usize, in_y : isize) -> cat {
cat {
meows: in_x,
how_hungry: in_y
}
}
}
@@ -1,19 +0,0 @@
pub mod kitties {
pub struct cat {
meows : usize,
pub how_hungry : isize,
}
impl cat {
pub fn speak(&mut self) { self.meows += 1; }
pub fn meow_count(&mut self) -> usize { self.meows }
}
pub fn cat(in_x : usize, in_y : isize) -> cat {
cat {
meows: in_x,
how_hungry: in_y
}
}
}
+44 -31
View File
@@ -1,41 +1,54 @@
pub mod kitties {
pub struct cat {
meows : usize,
use std::fmt;
pub how_hungry : isize,
pub name : String,
#[derive(Clone)]
pub struct Cat {
meows: usize,
pub how_hungry: isize,
pub name: String,
}
impl Cat {
pub fn speak(&mut self) {
self.meow();
}
impl cat {
pub fn speak(&mut self) { self.meow(); }
pub fn eat(&mut self) -> bool {
if self.how_hungry > 0 {
println!("OM NOM NOM");
self.how_hungry -= 2;
return true;
} else {
println!("Not hungry!");
return false;
}
pub fn eat(&mut self) -> bool {
if self.how_hungry > 0 {
println!("OM NOM NOM");
self.how_hungry -= 2;
return true;
} else {
println!("Not hungry!");
return false;
}
}
impl cat {
pub fn meow(&mut self) {
println!("Meow");
self.meows += 1;
if self.meows % 5 == 0 {
self.how_hungry += 1;
}
pub fn noop(&self) {}
}
impl Cat {
pub fn meow(&mut self) {
println!("Meow");
self.meows += 1;
if self.meows % 5 == 0 {
self.how_hungry += 1;
}
}
pub fn cat(in_x : usize, in_y : isize, in_name: String) -> cat {
cat {
meows: in_x,
how_hungry: in_y,
name: in_name
}
pub fn meow_count(&self) -> usize {
self.meows
}
}
pub fn cat(in_x: usize, in_y: isize, in_name: String) -> Cat {
Cat { meows: in_x, how_hungry: in_y, name: in_name }
}
pub fn cat_unnamed(in_x: usize, in_y: isize) -> Cat {
Cat { meows: in_x, how_hungry: in_y, name: String::new() }
}
impl fmt::Display for Cat {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.name)
}
}
@@ -1,50 +0,0 @@
pub mod kitty {
use std::fmt;
pub struct cat {
meows : usize,
pub how_hungry : isize,
pub name : String,
}
impl fmt::Display for cat {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.name)
}
}
impl cat {
fn meow(&mut self) {
println!("Meow");
self.meows += 1;
if self.meows % 5 == 0 {
self.how_hungry += 1;
}
}
}
impl cat {
pub fn speak(&mut self) { self.meow(); }
pub fn eat(&mut self) -> bool {
if self.how_hungry > 0 {
println!("OM NOM NOM");
self.how_hungry -= 2;
return true;
}
else {
println!("Not hungry!");
return false;
}
}
}
pub fn cat(in_x : usize, in_y : isize, in_name: String) -> cat {
cat {
meows: in_x,
how_hungry: in_y,
name: in_name
}
}
}
@@ -1,5 +0,0 @@
pub mod animals {
pub trait noisy {
fn speak(&mut self);
}
}
@@ -1,17 +0,0 @@
//@ run-pass
//@ aux-build:cci_class_cast.rs
extern crate cci_class_cast;
use cci_class_cast::kitty::cat;
fn print_out(thing: Box<dyn ToString>, expected: String) {
let actual = (*thing).to_string();
println!("{}", actual);
assert_eq!(actual.to_string(), expected);
}
pub fn main() {
let nyan: Box<dyn ToString> = Box::new(cat(0, 2, "nyan".to_string())) as Box<dyn ToString>;
print_out(nyan, "nyan".to_string());
}
@@ -1,94 +0,0 @@
//@ run-pass
#![allow(non_camel_case_types)]
#![allow(dead_code)]
trait noisy {
fn speak(&mut self) -> isize;
}
struct dog {
barks: usize,
volume: isize,
}
impl dog {
fn bark(&mut self) -> isize {
println!("Woof {} {}", self.barks, self.volume);
self.barks += 1_usize;
if self.barks % 3_usize == 0_usize {
self.volume += 1;
}
if self.barks % 10_usize == 0_usize {
self.volume -= 2;
}
println!("Grrr {} {}", self.barks, self.volume);
self.volume
}
}
impl noisy for dog {
fn speak(&mut self) -> isize {
self.bark()
}
}
fn dog() -> dog {
dog {
volume: 0,
barks: 0_usize
}
}
#[derive(Clone)]
struct cat {
meows: usize,
how_hungry: isize,
name: String,
}
impl noisy for cat {
fn speak(&mut self) -> isize {
self.meow() as isize
}
}
impl cat {
pub fn meow_count(&self) -> usize {
self.meows
}
}
impl cat {
fn meow(&mut self) -> usize {
println!("Meow");
self.meows += 1_usize;
if self.meows % 5_usize == 0_usize {
self.how_hungry += 1;
}
self.meows
}
}
fn cat(in_x: usize, in_y: isize, in_name: String) -> cat {
cat {
meows: in_x,
how_hungry: in_y,
name: in_name
}
}
fn annoy_neighbors(critter: &mut dyn noisy) {
for _i in 0_usize..10 { critter.speak(); }
}
pub fn main() {
let mut nyan: cat = cat(0_usize, 2, "nyan".to_string());
let mut whitefang: dog = dog();
annoy_neighbors(&mut nyan);
annoy_neighbors(&mut whitefang);
assert_eq!(nyan.meow_count(), 10_usize);
assert_eq!(whitefang.volume, 1);
}
@@ -1,60 +0,0 @@
//@ run-pass
#![allow(dead_code)]
#![allow(unused_mut)]
#![allow(non_camel_case_types)]
//@ ignore-freebsd FIXME fails on BSD
trait noisy {
fn speak(&mut self);
}
struct cat {
meows: usize,
how_hungry: isize,
name: String,
}
impl noisy for cat {
fn speak(&mut self) { self.meow(); }
}
impl cat {
pub fn eat(&mut self) -> bool {
if self.how_hungry > 0 {
println!("OM NOM NOM");
self.how_hungry -= 2;
return true;
}
else {
println!("Not hungry!");
return false;
}
}
}
impl cat {
fn meow(&mut self) {
println!("Meow");
self.meows += 1;
if self.meows % 5 == 0 {
self.how_hungry += 1;
}
}
}
fn cat(in_x : usize, in_y : isize, in_name: String) -> cat {
cat {
meows: in_x,
how_hungry: in_y,
name: in_name
}
}
pub fn main() {
let mut nyan = cat(0, 2, "nyan".to_string());
let mut nyan: &mut dyn noisy = &mut nyan;
nyan.speak();
}
+1 -1
View File
@@ -1,4 +1,4 @@
//@ run-pass
//@ check-pass
#![allow(dead_code)]
#![allow(non_camel_case_types)]
-31
View File
@@ -1,31 +0,0 @@
//@ run-pass
#![allow(dead_code)]
#![allow(non_camel_case_types)]
/* Test that exporting a class also exports its
public fields and methods */
use kitty::cat;
mod kitty {
pub struct cat {
meows: usize,
name: String,
}
impl cat {
pub fn get_name(&self) -> String { self.name.clone() }
}
pub fn cat(in_name: String) -> cat {
cat {
name: in_name,
meows: 0
}
}
}
pub fn main() {
assert_eq!(cat("Spreckles".to_string()).get_name(),
"Spreckles".to_string());
}
@@ -1,59 +0,0 @@
//@ run-pass
#![allow(dead_code)]
#![allow(non_camel_case_types)]
//@ aux-build:cci_class_trait.rs
extern crate cci_class_trait;
use cci_class_trait::animals::noisy;
struct cat {
meows: usize,
how_hungry : isize,
name : String,
}
impl cat {
pub fn eat(&mut self) -> bool {
if self.how_hungry > 0 {
println!("OM NOM NOM");
self.how_hungry -= 2;
return true;
}
else {
println!("Not hungry!");
return false;
}
}
}
impl noisy for cat {
fn speak(&mut self) { self.meow(); }
}
impl cat {
fn meow(&mut self) {
println!("Meow");
self.meows += 1_usize;
if self.meows % 5_usize == 0_usize {
self.how_hungry += 1;
}
}
}
fn cat(in_x : usize, in_y : isize, in_name: String) -> cat {
cat {
meows: in_x,
how_hungry: in_y,
name: in_name
}
}
pub fn main() {
let mut nyan = cat(0_usize, 2, "nyan".to_string());
nyan.eat();
assert!(!nyan.eat());
for _ in 1_usize..10_usize { nyan.speak(); };
assert!(nyan.eat());
}
@@ -1,64 +0,0 @@
//@ run-pass
#![allow(non_camel_case_types)]
#![allow(dead_code)]
trait noisy {
fn speak(&mut self);
}
#[derive(Clone)]
struct cat {
meows : usize,
how_hungry : isize,
name : String,
}
impl cat {
fn meow(&mut self) {
println!("Meow");
self.meows += 1_usize;
if self.meows % 5_usize == 0_usize {
self.how_hungry += 1;
}
}
}
impl cat {
pub fn eat(&mut self) -> bool {
if self.how_hungry > 0 {
println!("OM NOM NOM");
self.how_hungry -= 2;
return true;
} else {
println!("Not hungry!");
return false;
}
}
}
impl noisy for cat {
fn speak(&mut self) { self.meow(); }
}
fn cat(in_x : usize, in_y : isize, in_name: String) -> cat {
cat {
meows: in_x,
how_hungry: in_y,
name: in_name.clone()
}
}
fn make_speak<C:noisy>(mut c: C) {
c.speak();
}
pub fn main() {
let mut nyan = cat(0_usize, 2, "nyan".to_string());
nyan.eat();
assert!(!nyan.eat());
for _ in 1_usize..10_usize {
make_speak(nyan.clone());
}
}
@@ -1,13 +0,0 @@
//@ run-pass
//@ aux-build:cci_class_2.rs
extern crate cci_class_2;
use cci_class_2::kitties::cat;
pub fn main() {
let nyan : cat = cat(52, 99);
let kitty = cat(1000, 2);
assert_eq!(nyan.how_hungry, 99);
assert_eq!(kitty.how_hungry, 2);
nyan.speak();
}
@@ -1,14 +0,0 @@
//@ run-pass
//@ aux-build:cci_class_3.rs
extern crate cci_class_3;
use cci_class_3::kitties::cat;
pub fn main() {
let mut nyan : cat = cat(52, 99);
let kitty = cat(1000, 2);
assert_eq!(nyan.how_hungry, 99);
assert_eq!(kitty.how_hungry, 2);
nyan.speak();
assert_eq!(nyan.meow_count(), 53);
}
-30
View File
@@ -1,30 +0,0 @@
//@ run-pass
#![allow(non_camel_case_types)]
struct cat {
meows : usize,
how_hungry : isize,
}
impl cat {
pub fn speak(&mut self) { self.meows += 1; }
pub fn meow_count(&mut self) -> usize { self.meows }
}
fn cat(in_x: usize, in_y: isize) -> cat {
cat {
meows: in_x,
how_hungry: in_y
}
}
pub fn main() {
let mut nyan: cat = cat(52, 99);
let kitty = cat(1000, 2);
assert_eq!(nyan.how_hungry, 99);
assert_eq!(kitty.how_hungry, 2);
nyan.speak();
assert_eq!(nyan.meow_count(), 53);
}
@@ -1,63 +0,0 @@
//@ run-pass
#![allow(dead_code)]
#![allow(non_camel_case_types)]
use std::fmt;
struct cat {
meows : usize,
how_hungry : isize,
name : String,
}
impl cat {
pub fn speak(&mut self) { self.meow(); }
pub fn eat(&mut self) -> bool {
if self.how_hungry > 0 {
println!("OM NOM NOM");
self.how_hungry -= 2;
return true;
}
else {
println!("Not hungry!");
return false;
}
}
}
impl cat {
fn meow(&mut self) {
println!("Meow");
self.meows += 1;
if self.meows % 5 == 0 {
self.how_hungry += 1;
}
}
}
fn cat(in_x : usize, in_y : isize, in_name: String) -> cat {
cat {
meows: in_x,
how_hungry: in_y,
name: in_name
}
}
impl fmt::Display for cat {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.name)
}
}
fn print_out(thing: Box<dyn ToString>, expected: String) {
let actual = (*thing).to_string();
println!("{}", actual);
assert_eq!(actual.to_string(), expected);
}
pub fn main() {
let nyan: Box<dyn ToString> = Box::new(cat(0, 2, "nyan".to_string())) as Box<dyn ToString>;
print_out(nyan, "nyan".to_string());
}
-20
View File
@@ -1,20 +0,0 @@
//@ run-pass
#![allow(dead_code)]
#![allow(non_camel_case_types)]
struct cat {
name : String,
}
fn cat(in_name: String) -> cat {
cat {
name: in_name
}
}
pub fn main() {
let _nyan = cat("nyan".to_string());
}
+109 -3
View File
@@ -2,12 +2,118 @@
//@ aux-build:cci_class_4.rs
extern crate cci_class_4;
use cci_class_4::kitties::cat;
use cci_class_4::*;
pub fn main() {
fn simple_cross_crate() {
let nyan: Cat = cat_unnamed(52, 99);
let kitty = cat_unnamed(1000, 2);
assert_eq!(nyan.how_hungry, 99);
assert_eq!(kitty.how_hungry, 2);
nyan.noop();
}
fn cross_crate() {
let mut nyan = cat(0_usize, 2, "nyan".to_string());
nyan.eat();
assert!(!nyan.eat());
for _ in 1_usize..10_usize { nyan.speak(); };
for _ in 1_usize..10_usize {
nyan.speak();
}
assert!(nyan.eat());
}
fn print_out(thing: Box<dyn ToString>, expected: String) {
let actual = (*thing).to_string();
println!("{}", actual);
assert_eq!(actual.to_string(), expected);
}
fn separate_impl() {
let nyan: Box<dyn ToString> = Box::new(cat(0, 2, "nyan".to_string())) as Box<dyn ToString>;
print_out(nyan, "nyan".to_string());
}
trait Noisy {
fn speak(&mut self) -> isize;
}
impl Noisy for Cat {
fn speak(&mut self) -> isize {
self.meow();
0
}
}
fn make_speak<C: Noisy>(mut c: C) {
c.speak();
}
fn implement_traits() {
let mut nyan = cat(0_usize, 2, "nyan".to_string());
nyan.eat();
assert!(!nyan.eat());
for _ in 1_usize..10_usize {
make_speak(nyan.clone());
}
}
struct Dog {
barks: usize,
volume: isize,
}
impl Dog {
fn bark(&mut self) -> isize {
println!("Woof {} {}", self.barks, self.volume);
self.barks += 1_usize;
if self.barks % 3_usize == 0_usize {
self.volume += 1;
}
if self.barks % 10_usize == 0_usize {
self.volume -= 2;
}
println!("Grrr {} {}", self.barks, self.volume);
self.volume
}
}
impl Noisy for Dog {
fn speak(&mut self) -> isize {
self.bark()
}
}
fn dog() -> Dog {
Dog { volume: 0, barks: 0_usize }
}
fn annoy_neighbors(critter: &mut dyn Noisy) {
for _i in 0_usize..10 {
critter.speak();
}
}
fn multiple_types() {
let mut nyan: Cat = cat(0_usize, 2, "nyan".to_string());
let mut whitefang: Dog = dog();
annoy_neighbors(&mut nyan);
annoy_neighbors(&mut whitefang);
assert_eq!(nyan.meow_count(), 10_usize);
assert_eq!(whitefang.volume, 1);
}
fn cast_to_trait() {
let mut nyan = cat(0, 2, "nyan".to_string());
let nyan: &mut dyn Noisy = &mut nyan;
nyan.speak();
}
fn main() {
simple_cross_crate();
cross_crate();
separate_impl();
implement_traits();
multiple_types();
cast_to_trait();
}
@@ -1,4 +1,4 @@
//@ run-pass
//@ check-pass
#![allow(dead_code)]
#![allow(non_camel_case_types)]
@@ -1,12 +0,0 @@
//@ run-pass
//@ aux-build:cci_class.rs
extern crate cci_class;
use cci_class::kitties::cat;
pub fn main() {
let nyan : cat = cat(52, 99);
let kitty = cat(1000, 2);
assert_eq!(nyan.how_hungry, 99);
assert_eq!(kitty.how_hungry, 2);
}
@@ -1,28 +0,0 @@
//@ run-pass
#![allow(dead_code)]
#![allow(non_camel_case_types)]
struct cat {
meows : usize,
how_hungry : isize,
}
impl cat {
pub fn speak(&mut self) {}
}
fn cat(in_x : usize, in_y : isize) -> cat {
cat {
meows: in_x,
how_hungry: in_y
}
}
pub fn main() {
let mut nyan : cat = cat(52, 99);
let kitty = cat(1000, 2);
assert_eq!(nyan.how_hungry, 99);
assert_eq!(kitty.how_hungry, 2);
nyan.speak();
}
-23
View File
@@ -1,23 +0,0 @@
//@ run-pass
#![allow(dead_code)]
#![allow(non_camel_case_types)]
struct cat {
meows : usize,
how_hungry : isize,
}
fn cat(in_x : usize, in_y : isize) -> cat {
cat {
meows: in_x,
how_hungry: in_y
}
}
pub fn main() {
let nyan : cat = cat(52, 99);
let kitty = cat(1000, 2);
assert_eq!(nyan.how_hungry, 99);
assert_eq!(kitty.how_hungry, 2);
}
-51
View File
@@ -1,51 +0,0 @@
//@ run-pass
#![allow(dead_code)]
#![allow(non_camel_case_types)]
struct cat {
meows : usize,
how_hungry : isize,
name : String,
}
impl cat {
pub fn speak(&mut self) { self.meow(); }
pub fn eat(&mut self) -> bool {
if self.how_hungry > 0 {
println!("OM NOM NOM");
self.how_hungry -= 2;
return true;
} else {
println!("Not hungry!");
return false;
}
}
}
impl cat {
fn meow(&mut self) {
println!("Meow");
self.meows += 1_usize;
if self.meows % 5_usize == 0_usize {
self.how_hungry += 1;
}
}
}
fn cat(in_x : usize, in_y : isize, in_name: String) -> cat {
cat {
meows: in_x,
how_hungry: in_y,
name: in_name
}
}
pub fn main() {
let mut nyan = cat(0_usize, 2, "nyan".to_string());
nyan.eat();
assert!(!nyan.eat());
for _ in 1_usize..10_usize { nyan.speak(); };
assert!(nyan.eat());
}
@@ -1,4 +1,4 @@
//@ run-pass
//@ check-pass
#![allow(overflowing_literals)]
@@ -1,14 +0,0 @@
//@ run-pass
#![allow(non_camel_case_types)]
// We can export tags without exporting the variants to create a simple
// sort of ADT.
mod foo {
pub enum t { t1, }
pub fn f() -> t { return t::t1; }
}
pub fn main() { let _v: foo::t = foo::f(); }
@@ -1,8 +0,0 @@
//@ run-pass
#![allow(non_camel_case_types)]
mod foo {
pub enum t { t1, }
}
pub fn main() { let _v = foo::t::t1; }
+1 -1
View File
@@ -1,4 +1,4 @@
//@ run-pass
//@ check-pass
#![allow(dead_code)]
#![allow(non_camel_case_types)]
@@ -1,4 +1,4 @@
//@ run-pass
//@ check-pass
#![allow(dead_code)]
pub use Foo::*;
@@ -1,4 +1,4 @@
//@ run-pass
//@ check-pass
#![allow(dead_code)]
mod m2 {
+1 -1
View File
@@ -1,4 +1,4 @@
//@ run-pass
//@ check-pass
#![allow(dead_code)]
enum Foo {
@@ -1,4 +1,4 @@
//@ run-pass
//@ check-pass
#![allow(dead_code)]
/*
@@ -1,4 +1,4 @@
//@ run-pass
//@ check-pass
#![allow(unused_unsafe)]
#![allow(unused_variables)]
-14
View File
@@ -1,14 +0,0 @@
//@ run-pass
// Issue #50.
struct X { foo: String, bar: String }
pub fn main() {
let x = X {foo: "hello".to_string(), bar: "world".to_string()};
println!("{}", x.foo.clone());
println!("{}", x.bar.clone());
}
+1 -1
View File
@@ -1,4 +1,4 @@
//@ run-pass
//@ check-pass
#![allow(dead_code)]
#![allow(non_camel_case_types)]
@@ -1,4 +1,4 @@
//@ run-pass
//@ check-pass
#![allow(dead_code)]
mod foo {
+1 -1
View File
@@ -1,4 +1,4 @@
//@ run-pass
//@ check-pass
#![allow(dead_code)]
#![allow(non_camel_case_types)]
@@ -1,11 +0,0 @@
//@ run-pass
#![allow(dead_code)]
#![allow(non_camel_case_types)]
enum color {
red = 1,
blue = 2,
}
pub fn main() {}
@@ -1,9 +0,0 @@
//@ run-pass
#[allow(dead_code)]
#[derive(Debug)]
struct Foo(isize, isize);
pub fn main() {
let x = Foo(1, 2);
println!("{:?}", x);
}
@@ -1,4 +1,5 @@
//@ run-pass
#[derive(Debug)]
struct Foo(isize, isize);
pub fn main() {
@@ -7,4 +8,16 @@ pub fn main() {
println!("{} {}", y, z);
assert_eq!(y, 1);
assert_eq!(z, 2);
let x = Foo(1, 2);
match x {
Foo(a, b) => {
assert_eq!(a, 1);
assert_eq!(b, 2);
println!("{} {}", a, b);
}
}
let x = Foo(1, 2);
assert_eq!(format!("{x:?}"), "Foo(1, 2)");
}
@@ -1,13 +0,0 @@
//@ run-pass
struct Foo(isize, isize);
pub fn main() {
let x = Foo(1, 2);
match x {
Foo(a, b) => {
assert_eq!(a, 1);
assert_eq!(b, 2);
println!("{} {}", a, b);
}
}
}
@@ -1,7 +0,0 @@
//@ run-pass
#![allow(dead_code)]
struct Foo(isize, isize, isize);
pub fn main() {
}
@@ -1,4 +1,4 @@
//@ run-pass
//@ check-pass
pub struct Z(#[allow(dead_code)] &'static Z);
pub fn main() {}
@@ -1,4 +1,4 @@
//@ run-pass
//@ check-pass
#![allow(dead_code)]
enum Foo {