rust-random / rust-random/rand

rand fail to compile on version 0.8.7

Open Beginner friendly
#1,824 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
2.1k
Forks
512
Avg merge
3d 9h
Merged PRs (30d)
7

Description

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

https://github.com/rust-random/rand/blob/90ff85ea8c31ad2b8a1ed664f251ec376967eb64/src/distributions/uniform.rs#L605

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:

https://github.com/rust-random/rand/blob/bb1262f703ca04e4ce56be78e1dc4e204cd6a998/src/distr/uniform_other.rs#L46

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at src/distributions/uniform.rs:605 and compare the corresponding change in src/distr/uniform_other.rs:46. Reproduce the rand v0.8.7 compilation failure with the cargo-install-update install-update mago command, then verify the corrected crate and Mago build compile successfully, including on the reported openSUSE environment.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
76/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.