瀏覽代碼

[typer] don't create a class field for every enum field (#11452)

They only exist so that type_field can find them easily, but it might as well just check the constructors, which it already does anyway.
Simon Krajewski 1 年之前
父節點
當前提交
87828d4e35
共有 3 個文件被更改,包括 12 次插入14 次删除
  1. 2 2
      src/core/tFunctions.ml
  2. 7 6
      src/typing/fields.ml
  3. 3 6
      src/typing/typeloadModule.ml

+ 2 - 2
src/core/tFunctions.ml

@@ -945,9 +945,9 @@ let class_module_type c =
 	let t = mk_anon ~fields:c.cl_statics (ref (ClassStatics c)) in
 	{ (mk_typedef c.cl_module path c.cl_pos null_pos t) with t_private = true}
 
-let enum_module_type en fields =
+let enum_module_type en =
 	let path = ([], "Enum<" ^ (s_type_path en.e_path) ^ ">") in
-	let t = mk_anon ~fields (ref (EnumStatics en)) in
+	let t = mk_anon (ref (EnumStatics en)) in
 	{(mk_typedef en.e_module path en.e_pos null_pos t) with t_private = true}
 
 let abstract_module_type a tl =

+ 7 - 6
src/typing/fields.ml

@@ -333,11 +333,6 @@ let type_field cfg ctx e i p mode (with_type : WithType.t) =
 				let f = PMap.find i a.a_fields in
 				if has_class_field_flag f CfImpl && not (has_class_field_flag f CfEnum) then display_error ctx.com "Cannot access non-static abstract field statically" pfield;
 				match !(a.a_status) with
-				| EnumStatics en ->
-					let c = try PMap.find f.cf_name en.e_constrs with Not_found -> die "" __LOC__ in
-					let fmode = FEnum (en,c) in
-					let t = enum_field_type ctx en c p in
-					AKExpr (mk (TField (e,fmode)) t p)
 				| ClassStatics c ->
 					field_access f (FHStatic c)
 				| _ ->
@@ -350,7 +345,13 @@ let type_field cfg ctx e i p mode (with_type : WithType.t) =
 						let et = type_module_type ctx mt p in
 						type_field_by_e type_field_by_type et
 					) a
-				| _ -> raise Not_found
+				| EnumStatics en ->
+					let c = PMap.find i en.e_constrs in
+					let fmode = FEnum (en,c) in
+					let t = enum_field_type ctx en c p in
+					AKExpr (mk (TField (e,fmode)) t p)					
+				| _ ->
+					raise Not_found
 			)
 		| TMono r ->
 			let mk_field () = {

+ 3 - 6
src/typing/typeloadModule.ml

@@ -379,10 +379,9 @@ module TypeLevel = struct
 			ef_meta = c.ec_meta;
 		} in
 		DeprecationCheck.check_is ctx.com ctx.m.curmod e.e_meta f.ef_meta f.ef_name f.ef_meta f.ef_name_pos;
-		let cf = class_field_of_enum_field f in
 		if ctx.is_display_file && DisplayPosition.display_position#enclosed_in f.ef_name_pos then
 			DisplayEmitter.display_enum_field ctx e f p;
-		f,cf
+		f
 
 	let init_class ctx c d p =
 		if ctx.is_display_file && DisplayPosition.display_position#enclosed_in (pos d.d_name) then
@@ -504,12 +503,10 @@ module TypeLevel = struct
 		let names = ref [] in
 		let index = ref 0 in
 		let is_flat = ref true in
-		let fields = ref PMap.empty in
 		List.iter (fun c ->
 			if PMap.mem (fst c.ec_name) e.e_constrs then raise_typing_error ("Duplicate constructor " ^ fst c.ec_name) (pos c.ec_name);
-			let f,cf = load_enum_field ctx e et is_flat index c in
+			let f = load_enum_field ctx e et is_flat index c in
 			e.e_constrs <- PMap.add f.ef_name f e.e_constrs;
-			fields := PMap.add cf.cf_name cf !fields;
 			incr index;
 			names := (fst c.ec_name) :: !names;
 			if Meta.has Meta.InheritDoc f.ef_meta then
@@ -517,7 +514,7 @@ module TypeLevel = struct
 		) (!constructs);
 		e.e_names <- List.rev !names;
 		e.e_extern <- e.e_extern;
-		unify ctx (TType(enum_module_type e !fields,[])) e.e_type p;
+		unify ctx (TType(enum_module_type e,[])) e.e_type p;
 		if !is_flat then e.e_meta <- (Meta.FlatEnum,[],null_pos) :: e.e_meta;
 		if Meta.has Meta.InheritDoc e.e_meta then
 			delay ctx PConnectField (fun() -> InheritDoc.build_enum_doc ctx e);