fpt and WinFPT Reference Manual - Command-line Commands

| SimCon Home | Ref Manual Home |

CHECK OVERLOADED ASSIGNMENTS

Syntax:

[DO NOT] CHECK OVERLOADED ASSIGNMENTS

Function:

The Fortran standard allows direct assignment of objects of derived types. The standard also allows the assignment of objects of derived types to be made by calling a subroutine which overloads the assignment operator. A consequence is that if the user intends to overload the assignment of a derived type but any error is made in declaring or exporting the overload of the assignment operator, the assignments of the derived type will be made directly without any apparent error. This error occurred in a major climate and weather modelling code analysed by the authors and may have invalidated important results.

The command CHECK OVERLOADED ASSIGNMENTS commands fpt to check all assignments of objects of derived types where an overloaded assignment exists and to report cases where the overload is not made.

The error occurs in the code below. The module m contains the INTERFACE ASSIGNMENT(=) interface to overload the assignment of the derived type lab_int. In the subroutine which overloads the assignment, two of the three type components are copied, but the third is a counter for assignments and is incremented. There is a leading PRIVATE statement in the module, but the module exports the lab_int type and the subroutine m_copy_lab_int in the PUBLIC statement at label 101. The subroutine m_copy_lab_int is within the scope of the module and should therefore use the subroutine copy_lab_int to overload the assignment. Critically, the interface for assignment itself is not exported from the module and is therefore not available to the main program, t.

MODULE m PRIVATE ! Labelled integer type TYPE lab_int INTEGER n CHARACTER*16 label INTEGER write_count END TYPE lab_int INTERFACE ASSIGNMENT(=) MODULE PROCEDURE copy_lab_int END INTERFACE ASSIGNMENT (=) 101 PUBLIC lab_int,m_copy_lab_int CONTAINS SUBROUTINE copy_lab_int(lab_int_out,lab_int_in) TYPE (lab_int), INTENT(INOUT) :: lab_int_out TYPE (lab_int), INTENT(IN) :: lab_int_in lab_int_out%label = lab_int_in%label lab_int_out%n = lab_int_in%n lab_int_out%write_count = lab_int_out%write_count+1 END SUBROUTINE copy_lab_int SUBROUTINE m_copy_lab_int(lab_int_out,lab_int_in) TYPE (lab_int) lab_int_out,lab_int_in lab_int_out=lab_int_in END SUBROUTINE m_copy_lab_int END MODULE m PROGRAM t USE m TYPE (lab_int) li1,li2 li1%n = 100 li1%label = "Label 100" li1%write_count = 0 li2%write_count = 0 li2 = li1 WRITE(6,'("li2 = li1: ",I6,4X,A16,I4)')li2 CALL m_copy_lab_int(li2, li1) WRITE(6,'("m_copy_lab_int(li2, li1): ",I6,4X,A16,I4)')li2 END PROGRAM t

When this program is run, the results (Compaq Visual Fortran, Salford FTN95, gfortran) are:

li2 = li1: 100 Label 100 0 m_copy_lab_int(li2, li1): 100 Label 100 1

The write_count component in li2 is not incremented in the first assignment in the main program. It is incremented in the subroutine m_copy_lab_int because the overload is in scope in this routine.

If the code at label 101 is amended to export the assignment(=), e.g.

101 PUBLIC lab_int,m_copy_lab_int, assignment(=)

the code then runs with the result:

li2 = li1: 100 Label 100 1 m_copy_lab_int(li2, li1): 100 Label 100 2

The write_count component of li2 is incremented for both assignments.

If the PUBLIC declaration for interface(=) is omitted, fpt reports the problem:

FPT> check overloaded assignments Check of Overloaded Assignments =============================== ------------------------------------------------------------------------------ WARNING Number 3513 Severity 1 (Worst 0) Count 1 Line 52 File: j:\fpt\t\t.f90 li2=li1 ^ INTERFACE ASSIGNMENT(=) exists but assignment not overloaded ------------------------------------------------------------------------------ 1 inconsistencies detected where variables of derived types for which INTERFACE ASSIGNMENT(=) is specified are copied without overloading. FPT>

Where to Use this Command

Operating system command line Yes
Configuration file, config.fsp Yes
Specification (fsp) files, *.fsp Yes
Interactively, to FPT> prompt Yes
Interactive command files Yes
Embedded in the Fortran code Yes

Default

The check is not made by default (Note that defaults may be changed in the configuration file).

Copyright ©1995 to 2024 Software Validation Ltd. All rights reserved.