Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rustc can't check constness of foreign globals #9866

Closed
LeoTestard opened this issue Oct 15, 2013 · 3 comments
Closed

Rustc can't check constness of foreign globals #9866

LeoTestard opened this issue Oct 15, 2013 · 3 comments
Labels
A-codegen Area: Code generation I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️

Comments

@LeoTestard
Copy link
Contributor

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.

@nikomatsakis
Copy link
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.

@LeoTestard
Copy link
Contributor Author

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.

@huonw
Copy link
Member

huonw commented May 15, 2014

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

@huonw huonw closed this as completed May 15, 2014
flip1995 pushed a commit to flip1995/rust that referenced this issue Dec 1, 2022
Fix ICE in `unused_rounding`

fixes rust-lang#9866
changelog: `unused_rounding`: Fix ICE when using the `_` separator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-codegen Area: Code generation I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️
Projects
None yet
Development

No branches or pull requests

4 participants