Browse Source

make sure constructor is not always kept with DCE if subclass does not have one

Nicolas Cannasse 13 years ago
parent
commit
34e29e6706
2 changed files with 6 additions and 1 deletions
  1. 1 1
      typeload.ml
  2. 5 0
      typer.ml

+ 1 - 1
typeload.ml

@@ -1041,7 +1041,7 @@ let init_class ctx c p herits fields =
 						(match e.eexpr with
 						| TBlock [] | TBlock [{ eexpr = TConst _ }] | TConst _ | TObjectDecl [] -> ()
 						| _ -> c.cl_init <- Some e);
-					mark_used cf;
+					if not constr then mark_used cf;
 					cf.cf_expr <- Some (mk (TFunction f) t p);
 					cf.cf_type <- t;
 				end;

+ 5 - 0
typer.ml

@@ -84,6 +84,9 @@ let rec mark_used_class ctx c =
 		| _ -> ()
 	end
 
+let mark_used_field ctx f =
+	if ctx.com.dead_code_elimination && not (has_meta ":?used" f.cf_meta) then f.cf_meta <- (":?used",[],f.cf_pos) :: f.cf_meta
+
 type type_class =
 	| KInt
 	| KFloat
@@ -1609,6 +1612,7 @@ and type_expr ctx ?(need_val=true) (e,p) =
 			if PMap.mem name ctx.locals then error ("Local variable " ^ name ^ " is preventing usage of this class here") p;
 			let ct, f = get_constructor c params p in
 			if not f.cf_public && not (is_parent c ctx.curclass) && not ctx.untyped then display_error ctx "Cannot access private constructor" p;
+			mark_used_field ctx f;
 			(match f.cf_kind with
 			| Var { v_read = AccRequire r } -> error_require r p
 			| _ -> ());
@@ -1870,6 +1874,7 @@ and type_call ctx e el p =
 		| None -> error "Current class does not have a super" p
 		| Some (c,params) ->
 			let ct, f = get_constructor c params p in
+			mark_used_field ctx f;
 			let el, _ = (match follow ct with
 			| TFun (args,r) ->
 				unify_call_params ctx (Some ("new",f.cf_meta)) el args r p false