Selaa lähdekoodia

Improve duplicate field range (#11042)

RblSb 2 vuotta sitten
vanhempi
commit
089ff00fcd

+ 2 - 1
src/typing/typeload.ml

@@ -524,8 +524,9 @@ and load_complex_type' ctx allow_display (t,p) =
 		let displayed_field = ref None in
 		let rec loop acc f =
 			let n = fst f.cff_name in
+			let pf = snd f.cff_name in
 			let p = f.cff_pos in
-			if PMap.mem n acc then typing_error ("Duplicate field declaration : " ^ n) p;
+			if PMap.mem n acc then typing_error ("Duplicate field declaration : " ^ n) pf;
 			let topt = function
 				| None -> typing_error ("Explicit type required for field " ^ n) p
 				| Some t -> load_complex_type ctx allow_display t

+ 2 - 2
src/typing/typer.ml

@@ -892,7 +892,7 @@ and type_object_decl ctx fl with_type p =
 		let extra_fields = ref [] in
 		let fl = List.map (fun ((n,pn,qs),e) ->
 			let is_valid = Lexer.is_valid_identifier n in
-			if PMap.mem n !fields then typing_error ("Duplicate field in object declaration : " ^ n) p;
+			if PMap.mem n !fields then typing_error ("Duplicate field in object declaration : " ^ n) pn;
 			let is_final = ref false in
 			let e = try
 				let t = match !dynamic_parameter with
@@ -935,7 +935,7 @@ and type_object_decl ctx fl with_type p =
 	let type_plain_fields () =
 		let rec loop (l,acc) ((f,pf,qs),e) =
 			let is_valid = Lexer.is_valid_identifier f in
-			if PMap.mem f acc then typing_error ("Duplicate field in object declaration : " ^ f) p;
+			if PMap.mem f acc then typing_error ("Duplicate field in object declaration : " ^ f) pf;
 			let e = type_expr ctx e (WithType.named_structure_field f) in
 			(match follow e.etype with TAbstract({a_path=[],"Void"},_) -> typing_error "Fields of type Void are not allowed in structures" e.epos | _ -> ());
 			let cf = mk_field f e.etype (punion pf e.epos) pf in

+ 37 - 0
tests/misc/projects/Issue10961/Main.hx

@@ -0,0 +1,37 @@
+class Main {
+	static function main() {
+		final foo:Foo = {
+			a: 0,
+			b: 0,
+			c: 0,
+			a: 0,
+		}
+		final foo = {
+			a: 0,
+			b: 0,
+			c: 0,
+			a: 0,
+		}
+		final foo:CFoo = {
+			a: 0,
+			b: 0,
+			a: 0,
+		}
+	}
+}
+
+typedef Foo = {
+	a:Int,
+	b:Int,
+	c:Int,
+}
+
+typedef FooAdd = {
+	d:Int,
+}
+
+@:structInit
+class CFoo {
+	var a:Int;
+	var b:Int;
+}

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

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

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

@@ -0,0 +1,3 @@
+Main.hx:7: characters 4-5 : Duplicate field in object declaration : a
+Main.hx:13: characters 4-5 : Duplicate field in object declaration : a
+Main.hx:18: characters 4-5 : Duplicate field in object declaration : a