Skip to content

Modifyable ArrayLiterals in AST cause unspecified behavior #15740

Open
@BlobCodes

Description

@BlobCodes

Discussion

Currently, crystal allows modifying all information stored in array/tuple literals in the AST in macros (real literals, not ArrayLiterals obtained from macro methods, those copy their contents).
The current behavior resulting from modifying those literals could be unexpected (and untested?).

I think it would be best to make all arrays already merged into the AST copy on access (or COW?), except const arrays (since those are very often used for storing variable macro state).

Example:

class Foo
  def bar(arg) : Array(Int32)
    [0]
  end
end

puts Foo.new.bar(1) #=> [0]

def change_method
  {% Foo.methods.find(&.name.==("bar")).body[0] = 1 %}
end
change_method

puts Foo.new.bar("test") #=> [1]
puts Foo.new.bar(1) #=> [0]
puts Foo.new.bar(1u8) #=> [1]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions