Skip to content

Cannot get a 'static reference to an explicit memory location #18267

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

Closed
simias opened this issue Oct 23, 2014 · 7 comments
Closed

Cannot get a 'static reference to an explicit memory location #18267

simias opened this issue Oct 23, 2014 · 7 comments

Comments

@simias
Copy link

simias commented Oct 23, 2014

I was playing around trying to get rust to let me get a reference to an explicit memory location (in order to be able to map HW registers in a nice way for the Zinc project) and ended up with that code:

struct S {
    some_stuff: u32,
}

const SOME_S: &'static S = unsafe { &*(0x100 as *const S) };

fn main() {
    println!("{}", SOME_S.some_stuff);
}

Which produces the following error:

rustc: /build/rust-git/src/rust/src/llvm/include/llvm/Support/Casting.h:237: typename llvm::cast_retty<X, Y*>::ret_type llvm::cast(Y*) [with X = llvm::GlobalVariable; Y = llvm::Value; typename llvm::cast_retty<X, Y*>::ret_type = llvm::GlobalVariable*]: Assertion isa(Val) && "cast() argument of incompatible type!"' failed.`

Casting the raw pointer in an intermediate function builds and runs fine however (assuming you have something mapped at 0x100 of course):

struct S {
    some_stuff: u32,
}

const SOME_S: *const S = 0x100 as *const S;

pub fn get_ref<T>(t: *const T) -> &'static T {
    unsafe {
        &*t
    }
}

fn main() {
    println!("{}", get_ref(SOME_S).some_stuff);
}
@mahkoh
Copy link
Contributor

mahkoh commented Oct 23, 2014

&*(0x100 as *const S)

Pointer arithmetic is not implemented. rust-lang/rfcs#259

@simias
Copy link
Author

simias commented Oct 23, 2014

Ah, this is unfortunate... I didn't expect pointer arithmetic was involved in that expression.

@simias
Copy link
Author

simias commented Oct 23, 2014

Should I close this issue right now? I feel like even if we don't consider the lack of pointer arithmetic in constant expression a bug in current rust the error message itself could be improved.

@kmcallister kmcallister added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. I-crash Issue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics. labels Oct 24, 2014
@alexcrichton alexcrichton added I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ and removed I-crash Issue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics. labels Mar 30, 2015
@alexcrichton
Copy link
Member

Updated error:

foo.rs:8:20: 8:26 error: internal compiler error: constant expression should not reach expr::trans_def
foo.rs:8     println!("{}", SOME_S.some_stuff);
                            ^~~~~~
note: in expansion of format_args!
<std macros>:2:25: 2:58 note: expansion site
<std macros>:1:1: 2:62 note: in expansion of print!
<std macros>:3:1: 3:54 note: expansion site
<std macros>:1:1: 3:58 note: in expansion of println!
foo.rs:8:5: 8:39 note: expansion site
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>', /home/rustbuild/src/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libsyntax/diagnostic.rs:130

@pmarcelll
Copy link
Contributor

This fails with a regular build error on the latest nightly:

test.rs:5:38: 5:58 error: raw pointers cannot be dereferenced in constants [E0396]
test.rs:5 const SOME_S: &'static S = unsafe { &*(0x100 as *const S) };
                                               ^~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error

@arielb1
Copy link
Contributor

arielb1 commented Jun 25, 2015

@pmarcelll

Right. However, we may still want to make that code work somehow.

@arielb1 arielb1 changed the title llvm assertion fails when attempting to get a 'static reference to an explicit memory location Cannot get a 'static reference to an explicit memory location Jun 25, 2015
@arielb1 arielb1 removed A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ labels Jun 25, 2015
@steveklabnik
Copy link
Member

Given that this would add a new feature, and now has a regular error, I'm going to close it in favor of an RFC. Thanks!

lnicola pushed a commit to lnicola/rust that referenced this issue Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants