Browse Source

add @:debug.mono and clean up printing

Simon Krajewski 8 months ago
parent
commit
400c79d4c9
3 changed files with 64 additions and 33 deletions
  1. 2 5
      src/core/error.ml
  2. 33 27
      src/core/tPrinting.ml
  3. 29 1
      src/typing/typeloadFunction.ml

+ 2 - 5
src/core/error.ml

@@ -185,11 +185,8 @@ module BetterErrors = struct
 		| TMono r ->
 			(match r.tm_type with
 			| None ->
-				let name = Printf.sprintf "Unknown<%d>" (try List.assq t (!ctx) with Not_found -> let n = List.length !ctx in ctx := (t,n) :: !ctx; n) in
-				List.fold_left (fun s modi -> match modi with
-					| MNullable _ -> Printf.sprintf "Null<%s>" s
-					| MOpenStructure | MDynamic -> s
-				) name r.tm_modifiers
+				let name = Printf.sprintf "Unknown<%d>" (try List.assq r (!ctx) with Not_found -> let n = List.length !ctx in ctx := (r,n) :: !ctx; n) in
+				s_mono_modifiers name r;
 			| Some t ->
 				s_type ctx t)
 		| TEnum (e,tl) ->

+ 33 - 27
src/core/tPrinting.ml

@@ -39,36 +39,42 @@ let rec s_mono_constraint_kind s_type constr =
 	in
 	loop constr
 
+and s_mono_modifiers s m =
+	List.fold_left (fun s modi -> match modi with
+		| MNullable _ -> Printf.sprintf "Null<%s>" s
+		| MOpenStructure | MDynamic -> s
+	) s m.tm_modifiers
+
+and s_mono ctx m =
+	match m.tm_type with
+	| None ->
+		let print_name id extra =
+			let s = if show_mono_ids then
+				Printf.sprintf "Unknown<%d>" id
+			else
+				"Unknown"
+			in
+			let s = s ^ extra in
+			s_mono_modifiers s m
+		in
+		begin try
+			let id = List.assq m (!ctx) in
+			print_name id ""
+		with Not_found ->
+			let id = List.length !ctx in
+			ctx := (m,id) :: !ctx;
+			let s_const =
+				let s = s_mono_constraint_kind (s_type ctx) (!monomorph_classify_constraints_ref m) in
+				if s = "" then s else " : " ^ s
+			in
+			print_name id s_const
+		end
+	| Some t -> s_type ctx t
+
 and s_type ctx t =
 	match t with
 	| TMono r ->
-		(match r.tm_type with
-		| None ->
-			let print_name id extra =
-				let s = if show_mono_ids then
-					Printf.sprintf "Unknown<%d>" id
-				else
-					"Unknown"
-				in
-				let s = s ^ extra in
-				List.fold_left (fun s modi -> match modi with
-					| MNullable _ -> Printf.sprintf "Null<%s>" s
-					| MOpenStructure | MDynamic -> s
-				) s r.tm_modifiers
-			in
-			begin try
-				let id = List.assq t (!ctx) in
-				print_name id ""
-			with Not_found ->
-				let id = List.length !ctx in
-				ctx := (t,id) :: !ctx;
-				let s_const =
-					let s = s_mono_constraint_kind (s_type ctx) (!monomorph_classify_constraints_ref r) in
-					if s = "" then s else " : " ^ s
-				in
-				print_name id s_const
-			end
-		| Some t -> s_type ctx t)
+		s_mono ctx r
 	| TEnum (e,tl) ->
 		s_type_path e.e_path ^ s_type_params ctx tl
 	| TInst (c,tl) ->

+ 29 - 1
src/typing/typeloadFunction.ml

@@ -157,7 +157,35 @@ let type_function ctx (args : function_arguments) ret e do_display p =
 		| _ -> e
 	in
 	List.iter (fun r -> r := Closed) ctx.e.opened;
-	List.iter (fun (m,p) -> safe_mono_close ctx m p) ctx.e.monomorphs.perfunction;
+	let close () = List.iter (fun (m,p) -> safe_mono_close ctx m p) ctx.e.monomorphs.perfunction; in
+	let mono_debug = Meta.has (Meta.Custom ":debug.mono") ctx.f.curfield.cf_meta in
+	if mono_debug then begin
+		let pctx = print_context () in
+		let print_mono i m =
+			Printf.sprintf "%4i: %s" i (s_mono pctx m)
+		in
+		print_endline "BEFORE:";
+		let monos = List.mapi (fun i (m,p) ->
+			let s = print_mono i m in
+			let spos = if p.pmin = -1 then
+				"unknown"
+			else begin
+				let l1,p1,_,_ = Lexer.get_pos_coords p in
+				Printf.sprintf "%i:%i" l1 p1
+			end in
+			print_endline (Printf.sprintf "%s (%s)" s spos);
+			(i,m,p,s)
+		) ctx.e.monomorphs.perfunction in
+		close();
+		print_endline "CHANGED:";
+		List.iter (fun (i,m,p,s) ->
+			let s' = print_mono i m in
+			if s <> s' then begin
+				print_endline s'
+			end
+		) monos
+	end else
+		close();
 	if is_position_debug then print_endline ("typing:\n" ^ (Texpr.dump_with_pos "" e));
 	e