- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
This example, adapted from the one in a recent thread ("Interface of an internal module function as class constructor"), exhibits what to me seems to be a bug in the compiler (12.0.2 - Windows, 12.0.3 - Linux). It is remarkable that the compiler reports no errors and the resulting program, when run, displays results that agree with what a programmer may have intended it to do! The program was obtained by removing polymorphic types from the referenced post.
If the module procedure name is changed, say to shapey, the assignment statement would be still in error, since it tries to access private components of the derived type.
Other compilers flag the program with syntax errors. For example, the Sun Linux compiler version 8.5 says:
"shape.f90", Line = 14, Column = 15: ERROR: "SHAPEX" is a type-name, therefore it must not be declared as a generic-name.
but that compiler does not claim to be an F2003 compiler.
[fortran]module shape_modThe program is in error because shapex is used as the name of a derived type as well as the name of a module procedure. As a consequence, the assignment statement in the main program (3rd line from the end) is ambiguous.
implicit none
private ! hide the type-bound procedure implementation procedures
public :: shapex, sprint
type shapex
private ! hide the underlying details
integer :: color
logical :: filled
integer :: x
integer :: y
end type shapex
interface shapex !BUG: is "shapex" type-name or procedure-name?
module procedure constructor
end interface
contains
function constructor(color, filled, x, y)
implicit none
type(shapex) :: constructor
integer :: color
logical :: filled
integer :: x
integer :: y
call initShape(constructor,color, filled, x, y)
end function constructor
subroutine initShape(shpe, color, filled, x, y)
! initialize shape objects
implicit none
type(shapex) :: shpe
integer :: color
logical :: filled
integer :: x
integer :: y
shpe%color = color
shpe%filled = filled
shpe%x = -x
shpe%y = -y
end subroutine initShape
subroutine sprint(shpe)
implicit none
type(shapex) :: shpe
print *, shpe%color, shpe%filled, shpe%x, shpe%y
end subroutine sprint
end module
program shape_prg
use shape_mod
implicit none
type(shapex) :: sh
sh = shapex(5, .true., 100, 200) ! BUG: is RHS a derived type constant or a type-bound-procedure call?
call sprint(sh)
end
[/fortran]
If the module procedure name is changed, say to shapey, the assignment statement would be still in error, since it tries to access private components of the derived type.
Other compilers flag the program with syntax errors. For example, the Sun Linux compiler version 8.5 says:
"shape.f90", Line = 14, Column = 15: ERROR: "SHAPEX" is a type-name, therefore it must not be declared as a generic-name.
but that compiler does not claim to be an F2003 compiler.
Link Copied
2 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The Sun Linux compiler is wrong. The code is legal in F2003. I quoted from the standard in the other topic.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks, Steve. The corresponding section numbers in the Fortran 2003 standard are slightly different: 16.2, 12.3.2.1 and 4.5.9.
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page