Silverfrost Forums

Welcome to our forums

Puzzle

10 Oct 2024 11:11 #31607

This is not a bug report, since I cannot determine what a standard conforming compiler should actually do with this code. So it's a puzzle for those that like that kind of thing.

module kind_mod
implicit none
integer, parameter :: dp = kind(1.d0)
end module kind_mod


module x_mod
use kind_mod
implicit none
real(dp) :: a = 1.d0
end module x_mod


module y_mod
use x_mod
implicit none
private            
public b          ! Only b is public, not clear to me if a from x_mod or dp from kind_mod
                  ! should be visible from the main program that uses y_mod
real(dp) :: b = 2.d0
end module y_mod


program main
use y_mod
print*, a         ! FTN95 prints the correct value, is this correct?
print*, b
print*, dp        ! FTN95 prints the correct value, is this correct?
end program main

! AI analysis
! Visibility of a:
! a is defined in x_mod and x_mod is used in y_mod. However, because y_mod declares private, 
! everything in x_mod (including a) should be private unless explicitly made public in y_mod. 
! Since a is not listed as public, it is not visible in main.
! Attempting to print a should cause a compilation error.
!
! Visibility of b:
! b is explicitly made public in y_mod, so it is visible in main and should print the value 
! of 2.0.
!
! Visibility of dp:
! dp is defined in kind_mod and is used by x_mod. x_mod is used by y_mod. While dp is 
! publicly accessible in kind_mod, it is not made private anywhere. Since y_mod does not 
! explicitly hide it, dp should be visible in main and should print the kind value for 
! double precision, which is typically 8.
!
! If the compiler adheres strictly to the Fortran standard: Attempting to access a would 
! result in a compilation error, and the program would not compile due to a being private.
!
10 Oct 2024 1:21 #31608

Ken

If 'implicit none' is added to the main program, FTN95 ought to fault the use of the variables a and dp.

It fails in this respect and I will note this as a bug that needs fixing.

10 Oct 2024 7:00 #31609

Paul, Thanks for clearing this up. It was the failure of 'IMPLICIT NONE' which caused the problem in the larger program I was looking at. The AI critique of simple example simply added to my confusion.

10 Oct 2024 11:02 #31610

Thanks, Kenneth, for this nice puzzling program. With these lines for the main program:

program main
use x_mod, only : a
use y_mod
use kind_mod
implicit none
print*, a         ! FTN95 prints the correct value, is this correct?
print*, b
print*, dp        ! FTN95 prints the correct value, is this correct?
end program main

The modified code works on Windows 11 with the current Intel IFX compiler, NAG 7219 and GFortran 11.4.

12 Oct 2024 4:21 #31612

mecej4, thanks for taking the time to look at this.

31 Jan 2025 7:07 #31886

This bug has now been fixed for the next release of FTN95.

Please login to reply.