Skip to content

Commit

Permalink
accept monomorphs as type parameters for safe casts (closes #4675)
Browse files Browse the repository at this point in the history
  • Loading branch information
Simn committed Nov 26, 2015
1 parent 4560d28 commit 30cda52
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
14 changes: 14 additions & 0 deletions tests/unit/src/unit/issues/Issue4675.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package unit.issues;

class Issue4675 extends Test {
function test() {
var m: Map<String, Dynamic> = new Map();
m.set("a", 1);
eq(1, someFunction(m));
}

public static function someFunction(arg: Dynamic) {
var map: Map<String, Dynamic> = cast(arg, Map<String, Dynamic>);
return map["a"];
}
}
13 changes: 7 additions & 6 deletions typer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3587,21 +3587,22 @@ and type_expr ctx (e,p) (with_type:with_type) =
mk (TCast (e,None)) (mk_mono()) p
| ECast (e, Some t) ->
let t = Typeload.load_complex_type ctx (pos e) t in
let check_param pt = match follow pt with
| TMono _ -> () (* This probably means that Dynamic wasn't bound (issue #4675). *)
| t when t == t_dynamic -> ()
| _ ->error "Cast type parameters must be Dynamic" p
in
let rec loop t = match follow t with
| TInst (_,params) | TEnum (_,params) ->
List.iter (fun pt ->
if follow pt != t_dynamic then error "Cast type parameters must be Dynamic" p;
) params;
List.iter check_param params;
(match follow t with
| TInst (c,_) ->
(match c.cl_kind with KTypeParameter _ -> error "Can't cast to a type parameter" p | _ -> ());
TClassDecl c
| TEnum (e,_) -> TEnumDecl e
| _ -> assert false);
| TAbstract (a,params) when Meta.has Meta.RuntimeValue a.a_meta ->
List.iter (fun pt ->
if follow pt != t_dynamic then error "Cast type parameters must be Dynamic" p;
) params;
List.iter check_param params;
TAbstractDecl a
| TAbstract (a,params) ->
loop (Abstract.get_underlying_type a params)
Expand Down

0 comments on commit 30cda52

Please sign in to comment.