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

Add flag to raise SyntaxErrors in eval at runtime #2310

Open
richardboehme opened this issue Nov 8, 2024 · 4 comments
Open

Add flag to raise SyntaxErrors in eval at runtime #2310

richardboehme opened this issue Nov 8, 2024 · 4 comments

Comments

@richardboehme
Copy link
Contributor

We implement eval with constant strings by compiling those strings during compile time. If an error occurs we fail compilation. This is a pretty nice feature of Natalie to ensure that code in evals is actually valid. However this also deviates from the behavior of ruby which only raises if the eval is actually called.

This is especially noticeable in our nightly spec runner. Currently there are at least 3 spec files that cannot compile because the spec explicitly tests that specific code raises a SyntaxError by using eval. One example:

it "raises a SyntaxError if using the argument in its default value" do
  a = 1
 -> {
    eval "proc { |a=a| a }"
  }.should raise_error(SyntaxError)
end

What do you think about this?

@herwinw
Copy link
Member

herwinw commented Nov 8, 2024

https://github.com/natalie-lang/natalie/blob/master/lib/natalie/compiler/pass1.rb#L2361-L2380

We do something similar with regexp: parse it at compile time, and generate instructions to raise an exception if it cannot be parsed (#2058).
It's one of those things where we are stuck between being a precompiled language while still trying to be like Ruby.

@richardboehme
Copy link
Contributor Author

Interesting! I think we should unify this and either raise at compile time or at runtime depending on the proposed flag?

@seven1m
Copy link
Member

seven1m commented Nov 12, 2024

I'm cool with a CLI flag to change behavior. Should we default to the most-Ruby-like behavior and have the flag enable "extra" checks? (In reality, the checks would be free, due to how we precompile, but that's probably counterintuitive for a user.)

  • Default: runtime error for bad eval
  • --extra-compiler-checks (or some such): compile time error for bad eval

I also like the idea of using the same approach for the regex check.

@richardboehme
Copy link
Contributor Author

I like this approach, staying as close to "normal" ruby behavior as possible!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants