about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorStefan Plantikow <stefan.plantikow@googlemail.com>2011-11-30 17:58:08 +0100
committerGraydon Hoare <graydon@mozilla.com>2011-12-02 11:38:24 -0800
commit29f7cdffa4688aa539be4f0f6d2eab7f5af8a2d6 (patch)
tree47a1d7d795e90841d4f38b3566616d36fe0b8069 /src
parentd116a6f2d39d6fe9484b5c1b5f818756a78fd3bc (diff)
downloadrust-29f7cdffa4688aa539be4f0f6d2eab7f5af8a2d6.tar.gz
rust-29f7cdffa4688aa539be4f0f6d2eab7f5af8a2d6.zip
ty: trans: added support for dropping trivial casts
Diffstat (limited to 'src')
-rw-r--r--src/comp/middle/trans.rs9
-rw-r--r--src/comp/middle/ty.rs10
-rw-r--r--src/comp/middle/typeck.rs10
-rw-r--r--src/comp/syntax/ast_util.rs9
4 files changed, 33 insertions, 5 deletions
diff --git a/src/comp/middle/trans.rs b/src/comp/middle/trans.rs
index 237b0089953..52eae92111f 100644
--- a/src/comp/middle/trans.rs
+++ b/src/comp/middle/trans.rs
@@ -4086,7 +4086,14 @@ fn trans_expr(bcx: @block_ctxt, e: @ast::expr, dest: dest) -> @block_ctxt {
         if !ty::expr_is_lval(tcx, a) { ret trans_expr(bcx, a, dest); }
         else { ret lval_to_dps(bcx, a, dest); }
       }
-      ast::expr_cast(val, _) { ret trans_cast(bcx, val, e.id, dest); }
+      ast::expr_cast(val, _) {
+        alt tcx.cast_map.find(e.id) {
+          option::none. { ret trans_cast(bcx, val, e.id, dest); }
+          some { alt option::get(some) {
+            ty::triv_cast. { ret trans_expr(bcx, val, dest); }
+          } }
+        }
+      }
       ast::expr_anon_obj(anon_obj) {
         ret trans_anon_obj(bcx, e.span, anon_obj, e.id, dest);
       }
diff --git a/src/comp/middle/ty.rs b/src/comp/middle/ty.rs
index 338a60d0fc7..e775fee1410 100644
--- a/src/comp/middle/ty.rs
+++ b/src/comp/middle/ty.rs
@@ -32,6 +32,7 @@ export ast_constr_to_constr;
 export bind_params_in_type;
 export block_ty;
 export constr;
+export cast_type;
 export constr_general;
 export constr_table;
 export count_ty_params;
@@ -102,6 +103,7 @@ export substitute_type_params;
 export t;
 export tag_variants;
 export tag_variant_with_id;
+export triv_cast;
 export triv_eq_ty;
 export ty_param_substs_opt_and_ty;
 export ty_param_kinds_and_ty;
@@ -201,6 +203,12 @@ type mt = {ty: t, mut: ast::mutability};
 // the types of AST nodes.
 type creader_cache = hashmap<{cnum: int, pos: uint, len: uint}, ty::t>;
 
+tag cast_type {
+    /* cast may be ignored after substituting primitive with machine types
+       since expr already has the right type */
+    triv_cast;
+}
+
 type ctxt =
     //        constr_table fn_constrs,
     // We need the ext_map just for printing the types of tags defined in
@@ -209,6 +217,7 @@ type ctxt =
       sess: session::session,
       def_map: resolve::def_map,
       ext_map: resolve::ext_map,
+      cast_map: hashmap<ast::node_id, cast_type>,
       node_types: node_type_table,
       items: ast_map::map,
       freevars: freevars::freevar_map,
@@ -396,6 +405,7 @@ fn mk_ctxt(s: session::session, dm: resolve::def_map,
           sess: s,
           def_map: dm,
           ext_map: em,
+          cast_map: ast_util::new_node_hash(),
           node_types: ntt,
           items: amap,
           freevars: freevars,
diff --git a/src/comp/middle/typeck.rs b/src/comp/middle/typeck.rs
index ae26d10dce8..f363ca62b27 100644
--- a/src/comp/middle/typeck.rs
+++ b/src/comp/middle/typeck.rs
@@ -2101,15 +2101,19 @@ fn check_expr_with_unifier(fcx: @fn_ctxt, expr: @ast::expr, unify: unifier,
       ast::expr_cast(e, t) {
         bot = check_expr(fcx, e);
         let t_1 = ast_ty_to_ty_crate(fcx.ccx, t);
-        // FIXME: there are more forms of cast to support, eventually.
+        let t_e = expr_ty(tcx, e);
 
-        if !(type_is_scalar(fcx, expr.span, expr_ty(tcx, e)) &&
-                 type_is_scalar(fcx, expr.span, 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)) {
             tcx.sess.span_err(expr.span,
                               "non-scalar cast: " +
                                   ty_to_str(tcx, expr_ty(tcx, e)) + " as " +
                                   ty_to_str(tcx, t_1));
         }
+
+        if ty::triv_eq_ty(tcx, t_1, t_e)
+            { tcx.cast_map.insert(expr.id, ty::triv_cast); }
         write::ty_only_fixup(fcx, id, t_1);
       }
       ast::expr_vec(args, mut) {
diff --git a/src/comp/syntax/ast_util.rs b/src/comp/syntax/ast_util.rs
index d8b52300e91..3129c28d4fa 100644
--- a/src/comp/syntax/ast_util.rs
+++ b/src/comp/syntax/ast_util.rs
@@ -1,4 +1,4 @@
-import std::{str, option};
+import std::{str, option, int, map};
 import codemap::span;
 import ast::*;
 
@@ -6,6 +6,13 @@ fn respan<copy T>(sp: span, t: T) -> spanned<T> {
     ret {node: t, span: sp};
 }
 
+fn new_node_hash<copy V>() -> map::hashmap<node_id, V> {
+    fn node_id_hash(&&i: node_id) -> uint { ret int::hash(i as int); }
+    fn node_id_eq(&&a: node_id, &&b: node_id) -> bool
+        { ret int::eq(a as int, b as int); }
+    ret map::mk_hashmap(node_id_hash, node_id_eq);
+}
+
 /* assuming that we're not in macro expansion */
 fn mk_sp(lo: uint, hi: uint) -> span {
     ret {lo: lo, hi: hi, expanded_from: codemap::os_none};