Fix handling of Exit_compiler exception in toplevel#9798
Conversation
dra27
left a comment
There was a problem hiding this comment.
LGTM - changes need to go in ocamlnat as well
b500590 to
f6f1380
Compare
Thanks for the quick review. I pushed the missing changes ( |
toplevel/opttoploop.ml
Outdated
| ignore(execute_phrase true ppf phr) | ||
| with | ||
| | End_of_file -> exit 0 | ||
| | End_of_file -> raise Compenv.Exit_compiler 0 |
There was a problem hiding this comment.
| | End_of_file -> raise Compenv.Exit_compiler 0 | |
| | End_of_file -> raise (Compenv.Exit_compiler 0) |
|
One thought - it's easy to accidentally add code which calls let main () =
match main () with
| exception Exit_compiler n -> n
| () -> 0
let main () =
let incorrect_exit = ref true in
let _ = at_exit (fun () -> if !incorrect_exit then Printf.eprintf "Stop the world; report a bug;\n"; exit 2) in
let result = main () in
incorrect_exit := false;
result |
The More generally, I don't think this kind of thing is necessary in |
|
Naive question: why would the toplevel use |
035fda0 to
e335feb
Compare
Fair question, I guess... Are you suggesting that it should use a different exception or that the name should be adjusted? For a name adjustment, I could think of something more general like |
|
Not having looked in details at the PR that introduced the change, I cannot tell what was the intended scope of the exit-protocol change, but the name suggests that it should have been restricted to ocaml{c,opt}. The fact that it silently broke something in the toplevel is a bit worrying. One fix may be to propagate the exit-protocol change in the toplevel as you suggest (but then we probably need a better name, and also to think twice about what the scope of the change really was, to understand whether other places (ocamldep ?) are affected as well). But maybe another fix would be to restrict the scope of the change, so that the toplevel is not affected as originally intended? (And if the latter also works, which of the two approaches do we believe is the better one?) |
Using an exception instead of calling Lines 1876 to 1884 in e41dc9c (in case we would like to restrict, rather than extend, the changes in #9688). |
|
We would of course severely frown on any user libraries which called For the |
|
@nojb thanks!
It was intended to only affect ocaml{c,opt}, but I forgot that other programs use the compiler's Main_args for options parsing. (Main_args exits the process during option parsing if it sees The same problem appears in other programs that use the compiler's option parsing. The list is: (
I wanted to avoid confusion with |
Unless there is an objection, I can do the changes for the rest of the tools and switch to the new name. Speak up if you think it is a bad idea! |
|
(I'm happy with |
|
I renamed the exception and adapted the rest of the tools in @stedolan's list. Review welcome! |
|
The code looks fine, but #9688 having essentially leaked to utilities as well is giving me second thoughts about how this is being done. It's fine to have to handle the |
I agree that the exception handling becomes too subtle and unintuitive. Perhaps another solution is simply not to "exit" after handling |
|
Instead of exiting, the main command-line interpreter could return a (I agree that it's an API smell to have a function named |
That would be nicer, but is incompatible with the API of Alternatively, we could move a copy of Arg into the compiler proper and give it a |
|
My idea was to expose an entry point somewhere to process command-line arguments, that would internally catch the exception and expose a |
|
That makes sense to me. There will be a bit of work needed because there are several different option-parsing entrypoints at the moment (some tools use If you do this refactoring, it would also be nice to refactor the handling of |
|
It looks like this PR started as a fix for an actual bug and is now lost in "wouldn't it be nicer if..." territory. Could the author and reviewers agree quickly on something that at least fixes the bug? |
|
I think there are three options:
I like the refactoring, but I don't have time before 4.12, and I expect that if the more temporary fix goes in then the refactoring will be a loooong way off. Thoughts? |
|
Sorry for having dropped the ball on this one. I am looking into implementing @gasche's suggestion. Something I noticed is that currently |
|
At the risk of sounding like a broken record: we have a bug here (e.g. Coq's autoconfiguration calls |
491f949 to
c1b00d8
Compare
|
OK, I rebased the current version of this PR (the "clunky fix"). |
|
No |
xavierleroy
left a comment
There was a problem hiding this comment.
From a quick reading of the code, this looks OK. I'm not a fan of the name Exit_status for the exception. Exit_with_status would be slightly better. Just Exit would work fine, and there's already enough qualification Compenv.Exit to avoid confusion with the Exit exception from the standard library.
Thanks. I went with |
xavierleroy
left a comment
There was a problem hiding this comment.
Thanks for the renaming. Let's merge before we change our minds again :-) This doesn't preclude a nicer refactoring later, but I prefer when bugs introduced by prior refactorings are fixed promptly.
Co-authored-by: Nicolás Ojeda Bär <n.oje.bar@gmail.com>
Adapts toplevel in the style of #9688 and fixes the issue reported in #9699 (comment).
cc @stedolan