julia icon indicating copy to clipboard operation
julia copied to clipboard

"@inferred requires a call expression" for do notation

Open aplavin opened this issue 4 months ago • 4 comments

Lambda functions work fine, while the same do-block throws an error:

julia> using Test

julia> @inferred map(x->x, [1,2])
2-element Vector{Int64}:
 1
 2

julia> @inferred map([1,2]) do x
       x
       end
ERROR: LoadError: @inferred requires a call expression

aplavin avatar Jul 28 '25 04:07 aplavin

gen_call_with_extracted_types already supports the required rewrite, just needs a few small tweaks in the macro itself.

Keno avatar Jul 29 '25 06:07 Keno

@aplavin Hi! 👋 I'd like to work on this issue.

The behavior here appears to come from the fact that @inferred only accepts a call expression in its macro expansion, and the do-block form (map([1,2]) do x ... end) lowers into a different AST shape than a standard call. As a result, the macro rejects the input before inference is even attempted.

I’m interested in implementing a fix so that @inferred supports do-block syntax in the same way it supports anonymous function calls. My plan is to update the macro to normalize or expand do-block forms into an equivalent call expression before the argument checks, allowing the expected inference test to run.

Before I begin preparing a PR, are there any prior discussions, preferred patterns for macro normalization, or compatibility concerns I should be aware of? If the approach sounds reasonable, please assign the issue to me—I’d be happy to take it on.

Thanks!

gsiddharthrao avatar Nov 17 '25 05:11 gsiddharthrao

@gsiddharthrao, Note that while LLMs and AI can be useful help when coding, in this repo, it is quite important to state when such a tool is extensively used, even if it is for writing comments.

Also, we don't, in general, assign issues to people to avoid cookie licking. You can still make a PR without anything being assigned to you.

Note https://github.com/JuliaLang/julia/pull/59463 seems to be an existing PR along this approach.

KristofferC avatar Nov 17 '25 13:11 KristofferC

Hey !! I would like to fix this issue

could you please redirect me to the source code from where it belongs

Mehul-Lakhwani avatar Nov 27 '25 18:11 Mehul-Lakhwani

Hi! I tried reproducing this and it seems related to macro expansion order. @inferred expects a call expression, but do-block syntax is lowered after macros run, so the macro sees a non-call expression and throws an error. Wrapping the expression in parentheses works: julia @inferred (map([1,2]) do x x end)

codermayankgit avatar Dec 17 '25 08:12 codermayankgit

Thanks for the clarification — that makes sense.

If this is considered expected behavior, I’d be happy to work on a small follow-up: either a documentation note mentioning the need for parentheses with do-blocks, or an improved error message pointing this out.

Please let me know what would be preferred.

arkgupta17 avatar Dec 18 '25 05:12 arkgupta17

are these accounts all from the same entity ...?

adienes avatar Dec 18 '25 12:12 adienes

are these accounts all from the same entity ...?

no

codermayankgit avatar Dec 19 '25 03:12 codermayankgit