Skip to content

Commit

Permalink
typeck: Fix for rust-lang#932
Browse files Browse the repository at this point in the history
  • Loading branch information
boggle committed Dec 5, 2011
1 parent f1eb7ce commit a69eab1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/comp/middle/typeck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2116,6 +2116,20 @@ fn check_expr_with_unifier(fcx: @fn_ctxt, expr: @ast::expr, unify: unifier,
let t_1 = ast_ty_to_ty_crate(fcx.ccx, t);
let t_e = expr_ty(tcx, e);

if ty::type_is_nil(tcx, t_e) {
tcx.sess.span_err(expr.span,
"cast from nil: " +
ty_to_str(tcx, expr_ty(tcx, e)) + " as " +
ty_to_str(tcx, t_1));
}

if ty::type_is_nil(tcx, t_1) {
tcx.sess.span_err(expr.span,
"cast to nil: " +
ty_to_str(tcx, expr_ty(tcx, e)) + " as " +
ty_to_str(tcx, t_1));
}

// FIXME there are more forms of cast to support, eventually.
if !( type_is_scalar(fcx, expr.span, t_e)
&& type_is_scalar(fcx, expr.span, t_1)) {
Expand Down
2 changes: 2 additions & 0 deletions src/test/compile-fail/cast-from-nil.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// error-pattern: cast from nil: () as u32
fn main() { let u = (assert true) as u32; }
2 changes: 2 additions & 0 deletions src/test/compile-fail/cast-to-nil.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// error-pattern: cast to nil: u32 as ()
fn main() { let u = 0u32 as (); }

0 comments on commit a69eab1

Please sign in to comment.