I also noticed that after fiddling with things in the REPL for a while, I no longer get the type warning. However, I see the type warning again after restarting the REPL.
The problem here seems to be that inlining gives up for x → x.^2, probably because it has to go through the whole broadcast machinery twice. This fixes it: @code_warntype (x->(Base.@_inline_meta; x.^2)).((1,)).
If you don’t need all the broadcasting semantics, you’re usually better off just using map instead, e.g. map(x->x.^2, (1,)) also infers just fine.
The latter map implementation is exactly what I was looking for.
I think I was under the wrong impression that broadcasting and map were somewhat equivalent.
I think the problem you are running into here is that the machinery to make broadcasting work puts quite a bit of strain on the compiler and because you are trying to broadcast twice here, it seems to fail to inline x -> x.^2. You can encourage inlining manually like this:
(note that on Julia versions older than 1.5, you need to write that as (x -> (@_inline_meta; x.^2)).((1,)))
A more general workaround for code like this is to rely less on broadcasting and prefer functions like map, which make inference’s job a lot easier. For example, this also infers just fine: