Skip to content

Commit

Permalink
add missing recursion when checking abstract casts
Browse files Browse the repository at this point in the history
closes #11676
  • Loading branch information
Simn committed Jun 12, 2024
1 parent ec35bac commit 2106be4
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/context/abstractCast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ let find_multitype_specialization com a pl p =
cf, follow m

let handle_abstract_casts ctx e =
let rec loop ctx e = match e.eexpr with
let rec loop e = match e.eexpr with
| TNew({cl_kind = KAbstractImpl a} as c,pl,el) ->
if not (Meta.has Meta.MultiType a.a_meta) then begin
(* This must have been a @:generic expansion with a { new } constraint (issue #4364). In this case
Expand Down Expand Up @@ -296,10 +296,11 @@ let handle_abstract_casts ctx e =
| TCast(e2,None) ->
{e1 with eexpr = TCast(find_field e2,None)}
| TField(e2,fa) ->
let e2 = loop e2 in
let a,pl,e2 = find_abstract e2 e2.etype in
let m = Abstract.get_underlying_type a pl in
let fname = field_name fa in
let el = List.map (loop ctx) el in
let el = List.map loop el in
begin try
let fa = quick_field m fname in
let get_fun_type t = match follow t with
Expand Down Expand Up @@ -353,11 +354,11 @@ let handle_abstract_casts ctx e =
in
find_field e1
with Not_found ->
Type.map_expr (loop ctx) e
Type.map_expr loop e
end
| _ ->
Type.map_expr (loop ctx) e
Type.map_expr loop e
in
loop ctx e
loop e
;;
Typecore.cast_or_unify_raise_ref := cast_or_unify_raise
55 changes: 55 additions & 0 deletions tests/misc/projects/Issue11676/Main.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
class Main {
static function main() {
var comp = new AComponent([1, 2, 3]);
trace(comp.doSomething());
}
}

interface Component<T> {
function doSomething():T;
}

@:forward
@:multiType
abstract AComponent<T>(Component<T>) {
public function new(value:T);

@:to public static inline function toInt(t:Component<Int>, value:Int):IntComponent {
return new IntComponent(value);
}

@:to public static inline function toIntArray(t:Component<Array<Int>>, value:Array<Int>):ArrayComponent<Int> {
return new ArrayComponent(value);
}
}

@:generic
@:remove
class ArrayComponent<T> implements Component<Array<T>> {
final value:Array<T>;

public function new(value:Array<T>) {
this.value = value;
var x = [];
for (i in 0...value.length) {
var y = new AComponent(this.value[i]).doSomething();
x.push(y);
}
}

public function doSomething():Array<T> {
return this.value;
}
}

class IntComponent implements Component<Int> {
final value:Int;

public function new(value:Int) {
this.value = value;
}

public function doSomething():Int {
return value;
}
}
2 changes: 2 additions & 0 deletions tests/misc/projects/Issue11676/compile.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-m Main
--interp
1 change: 1 addition & 0 deletions tests/misc/projects/Issue11676/compile.hxml.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Main.hx:4: [1,2,3]

0 comments on commit 2106be4

Please sign in to comment.