Skip to content

rand fail to compile on version 0.8.7 #1824

Description

@rodrigopedra

Common issues

Problem:

PR #1804 backported #1790 to the 0.8 branch.

After that, trying to compile v0.8.7 fails on openSUSE Tumbleweed.

Quick solution:

Change std::char::MAX to char::MAX on

if sampler.max() > std::char::MAX as u32 - CHAR_SURROGATE_LEN {

Details:

I use a Rust program called Mago. It is a tool for PHP code analysis, and it uses rand as a dependency.

Since Mago v1.44, compiling the rand crate when updating Mago using:

cargo-install-update install-update mago

Failed with the following error:

   Compiling rand v0.8.7
error[E0433]: cannot find module or crate `std` in this scope
   --> /home/rodrigo/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/rand-0.8.7/src/distributions/uniform.rs:605:24
    |
605 |     if sampler.max() > std::char::MAX as u32 - CHAR_SURROGATE_LEN {
    |                        ^^^ use of unresolved module or unlinked crate `std`
    |
    = help: if you wanted to use a crate named `std`, use `cargo add std` to add it to your `Cargo.toml`
help: consider importing this module
    |
109 + use core::char;
    |
help: if you import `char`, refer to it directly
    |
605 -     if sampler.max() > std::char::MAX as u32 - CHAR_SURROGATE_LEN {
605 +     if sampler.max() > char::MAX as u32 - CHAR_SURROGATE_LEN {
    |

I also tried compiling it on a fresh openSUSE install within a VM to rule out any custom config I could have on my main machine and got the same error.

As the original change in PR #1790 doesn't have the std:: namespace on the correspondent line:

if sampler.max() > char::MAX as u32 - CHAR_SURROGATE_LEN {

So I removed it from my local source copy at

/home/rodrigo/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/rand-0.8.7/src/distributions/uniform.rs:605

Resulting in:

    if sampler.max() > char::MAX as u32 - CHAR_SURROGATE_LEN {

And then running

cargo-install-update install-update mago

Succeeds in compiling both the crate and the Mago program.

I don't have any experience with Rust. Thus, I have no idea how to test this change properly, and I am also not sure if my "fix" is a proper fix or just masquerades the problem (by reading a constant from somewhere else not intended when removing the std:: namespace).

That is why I chose to open an issue and not send a PR.

If you need any further information about my setup or anything else, I am glad to provide it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions