Skip to content

Global const expr with macro causes compiler panic #29093

@leeopop

Description

@leeopop

I would like to have compile-time evaluation of my const values
from the environment variables.
I wonder whether I had some grammar mistakes, however the compiler panics.
This occurs all stable, beta, and nightly versions (from play.rust-lang.org).

I tried this code:

const DEFAULT_PACKET_BUFFER_SIZE : usize = 2048;

pub const PACKET_BUFFER_SIZE : usize = match option_env!("PACKET_BUFFER_SIZE") {
    None => DEFAULT_PACKET_BUFFER_SIZE,
    Some(num_str) => {
        match num_str.parse::<usize>() {
            Ok(num) => {
                match num.is_power_of_two() {
                    true => num,
                    _ => DEFAULT_PACKET_BUFFER_SIZE,
                }
                },
            _ => { DEFAULT_PACKET_BUFFER_SIZE },
        }
    }
};

fn main()
{
    println!("{}",DEFAULT_PACKET_BUFFER_SIZE);
}

I expected to see this happen:
compiles with environment variables, statically evaluated.

Instead, this happened:
compiler panic.

Meta

PS C:\workspace\git\FPS\src> cargo build --verbose
       Fresh winapi v0.2.4
       Fresh rustc-serialize v0.3.16
       Fresh libc v0.1.10
       Fresh winapi-build v0.1.1
       Fresh advapi32-sys v0.1.2
       Fresh rand v0.3.11
       Fresh num v0.1.27
   Compiling FPS v0.0.0 (file:///C:/workspace/git/FPS)
     Running `rustc lib.rs --crate-name fps --crate-type lib -g --out-dir C:\workspace\git\FPS\target\debug --emit=dep-i
nfo,link -L dependency=C:\workspace\git\FPS\target\debug -L dependency=C:\workspace\git\FPS\target\debug\deps --extern i
log2=C:\workspace\git\FPS\target\debug\deps\libilog2-d6c9b6aa20d924f8.rlib`
       Fresh ilog2 v0.2.2
N\env.rs:5:10: 5:17 error: internal compiler error: no enclosing scope found for scope: Misc(13)
N\env.rs:5     Some(num_str) => {
                    ^~~~~~~
note: the compiler unexpectedly panicked. this is a bug.
note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports
note: run with `RUST_BACKTRACE=1` for a backtrace
thread 'rustc' panicked at 'Box<Any>', ../src/libsyntax\diagnostic.rs:176


Could not compile `FPS`.

Caused by:
  Process didn't exit successfully: `rustc lib.rs --crate-name fps --crate-type lib -g --out-dir C:\workspace\git\FPS\ta
rget\debug --emit=dep-info,link -L dependency=C:\workspace\git\FPS\target\debug -L dependency=C:\workspace\git\FPS\targe
t\debug\deps --extern ilog2=C:\workspace\git\FPS\target\debug\deps\libilog2-d6c9b6aa20d924f8.rlib` (exit code: 101)
PS C:\workspace\git\FPS\src>
PS C:\workspace\git\FPS\src> rustc --version --verbose
rustc 1.3.0 (9a92aaf19 2015-09-15)
binary: rustc
commit-hash: 9a92aaf19a64603b02b4130fe52958cc12488900
commit-date: 2015-09-15
host: x86_64-pc-windows-gnu
release: 1.3.0

http://puu.sh/kM1Yo/036518bf32.png

Metadata

Metadata

Assignees

No one assigned

    Labels

    I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️T-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