Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR #51635 introduced function
check_src_module_wrap
which checks whether a module declaration is missing or whether ausing
/import
is declared before a module.The original implementation, based on manual checks via regexes, did not catch all possible cases. In particular, it did not consider multi-line comments, e.g.,
threw errors even though it should be valid.
Single-line comments, multi-line comments, and docstrings make it quite difficult to reliably check that a module is declared and not preceded by any using/import. Some examples are
Because of that, this commit avoids all manually-written, specialized conditions and employs
Meta.parse
to parse the code intoExpr
s. It then becomes very simple to check the condition of interest. The downside is increased runtime.[WITHOUT the shortcut; see bellow] I measured the time it takes to check three files of different sizes (the first measurements is the current version, the second is master):
The slowdown is dramatic (most of the time is spent reading the source file into a String).
I added a condition that checks if the file content starts with "module ", in which case the function returns immediatelly. This shortcut helps: LibSCIP.jl takes only 5.865 μs (instead of 102.265 ms). Of course, the shortcut is taken only in some cases...
I am submitting this PR anyway for discussion (CC: original author @IanButterworth).
Fixes #55792