Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assignment to _ sets the binding type of _ #57497

Open
xal-0 opened this issue Feb 21, 2025 · 1 comment
Open

Assignment to _ sets the binding type of _ #57497

xal-0 opened this issue Feb 21, 2025 · 1 comment
Labels
bug Indicates an unexpected problem or unintended behavior compiler:lowering Syntax lowering (compiler front end, 2nd stage)

Comments

@xal-0
Copy link
Member

xal-0 commented Feb 21, 2025

A syntax.jl test documents:

# `const` assignment to `_` drops the assignment effect,
# and the conversion, but not the rhs.
julia> _::Int = 1
1

julia> _::String = "a"
ERROR: cannot set type for global Main._. It already has a value or is already set to a different type.
Stacktrace:
 [1] top-level scope
   @ REPL[2]:1

On both 1.11 and nightly, a set_binding_type!or globaldecl, respectively, sneaks through.

1.11:

julia> Meta.@lower _::Int = 1
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope`
1 ─      global Main._
│   %2 = Int
│        Core.set_binding_type!(Main, :_, %2)
└──      return 1
))))

Nightly:

julia> Meta.@lower _::Int = 1
:($(Expr(:thunk, CodeInfo(
1 ─ %1 = Main.Int
│        $(Expr(:globaldecl, :(Main._), :(%1)))
│        $(Expr(:latestworld))
└──      return 1
))))
@xal-0 xal-0 added bug Indicates an unexpected problem or unintended behavior compiler:lowering Syntax lowering (compiler front end, 2nd stage) labels Feb 21, 2025
@xal-0
Copy link
Member Author

xal-0 commented Feb 21, 2025

Related: when :: appears on the left hand side of an assignment, it has different behaviour depending on whether it is attached to a variable name or another expression. x[]::T = y performs a typeassert on x[], disregarding the type of y.

julia> x = Ref{Any}(123)
Base.RefValue{Any}(123)

julia> x[]::Int = "hello"
"hello"

Lowering of x[]::Int = "hello":

1 ─ %1 = Main.x
│   %2 =   dynamic Base.getindex(%1)
│   %3 = Main.Int
│          dynamic Core.typeassert(%2, %3)
│   %5 = Main.x
│          dynamic Base.setindex!(%5, "hello")
└──      return "hello"

This also has the unfortunate side effect of making x::T, y::U = a, b different from (x, y)::Tuple{T, U} = a, b:

x::T, y::U = a, b:

1 ─ %1  = Main.a
│   %2  = Main.b
│   %3  = Main.T
│         $(Expr(:globaldecl, :(Main.x), :(%3)))
│         $(Expr(:latestworld))
│         $(Expr(:globaldecl, :(Main.x)))
│         $(Expr(:latestworld))
│   %8  =   builtin Core.get_binding_type(Main, :x)
│         #s1 = %1
│   %10 = #s1
│   %11 =   dynamic %10 isa %8
└──       goto #3 if not %11
2 ─       goto #4
3 ─ %14 = #s1
└──       #s1 = Base.convert(%8, %14)
4 ┄ %16 = #s1
│           dynamic Base.setglobal!(Main, :x, %16)
│   %18 = Main.U
│         $(Expr(:globaldecl, :(Main.y), :(%18)))
│         $(Expr(:latestworld))
│         $(Expr(:globaldecl, :(Main.y)))
│         $(Expr(:latestworld))
│   %23 =   builtin Core.get_binding_type(Main, :y)
│         @_2 = %2
│   %25 = @_2
│   %26 =   dynamic %25 isa %23
└──       goto #6 if not %26
5 ─       goto #7
6 ─ %29 = @_2
└──       @_2 = Base.convert(%23, %29)
7 ┄ %31 = @_2
│           dynamic Base.setglobal!(Main, :y, %31)
│   %33 =   dynamic Core.tuple(%1, %2)
└──       return %33

(x, y)::Tuple{T, U} = a, b:

1 ─ %1  = Main.x
│   %2  = Main.y
│   %3  =   dynamic Core.tuple(%1, %2)
│   %4  = Main.Tuple
│   %5  = Main.T
│   %6  = Main.U
│   %7  =   builtin Core.apply_type(%4, %5, %6)
│           dynamic Core.typeassert(%3, %7)
│         $(Expr(:globaldecl, :(Main.x)))
│         $(Expr(:latestworld))
│   %11 = Main.a
│   %12 =   builtin Core.get_binding_type(Main, :x)
│         #s1 = %11
│   %14 = #s1
│   %15 =   dynamic %14 isa %12
└──       goto #3 if not %15
2 ─       goto #4
3 ─ %18 = #s1
└──       #s1 = Base.convert(%12, %18)
4 ┄ %20 = #s1
│           dynamic Base.setglobal!(Main, :x, %20)
│         $(Expr(:globaldecl, :(Main.y)))
│         $(Expr(:latestworld))
│   %24 = Main.b
│   %25 =   builtin Core.get_binding_type(Main, :y)
│         @_2 = %24
│   %27 = @_2
│   %28 =   dynamic %27 isa %25
└──       goto #6 if not %28
5 ─       goto #7
6 ─ %31 = @_2
└──       @_2 = Base.convert(%25, %31)
7 ┄ %33 = @_2
│           dynamic Base.setglobal!(Main, :y, %33)
│   %35 = Main.a
│   %36 = Main.b
│   %37 =   dynamic Core.tuple(%35, %36)
└──       return %37

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior compiler:lowering Syntax lowering (compiler front end, 2nd stage)
Projects
None yet
Development

No branches or pull requests

1 participant