|
| 1 | +#![feature(box_patterns)] |
1 | 2 | mod field_access { |
2 | 3 | #[derive(Debug)] |
3 | 4 | struct S; |
4 | | - |
5 | 5 | #[derive(Debug)] |
6 | 6 | struct MyThing { |
7 | 7 | a: S, |
@@ -2351,139 +2351,30 @@ mod tuples { |
2351 | 2351 | } |
2352 | 2352 | } |
2353 | 2353 |
|
2354 | | -pub mod pattern_matching { |
2355 | | - struct MyRecordStruct<T1, T2> { |
2356 | | - value1: T1, |
2357 | | - value2: T2, |
2358 | | - } |
2359 | | - |
2360 | | - struct MyTupleStruct<T1, T2>(T1, T2); |
2361 | | - |
2362 | | - enum MyEnum<T1, T2> { |
2363 | | - Variant1 { value1: T1, value2: T2 }, |
2364 | | - Variant2(T2, T1), |
2365 | | - } |
| 2354 | +pub mod pattern_matching; |
| 2355 | +pub mod pattern_matching_experimental { |
| 2356 | + pub fn box_patterns() { |
| 2357 | + let boxed_value = Box::new(100i32); // $ method=new |
2366 | 2358 |
|
2367 | | - pub fn f() -> Option<()> { |
2368 | | - let value = Some(42); |
2369 | | - if let Some(mesg) = value { |
2370 | | - let mesg = mesg; // $ type=mesg:i32 |
2371 | | - println!("{mesg}"); |
2372 | | - } |
2373 | | - match value { |
2374 | | - Some(mesg) => { |
2375 | | - let mesg = mesg; // $ type=mesg:i32 |
2376 | | - println!("{mesg}"); |
| 2359 | + // BoxPat - Box patterns (requires feature flag) |
| 2360 | + match boxed_value { |
| 2361 | + box 100 => { |
| 2362 | + println!("Boxed 100"); |
2377 | 2363 | } |
2378 | | - None => (), |
2379 | | - }; |
2380 | | - let mesg = value.unwrap(); // $ method=unwrap |
2381 | | - let mesg = mesg; // $ type=mesg:i32 |
2382 | | - println!("{mesg}"); |
2383 | | - let mesg = value?; // $ type=mesg:i32 |
2384 | | - println!("{mesg}"); |
2385 | | - |
2386 | | - let value2 = &Some(42); |
2387 | | - if let &Some(mesg) = value2 { |
2388 | | - let mesg = mesg; // $ type=mesg:i32 |
2389 | | - println!("{mesg}"); |
2390 | | - } |
2391 | | - |
2392 | | - let value3 = 42; |
2393 | | - if let ref mesg = value3 { |
2394 | | - let mesg = mesg; // $ type=mesg:&T.i32 |
2395 | | - println!("{mesg}"); |
2396 | | - } |
2397 | | - |
2398 | | - let value4 = Some(42); |
2399 | | - if let Some(ref mesg) = value4 { |
2400 | | - let mesg = mesg; // $ type=mesg:&T.i32 |
2401 | | - println!("{mesg}"); |
2402 | | - } |
2403 | | - |
2404 | | - let ref value5 = 42; |
2405 | | - let x = value5; // $ type=x:&T.i32 |
2406 | | - |
2407 | | - let my_record_struct = MyRecordStruct { |
2408 | | - value1: 42, |
2409 | | - value2: false, |
2410 | | - }; |
2411 | | - if let MyRecordStruct { value1, value2 } = my_record_struct { |
2412 | | - let x = value1; // $ type=x:i32 |
2413 | | - let y = value2; // $ type=y:bool |
2414 | | - (); |
2415 | | - } |
2416 | | - |
2417 | | - let my_tuple_struct = MyTupleStruct(42, false); |
2418 | | - if let MyTupleStruct(value1, value2) = my_tuple_struct { |
2419 | | - let x = value1; // $ type=x:i32 |
2420 | | - let y = value2; // $ type=y:bool |
2421 | | - (); |
2422 | | - } |
2423 | | - |
2424 | | - let my_enum1 = MyEnum::Variant1 { |
2425 | | - value1: 42, |
2426 | | - value2: false, |
2427 | | - }; |
2428 | | - match my_enum1 { |
2429 | | - MyEnum::Variant1 { value1, value2 } => { |
2430 | | - let x = value1; // $ type=x:i32 |
2431 | | - let y = value2; // $ type=y:bool |
2432 | | - (); |
2433 | | - } |
2434 | | - MyEnum::Variant2(value1, value2) => { |
2435 | | - let x = value1; // $ type=x:bool |
2436 | | - let y = value2; // $ type=y:i32 |
2437 | | - (); |
| 2364 | + box x => { |
| 2365 | + let unboxed = x; // $ MISSING: type=unboxed:i32 |
| 2366 | + println!("Boxed value: {}", unboxed); |
2438 | 2367 | } |
2439 | 2368 | } |
2440 | 2369 |
|
2441 | | - let my_nested_enum = MyEnum::Variant2( |
2442 | | - false, |
2443 | | - MyRecordStruct { |
2444 | | - value1: 42, |
2445 | | - value2: "string", |
2446 | | - }, |
2447 | | - ); |
2448 | | - |
2449 | | - match my_nested_enum { |
2450 | | - MyEnum::Variant2( |
2451 | | - value1, |
2452 | | - MyRecordStruct { |
2453 | | - value1: x, |
2454 | | - value2: y, |
2455 | | - }, |
2456 | | - ) => { |
2457 | | - let a = value1; // $ type=a:bool |
2458 | | - let b = x; // $ type=b:i32 |
2459 | | - let c = y; // $ type=c:&T.str |
2460 | | - (); |
| 2370 | + // Nested box pattern |
| 2371 | + let nested_box = Box::new(Box::new(42i32)); // $ method=new |
| 2372 | + match nested_box { |
| 2373 | + box box x => { |
| 2374 | + let nested_unboxed = x; // $ MISSING: type=nested_unboxed:i32 |
| 2375 | + println!("Nested boxed: {}", nested_unboxed); |
2461 | 2376 | } |
2462 | | - _ => (), |
2463 | 2377 | } |
2464 | | - |
2465 | | - let opt1 = Some(Default::default()); // $ type=opt1:T.i32 method=default |
2466 | | - #[rustfmt::skip] |
2467 | | - let _ = if let Some::<i32>(x) = opt1 |
2468 | | - { |
2469 | | - x; // $ type=x:i32 |
2470 | | - }; |
2471 | | - |
2472 | | - let opt2 = Some(Default::default()); // $ type=opt2:T.i32 method=default |
2473 | | - #[rustfmt::skip] |
2474 | | - let _ = if let Option::Some::<i32>(x) = opt2 |
2475 | | - { |
2476 | | - x; // $ type=x:i32 |
2477 | | - }; |
2478 | | - |
2479 | | - let opt3 = Some(Default::default()); // $ type=opt3:T.i32 method=default |
2480 | | - #[rustfmt::skip] |
2481 | | - let _ = if let Option::<i32>::Some(x) = opt3 |
2482 | | - { |
2483 | | - x; // $ type=x:i32 |
2484 | | - }; |
2485 | | - |
2486 | | - None |
2487 | 2378 | } |
2488 | 2379 | } |
2489 | 2380 |
|
@@ -2515,5 +2406,6 @@ fn main() { |
2515 | 2406 | method_determined_by_argument_type::f(); // $ method=f |
2516 | 2407 | tuples::f(); // $ method=f |
2517 | 2408 | dereference::test(); // $ method=test |
2518 | | - pattern_matching::f(); // $ method=f |
| 2409 | + pattern_matching::test_all_patterns(); // $ method=test_all_patterns |
| 2410 | + pattern_matching_experimental::box_patterns(); // $ method=box_patterns |
2519 | 2411 | } |
0 commit comments