It relies on repr being invariant under quote interpolation, which may not be the case. For simple cases like integers and floats this works, because these objects are literals & placed into the IR directly, but for more complicated expressions this is no longer the case:
julia> struct Foo
a::Int
end
julia> test_constant_folding(() -> Foo(1), Foo(1))
Test Failed at REPL[9]:10
Expression: repr(only(code)) == repr(:(return $r))
Evaluated: ":(return \$(QuoteNode(Foo(1))))" == ":(return Foo(1))"
ERROR: There was an error during testing
Even without repr your property doesn’t hold, because interpolation is not in general the same as when a value is constructed by the compiler:
#=
dump(only(code))
cmp = :(return $r)
dump(cmp)
=#
Core.ReturnNode
val: QuoteNode
value: Foo
a: Int64 1
Expr
head: Symbol return
args: Array{Any}((1,))
1: Foo
a: Int64 1
That’s why I’m saying that the “most blessed” way of doing this is through the constant folding effects/type inference.