Browse Source

Improve extra field error range (#11335)

* Improve extra field error range

* update range

* fail
RblSb 1 year ago
parent
commit
d52937e537

+ 7 - 2
src/typing/typer.ml

@@ -904,7 +904,7 @@ and type_object_decl ctx fl with_type p =
 				(try type_eq EqStrict e.etype t; e with Unify_error _ -> mk (TCast (e,None)) t e.epos)
 				(try type_eq EqStrict e.etype t; e with Unify_error _ -> mk (TCast (e,None)) t e.epos)
 			with Not_found ->
 			with Not_found ->
 				if is_valid then
 				if is_valid then
-					extra_fields := n :: !extra_fields;
+					extra_fields := (n,pn) :: !extra_fields;
 				type_expr ctx e WithType.value
 				type_expr ctx e WithType.value
 			in
 			in
 			if is_valid then begin
 			if is_valid then begin
@@ -923,7 +923,12 @@ and type_object_decl ctx fl with_type p =
 				| depth -> raise_or_display ctx [Unify_custom ("Object requires fields: " ^ (String.concat ", " depth))] p);
 				| depth -> raise_or_display ctx [Unify_custom ("Object requires fields: " ^ (String.concat ", " depth))] p);
 			(match !extra_fields with
 			(match !extra_fields with
 			| [] -> ()
 			| [] -> ()
-			| _ -> raise_or_display ctx (List.map (fun n -> has_extra_field t n) !extra_fields) p);
+			| _ ->
+				List.iter (fun (n,pn) ->
+					let err = has_extra_field t n in
+					raise_or_display ctx [err] pn
+				) !extra_fields
+			);
 		end;
 		end;
 		t, fl
 		t, fl
 	in
 	in

+ 19 - 0
tests/misc/projects/Issue11334/Main.hx

@@ -0,0 +1,19 @@
+class Main {
+	static function main() {
+		final foo:Foo = {
+			a: () -> {
+				trace(123);
+			},
+			b: () -> {
+				trace(123);
+			},
+			c: 123,
+			d: 123,
+		}
+	}
+}
+
+typedef Foo = {
+	a:() -> Void,
+	c:Int,
+}

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

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

+ 2 - 0
tests/misc/projects/Issue11334/compile-fail.hxml.stderr

@@ -0,0 +1,2 @@
+Main.hx:11: characters 4-5 : { d : Int, c : Int, b : () -> Void, a : () -> Void } has extra field d
+Main.hx:7: characters 4-5 : { d : Int, c : Int, b : () -> Void, a : () -> Void } has extra field b

+ 1 - 1
tests/misc/projects/Issue3192/compile1-fail.hxml.stderr

@@ -1 +1 @@
-Main1.hx:3: characters 19-33 : { b : Int, a : Int } has extra field b
+Main1.hx:3: characters 27-28 : { b : Int, a : Int } has extra field b