- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi all,
I use ifort 15.0.0.108 and observed the following issue:
If I use the [fortran]use module, only : var_a[/fortran] instead of [fortran]use module[/fortran] in an interface I get IMHO a false positive warning of unused variables. This can be seen best in the following example compiled with ‘/warn:unuse’:
warning_unused.f90:
[fortran]
program warning_unused
use mod_bar
implicit none
! Interfaces
interface
subroutine foo(text)
!use mod_bar ! as expected no waring
use mod_bar, only : var_a, var_b ! warning_unused.f90(22): remark #7712: This variable has not been used. [VAR_A] [VAR_B]
implicit none
character(len=*) :: text
end subroutine foo
end interface
! Variables
character(len=20) :: text
! Body of warning_unused
text = 'Hello World'
call foo(trim(text))
end program warning_unused
mod_bar.f90:
module mod_bar
integer :: var_a, var_b
end module mod_bar
foo.f90:
subroutine foo(text)
use mod_bar, only : var_a, var_b
implicit none
character(len=*) :: text
write(*,'(a)') text
var_a = 10
var_b = 20
end subroutine foo
[/fortran]
If I leave out the only statement I get no warning, as I expected. But with ‘only : var_a, var_b’ I get:
\warning_unused.f90(22): remark #7712: This variable has not been used. [VAR_A]
\warning_unused.f90(22): remark #7712: This variable has not been used. [VAR_B]
This is not what I like to have. Because now I have to look twice on the warning and have to check, whether it is in an interface…
Have I done something wrong?
Best regards, Johannes
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
All the issues are related to just the interface block. The declarations within the interface block do not need anything from the module. Therefore, the unrestricted USE as well as the USE, ONLY line serves no useful purpose in the interface block. In such a case the best response would have been to issue a warning that the USE statement itself was not needed/used, or that none of the entities in the module were used. In addition, if there is an ONLY clause and the items in it are not used in the interface block, I should expect them to be flagged individually as being unused.
In this situation I would perhaps leave the unneeded USE statements in the interface blocks, but commented out, as their main purpose appears to be to warn the programmer that in the corresponding implementation of the subprogram the module(s) are, in fact, needed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I echo mecej4's sentiments. In adding "USE mod_bar" you are saying to the compiler "have a look in module mod_bar for definitions of things" but with the "ONLY : var_a" you are specifically saying you are using var_a from that module, you aren't so it is a valid warning.
Incidently, as mod_bar is used (line 02) in the scope in which the interface is defined you could have just used the "IMPORT" command in the interface which would have made mod_bar available in the interface block if it had been needed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks mecej4 and app4619 for your comments.
I thought before the use ... have to be in the interface, bacause otherwise a not matching interface error will occur. OK, it's not the case, because here the content of the module is not used for interfacing.
The help says:
interface-body |
Is one or more function or subroutine subprograms or a procedure pointer. A function must end with END FUNCTION and a subroutine must end with END SUBROUTINE. The subprogram must not contain a statement function or a DATA, ENTRY, or FORMAT statement; an entry name can be used as a procedure name. The subprogram can contain a USE statement. |
Still it is not clear to me why with the "ONLY : var_a, var_b" statement the warning occurs and not if I use the whole content of the module, which is the same here. In both cases var_a and var_b are not used in foo. I think it is a bit inconsequent. Nevertheless, it would be nice if the warning could be more differentiated in this point.
For future, I will take the advice from mecej4 and uncomment the use statements in the interface, whether they have the 'only' attribute or not. In this way others can see that the subroutines makes use of modules without to open them.
Thanks for clarification,
Johannes
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Without the ONLY, all the definitions from the module are accessible but not actually referenced in the source. With the ONLY you have an explicit reference, effectively a local declaration.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi Steve,
thanks for clarifying the difference with the only attribute. However, sometimes I need from a parameter module only the working precision (real kind = rk) parameter in the interface but in the actual subroutine some more general parameter (e.g. pi). In this case it means that the maintenance of the interface would require more time.
[fortran]use mod_paramterer, only : rk !, pi[/fortran]
Not nice, but I can live with it.
I've read in another forum that one should avoid using the whole content of a big module, because the namespace is polluted (big = 30 to 40 scalar parameters in a module). Before I read about namespace pollution, I used to use the whole content of a module and encountered no issue? What can happen if I don't use the only attribute? If it makes no problem in procedural Fortran, I would stick to old way, because the interfaces a easier to maintain...
Best regards,
Johannes
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Johannes wrote:
.. sometimes I need from a parameter module only the working precision (real kind = rk) parameter in the interface but in the actual subroutine some more general parameter (e.g. pi). In this case it means that the maintenance of the interface would require more time.
use mod_paramterer, only : rk !, pi
Not nice, but I can live with it.
..
As mentioned by app4619 in Quote #3, look into IMPORT statement which was added to the language standard to address such issues. With this feature, generally you will not require USE statements in INTERFACE blocks and you can use constructs such as the one shown below:
MODULE Bar USE mod_parameter, ONLY : RK, PI, .. USE Foo, ONLY : ... IMPLICIT NONE ... INTERFACE SUBROUTINE SomeSub(..) IMPORT :: RK ... REAL(RK), ..
My preference is to always add the , ONLY : construct to USE statements. It provides several benefits including the prevention of namespace pollution as you mention. And I like the /warn:unused compiler flag getting extended to entities brought in via the USE statement; it helps me clean up a lot of existing code.
Separately, consider minimizing the use of INTERFACE blocks only to procedures invoked from "external" (for which you may not have handy Fortran interfaces) entities e.g., 3rd party libraries, mixed-language scenarios, etc. Where you have control in terms of Fortran code, consider module procedures.

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page