Skip to content

Rustc can't check constness of foreign globals #9866

Closed

Description

@LeoTestard

The following code cause Rustc to fail:

extern
{
    static foo: u64;
}

static bar: u64 = foo;

fn main()
{
}

With the following message :

task '<unnamed>' failed at 'const not bound to an item', /Users/leo/Downloads/rust/src/librustc/middle/check_const.rs:269
error: internal compiler error: unexpected failure
note: the compiler hit an unexpected failure path. this is a bug
note: try running with RUST_LOG=rustc=1 to get further details and report the results to github.com/mozilla/rust/issues
task '<unnamed>' failed at 'explicit failure', /Users/leo/Downloads/rust/src/librustc/rustc.rs:391

Shouldn't Rustc be able to compile this code ? Since the foreign global is static, it should be possible to use it in constant expressions, juste like any static declared in Rust code.

Activity

  1. nikomatsakis commented on Oct 15, 2013

    @nikomatsakis
    Contributor

    We should fail in a more graceful way, but I suspect we should still
    fail. Rust statics can be evaluated at compilation time, but C statics
    generally cannot, they are just memory locations basically. I guess
    it's plausible that if the C library were available we could inspect
    it's read-only segment in some cases, but that sounds very
    complicated.

  2. LeoTestard commented on Oct 15, 2013

    @LeoTestard
    ContributorAuthor

    Yeah you're right.
    And no, I don't think extract the value from the C library is a viable thing, the extern address could as well not be available until link-time (if it is declared in a linker script, or whatever).
    I'm realizing this issue was stupid, foreign globals cannot be reliably evaluated at compile-time.
    But still, the compiler should report a proper error message like "Impossible to use foreign global in a constant expression context" rather than failing.

  3. huonw commented on May 15, 2014

    @huonw
    Contributor

    Closing as a dupe of the more modern #14227 (since the compiler behaviour has changed).

  4. added a commit that references this issue on Dec 1, 2022
    1cdb06d
  5. added a commit that references this issue on Aug 21, 2026
    6eecd84
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-codegenI-ICE

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions