Skip to content

Commit

Permalink
Investigate some ctx.m mutations (#11840)
Browse files Browse the repository at this point in the history
* what does this actually do?

* but how about this?

* how far can we take this?

* I finally broke something

* one more?
  • Loading branch information
Simn authored Nov 26, 2024
1 parent 384c678 commit d989ab3
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 117 deletions.
3 changes: 2 additions & 1 deletion src/context/display/displayTexpr.ml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ let actually_check_display_field ctx c cff p =
let cff = TypeloadFields.transform_field (ctx,cctx) c cff (ref []) (pos cff.cff_name) in
let display_modifier = Typeload.check_field_access ctx cff in
let fctx = TypeloadFields.create_field_context ctx cctx cff true display_modifier in
let cf = TypeloadFields.init_field (ctx,cctx,fctx) cff in
let cf = TypeloadFields.create_class_field cctx cff in
TypeloadFields.init_field (ctx,cctx,fctx) cff cf;
flush_pass ctx.g PTypeField ("check_display_field",(fst c.cl_path @ [snd c.cl_path;fst cff.cff_name]));
ignore(follow cf.cf_type)

Expand Down
6 changes: 3 additions & 3 deletions src/context/typecore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ type typer_module = {
}

type typer_class = {
mutable curclass : tclass; (* TODO: should not be mutable *)
curclass : tclass;
mutable tthis : t;
mutable get_build_infos : unit -> (module_type * t list * class_field list) option;
}
Expand Down Expand Up @@ -150,7 +150,7 @@ and typer_expr = {
}

and typer_field = {
mutable curfield : tclass_field;
curfield : tclass_field;
mutable locals : (string, tvar) PMap.t;
mutable vthis : tvar option;
mutable untyped : bool;
Expand All @@ -165,7 +165,7 @@ and typer = {
com : context;
t : basic_types;
g : typer_globals;
mutable m : typer_module;
m : typer_module;
c : typer_class;
f : typer_field;
e : typer_expr;
Expand Down
112 changes: 57 additions & 55 deletions src/filters/filters.ml
Original file line number Diff line number Diff line change
Expand Up @@ -251,27 +251,29 @@ let mark_switch_break_loops e =
in
run e

let rec fix_return_dynamic_from_void_function return_is_void e =
match e.eexpr with
| TFunction fn ->
let is_void = ExtType.is_void (follow fn.tf_type) in
let body = fix_return_dynamic_from_void_function is_void fn.tf_expr in
{ e with eexpr = TFunction { fn with tf_expr = body } }
| TReturn (Some return_expr) when return_is_void && t_dynamic == follow return_expr.etype ->
let return_pos = { e.epos with pmax = return_expr.epos.pmin - 1 } in
let exprs = [
fix_return_dynamic_from_void_function return_is_void return_expr;
{ e with eexpr = TReturn None; epos = return_pos };
] in
{ e with
eexpr = TMeta (
(Meta.MergeBlock, [], null_pos),
mk (TBlock exprs) e.etype e.epos
);
}
| _ -> Type.map_expr (fix_return_dynamic_from_void_function return_is_void) e

let check_abstract_as_value e =
let fix_return_dynamic_from_void_function _ e =
let rec loop return_is_void e = match e.eexpr with
| TFunction fn ->
let is_void = ExtType.is_void (follow fn.tf_type) in
let body = loop is_void fn.tf_expr in
{ e with eexpr = TFunction { fn with tf_expr = body } }
| TReturn (Some return_expr) when return_is_void && t_dynamic == follow return_expr.etype ->
let return_pos = { e.epos with pmax = return_expr.epos.pmin - 1 } in
let exprs = [
loop return_is_void return_expr;
{ e with eexpr = TReturn None; epos = return_pos };
] in
{ e with
eexpr = TMeta (
(Meta.MergeBlock, [], null_pos),
mk (TBlock exprs) e.etype e.epos
);
}
| _ -> Type.map_expr (loop return_is_void) e
in
loop true e

let check_abstract_as_value _ e =
let rec loop e =
match e.eexpr with
| TField ({ eexpr = TTypeExpr _ }, _) -> ()
Expand Down Expand Up @@ -489,30 +491,30 @@ let destruction tctx detail_times main locals =
tctx
detail_times
(* This has to run after DCE, or otherwise its condition always holds. *)
["insert_save_stacks",Exceptions.insert_save_stacks tctx]
["insert_save_stacks",Exceptions.insert_save_stacks]
)
com.types;
let type_filters = [
Exceptions.patch_constructors tctx; (* TODO: I don't believe this should load_instance anything at this point... *)
check_private_path com;
Naming.apply_native_paths;
add_rtti com;
(match com.platform with | Jvm -> (fun _ -> ()) | _ -> (fun mt -> AddFieldInits.add_field_inits tctx.c.curclass.cl_path locals com mt));
(match com.platform with Hl -> (fun _ -> ()) | _ -> add_meta_field com);
check_void_field;
(match com.platform with | Cpp -> promote_first_interface_to_super | _ -> (fun _ -> ()));
commit_features com;
(if com.config.pf_reserved_type_paths <> [] then check_reserved_type_paths com else (fun _ -> ()));
Exceptions.patch_constructors; (* TODO: I don't believe this should load_instance anything at this point... *)
(fun _ -> check_private_path com);
(fun _ -> Naming.apply_native_paths);
(fun _ -> add_rtti com);
(match com.platform with | Jvm -> (fun _ _ -> ()) | _ -> (fun tctx mt -> AddFieldInits.add_field_inits tctx.c.curclass.cl_path locals com mt));
(match com.platform with Hl -> (fun _ _ -> ()) | _ -> (fun _ -> add_meta_field com));
(fun _ -> check_void_field);
(fun _ -> (match com.platform with | Cpp -> promote_first_interface_to_super | _ -> (fun _ -> ())));
(fun _ -> commit_features com);
(fun _ -> (if com.config.pf_reserved_type_paths <> [] then check_reserved_type_paths com else (fun _ -> ())));
] in
with_timer detail_times "type 3" None (fun () ->
List.iter (fun t ->
begin match t with
| TClassDecl c ->
tctx.c.curclass <- c
| _ ->
()
end;
List.iter (fun f -> f t) type_filters
let tctx = match t with
| TClassDecl c ->
TyperManager.clone_for_class tctx c
| _ ->
tctx
in
List.iter (fun f -> f tctx t) type_filters
) com.types;
);
com.callbacks#run com.error_ext com.callbacks#get_after_filters;
Expand Down Expand Up @@ -705,20 +707,20 @@ let run tctx main before_destruction =
NullSafety.run com new_types;
(* PASS 1: general expression filters *)
let filters = [
"ForRemap",ForRemap.apply tctx;
"handle_abstract_casts",AbstractCast.handle_abstract_casts tctx;
"ForRemap",ForRemap.apply;
"handle_abstract_casts",AbstractCast.handle_abstract_casts;
] in
List.iter (run_expression_filters tctx detail_times filters) new_types;
let filters = [
"local_statics",LocalStatic.run tctx;
"fix_return_dynamic_from_void_function",fix_return_dynamic_from_void_function true;
"check_local_vars_init",check_local_vars_init tctx;
"local_statics",LocalStatic.run;
"fix_return_dynamic_from_void_function",fix_return_dynamic_from_void_function;
"check_local_vars_init",check_local_vars_init;
"check_abstract_as_value",check_abstract_as_value;
"Tre",if defined com Define.AnalyzerOptimize then Tre.run tctx else (fun e -> e);
"reduce_expression",Optimizer.reduce_expression tctx;
"inline_constructors",InlineConstructors.inline_constructors tctx;
"Exceptions_filter",Exceptions.filter tctx;
"captured_vars",CapturedVars.captured_vars com;
"Tre",if defined com Define.AnalyzerOptimize then Tre.run else (fun _ e -> e);
"reduce_expression",Optimizer.reduce_expression;
"inline_constructors",InlineConstructors.inline_constructors;
"Exceptions_filter",Exceptions.filter;
"captured_vars",(fun _ -> CapturedVars.captured_vars com);
] in
List.iter (run_expression_filters tctx detail_times filters) new_types;
(* PASS 1.5: pre-analyzer type filters *)
Expand All @@ -739,13 +741,13 @@ let run tctx main before_destruction =
enter_stage com CAnalyzerDone;
let locals = RenameVars.init com in
let filters = [
"sanitize",Optimizer.sanitize com;
"add_final_return",if com.config.pf_add_final_return then add_final_return else (fun e -> e);
"sanitize",(fun _ e -> Optimizer.sanitize com e);
"add_final_return",(fun _ -> if com.config.pf_add_final_return then add_final_return else (fun e -> e));
"RenameVars",(match com.platform with
| Eval -> (fun e -> e)
| Jvm -> (fun e -> e)
| _ -> (fun e -> RenameVars.run tctx.c.curclass.cl_path locals e));
"mark_switch_break_loops",mark_switch_break_loops;
| Eval -> (fun _ e -> e)
| Jvm -> (fun _ e -> e)
| _ -> (fun tctx e -> RenameVars.run tctx.c.curclass.cl_path locals e));
"mark_switch_break_loops",(fun _ -> mark_switch_break_loops);
] in
List.iter (run_expression_filters tctx detail_times filters) new_types;
with_timer detail_times "callbacks" None (fun () ->
Expand Down
26 changes: 13 additions & 13 deletions src/filters/filtersCommon.ml
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,26 @@ let is_overridden cls field =

let run_expression_filters ?(ignore_processed_status=false) ctx detail_times filters t =
let com = ctx.com in
let run identifier e =
let run (ctx : typer) identifier e =
List.fold_left (fun e (filter_name,f) ->
FilterContext.with_timer detail_times filter_name identifier (fun () -> f e)
FilterContext.with_timer detail_times filter_name identifier (fun () -> f ctx e)
) e filters
in
match t with
| TClassDecl c when is_removable_class c -> ()
| TClassDecl c ->
ctx.c.curclass <- c;
ctx.m <- TypeloadModule.make_curmod ctx.com ctx.g c.cl_module;
let rec process_field f =
if ignore_processed_status || not (has_class_field_flag f CfPostProcessed) then begin
ctx.f.curfield <- f;
(match f.cf_expr with
| Some e when not (is_removable_field com f) ->
let identifier = Printf.sprintf "%s.%s" (s_type_path c.cl_path) f.cf_name in
f.cf_expr <- Some (rec_stack_loop AbstractCast.cast_stack f (run (Some identifier)) e);
let ctx = TyperManager.clone_for_module ctx (TypeloadModule.make_curmod ctx.com ctx.g c.cl_module) in
let ctx = TyperManager.clone_for_class ctx c in
let rec process_field cf =
if ignore_processed_status || not (has_class_field_flag cf CfPostProcessed) then begin
let ctx = TyperManager.clone_for_field ctx cf cf.cf_params in
(match cf.cf_expr with
| Some e when not (is_removable_field com cf) ->
let identifier = Printf.sprintf "%s.%s" (s_type_path c.cl_path) cf.cf_name in
cf.cf_expr <- Some (rec_stack_loop AbstractCast.cast_stack cf (run ctx (Some identifier)) e);
| _ -> ());
end;
List.iter process_field f.cf_overloads
List.iter process_field cf.cf_overloads
in
List.iter process_field c.cl_ordered_fields;
List.iter process_field c.cl_ordered_statics;
Expand All @@ -85,7 +85,7 @@ let run_expression_filters ?(ignore_processed_status=false) ctx detail_times fil
| None -> ()
| Some e ->
let identifier = Printf.sprintf "%s.__init__" (s_type_path c.cl_path) in
TClass.set_cl_init c (run (Some identifier) e))
TClass.set_cl_init c (run ctx (Some identifier) e))
| TEnumDecl _ -> ()
| TTypeDecl _ -> ()
| TAbstractDecl _ -> ()
Expand Down
17 changes: 8 additions & 9 deletions src/typing/macroContext.ml
Original file line number Diff line number Diff line change
Expand Up @@ -608,10 +608,10 @@ and flush_macro_context mint mctx =
mctx.com.Common.modules <- modules;
(* we should maybe ensure that all filters in Main are applied. Not urgent atm *)
let expr_filters = [
"handle_abstract_casts",AbstractCast.handle_abstract_casts mctx;
"local_statics",LocalStatic.run mctx;
"Exceptions",Exceptions.filter mctx;
"captured_vars",CapturedVars.captured_vars mctx.com;
"handle_abstract_casts",AbstractCast.handle_abstract_casts;
"local_statics",LocalStatic.run;
"Exceptions",Exceptions.filter;
"captured_vars",(fun _ -> CapturedVars.captured_vars mctx.com);
] in
(*
some filters here might cause side effects that would break compilation server.
Expand Down Expand Up @@ -752,15 +752,15 @@ let load_macro_module mctx com cpath display p =
let old = mctx.com.display in
if display then mctx.com.display <- com.display;
let mloaded = TypeloadModule.load_module ~origin:MDepFromMacro mctx m p in
mctx.m <- {
(* mctx.m <- {
curmod = mloaded;
import_resolution = new resolution_list ["import";s_type_path cpath];
own_resolution = None;
enum_with_type = None;
module_using = [];
import_statements = [];
is_display_file = (com.display.dms_kind <> DMNone && DisplayPosition.display_position#is_in_file (Path.UniqueKey.lazy_key mloaded.m_extra.m_file));
};
}; *)
mloaded,(fun () -> mctx.com.display <- old)

let load_macro'' com mctx display cpath f p =
Expand Down Expand Up @@ -794,15 +794,15 @@ let load_macro'' com mctx display cpath f p =
restore();
if not com.is_macro_context then flush_macro_context mint mctx;
mctx.com.cached_macros#add (cpath,f) meth;
mctx.m <- {
(* mctx.m <- {
curmod = null_module;
import_resolution = new resolution_list ["import";s_type_path cpath];
own_resolution = None;
enum_with_type = None;
module_using = [];
import_statements = [];
is_display_file = false;
};
}; *)
t();
meth

Expand Down Expand Up @@ -1017,7 +1017,6 @@ let type_macro ctx mode cpath f (el:Ast.expr list) p =
e

let call_macro mctx args margs call p =
mctx.c.curclass <- null_class;
let el, _ = CallUnification.unify_call_args mctx args margs t_dynamic p false false false in
call (List.map (fun e -> try Interp.make_const e with Exit -> raise_typing_error "Argument should be a constant" e.epos) el)

Expand Down
Loading

0 comments on commit d989ab3

Please sign in to comment.