- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
How about:
if(.false.) res = res ! code elided in release build
Jim Dempsey

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page