Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28528 Discussions

ifort error initializing a user-defined type with no components

imhere
Beginner
695 Views

When I try to compile the program below (just "ifort file.f90"), I get an error:

file.f90(15): warning #6178: The return value of this FUNCTION has not been defined.   [RES]
function mytype_constructor() result(res)
-------------------------------------^

The problem goes away if I add a dummy initialized component to "mytype" (commented out line).

module foo

type :: mytype
 !integer :: dummy = 0
 contains
 procedure :: run => myrun
end type mytype

interface mytype
 module procedure :: mytype_constructor
end interface

contains

function mytype_constructor() result(res)
 type(mytype) :: res
 write(6,*) 'Creating mytype'
 ! just to avoid unused warning:
 if (.false.) then ; associate(tmp => res) ; end associate ; end if
end function mytype_constructor

subroutine myrun(this, val)
 class(mytype) :: this
 integer :: val
 write(6,*) 'Hello, the value is: ',val
 ! just to avoid unused warning:
 if (.false.) then ; associate(tmp => this) ; end associate ; end if
end subroutine myrun

end module foo

!===========================

program main

use foo
type(mytype) :: object

object = mytype()
call object%run(42)

end program main

 

This is with the latest ifort (ifort version 2021.7.0 or ifx 2022.2.0), it doesn't happen with 19.1.3 or 2021.0.4.

0 Kudos
1 Solution
Arjen_Markus
Honored Contributor I
688 Views

New versions of compilers are often better at detecting errors or mistakes in the code. In your case, the error, glitch or whatever is the suitable word is probably not detected by the older release. It does not mean to say that the new compiler is complaining unjustly. Also note that it is a warning, not an error.

View solution in original post

4 Replies
Arjen_Markus
Honored Contributor I
689 Views

New versions of compilers are often better at detecting errors or mistakes in the code. In your case, the error, glitch or whatever is the suitable word is probably not detected by the older release. It does not mean to say that the new compiler is complaining unjustly. Also note that it is a warning, not an error.

imhere
Beginner
620 Views

To rephrase the implicit question: What would be the "correct" way (without causing warnings) of writing a constructor function for a user-defined type that has no definable components?

0 Kudos
imhere
Beginner
683 Views

You're right about it being a warning. I just noticed it first with "-warn errors" (in which case it's of course an error), and when I saw it kept coming I assumed it was still an error, my bad.

0 Kudos
jimdempseyatthecove
Honored Contributor III
618 Views

How about:

 

if(.false.) res = res ! code elided in release build

 

Jim Dempsey

0 Kudos
Reply