Skip to content

Commit

Permalink
[display] deal with static extension hovers properly
Browse files Browse the repository at this point in the history
closes #11285
  • Loading branch information
Simn authored and kLabz committed Mar 4, 2024
1 parent a78f78a commit 333cc39
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 15 deletions.
5 changes: 4 additions & 1 deletion src/context/typecore.ml
Expand Up @@ -713,10 +713,13 @@ let get_next_stored_typed_expr_id =
let uid = ref 0 in
(fun() -> incr uid; !uid)

let make_stored_id_expr id p =
(EConst (Int (string_of_int id, None))), p

let store_typed_expr com te p =
let id = get_next_stored_typed_expr_id() in
com.stored_typed_exprs#add id te;
let eid = (EConst (Int (string_of_int id, None))), p in
let eid = make_stored_id_expr id p in
id,((EMeta ((Meta.StoredTypedExpr,[],null_pos), eid)),p)

let push_this ctx e = match e.eexpr with
Expand Down
4 changes: 2 additions & 2 deletions src/typing/calls.ml
Expand Up @@ -218,8 +218,8 @@ let rec acc_get ctx g =
| AKUsingAccessor sea | AKUsingField sea when ctx.in_display ->
(* Generate a TField node so we can easily match it for position/usage completion (issue #1968) *)
let e_field = FieldAccess.get_field_expr sea.se_access FGet in
(* TODO *)
(* let ec = {ec with eexpr = (TMeta((Meta.StaticExtension,[],null_pos),ec))} in *)
let id,_ = store_typed_expr ctx.com sea.se_this e_field.epos in
let e_field = {e_field with eexpr = (TMeta((Meta.StaticExtension,[make_stored_id_expr id e_field.epos],null_pos),e_field))} in
let t = match follow e_field.etype with
| TFun (_ :: args,ret) -> TFun(args,ret)
| t -> t
Expand Down
4 changes: 0 additions & 4 deletions src/typing/macroContext.ml
Expand Up @@ -883,9 +883,5 @@ let interpret ctx =
let setup() =
Interp.setup Interp.macro_api

let type_stored_expr ctx e1 =
let id = match e1 with (EConst (Int (s, _)),_) -> int_of_string s | _ -> die "" __LOC__ in
TyperBase.get_stored_typed_expr ctx id

;;
load_macro_ref := load_macro;
2 changes: 1 addition & 1 deletion src/typing/matcher.ml
Expand Up @@ -552,7 +552,7 @@ module Pattern = struct
ignore(TyperDisplay.handle_edisplay ctx e (display_mode()) MGet (WithType.with_type t));
pat
| EMeta((Meta.StoredTypedExpr,_,_),e1) ->
let e1 = MacroContext.type_stored_expr ctx e1 in
let e1 = TyperBase.type_stored_expr ctx e1 in
loop (TExprToExpr.convert_expr e1)
| _ ->
fail()
Expand Down
2 changes: 1 addition & 1 deletion src/typing/typer.ml
Expand Up @@ -1677,7 +1677,7 @@ and type_meta ?(mode=MGet) ctx m e1 with_type p =
| _ -> e()
end
| (Meta.StoredTypedExpr,_,_) ->
MacroContext.type_stored_expr ctx e1
type_stored_expr ctx e1
| (Meta.NoPrivateAccess,_,_) ->
ctx.meta <- List.filter (fun(m,_,_) -> m <> Meta.PrivateAccess) ctx.meta;
e()
Expand Down
4 changes: 4 additions & 0 deletions src/typing/typerBase.ml
Expand Up @@ -177,6 +177,10 @@ let get_stored_typed_expr ctx id =
let e = ctx.com.stored_typed_exprs#find id in
Texpr.duplicate_tvars (fun e -> get_this ctx e.epos) e

let type_stored_expr ctx e1 =
let id = match e1 with (EConst (Int (s, _)),_) -> int_of_string s | _ -> die "" __LOC__ in
get_stored_typed_expr ctx id

let assign_to_this_is_allowed ctx =
match ctx.curclass.cl_kind with
| KAbstractImpl _ ->
Expand Down
16 changes: 11 additions & 5 deletions src/typing/typerDisplay.ml
Expand Up @@ -346,20 +346,26 @@ and display_expr ctx e_ast e dk mode with_type p =
let fa = get_constructor_access c params p in
fa.fa_field,c
in
let maybe_expand_overload e e_on host cf = match mode with
let maybe_expand_overload el_typed e e_on host cf = match mode with
| MCall el when cf.cf_overloads <> [] ->
let fa = FieldAccess.create e_on cf host false p in
let fcc = unify_field_call ctx fa [] el p false in
let fcc = unify_field_call ctx fa el_typed el p false in
FieldAccess.get_field_expr {fa with fa_field = fcc.fc_field} FCall
| _ ->
e
in
let e,el_typed = match e.eexpr with
| TMeta((Meta.StaticExtension,[e_self],_),e1) ->
e1,[type_stored_expr ctx e_self]
| _ ->
e,[]
in
(* If we display on a TField node that points to an overloaded field, let's try to unify the field call
in order to resolve the correct overload (issue #7753). *)
let e = match e.eexpr with
| TField(e1,FStatic(c,cf)) -> maybe_expand_overload e e1 (FHStatic c) cf
| TField(e1,(FInstance(c,tl,cf) | FClosure(Some(c,tl),cf))) -> maybe_expand_overload e e1 (FHInstance(c,tl)) cf
| TField(e1,(FAnon cf | FClosure(None,cf))) -> maybe_expand_overload e e1 FHAnon cf
| TField(e1,FStatic(c,cf)) -> maybe_expand_overload el_typed e e1 (FHStatic c) cf
| TField(e1,(FInstance(c,tl,cf) | FClosure(Some(c,tl),cf))) -> maybe_expand_overload el_typed e e1 (FHInstance(c,tl)) cf
| TField(e1,(FAnon cf | FClosure(None,cf))) -> maybe_expand_overload el_typed e e1 FHAnon cf
| _ -> e
in
match ctx.com.display.dms_kind with
Expand Down
2 changes: 1 addition & 1 deletion tests/display/build.hxml
Expand Up @@ -5,4 +5,4 @@
-lib haxeserver
--interp
-D use-rtti-doc
#-D test=9133
-D test=11285
34 changes: 34 additions & 0 deletions tests/display/src/cases/Issue11285.hx
@@ -0,0 +1,34 @@
package cases;

class Issue11285 extends DisplayTestCase {
/**
using Issue11285.MathTools;
function main() {
var float = 0.0;
var int = 0;
float.wrapA{-1-}round(0, 1);
int.wrap{-2-}Around(0, 1);
}
class MathTools {
extern overload public static inline function {-3-}wrapAround{-4-}(v:Int, min:Int, max:Int):Int {
var range = max - min;
return min + (((v - min) % range) + range) % range;
}
extern overload public static inline function {-5-}wrapAround{-6-}(v:Float, min:Float, max:Float):Float {
var range = max - min;
return min + (((v - min) % range) + range) % range;
}
}
**/
function test() {
eq(range(5, 6), position(pos(1)));
eq("(v : Float, min : Float, max : Float) -> Float", type(pos(1)));

eq(range(3, 4), position(pos(2)));
eq("(v : Int, min : Int, max : Int) -> Int", type(pos(2)));
}
}

0 comments on commit 333cc39

Please sign in to comment.