Bläddra i källkod

[typer] constrain-mono all the things

closes #9556
Simon Krajewski 5 år sedan
förälder
incheckning
132c899e46

+ 1 - 1
src/typing/calls.ml

@@ -530,7 +530,7 @@ let rec acc_get ctx g p =
 				let f = match c.cl_kind with
 					| KAbstractImpl a when Meta.has Meta.Enum a.a_meta ->
 						(* Enum abstracts have to apply their type parameters because they are basically statics with type params (#8700). *)
-						let monos = List.map (fun _ -> mk_mono()) a.a_params in
+						let monos = Monomorph.spawn_constrained_monos (fun t -> t) a.a_params in
 						apply_params a.a_params monos;
 					| _ -> (fun t -> t)
 				in

+ 4 - 3
src/typing/matcher.ml

@@ -343,8 +343,9 @@ module Pattern = struct
 				let e1 = type_expr ctx e1 (WithType.with_type t) in
 				begin match e1.eexpr,follow e1.etype with
 					| TField(_, FEnum(en,ef)),TFun(_,TEnum(_,tl)) ->
-						let monos = List.map (fun _ -> mk_mono()) ef.ef_params in
-						let map t = apply_params en.e_params tl (apply_params ef.ef_params monos t) in
+						let map = apply_params en.e_params tl in
+						let monos = Monomorph.spawn_constrained_monos map ef.ef_params in
+						let map t = map (apply_params ef.ef_params monos t) in
 						unify ctx (map ef.ef_type) e1.etype e1.epos;
 						let args = match follow e1.etype with
 							| TFun(args,r) ->
@@ -959,7 +960,7 @@ module Compile = struct
 	let rec get_sub_subjects mctx e con arg_positions =
 		match fst con with
 		| ConEnum(en,ef) ->
-			let tl = List.map (fun _ -> mk_mono()) en.e_params in
+			let tl = Monomorph.spawn_constrained_monos (fun t -> t) en.e_params in
 			let t_en = TEnum(en,tl) in
 			let e = if not (type_iseq t_en e.etype) then mk (TCast(e,None)) t_en e.epos else e in
 			begin match follow ef.ef_type with

+ 2 - 2
src/typing/typeloadFields.ml

@@ -846,7 +846,7 @@ let bind_var (ctx,cctx,fctx) cf e =
 					let e = require_constant_expression e "Inline variable initialization must be a constant value" in
 					begin match c.cl_kind with
 						| KAbstractImpl a when Meta.has Meta.Enum cf.cf_meta && Meta.has Meta.Enum a.a_meta ->
-							unify ctx t (TAbstract(a,(List.map (fun _ -> mk_mono()) a.a_params))) p;
+							unify ctx t (TAbstract(a,(Monomorph.spawn_constrained_monos (fun t -> t) a.a_params))) p;
 							let e1 = match e.eexpr with TCast(e1,None) -> e1 | _ -> e in
 							unify ctx e1.etype a.a_this e1.epos
 						| _ ->
@@ -905,7 +905,7 @@ let check_abstract (ctx,cctx,fctx) c cf fd t ret p =
 	match cctx.abstract with
 		| Some a ->
 			let m = mk_mono() in
-			let ta = TAbstract(a, List.map (fun _ -> mk_mono()) a.a_params) in
+			let ta = TAbstract(a,Monomorph.spawn_constrained_monos (fun t -> t) a.a_params) in
 			let tthis = if fctx.is_abstract_member || Meta.has Meta.To cf.cf_meta then monomorphs a.a_params a.a_this else a.a_this in
 			let allows_no_expr = ref (Meta.has Meta.CoreType a.a_meta) in
 			let rec loop ml =

+ 2 - 2
src/typing/typer.ml

@@ -632,7 +632,7 @@ let rec type_binop ctx op e1 e2 is_assign_op with_type p =
 			]) t p
 		| AKUsing(ef,c,cf,et,_) ->
 			(* abstract setter + getter *)
-			let ta = match c.cl_kind with KAbstractImpl a -> TAbstract(a, List.map (fun _ -> mk_mono()) a.a_params) | _ -> die "" __LOC__ in
+			let ta = match c.cl_kind with KAbstractImpl a -> TAbstract(a,Monomorph.spawn_constrained_monos (fun t -> t) a.a_params) | _ -> die "" __LOC__ in
 			let ret = match follow ef.etype with
 				| TFun([_;_],ret) -> ret
 				| _ -> error "Invalid field type for abstract setter" p
@@ -1396,7 +1396,7 @@ and type_access ctx e p mode =
 			| TTypeExpr (TClassDecl c) ->
 				if mode = MSet then error "Cannot set constructor" p;
 				if mode = MCall then error ("Cannot call constructor like this, use 'new " ^ (s_type_path c.cl_path) ^ "()' instead") p;
-				let monos = List.map (fun _ -> mk_mono()) (match c.cl_kind with KAbstractImpl a -> a.a_params | _ -> c.cl_params) in
+				let monos = Monomorph.spawn_constrained_monos (fun t -> t) (match c.cl_kind with KAbstractImpl a -> a.a_params | _ -> c.cl_params) in
 				let ct, cf = get_constructor ctx c monos p in
 				check_constructor_access ctx c cf p;
 				let args = match follow ct with TFun(args,ret) -> args | _ -> die "" __LOC__ in

+ 8 - 0
tests/misc/projects/Issue9556/Main.hx

@@ -0,0 +1,8 @@
+class Main<T:Int> {
+	static public function main() {
+		var f = Main.new;
+		f("foo");
+	}
+
+	function new(t:T) {}
+}

+ 1 - 0
tests/misc/projects/Issue9556/compile-fail.hxml

@@ -0,0 +1 @@
+-main Main

+ 3 - 0
tests/misc/projects/Issue9556/compile-fail.hxml.stderr

@@ -0,0 +1,3 @@
+Main.hx:4: characters 5-10 : Constraint check failure for Main.T
+Main.hx:4: characters 5-10 : String should be Int
+Main.hx:4: characters 5-10 : For function argument 't'