瀏覽代碼

prevent too many 'extra field' errors.

Nicolas Cannasse 18 年之前
父節點
當前提交
9c2fab43fa
共有 2 個文件被更改,包括 9 次插入4 次删除
  1. 6 3
      type.ml
  2. 3 1
      typer.ml

+ 6 - 3
type.ml

@@ -57,6 +57,7 @@ and tfunc = {
 and anon_status =
 	| Closed
 	| Opened
+	| Const
 	| Statics of tclass
 	| EnumStatics of tenum
 
@@ -171,7 +172,6 @@ type module_def = {
 let mk e t p = { eexpr = e; etype = t; epos = p }
 
 let not_opened = ref Closed
-let const_status = ref Closed
 let is_closed a = !(a.a_status) <> Opened
 let mk_anon fl = TAnon { a_fields = fl; a_status = not_opened; }
 
@@ -651,9 +651,12 @@ let rec unify a b =
 					if not (link (ref None) a f2.cf_type) then error [];
 					a1.a_fields <- PMap.add n f2 a1.a_fields
 			) a2.a_fields;
-			if a1.a_status == const_status && not (PMap.is_empty a2.a_fields) then
+			(match !(a1.a_status) with
+			| Const when not (PMap.is_empty a2.a_fields) ->
 				PMap.iter (fun n _ -> if not (PMap.mem n a2.a_fields) then error [has_extra_field a n]) a1.a_fields;
-			if !(a1.a_status) = Opened then a1.a_status := Closed;
+			| Opened ->
+				a1.a_status := Closed
+			| _ -> ());
 			if !(a2.a_status) = Opened then a2.a_status := Closed;
 		with
 			Unify_error l -> error (cannot_unify a b :: l))

+ 3 - 1
typer.ml

@@ -1714,7 +1714,9 @@ and type_expr ctx ?(need_val=true) (e,p) =
 			((f,e) :: l, PMap.add f cf acc)
 		in
 		let fields , types = List.fold_left loop ([],PMap.empty) fl in
-		mk (TObjectDecl fields) (TAnon { a_fields = types; a_status = const_status }) p
+		let x = ref Const in
+		ctx.opened <- x :: ctx.opened;
+		mk (TObjectDecl fields) (TAnon { a_fields = types; a_status = x }) p
 	| EArrayDecl el ->
 		let t = ref (mk_mono()) in
 		let el = List.map (fun e ->