gccrs icon indicating copy to clipboard operation
gccrs copied to clipboard

Rust result type requires lang items

Open philberty opened this issue 3 years ago • 3 comments

I tried this code:

pub enum Result<T, E> {
    #[lang = "Ok"]
    Ok(T),

    #[lang = "Err"]
    Err(E),
}

fn main() {
    let result = Ok(123);
    let err = Err(false);
}

I expected to see this happen: compile without error Instead, this happened:

<source>:10:18: error: Cannot find path 'Ok' in this scope
   10 |     let result = Ok(123);
      |                  ^
<source>:11:15: error: Cannot find path 'Err' in this scope
   11 |     let err = Err(false);
      |               ^

Meta

  • What version of Rust GCC were you using, git sha if possible. 69f6be3ee483c9895b4b5187a44b3e1c8be2ba63

philberty avatar Jan 04 '22 14:01 philberty

This is not because of the lang items. This is because Ok and Err are re-exported separately in the prelude: https://github.com/rust-lang/rust/blob/2b681ac06b1a6b7ea39525e59363ffee0d1a68e5/library/core/src/prelude/v1.rs#L44

bjorn3 avatar Jan 04 '22 14:01 bjorn3

Ah, brilliant news. Now I need to know why are they defined as lang items?

philberty avatar Jan 04 '22 14:01 philberty

They are used by expr_ok and expr_try which seem to only be used by the deprecated RustcEncodable and RustcDecodable derive macros: https://github.com/rust-lang/rust/blob/2b681ac06b1a6b7ea39525e59363ffee0d1a68e5/compiler/rustc_expand/src/build.rs#L348-L357

bjorn3 avatar Jan 04 '22 17:01 bjorn3