natalie-lang / natalie

a work-in-progress Ruby compiler, written in Ruby and C++
https://natalie-lang.org
MIT License
931 stars 61 forks source link

Bug with evaluation of method argument default value #2201

Open seven1m opened 1 month ago

seven1m commented 1 month ago

We shouldn't always evaluate the default value of an argument. Given the following code:

# default.rb

def foo(bar = (bar_missing = true))
  p(bar:, bar_missing:)
end

foo
foo(1)

CRuby produces:

→ ruby default.rb                                                                                                                                                                
{:bar=>true, :bar_missing=>true}
{:bar=>1, :bar_missing=>nil}

But Natalie produces:

→ bin/natalie default.rb
{:bar=>true, :bar_missing=>true}
{:bar=>1, :bar_missing=>true}
seven1m commented 1 month ago

I guess a more straightforward way to see this is:

def bar
  raise 'wat'
end

def foo(arg = bar)
  arg
end

p foo(1)

This raises an error with Natalie.