Silverfrost Forums

Welcome to our forums

EQUIVALENCE compiler issue

18 Dec 2016 1:49 #18564

I make use of equivalence rarely, usually to work around some issue with data typing, or trying to use a logical for a %rb. The code snippet below compiles perfectly, but the execution shows that the EQUIVALENCE is not in force.

	logical	record_function_enabled,playback_function_enabled
	integer record_fileunit,playback_fileunit
	equivalence (record_function_enabled,  record_fileunit),(playback_function_enabled,  playback_fileunit)
	common/record_playback_common/record_function_enabled,playback_function_enabled,record_fileunit,playback_fileunit
	print *,loc(record_function_enabled),loc(record_fileunit)
    end

The LOC of these two variables should be the same, but they are not.

18 Dec 2016 2:24 #18565

The problem is with your COMMON, but the compiler does not give you a good diagnostic message about it.

You are not allowed to specify any member of a common block more than once in the list of items to be placed in common. Because of the equivalence, you are doing this forbidden thing.

Replace the common block declaration by, say,

common/record_playback_common/record_function_enabled,playback_function_enabled

and your errors should be resolved.

Note, however, that according to the Fortran standard when you place two different types (logical and integer, for example) in an equivalence pair, defining one of the pair makes the other undefined. The wording in the standard:

16.5.6 Events that cause variables to become undefined 45 Variables become undefined as follows: 46 (1) When a variable of a given type becomes defined, all associated variables of different type 47 become undefined.

Few compilers, unless asked to do so, will actually make this 'undefinition' happen, but do note that you are taking a risk by relying on the compiler's turning a blind eye on this.

18 Dec 2016 3:30 #18566

Fully aware that what I did was not allowed. Occasionally, one does make an error, especially when developing new code, yes?

I expected an error message, either at the EQUIVALENCE or at the COMMON declarations. Got neither, needed reporting.

18 Dec 2016 3:45 #18567

Yes, of course, but I would have expected a compile time error, not a run-time error. Gfortran says: Error: Symbol 'record_fileunit' at (1) is already in a COMMON block.

I can now see why you added the PRINT statement with LOC() -- the compiler built the application with no error messages!

I hope Paul will have this case looked at.

19 Dec 2016 2:30 #18576

Thanks for the feedback. I have made a note that the compiler should trap this error.

30 Jan 2017 1:46 #18811

This has now been fixed for the next release.

30 Jan 2017 11:51 #18814

Thanks, Paul!

Please login to reply.