Search code examples
rustlanguage-lawyerdynamic-dispatch

Error [E0433] when `dyn` used with absolute path


I was playing with a dyn traits introduced in Rust 1.27 and stumbled into this compiler error:

error[E0433]: failed to resolve. Use of undeclared type or module `dyn`
 --> src\main.rs:1:30
  |
1 | fn _run0() -> Result<(), Box<dyn ::std::error::Error>> { Ok(()) }
  |                              ^^^ Use of undeclared type or module `dyn`

All other variants compile fine:

fn _run0() -> Result<(), Box<dyn ::std::error::Error>> { Ok(()) } // Error

fn _run1() -> Result<(), Box<dyn std::error::Error>> { Ok(()) } // Ok

fn _run2() -> Result<(), Box<::std::error::Error>> { Ok(()) } // Ok

Is it intended behavior?


rustc 1.27.0 (3eda71b00 2018-06-19)


Solution

  • This is a backwards compatibility "gotcha" of the fact that dyn is a contextual keyword. Before the new syntax was added, you can write this code which uses dyn as a module name:

    mod dyn {
        pub trait Error {}
    }
    
    fn example() -> Box<dyn ::Error> {
    //                     ^ space doesn't matter
        unimplemented!()
    }
    

    This cannot stop compiling, so it must be parsed as a path component.

    You can add parenthesis to be explicit:

    fn example() -> Box<dyn (::dyn::Error)> { /* ... */ }
    

    In the 2018 edition, you can use crate at the beginning of a path:

    fn example() -> Box<dyn crate::dyn::Error> { /* ... */ }