|
@@ -3119,15 +3119,37 @@ and type_expr ctx (e,p) (with_type:with_type) =
|
|
|
| ECall (e,el) ->
|
|
|
type_call ctx e el with_type p
|
|
|
| ENew (t,el) ->
|
|
|
- let t = Typeload.load_instance ctx t p true in
|
|
|
- let ct = (match follow t with
|
|
|
+ let unify_constructor_call c params f ct = match follow ct with
|
|
|
+ | TFun (args,r) ->
|
|
|
+ (try
|
|
|
+ fst (unify_call_params ctx (Some (TInst(c,params),f)) el args r p false)
|
|
|
+ with Error (e,p) ->
|
|
|
+ display_error ctx (error_msg e) p;
|
|
|
+ [])
|
|
|
+ | _ ->
|
|
|
+ error "Constructor is not a function" p
|
|
|
+ in
|
|
|
+ let t = try
|
|
|
+ follow (Typeload.load_instance ctx t p true)
|
|
|
+ with Codegen.Generic_Exception _ ->
|
|
|
+ (* Try to infer generic parameters from the argument list (issue #2044) *)
|
|
|
+ match Typeload.load_type_def ctx p t with
|
|
|
+ | TClassDecl ({cl_constructor = Some cf} as c) ->
|
|
|
+ let monos = List.map (fun _ -> mk_mono()) c.cl_types in
|
|
|
+ let ct, f = get_constructor ctx c monos p in
|
|
|
+ ignore (unify_constructor_call c monos f ct);
|
|
|
+ Codegen.build_generic ctx c p monos
|
|
|
+ | mt ->
|
|
|
+ error ((s_type_path (t_infos mt).mt_path) ^ " cannot be constructed") p
|
|
|
+ in
|
|
|
+ let ct = (match t with
|
|
|
| TAbstract (a,pl) ->
|
|
|
(match a.a_impl with
|
|
|
| None -> t
|
|
|
| Some c -> TInst (c,pl))
|
|
|
| _ -> t
|
|
|
) in
|
|
|
- (match follow ct with
|
|
|
+ (match ct with
|
|
|
| TInst ({cl_kind = KTypeParameter tl} as c,params) ->
|
|
|
if not (Typeload.is_generic_parameter ctx c) then error "Only generic type parameters can be constructed" p;
|
|
|
let el = List.map (fun e -> type_expr ctx e Value) el in
|
|
@@ -3148,16 +3170,7 @@ and type_expr ctx (e,p) (with_type:with_type) =
|
|
|
(match f.cf_kind with
|
|
|
| Var { v_read = AccRequire (r,msg) } -> (match msg with Some msg -> error msg p | None -> error_require r p)
|
|
|
| _ -> ());
|
|
|
- let el = (match follow ct with
|
|
|
- | TFun (args,r) ->
|
|
|
- (try
|
|
|
- fst (unify_call_params ctx (Some (TInst(c,params),f)) el args r p false)
|
|
|
- with Error (e,p) ->
|
|
|
- display_error ctx (error_msg e) p;
|
|
|
- [])
|
|
|
- | _ ->
|
|
|
- error "Constructor is not a function" p
|
|
|
- ) in
|
|
|
+ let el = unify_constructor_call c params f ct in
|
|
|
(match c.cl_kind with
|
|
|
| KAbstractImpl a when not (Meta.has Meta.MultiType a.a_meta) ->
|
|
|
let ta = TAnon { a_fields = c.cl_statics; a_status = ref (Statics c) } in
|