Skip to content

Conversation

@sabiwara
Copy link
Contributor

@sabiwara sabiwara commented Nov 2, 2024

I didn't fix all tests yet, this is a proof of concept for discussion.

The idea is to introduce a shallow validation when using unquote, to give early feedback to meta-programmers when building obviously invalid AST which might fail later down the lane in a way that would then be non-obvious and harder to debug.

In order to avoid any slowdown especially when building complex ASTs, we do not loop on the whole AST (args just have to be a list), but this could catch many trivial errors e.g. forgetting to escape a map.
I had the idea on the back of my mind for a while, but #13948 reminded me of it.

Note: This could perhaps just be a warning for now if we want to be conservative.

@josevalim
Copy link
Member

LGTM.


shallow_valid_ast(Expr) when is_list(Expr) -> lists:all(fun shallow_valid_ast/1, Expr);
shallow_valid_ast(Expr) when is_atom(Expr); is_binary(Expr); is_number(Expr); is_pid(Expr) -> true;
shallow_valid_ast({Left, Right}) -> shallow_valid_ast(Left) andalso shallow_valid_ast(Right);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am thinking we should skip this one... the combination of traversing lists and two-element tuples mean we can traverse large keywords? 🤔 Or perhaps we pass a flag to control the level, so we don't recur beyond the first 2-elem tuple/list?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keywords was what I had in mind, I didn't think about large ones (is it so common in AST?)
I like the idea of having some nested flag to avoid doing a deep check 👍 will update

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 96bb33a.

  • we can now only check one depth of lists
  • I don't think having deeply nested size-2 tuples is such a realistic scenario, so I'm not checking for the depth there to keep it simple

@sabiwara sabiwara merged commit cc68ad9 into elixir-lang:main Nov 3, 2024
9 checks passed
@sabiwara sabiwara deleted the invalid-unquote branch November 3, 2024 09:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants