Skip to content
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

Warn about about allocation on assignment when reallocation may occur #248

Open
Beliavsky opened this issue Dec 20, 2024 · 2 comments
Open
Labels
rule A new rule for the linter

Comments

@Beliavsky
Copy link

Allocation on assignment of an allocatable array is part of the language, but sometimes people do it inadvertently (I have), so an optional warning could be useful. Ideally the warning would not occur when the allocatable array is set to an array of the same size or to a scalar. For example,

integer, allocatable :: v(:), w(:)
v = [3, 5] ! warn
w = [2, 4, 6] ! warn
v = 0 ! don't warn
v = v + 10 ! don't warn
v = w ! warn

@certik has a relevant post https://fortran-lang.discourse.group/t/allocatable-vs-adjustable/8957/59

@ZedThree ZedThree added the rule A new rule for the linter label Dec 20, 2024
@ZedThree
Copy link
Member

This probably requires some heavier static analysis than what we can currently do -- actually, in general, this might only be possible at runtime.

@certik
Copy link

certik commented Dec 20, 2024

I think @Beliavsky's idea is to only warn if the compiler / static analyzer can't figure out and guarantee at compile time that no reallocation will happen. This can't be done in general, so there will be some warnings, and a user can silence them by converting to v(:) = [3, 5].

I think that would be useful. The way LFortran does it is that we currently don't allow LHS reallocation by default (you have to enable it with a compiler option). So it forces you to write your code in such a way that the LHS always has the correct pre-allocated size. Then if you compile with LHS reallocation (in any compiler), you know it will not reallocate silently, you will only incur the (hopefully small) runtime check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule A new rule for the linter
Projects
None yet
Development

No branches or pull requests

3 participants