Skip to content

Commit 5b0c103

Browse files
committed
Make it possible to cast unsafe pointers with the 'as' operator
1 parent 0a20eed commit 5b0c103

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/comp/middle/trans.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -3269,12 +3269,13 @@ fn trans_cast(cx: @block_ctxt, e: @ast::expr, id: ast::node_id,
32693269
check (type_has_static_size(ccx, t_out));
32703270
let ll_t_out = type_of(ccx, e.span, t_out);
32713271

3272-
tag kind { native_; integral; float; other; }
3272+
tag kind { pointer; integral; float; other; }
32733273
fn t_kind(tcx: ty::ctxt, t: ty::t) -> kind {
32743274
ret if ty::type_is_fp(tcx, t) {
32753275
float
3276-
} else if ty::type_is_native(tcx, t) {
3277-
native_
3276+
} else if ty::type_is_native(tcx, t) ||
3277+
ty::type_is_unsafe_ptr(tcx, t) {
3278+
pointer
32783279
} else if ty::type_is_integral(tcx, t) {
32793280
integral
32803281
} else { other };
@@ -3301,13 +3302,13 @@ fn trans_cast(cx: @block_ctxt, e: @ast::expr, id: ast::node_id,
33013302
FPToSI(e_res.bcx, e_res.val, ll_t_out)
33023303
} else { FPToUI(e_res.bcx, e_res.val, ll_t_out) }
33033304
}
3304-
{in: integral., out: native_.} {
3305+
{in: integral., out: pointer.} {
33053306
IntToPtr(e_res.bcx, e_res.val, ll_t_out)
33063307
}
3307-
{in: native_., out: integral.} {
3308+
{in: pointer., out: integral.} {
33083309
PtrToInt(e_res.bcx, e_res.val, ll_t_out)
33093310
}
3310-
{in: native_., out: native_.} {
3311+
{in: pointer., out: pointer.} {
33113312
PointerCast(e_res.bcx, e_res.val, ll_t_out)
33123313
}
33133314
_ { ccx.sess.bug("Translating unsupported cast.") }

src/comp/middle/ty.rs

+8
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ export type_is_bot;
153153
export type_is_box;
154154
export type_is_boxed;
155155
export type_is_unique_box;
156+
export type_is_unsafe_ptr;
156157
export type_is_vec;
157158
export type_is_fp;
158159
export type_allows_implicit_copy;
@@ -885,6 +886,13 @@ pure fn type_is_unique_box(cx: ctxt, ty: t) -> bool {
885886
}
886887
}
887888

889+
pure fn type_is_unsafe_ptr(cx: ctxt, ty: t) -> bool {
890+
alt struct(cx, ty) {
891+
ty_ptr(_) { ret true; }
892+
_ { ret false; }
893+
}
894+
}
895+
888896
pure fn type_is_vec(cx: ctxt, ty: t) -> bool {
889897
ret alt struct(cx, ty) {
890898
ty_vec(_) { true }

0 commit comments

Comments
 (0)