@@ -419,7 +419,7 @@ function gen_loop(inds, inds_ax, fn_args, lhs, rhs, opts...)
419419 end
420420 ret_name = lhs. args[1 ]
421421 ret_size = Expr[]
422- ret_type = _infer_ret_type (inds, fn_args, rhs)
422+ ret_type = _infer_ret_type (inds, inds_ax, fn_args, rhs)
423423 loop_head = Expr (:block )
424424 for (i, ind) in enumerate (inds)
425425 array_name, dims = first (inds_ax[ind])
@@ -480,16 +480,16 @@ function _parse_opt(opt::Expr)
480480 end
481481end
482482
483- function _infer_ret_type (inds, fn_args, ex)
484- ex_replace = MacroTools . postwalk ( ex) do sym
485- if sym isa Symbol && sym in inds
486- return :begin # replace the iteration symbol with first index
487- else
488- return sym
489- end
483+ function _infer_ret_type (inds, inds_ax, fn_args, ex)
484+ ex′ = Expr ( :block , ex)
485+ for ind in inds
486+ array_name, dims = first (inds_ax[ind])
487+ dim = first (dims)
488+ assignment = :( $ ind = firstindex ( $ array_name, $ dim))
489+ pushfirst! (ex′ . args, assignment)
490490 end
491491 args = collect (fn_args)
492- promote_op_fn = Expr (:(-> ), Expr (:tuple , args... ), ex_replace )
492+ promote_op_fn = Expr (:(-> ), Expr (:tuple , args... ), ex′ )
493493 args_type = map (arg -> :(typeof ($ arg)), args)
494494 ret_type = :(Base. promote_op ($ promote_op_fn, $ (args_type... )))
495495 return ret_type # expression for inferring the return type
0 commit comments