Fix a 5.2 source-quotation-in-error-messages regression for dune+ppxlib#12991
Fix a 5.2 source-quotation-in-error-messages regression for dune+ppxlib#12991gasche wants to merge 1 commit intoocaml:trunkfrom
Conversation
We recently simplified the compiler logic to quote source file fragments, by keeping the whole input file in memory instead of reopening the input file when the error lies outside the lexing buffer. This fixes issue with source files that contain preprocessor directives (typically, they are the output of a source-to-source preprocessor). This simplification is a regression for build systems like Dune that perform a source-to-AST preprocessing step, then call the compiler with the binary AST as input: in this case there is no source input file in memory when printing errors, so source quotations are gone. The present commit restores the fallback logic that we had in 5.1 and earlier to apply in this case, when given AST as input.
|
I decided to write a test for this case, and I realized something confusing: there appears to already be a test for this case, and it passes (on trunk): This test takes an input file with a type error, foo.ml, generates a binary AST file Right now I am a bit confused because I don't understand why this test passes on trunk, and what the difference is with the ppxlib usecase. |
|
The reason why the test succeeds in trunk (without the PR) is that there is already logic in the Pparse module to read the original source of binary ASTs for error-message purposes: Lines 186 to 199 in b2cd7e5 The call to So when provided an input AST, the compiler immediately tries to read the original source file (as indicated in the binary-AST medata) and put it in the lexing buffer. In particular, when we see an error later on, the lexing buffer is available to quote the source (without the additional fallback logic I included in this PR). Now something appears to go wrong in the dune+ppxlib usecase, so that this initial
@anmonteiro being a distinguished OCaml frontend hacker by now, is there a chance that you would be motivated to sort out what is happening here? |
|
Hey @gasche, I believe the reason is that ppxlib calls |
anmonteiro
left a comment
There was a problem hiding this comment.
LGTM and fixes the regression I reported.
The code looks almost the same as what had been in the compiler for a while and then removed in 666de0e
|
The code that you are pointing out is how ppxlib consumes its input. I previously assumed that the regression you reported occurred when the compiler consumes the output of the ppxlib driver. In fact, re-reading your report, the problematic error message is emitted by the ppxlib driver, not the compiler later on, and my fix has an effect because the driver calls the compiler error-printing logic through compiler-libs. Right? |
This is correct. EDIT: and that's why I wrote that I wasn't sure whether this needed to be fixed in ppxlib (perhaps, too, for future-proofing). |
|
The ppxlib fix would be to imitate the compiler by parsing the whole source at once and setting (Of course ppxlib has a weirdly modified copy of the compiler driver because they have an additional use-case (reading source from standard input) that they never bothered to report upstream to have compiler-libs support it.) |
cc @NathanReb |
|
My current thinking: now that I understand the issue better, I think that changing ppxlib may be better than reintroducing (a small amount of) old complexity in the compiler, because both systems would end up simpler. If for some reason fixing ppxlib does not work out, the present PR is better than doing nothing. |
|
I'm a bit rusty on these matters. I'll look into the ppxlib side of things and report back to you ASAP! |
|
From what I understand, it seems that properly setting @anmonteiro do you have a reproduction somewhere that I can work with? I'm a bit surprised as our test suite does rely on error reporting working properly and I didn't see any such failures when working with 5.2 recently. |
Yes, modulo the fact that the compiler now expects |
Nevermind, I reproduced it and added it to our test suite. Most of our test that relied on reporting located exceptions used I'll work on fixing this on ppxlib's |
|
After reflecting for a bit and considering other use cases, I think I found one that justifies this change:
|
|
Since the regression is fixed in ppxlib, I am removing this PR from the 5.2 milestone. I am not sure if it still is worth it to discuss this PR in the light of melange? |
|
@anmonteiro what do you think? |
|
This is not necessary for Melange if it's fixed in ppxlib, apart from the newly broken usecase that all OCaml programs will also face: from #12991 (comment)
So if you're comfortable breaking source quotations for alerts emitted from a PPX (which I don't even know if it's a supported use case, but we're using it in melange), then I'm OK closing it. |
|
This is not such a big change in the compiler, so I would be tempted to try to accomodate these niche (but not unreasonable) use-cases. @Octachron, what do you think? |
|
I am not sure I follow why the alert printer would behave differently than the error printer? |
|
They’re different printers. In that case the error printer would come from ppxlib (which now has this fix), but the alert printer would come from |
|
They are the same printers? The |
|
Yes I would assume it just works but I can add a test for it and check! |
Got it, thanks. This seems to be a melange-specific issue. I created a failing repro that passes with OCaml + ppxlib. OK to close for me. |
|
Actually, revisiting this again, it doesn't seem to be a Melange bug at all. I prepared a reproduction in this repository: https://github.com/anmonteiro/no-source-quotation-repro If you run the instructions in the README (building a ppx rewriter and passing it to ocamlc as If you run it with OCaml 5.1, the source quotation is present. |
|
OK, so I changed my mind again: there is a bug, but it's not in OCaml. Turns out, I believe the ppxlib fix is incomplete. When reading binary AST, we want to set the input lexbuf as well. I submitted ocaml-ppx/ppxlib#483 with an example implementation. |
|
My understanding -- for now -- is that having the fix on the ppxlib side worked, and that there is no change necessary in the compiler now. So I am closing this. Please let me know if my understanding is wrong and we can reconsider. |
|
I am hitting an issue today that looks suspiciously close to the issue discussed above. There seems to be a regression in 5.4 compared to 5.3, the file where the error occurs is not reported anymore. It is necessary for some ppx to be configured for the bug to occur (otherwise the error is located correctly). This is using ppxlib 0.37.0. @anmonteiro, @NathanReb, do you have an idea whether this is the same issue or something different? My understanding was that the issue had been fixed in ppxlib, does this turn out to be wrong? |
#12403 simplified the compiler logic to quote source file fragments, by keeping the whole input file in memory instead of reopening the input file when the error lies outside the lexing buffer. This fixed issue with source files that contain preprocessor directives (typically, they are the output of a source-to-source preprocessor), see #12238.
This simplification is a regression for build systems like Dune that perform a source-to-AST preprocessing step, then call the compiler with the binary AST as input: in this case there is no source input file in memory when printing errors, so source quotations are gone. This was reported by @anmonteiro in #12403 (comment) .
The present PR restores the fallback logic that we had in 5.1 and earlier to apply in this case: when there is nothing in the lexing buffer, try to reopen the input source file. (I suspect that this reintroduces the possibility of showing incoherent source fragments when the input-file information is wrong, but oh well.)
@anmonteiro tested the patch and confirms that it fixes the dune+ppxlib issue.