Skip to content

Compiler "suggests" boxing type to access method that is not callable without bringing trait into scope #84812

Closed
@BoxyUwU

Description

@BoxyUwU

Given the following code: link

fn main() {
    let mut rng = rand::thread_rng();
    // uncomment me for Box<Box<ThreadRng>> error
    //let mut rng = Box::new(rng);
    rng.gen::<u32>();
}

The current output is:

error[E0599]: no method named `gen` found for struct `ThreadRng` in the current scope
  --> src/main.rs:5:9
   |
5  |     rng.gen::<u32>();
   |         ^^^ method not found in `ThreadRng`
   | 
  ::: /playground/.cargo/registry/src/github.com-1ecc6299db9ec823/rand-0.8.3/src/rng.rs:91:8
   |
91 |     fn gen<T>(&mut self) -> T
   |        --- the method is available for `Box<ThreadRng>` here
   |
   = help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
   |
1  | use rand::Rng;
   |

uncommenting the let mut rng = Box::new(rng); line gives the following output:

error[E0599]: no method named `gen` found for struct `Box<ThreadRng>` in the current scope
  --> src/main.rs:5:9
   |
5  |     rng.gen::<u32>();
   |         ^^^ method not found in `Box<ThreadRng>`
   | 
  ::: /playground/.cargo/registry/src/github.com-1ecc6299db9ec823/rand-0.8.3/src/rng.rs:91:8
   |
91 |     fn gen<T>(&mut self) -> T
   |        --- the method is available for `Box<Box<ThreadRng>>` here
   |
   = help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
   |
1  | use rand::Rng;
   |

and this just keeps adding more Box<Box<Box<Box<Box<...<ThreadRng>>>>>>>>> forever if you keep boxing in an attempt to access the "available method" 😆

@estebank

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions