Browse Source

[typer] fix various convert_type situations

closes #6063
Simon Krajewski 3 years ago
parent
commit
ee349c0071
2 changed files with 72 additions and 3 deletions
  1. 37 3
      src/core/tOther.ml
  2. 35 0
      tests/unit/src/unit/issues/Issue6063.hx

+ 37 - 3
src/core/tOther.ml

@@ -35,20 +35,54 @@ module TExprToExpr = struct
 		| TAbstract (a,pl) ->
 			tpath a.a_path a.a_module.m_path (List.map tparam pl)
 		| TFun (args,ret) ->
-			CTFunction (List.map (fun (_,_,t) -> convert_type' t) args, (convert_type' ret))
+			CTFunction (List.map (fun (n,o,t) ->
+				let ct = convert_type' t in
+					let ct = if n = "" then ct else CTNamed((n,null_pos),ct),null_pos in
+					if o then CTOptional ct,null_pos else ct
+				) args, (convert_type' ret))
 		| TAnon a ->
 			begin match !(a.a_status) with
 			| Statics c -> tpath ([],"Class") ([],"Class") [TPType (tpath c.cl_path c.cl_path [],null_pos)]
 			| EnumStatics e -> tpath ([],"Enum") ([],"Enum") [TPType (tpath e.e_path e.e_path [],null_pos)]
 			| _ ->
 				CTAnonymous (PMap.foldi (fun _ f acc ->
+					let access = ref [] in
+					let add flag =
+						access := (flag,null_pos) :: !access;
+					in
+					if has_class_field_flag f CfPublic then add APublic else add APrivate;
+					if has_class_field_flag f CfFinal then add AFinal;
+					if has_class_field_flag f CfExtern then add AExtern;
+					let kind = match (f.cf_kind,follow f.cf_type) with
+						| (Var v,ret) ->
+							let var_access_to_string va get_or_set = match va with
+								| AccNormal | AccCtor | AccInline | AccRequire _ -> "default"
+								| AccNo -> "null"
+								| AccNever -> "never"
+								| AccCall -> get_or_set
+							in
+							let read = (var_access_to_string v.v_read "get",null_pos) in
+							let write = (var_access_to_string v.v_write "set",null_pos) in
+							FProp (read,write,mk_type_hint f.cf_type null_pos,None)
+						| Method _,TFun(args,ret) ->
+							FFun({
+								f_params = [];
+								f_args = List.map (fun (n,o,t) ->
+									((n,null_pos),o,[],Some (convert_type t,null_pos),None)
+								) args;
+								f_type = Some (convert_type ret,null_pos);
+								f_expr = None;
+							})
+						| _ ->
+							die "" __LOC__
+					in
 					{
 						cff_name = f.cf_name,null_pos;
-						cff_kind = FVar (mk_type_hint f.cf_type null_pos,None);
+						cff_kind = kind;
 						cff_pos = f.cf_pos;
 						cff_doc = f.cf_doc;
 						cff_meta = f.cf_meta;
-						cff_access = [];
+						cff_access = !access;
 					} :: acc
 				) a.a_fields [])
 			end

+ 35 - 0
tests/unit/src/unit/issues/Issue6063.hx

@@ -0,0 +1,35 @@
+package unit.issues;
+
+import haxe.macro.Context;
+import haxe.macro.Expr;
+import unit.HelperMacros.*;
+
+class Issue6063 extends Test {
+	macro static function convert(e:Expr) {
+		var t = Context.typeof(e);
+		var ct = Context.toComplexType(t);
+		return macro @:pos(e.pos) (null : $ct);
+	}
+
+	function testFinal() {
+		#if !macro
+		var x:{final s:String;} = null;
+		var y = convert(x);
+		x = y;
+		t(typeError(y.s = "foo"));
+		#end
+	}
+
+	function testAccess() {
+		var o:{var x(default, null):Int;} = null;
+		var o2 = convert(o);
+		o = o2;
+		t(typeError(o2.x = 12));
+	}
+
+	function testFunction() {
+		var f:(?a:String) -> Void = null;
+		var e = convert(f);
+		eq("(?a : String) -> Void", typeString(e));
+	}
+}