implicit save and floating point literal gotchas #731
Merged
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.
Implicit saves
(https://fortran-lang.org/learn/quickstart/gotchas/#implied-save) is a gotcha in Fortran that can lead to unintended behavior. Writing something likeinteger :: c=0
is a one-shot initialization, where subsequent calls of the function will actually remember the variable value if changed.As a first pass at identifying possible issues, I am looking for one-shot declarations using:
grep -v "parameter" */*/*f90 | grep "::.*=" | grep -v pointer | grep -v save
These can be solved by adding an explicit
save
orparameter
keyword when needed. Or, if the variable should not be saved, then set its value in a new line.So far, I've just added some obvious parameter keywords. In the future we can detect possible issues with compiler flags, but just want to clean up obvious fixes first. There should not be an impact on the code results.
There is also the issue that
1_dp
and1.0_dp
are not the same thing in Fortran (see: https://fortran-lang.org/learn/quickstart/gotchas/#floating-point-literal-constants-again). The former actually becomes a integer literal constant, and can have the wrong consequences if we are using them to compute real numbers. So I made a few changes in the code fixing these. It should not have an effect because the issue usually shows up when you are composing 2 of these integer literal constants, but is better practice to have the.0_dp
added