Rust: Ok(T) is not an irrefutable pattern even when its type is Result<T, !>

Created on 7 May 2020  路  2Comments  路  Source: rust-lang/rust

I tried this code:
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=165744f32b174e1068e0813fe906735f

#![feature(never_type)]
struct A{n: usize}

fn a() -> Result<A, !> { Ok(A{n: 5}) }

fn main(){
  let Ok(A{ n }) = a();
}

I expected the code to compile, because Err is obviously an irrefutable pattern here.

I'm not even sure this is a bug (maybe rust's typesystem cannot understand that n inconstructible pattern on a n + 1 variants enum make the last pattern irrefutable ?)

C-bug

Most helpful comment

The example code compiles with #![feature(exhaustive_patterns)]

All 2 comments

The example code compiles with #![feature(exhaustive_patterns)]

Oh i wasn't aware of this feature, tyvm, i'm closing this 馃槃

Was this page helpful?
0 / 5 - 0 ratings