Skip to content

Commit

Permalink
Improve duplicate field range (#11042)
Browse files Browse the repository at this point in the history
  • Loading branch information
RblSb authored Mar 27, 2023
1 parent 7d9faa2 commit 089ff00
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/typing/typeload.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/typing/typer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
37 changes: 37 additions & 0 deletions tests/misc/projects/Issue10961/Main.hx
Original file line number Diff line number Diff line change
@@ -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 changes: 2 additions & 0 deletions tests/misc/projects/Issue10961/compile-fail.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--main Main
--interp
3 changes: 3 additions & 0 deletions tests/misc/projects/Issue10961/compile-fail.hxml.stderr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 089ff00

Please sign in to comment.