-
-
Notifications
You must be signed in to change notification settings - Fork 52
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
Chocking on interpolation with interpolation #249
Comments
2 issues play in here: First: There are dynamic strings that produce AST that can only be the result of parsing heredocs. And the signature for these ASTs of "must have been a heredoc and can only be produced from a heredoc" are almost random. Unparser has some heuristics to trigger heredoc emits, and sometimes (which happened here): We end up with triggering heredocs where we should not have. But somtimes emitting more heredocs makes the emitter code easier, while still producing the same AST on parsing. Second: Heredocs need 'parallel' dispatch, as they have a very dislocal effect on the token stream. Its likely that one of the parrallel all sides is missing hence not getting you the body of the heredoc, only the header. Thanks for reporting: Right now, I'm super busy commercially, assume this one takes a very long time to fix. |
Markus, |
@akimd its my end goal that unparser can round trip ALL ast that parser emits. Which may mean I'll have to ask the parser team to simplfy the ast. I'm close to ask them to give me a dedicated node for heredocs in the original source, this would make my part so much easier. |
@akimd I recently spend a bit more time to think about the problem. Overall I've to conclude that the current Not lossy for execution semantics, but lossy in terms of round trip semantics. Meaning you cloud certainly implement a ruby on top of that AST that executes correctly, but you cannot implement an unparser that produces source that produces the same AST, as critical information is lost/noisy. I've got a local AST builder that subclasses the offiical parser builder producing an AST that does not suffer this compression losses. Now the question is: Whats your use case? Do you round trip hand written ruby, (That may include heredocs / dynamic strings)? Or do you generate an AST from other sources to than unparse with unparser? |
Hey Markus, Unparser is the final part of a parse|transform|unparse pipe. We don't care about exact syntactic round tripping, we just care about the (execution) semantics. Cheers! |
ageless version ;)
You are using |
Markus, |
@akimd |
Markus, |
So are you using the modernized AST Format or not? |
Hi Markus, |
kk, so as a recomemndation: The parsers default AST format is lossy on semantics, and unparser only supports the modern one for that reason. Also all other major users of parser opt into the modern format for that reason, and it can very well be that you have semantic issues as you process the default format. |
Also a fix for this issue will probably only ever land in the modern AST format. |
I perfectly understand all these points, but the decision is not mine. I'm just a proxy here. |
@akimd I'd like to emphasize here that the "default parser AST" is often wrong when it comes to execution semantics! If the semantics of the executed ruby are important: Its very much recommended to opt into modern AST format. |
Hi Markus, |
@akimd Is this still affecting you? I can't promise anything, but given what I ran into was a very similar problem (see the mention above), and I found a solution that worked for that case, I might see if I can get a chance to look into this one too. I reduced the error case at the start of this ticket to this (the ternary doesn't appear to make a difference at all):
Which still produces this (after applying my fix from the other ticket):
That puts the location of the missing heredoc output somewhere in a relatively small number of places. |
Hi @vidarh, I'm not in charge of the piece of code where this first happened (and I don't have access to it). So by necessity workarounds must have been installed, and I don't know which. IOW, we no longer depend on this. Cheers! |
* This is an entirely new approach. * Instead to find the "correct" dstr segments we simply try all and unparse the first one that round trips. * This so far guarantees we always get good concrete syntax, but it can be time intensive as the combinatoric space of possible dynamic string sequence is quadratic with the dstr children size. * For this reason we try above (currently) dstr children to unparse as heredoc first. * Passes the entire corpus and fixes bugs. [fix #249]
@akimd I found a fix for this issue (in fact the entire dstr saga): #366 That branch does not pass CI at the time of me posting this comment (still working through mutation coverage)but check this out (run from that branch):
|
Hi Markus, I think I face the same issue again, under a different disguise. require 'parser/current'
require 'unparser'
ast = Unparser.parse(<<~'RUBY')
def get_val(var: nil, **opts)
foo format: <<~JS
a;
#{transform};
b;
JS
end
RUBY
p ast
puts Unparser.unparse(ast)
Cheers! |
@akimd very likely this is fixed by #366 but that PR is not yet ready for prime time. It uses an exhaustive search algorithm to select round tripping dstr segments. And this has factorial runtime, its guaranteed to be correct but for large dstr constructs its to slow. So I've to improve the search algorithm, still this entire issue class may be possible to be eliminated soon. |
* This is an entirely new approach. * Instead to find the "correct" dstr segments we simply try all and unparse the first one that round trips. * This so far guarantees we always get good concrete syntax, but it can be time intensive as the combinatoric space of possible dynamic string sequence is quadratic with the dstr children size. * For this reason we try above (currently) dstr children to unparse as heredoc first. * Passes the entire corpus and fixes bugs. [fix #249]
@akimd This is fixed in #366 but #366 is not release ready yet, its a big change on how unparser does dynamic string literals and needs more testing.
As always your bugreports are appreciated! |
* This is an entirely new approach. * Instead to find the "correct" dstr segments we simply try all and unparse the first one that round trips. * This so far guarantees we always get good concrete syntax, but it can be time intensive as the combinatoric space of possible dynamic string sequence is quadratic with the dstr children size. * For this reason we try above (currently) dstr children to unparse as heredoc first. * Passes the entire corpus and fixes bugs. [fix #249]
* This is an entirely new approach. * Instead to find the "correct" dstr segments we simply try all and unparse the first one that round trips. * This so far guarantees we always get good concrete syntax, but it can be time intensive as the combinatoric space of possible dynamic string sequence is quadratic with the dstr children size. * For this reason we try above (currently) dstr children to unparse as heredoc first. * Passes the entire corpus and fixes bugs. [fix #249]
* This is an entirely new approach. * Instead to find the "correct" dstr segments we simply try all and unparse the first one that round trips. * This so far guarantees we always get good concrete syntax, but it can be time intensive as the combinatoric space of possible dynamic string sequence is quadratic with the dstr children size. * For this reason we try above (currently) dstr children to unparse as heredoc first. * Passes the entire corpus and fixes bugs. [fix #249]
* This is an entirely new approach. * Instead to find the "correct" dstr segments we simply try all and unparse the first one that round trips. * This so far guarantees we always get good concrete syntax, but it can be time intensive as the combinatoric space of possible dynamic string sequence is quadratic with the dstr children size. * For this reason we try above (currently) dstr children to unparse as heredoc first. * Passes the entire corpus and fixes bugs. [fix #249]
* This is an entirely new approach. * Instead to find the "correct" dstr segments we simply try all and unparse the first one that round trips. * This so far guarantees we always get good concrete syntax, but it can be time intensive as the combinatoric space of possible dynamic string sequence is quadratic with the dstr children size. * For this reason we try above (currently) dstr children to unparse as heredoc first. * Passes the entire corpus and fixes bugs. [fix #249]
* This is an entirely new approach. * Instead to find the "correct" dstr segments we simply try all and unparse the first one that round trips. * This so far guarantees we always get good concrete syntax, but it can be time intensive as the combinatoric space of possible dynamic string sequence is quadratic with the dstr children size. * For this reason we try above (currently) dstr children to unparse as heredoc first. * Passes the entire corpus and fixes bugs. [fix #249]
* This is an entirely new approach. * Instead to find the "correct" dstr segments we simply try all and unparse the first one that round trips. * This so far guarantees we always get good concrete syntax, but it can be time intensive as the combinatoric space of possible dynamic string sequence is quadratic with the dstr children size. * For this reason we try above (currently) dstr children to unparse as heredoc first. * Passes the entire corpus and fixes bugs. [fix #249]
* This is an entirely new approach. * Instead to find the "correct" dstr segments we simply try all and unparse the first one that round trips. * This so far guarantees we always get good concrete syntax, but it can be time intensive as the combinatoric space of possible dynamic string sequence is quadratic with the dstr children size. * For this reason we try above (currently) dstr children to unparse as heredoc first. * Passes the entire corpus and fixes bugs. [fix #249]
Hi Markus!
Weird things happen when there are interpolations within interpolations.
I agree no human being would write such code. But our generator does :(
Cheers!
The text was updated successfully, but these errors were encountered: