Skip to content

Commit 50277ec

Browse files
committed
Permit casting region pointers to unsafe ones.
1 parent 2bc301f commit 50277ec

File tree

8 files changed

+142
-3
lines changed

8 files changed

+142
-3
lines changed

src/librustc/middle/check_const.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ pub fn check_expr(sess: Session,
107107
expr_lit(_) => (),
108108
expr_cast(_, _) => {
109109
let ety = ty::expr_ty(tcx, e);
110-
if !ty::type_is_numeric(ety) {
110+
if !ty::type_is_numeric(ety) && !ty::type_is_unsafe_ptr(ety) {
111111
sess.span_err(e.span, ~"can not cast to `" +
112112
ppaux::ty_to_str(tcx, ety) +
113113
~"` in a constant expression");

src/librustc/middle/trans/consts.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ pub fn const_autoderef(cx: @CrateContext, ty: ty::t, v: ValueRef)
110110
let mut v1 = v;
111111
loop {
112112
// Only rptrs can be autoderef'ed in a const context.
113-
match ty::get(ty).sty {
113+
match ty::get(t1).sty {
114114
ty::ty_rptr(_, mt) => {
115115
t1 = mt.ty;
116116
v1 = const_deref(cx, v1);
@@ -338,6 +338,9 @@ fn const_expr_unchecked(cx: @CrateContext, e: @ast::expr) -> ValueRef {
338338
integral or float")
339339
}
340340
}
341+
(expr::cast_pointer, expr::cast_pointer) => {
342+
llvm::LLVMConstPointerCast(v, llty)
343+
}
341344
_ => {
342345
cx.sess.impossible_case(e.span,
343346
~"bad combination of types for cast")

src/librustc/middle/typeck/check/mod.rs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ use middle::typeck::check::method::TransformTypeNormally;
9393
use middle::typeck::check::regionmanip::replace_bound_regions_in_fn_sig;
9494
use middle::typeck::check::vtable::{LocationInfo, VtableContext};
9595
use middle::typeck::CrateCtxt;
96-
use middle::typeck::infer::{resolve_type, force_tvar};
96+
use middle::typeck::infer::{resolve_type, force_tvar, mk_eqty};
9797
use middle::typeck::infer;
9898
use middle::typeck::rscope::{binding_rscope, bound_self_region};
9999
use middle::typeck::rscope::{RegionError};
@@ -2452,6 +2452,44 @@ pub fn check_expr_with_unifier(fcx: @mut FnCtxt,
24522452
let t_1_is_scalar = type_is_scalar(fcx, expr.span, t_1);
24532453
if type_is_c_like_enum(fcx,expr.span,t_e) && t_1_is_scalar {
24542454
/* this case is allowed */
2455+
} else if type_is_region_ptr(fcx, expr.span, t_e) &&
2456+
type_is_unsafe_ptr(fcx, expr.span, t_1) {
2457+
2458+
fn is_vec(t: ty::t) -> bool {
2459+
match ty::get(t).sty {
2460+
ty::ty_evec(_,_) => true,
2461+
_ => false
2462+
}
2463+
}
2464+
fn types_compatible(fcx: @mut FnCtxt, sp: span, t1: ty::t,
2465+
t2: ty::t) -> bool {
2466+
if !is_vec(t1) {
2467+
false
2468+
} else {
2469+
let el = ty::sequence_element_type(fcx.tcx(), t1);
2470+
infer::mk_eqty(fcx.infcx(), false, sp, el, t2).is_ok()
2471+
}
2472+
}
2473+
2474+
// Due to the limitations of LLVM global constants,
2475+
// region pointers end up pointing at copies of
2476+
// vector elements instead of the original values.
2477+
// To allow unsafe pointers to work correctly, we
2478+
// need to special-case obtaining an unsafe pointer
2479+
// from a region pointer to a vector.
2480+
2481+
/* this cast is only allowed from &[T] to *T or
2482+
&T to *T. */
2483+
let te = structurally_resolved_type(fcx, e.span, t_e);
2484+
match (&ty::get(te).sty, &ty::get(t_1).sty) {
2485+
(&ty::ty_rptr(_, mt1), &ty::ty_ptr(mt2))
2486+
if types_compatible(fcx, e.span, mt1.ty, mt2.ty) => {
2487+
/* this case is allowed */
2488+
}
2489+
_ => {
2490+
demand::coerce(fcx, e.span, t_1, e);
2491+
}
2492+
}
24552493
} else if !(type_is_scalar(fcx,expr.span,t_e) && t_1_is_scalar) {
24562494
/*
24572495
If more type combinations should be supported than are
@@ -3081,6 +3119,16 @@ pub fn type_is_scalar(fcx: @mut FnCtxt, sp: span, typ: ty::t) -> bool {
30813119
return ty::type_is_scalar(typ_s);
30823120
}
30833121

3122+
pub fn type_is_unsafe_ptr(fcx: @mut FnCtxt, sp: span, typ: ty::t) -> bool {
3123+
let typ_s = structurally_resolved_type(fcx, sp, typ);
3124+
return ty::type_is_unsafe_ptr(typ_s);
3125+
}
3126+
3127+
pub fn type_is_region_ptr(fcx: @mut FnCtxt, sp: span, typ: ty::t) -> bool {
3128+
let typ_s = structurally_resolved_type(fcx, sp, typ);
3129+
return ty::type_is_region_ptr(typ_s);
3130+
}
3131+
30843132
pub fn type_is_c_like_enum(fcx: @mut FnCtxt, sp: span, typ: ty::t) -> bool {
30853133
let typ_s = structurally_resolved_type(fcx, sp, typ);
30863134
return ty::type_is_c_like_enum(fcx.ccx.tcx, typ_s);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2012 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 main() {
12+
let foo = ['h' as u8, 'i' as u8, 0 as u8];
13+
let bar = &foo as *u8; //~ ERROR mismatched types
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2012 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+
const a: &static/str = &"foo";
12+
const b: *u8 = a as *u8; //~ ERROR non-scalar cast
13+
const c: *u8 = &a as *u8; //~ ERROR mismatched types
14+
15+
fn main() {
16+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2012 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+
const a: [u8 * 3] = ['h' as u8, 'i' as u8, 0 as u8];
12+
const b: *i8 = &a as *i8; //~ ERROR mismatched types
13+
14+
fn main() {
15+
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2012 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+
extern fn foo() {}
12+
13+
const x: *u8 = foo;
14+
const y: *libc::c_void = x as *libc::c_void;
15+
const a: &static/int = &10;
16+
const b: *int = a as *int;
17+
18+
fn main() {
19+
assert x as *libc::c_void == y;
20+
assert a as *int == b;
21+
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2012 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+
const a: [u8 * 3] = ['h' as u8, 'i' as u8, 0 as u8];
12+
const c: &static/[u8 * 3] = &a;
13+
const b: *u8 = c as *u8;
14+
15+
fn main() {
16+
let foo = &a as *u8;
17+
assert unsafe { str::raw::from_bytes(a) } == ~"hi\x00";
18+
assert unsafe { str::raw::from_buf(foo) } == ~"hi";
19+
assert unsafe { str::raw::from_buf(b) } == ~"hi";
20+
assert unsafe { *b == a[0] };
21+
assert unsafe { *(&c[0] as *u8) == a[0] };
22+
}

0 commit comments

Comments
 (0)