Skip to content

Commit cc7f844

Browse files
committed
Add a UI test with code that only works with homogeneous try
1 parent 86c3ba7 commit cc7f844

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//@ check-pass
2+
//@ edition: 2018
3+
4+
#![feature(try_blocks)]
5+
#![crate_type = "lib"]
6+
7+
// A few examples of code that only works with homogeneous `try`
8+
9+
pub fn index_or_zero(x: &[i32], i: usize, j: usize) -> i32 {
10+
// With heterogeneous `try` this fails because
11+
// it tries to call a method on a type variable.
12+
try { x.get(i)? + x.get(j)? }.unwrap_or(0)
13+
}
14+
15+
pub fn do_nothing_on_errors(a: &str, b: &str) {
16+
// With heterogeneous `try` this fails because
17+
// an underscore pattern doesn't constrain the output type.
18+
let _ = try {
19+
let a = a.parse::<i32>()?;
20+
let b = b.parse::<u32>()?;
21+
println!("{a} {b}");
22+
};
23+
}
24+
25+
pub fn print_error_once(a: &str, b: &str) {
26+
match try {
27+
let a = a.parse::<i32>()?;
28+
let b = b.parse::<u32>()?;
29+
(a, b)
30+
} {
31+
Ok(_pair) => {}
32+
// With heterogeneous `try` this fails because
33+
// nothing constrains the error type in `e`.
34+
Err(e) => eprintln!("{e}"),
35+
}
36+
}

0 commit comments

Comments
 (0)