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

results with varying length type parameters in TBP function overrides

joseph_battelle
Beginner
1,109 Views
[edited to add the empty parens on functions to be F2003 compliant -- thx Abhi]
Visual Fortran 11.1.065 [IA-32] compiles the invalid code below without error. If you combine the two modules into one you do get a reasonable error (error #8280: An overriding binding and its corresponding overridden binding must both be either subroutines or functions with the same result type, kind, and shape. [NAME]) Note in the example below the length of the return type varies based on an internal parameter so the polymorphic function doesn't have a consistent length type parameter. I've seen crashes with this sort of code. After I dig through the standard I'll update the thread to add my take on validity. (see addtl responses)

Compiles OK (but may crash at runtime)
[fortran]module M10

    type :: B
    contains
        procedure, nopass :: name => B_name       
    end type B
    
contains
    
    function B_name() result 
    character(len=*), parameter :: c = 'short_name'
    character(len=len(c)) :: r
        r = c
    end function B_name
          
end module M10

module M11

    use M10
    
    type, extends(B) :: D
    contains
        procedure, nopass :: name => D_name       
    end type

contains
    function D_name() result 
    character(len=*), parameter :: c = 'longer_name_is_longer'
    character(len=len(c)) :: r
        r = c !may crash here
    end function D_name 
end module M11[/fortran]
Compiles with Error 8280 described above:
[fortran]module M10

    type :: B
    contains
        procedure, nopass :: name => B_name       
    end type B
    
    type, extends(B) :: D
    contains
        procedure, nopass :: name => D_name       
    end type
    
contains
    
    function B_name() result 
    character(len=*), parameter :: c = 'short_name'
    character(len=len(c)) :: r
        r = c
    end function B_name          

    function D_name() result 
    character(len=*), parameter :: c = 'longer_name_is_longer'
    character(len=len(c)) :: r
        r = c
    end function D_name 
end module M10[/fortran]
0 Kudos
1 Solution
Steven_L_Intel1
Employee
1,109 Views
I have escalated this as issue DPD200157909.

View solution in original post

0 Kudos
6 Replies
joseph_battelle
Beginner
1,109 Views
[edited to add the empty parens to be F2003 compliant -- thx Abhi]
Forgot to mention that if you change the result type to a pointer with a (len=:) type parameter then the code below compiles without error as you would expect (because the type parameters are consistent); note the added saved variable because pointers can't target parameters.
Compiles OK:
[fortran]module M12

    type :: B
    contains
        procedure, nopass :: name => B_name       
    end type B
    
    type, extends(B) :: D
    contains
        procedure, nopass :: name => D_name       
    end type
    
contains
    
    function B_name() result 
    character(len=*), parameter :: c = 'short_name'
    character(len=len(c)), save, target :: v = c
    character(len=:), pointer :: r
        r => v
    end function B_name          

    function D_name() result 
    character(len=*), parameter :: c = 'longer_name_is_longer'
    character(len=len(c)), save, target :: v = c    
    character(len=:), pointer :: r
        r => v
    end function D_name 
end module M12[/fortran]
0 Kudos
joseph_battelle
Beginner
1,109 Views
[Based on the standard wording below I think the merged module behavior is correct and bug pointed out in this thread is that the compiler is not rejecting the code when split into two modules]
From:6 4.5.6.2 Type-bound procedure overriding
...
(7) Either both shall be subroutines or both shall be functions having the same result characteristics (12.2.2).
12.2.2 Characteristics of function results
The characteristics of a function result are its type, type parameters (if any), rank, whether it is polymorphic, whether it is allocatable, whether it is a pointer, and whether it is a procedure pointer. If afunction result is an array that is not allocatable or a pointer, its shape is a characteristic. If a typeparameter of a function result or a bound of a function result array is not an initialization expression, theexact dependence on the entities in the expression is a characteristic. If type parameters of a functionresult are deferred, which parameters are deferred is a characteristic. If the length of a character functionresult is assumed, this is a characteristic.
0 Kudos
abhimodak
New Contributor I
1,109 Views
Hi Joseph

Just an observation; most likely you already know this:- changing function to subroutine traps it.

Abhi

p.s. A very minor point: As per standard, Function must have () after the function name. i.e. Function B_name() result, the paranthesis should not be omitted. IVF correctly gives a warning with /stand:f03.
0 Kudos
joseph_battelle
Beginner
1,109 Views
Thanks a lot for your point! I have been running with /stand:f03 turned off because of conflicts I found in release 5 of 11.1 that kept me from using some f2003 features. I need to start running with it turned on by default and turning it off for modules that break with it (if they do.)
0 Kudos
Steven_L_Intel1
Employee
1,110 Views
I have escalated this as issue DPD200157909.
0 Kudos
Steven_L_Intel1
Employee
1,109 Views
This has been fixed for a release later this year.
0 Kudos
Reply