Skip to content

Commit ed861c4

Browse files
committed
Fix ICE when casting on constant expressions. Closes rust-lang#9867
1 parent 2cb96a4 commit ed861c4

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

src/librustc/middle/trans/consts.rs

+16
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,22 @@ fn const_expr_unadjusted(cx: @mut CrateContext,
477477
(expr::cast_integral, expr::cast_pointer) => {
478478
llvm::LLVMConstIntToPtr(v, llty.to_ref())
479479
}
480+
(expr::cast_pointer, expr::cast_integral) => {
481+
// it's impossible to relocate a link-time constant if the
482+
// destination is not exactly of the same size than the pointer
483+
// if this is the case, emit an error rather than letting
484+
// LLVM report the error
485+
let bty = type_of::type_of(cx, basety);
486+
let csize = machine::llsize_of_alloc(cx, bty);
487+
let tsize = machine::llsize_of_alloc(cx, llty);
488+
if csize != tsize {
489+
cx.sess.span_fatal(e.span,
490+
"Cannot cast relocated expression to \
491+
a different size because its value is not known at \
492+
compile time");
493+
}
494+
llvm::LLVMConstPtrToInt(v, llty.to_ref())
495+
}
480496
_ => {
481497
cx.sess.impossible_case(e.span,
482498
"bad combination of types for cast")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn foo() {
12+
}
13+
14+
// should fail on both 32 and 64 bits archs
15+
static a: u16 = foo as u16;
16+
//~^ ERROR cannot cast relocated expression to a different size
17+
18+
fn main() {
19+
}

src/test/run-pass/const-ptr-cast.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn foo() {
12+
}
13+
14+
static a: uint = foo as uint;
15+
16+
fn main() {
17+
println!("{:u}", a);
18+
}

0 commit comments

Comments
 (0)