From 5f070b4cc85751a6afc5cc60d8fec8556349c0ca Mon Sep 17 00:00:00 2001 From: RblSb Date: Tue, 30 Nov 2021 08:40:23 +0300 Subject: [PATCH 1/6] Abstract abstract --- src/syntax/grammar.mly | 3 +- src/typing/typer.ml | 14 ++++ tests/misc/projects/Issue10482/Main.hx | 7 ++ .../projects/Issue10482/compile-fail.hxml | 2 + .../Issue10482/compile-fail.hxml.stderr | 3 + tests/unit/src/unit/issues/Issue10482.hx | 72 +++++++++++++++++++ 6 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 tests/misc/projects/Issue10482/Main.hx create mode 100644 tests/misc/projects/Issue10482/compile-fail.hxml create mode 100644 tests/misc/projects/Issue10482/compile-fail.hxml.stderr create mode 100644 tests/unit/src/unit/issues/Issue10482.hx diff --git a/src/syntax/grammar.mly b/src/syntax/grammar.mly index 10dd44a7ea4..c29bab9a547 100644 --- a/src/syntax/grammar.mly +++ b/src/syntax/grammar.mly @@ -1320,6 +1320,7 @@ and expr = parser | [< '(Kwd Final,p1); v = parse_var_decl true >] -> (EVars [v],p1) | [< '(Const c,p); s >] -> expr_next (EConst c,p) s | [< '(Kwd This,p); s >] -> expr_next (EConst (Ident "this"),p) s + | [< '(Kwd Abstract,p); s >] -> expr_next (EConst (Ident "abstract"),p) s | [< '(Kwd True,p); s >] -> expr_next (EConst (Ident "true"),p) s | [< '(Kwd False,p); s >] -> expr_next (EConst (Ident "false"),p) s | [< '(Kwd Null,p); s >] -> expr_next (EConst (Ident "null"),p) s @@ -1674,4 +1675,4 @@ let rec parse_macro_cond s = cond with e -> parsing_macro_cond := false; - raise e \ No newline at end of file + raise e diff --git a/src/typing/typer.ml b/src/typing/typer.ml index 2303f96a69a..63ce3c508c9 100644 --- a/src/typing/typer.ml +++ b/src/typing/typer.ml @@ -281,6 +281,20 @@ let rec type_ident_raise ctx i p mode with_type = AKExpr (get_this ctx p) | (MCall _, KAbstractImpl _) | (MGet, _)-> AKExpr(get_this ctx p) | _ -> AKNo i) + | "abstract" -> + begin match mode, ctx.curclass.cl_kind with + | MSet _, KAbstractImpl ab -> typing_error "Property 'abstract' is read-only" p; + | (MGet, KAbstractImpl ab) + | (MCall _, KAbstractImpl ab) -> + let t = TAbstract (ab,[]) in + let vtmp = alloc_var VGenerated "tmp" t p in + let var = mk (TVar (vtmp,Some (get_this ctx p))) t p in + let vexpr = mk (TLocal vtmp) t p in + let block = mk (TBlock [var;vexpr]) t p in + AKExpr block + | _ -> + typing_error "Property 'abstract' is reserved and only available in abstracts" p + end | "super" -> let t = (match ctx.curclass.cl_super with | None -> typing_error "Current class does not have a superclass" p diff --git a/tests/misc/projects/Issue10482/Main.hx b/tests/misc/projects/Issue10482/Main.hx new file mode 100644 index 00000000000..8f395e2955f --- /dev/null +++ b/tests/misc/projects/Issue10482/Main.hx @@ -0,0 +1,7 @@ +class Main { + static function main() { + abstract; + abstract(); + abstract + 1; + } +} diff --git a/tests/misc/projects/Issue10482/compile-fail.hxml b/tests/misc/projects/Issue10482/compile-fail.hxml new file mode 100644 index 00000000000..e2a3d27a190 --- /dev/null +++ b/tests/misc/projects/Issue10482/compile-fail.hxml @@ -0,0 +1,2 @@ +--main Main +--interp diff --git a/tests/misc/projects/Issue10482/compile-fail.hxml.stderr b/tests/misc/projects/Issue10482/compile-fail.hxml.stderr new file mode 100644 index 00000000000..77473a5b3ab --- /dev/null +++ b/tests/misc/projects/Issue10482/compile-fail.hxml.stderr @@ -0,0 +1,3 @@ +Main.hx:3: characters 3-11 : Property 'abstract' is reserved and only available in abstracts +Main.hx:4: characters 3-11 : Property 'abstract' is reserved and only available in abstracts +Main.hx:5: characters 3-11 : Property 'abstract' is reserved and only available in abstracts diff --git a/tests/unit/src/unit/issues/Issue10482.hx b/tests/unit/src/unit/issues/Issue10482.hx new file mode 100644 index 00000000000..fb04dba88c7 --- /dev/null +++ b/tests/unit/src/unit/issues/Issue10482.hx @@ -0,0 +1,72 @@ +package unit.issues; + +class Issue10482 extends Test { + function test() { + final ab = new MyAbstract(1); + final arr = ab.foo(); + eq('plus 2', arr[0]); + eq("call", arr[1]); + eq("hi", arr[2]); + + eq(1, ab.arr()[0].arr()[0].value()); + eq("plus 3", ab + 3); + eq("call", ab()); + eq("hi", ab.arr()[0].hi()); + + final ab = new MyInlineAbstract(1); + final arr = ab.foo(); + eq('plus 2', arr[0]); + eq("call", arr[1]); + eq("hi", arr[2]); + + eq(1, ab.arr()[0].arr()[0].value()); + eq("plus 3", ab + 3); + eq("call", ab()); + eq("hi", ab.arr()[0].hi()); + } +} + +abstract MyAbstract(Int) { + public function new(a):Void this = a; + + public function foo():Array { + return [ + abstract + 2, + abstract(), + abstract.hi() + ]; + } + + public function hi() return "hi"; + + public function arr() return [abstract]; + + @:op(a()) function call() return "call"; + + @:op(a + b) function plus(b) return 'plus $b'; + + public function value():Int return this; +} + +abstract MyInlineAbstract(Int) { + public inline function new(a):Void this = a; + + public inline function foo():Array { + return [ + abstract + 2, + abstract(), + abstract.hi() + ]; + } + + public function hi() return "hi"; + + public function arr() return [abstract]; + + @:op(a()) function call() return "call"; + + @:op(a + b) function plus(b) return 'plus $b'; + + public function value():Int return this; +} + From 1ef9c3fecd24059cea12887e854fee8007125fa3 Mon Sep 17 00:00:00 2001 From: RblSb Date: Tue, 30 Nov 2021 08:58:37 +0300 Subject: [PATCH 2/6] completion item huh? --- src/context/display/displayToplevel.ml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/context/display/displayToplevel.ml b/src/context/display/displayToplevel.ml index 840d259b990..166aace0b3c 100644 --- a/src/context/display/displayToplevel.ml +++ b/src/context/display/displayToplevel.ml @@ -430,6 +430,9 @@ let collect ctx tk with_type sort = | Some(c,tl) -> add (make_ci_literal "super" (tpair (TInst(c,tl)))) (Some "super") | None -> () end + | FunMemberAbstract -> + let t = TInst(ctx.curclass,List.map snd ctx.curclass.cl_params) in + add (make_ci_literal "abstract" (tpair t)) (Some "abstract"); | _ -> () end; From da83a2187bf72a1b9e47f63dc7921f9a10d2c9c9 Mon Sep 17 00:00:00 2001 From: RblSb Date: Tue, 30 Nov 2021 09:21:08 +0300 Subject: [PATCH 3/6] T_T --- src/typing/typer.ml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/typing/typer.ml b/src/typing/typer.ml index 63ce3c508c9..69204b4d8b6 100644 --- a/src/typing/typer.ml +++ b/src/typing/typer.ml @@ -287,10 +287,7 @@ let rec type_ident_raise ctx i p mode with_type = | (MGet, KAbstractImpl ab) | (MCall _, KAbstractImpl ab) -> let t = TAbstract (ab,[]) in - let vtmp = alloc_var VGenerated "tmp" t p in - let var = mk (TVar (vtmp,Some (get_this ctx p))) t p in - let vexpr = mk (TLocal vtmp) t p in - let block = mk (TBlock [var;vexpr]) t p in + let block = mk (TBlock [(get_this ctx p)]) t p in AKExpr block | _ -> typing_error "Property 'abstract' is reserved and only available in abstracts" p From 680ca03db32203f9410d3cac9c6d167296df07da Mon Sep 17 00:00:00 2001 From: RblSb Date: Thu, 2 Dec 2021 01:05:12 +0300 Subject: [PATCH 4/6] Handle generics --- src/typing/typer.ml | 7 ++++--- tests/unit/src/unit/issues/Issue10482.hx | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/typing/typer.ml b/src/typing/typer.ml index 69204b4d8b6..686f6a11eaf 100644 --- a/src/typing/typer.ml +++ b/src/typing/typer.ml @@ -286,9 +286,10 @@ let rec type_ident_raise ctx i p mode with_type = | MSet _, KAbstractImpl ab -> typing_error "Property 'abstract' is read-only" p; | (MGet, KAbstractImpl ab) | (MCall _, KAbstractImpl ab) -> - let t = TAbstract (ab,[]) in - let block = mk (TBlock [(get_this ctx p)]) t p in - AKExpr block + let tl = List.map snd ab.a_params in + let e = get_this ctx p in + let e = {e with etype = TAbstract (ab,tl)} in + AKExpr e | _ -> typing_error "Property 'abstract' is reserved and only available in abstracts" p end diff --git a/tests/unit/src/unit/issues/Issue10482.hx b/tests/unit/src/unit/issues/Issue10482.hx index fb04dba88c7..d4ad28929d7 100644 --- a/tests/unit/src/unit/issues/Issue10482.hx +++ b/tests/unit/src/unit/issues/Issue10482.hx @@ -23,6 +23,9 @@ class Issue10482 extends Test { eq("plus 3", ab + 3); eq("call", ab()); eq("hi", ab.arr()[0].hi()); + + final ab = new AbGeneric(1.5); + eq(1.5, ab.foo()); } } @@ -70,3 +73,14 @@ abstract MyInlineAbstract(Int) { public function value():Int return this; } +abstract AbGeneric(T) { + public function new(a:T):Void { + this = a; + } + public function foo() { + return abstract.bar(); + } + public function bar() { + return this; + } +} \ No newline at end of file From 908eafb2b06e7568758cc6b01fcfe61aab129dec Mon Sep 17 00:00:00 2001 From: RblSb Date: Thu, 2 Dec 2021 01:13:30 +0300 Subject: [PATCH 5/6] Nah, block --- src/typing/typer.ml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/typing/typer.ml b/src/typing/typer.ml index 686f6a11eaf..6c975585ba9 100644 --- a/src/typing/typer.ml +++ b/src/typing/typer.ml @@ -287,8 +287,8 @@ let rec type_ident_raise ctx i p mode with_type = | (MGet, KAbstractImpl ab) | (MCall _, KAbstractImpl ab) -> let tl = List.map snd ab.a_params in - let e = get_this ctx p in - let e = {e with etype = TAbstract (ab,tl)} in + let t = TAbstract (ab,tl) in + let e = mk (TBlock [(get_this ctx p)]) t p in AKExpr e | _ -> typing_error "Property 'abstract' is reserved and only available in abstracts" p From 16fb148cd16cda6fa398dfdf988333b5edfb1c63 Mon Sep 17 00:00:00 2001 From: RblSb Date: Fri, 3 Dec 2021 06:01:25 +0300 Subject: [PATCH 6/6] Revert block again, add display test --- src/typing/typer.ml | 4 ++-- tests/display/src/cases/Abstract.hx | 35 +++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/typing/typer.ml b/src/typing/typer.ml index 6c975585ba9..686f6a11eaf 100644 --- a/src/typing/typer.ml +++ b/src/typing/typer.ml @@ -287,8 +287,8 @@ let rec type_ident_raise ctx i p mode with_type = | (MGet, KAbstractImpl ab) | (MCall _, KAbstractImpl ab) -> let tl = List.map snd ab.a_params in - let t = TAbstract (ab,tl) in - let e = mk (TBlock [(get_this ctx p)]) t p in + let e = get_this ctx p in + let e = {e with etype = TAbstract (ab,tl)} in AKExpr e | _ -> typing_error "Property 'abstract' is reserved and only available in abstracts" p diff --git a/tests/display/src/cases/Abstract.hx b/tests/display/src/cases/Abstract.hx index b6ac3329a66..8df9c62a9e1 100644 --- a/tests/display/src/cases/Abstract.hx +++ b/tests/display/src/cases/Abstract.hx @@ -44,4 +44,39 @@ class Abstract extends DisplayTestCase { eq(false, hasField(fields, "instanceField", "() -> Void")); eq(true, hasField(fields, "staticField", "() -> Void")); } + + /** + abstract MyAbstract(String) { + public function new() this = "foo"; + + public function instanceField():Void { + {-1-} + } + static public function staticField():Void { + {-2-} + } + public function instanceField2():Void { + ab{-3-}stract; + } + } + abstract AbGeneric(T) { + public function new(a:T) this = a; + public function foo() { + return ab{-4-}stract.bar(); + } + public function bar() { + return th{-5-}is; + } + } + **/ + function test3():Void { + final fields = toplevel(pos(1)); + eq(true, hasToplevel(fields, "literal", "abstract")); + final fields = toplevel(pos(2)); + eq(false, hasToplevel(fields, "literal", "abstract")); + // TODO: improve display hints + // eq("cases.MyAbstract", type(pos(3))); + // eq("cases.AbGeneric", type(pos(4))); + eq("cases.AbGeneric.T", type(pos(5))); + } }