Bug 89645 - No IMPLICIT type error with: ASSOCIATE( X => function() )
Summary: No IMPLICIT type error with: ASSOCIATE( X => function() )
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 9.0
: P3 normal
Target Milestone: ---
Assignee: Paul Thomas
URL:
Keywords: rejects-valid
: 99065 (view as bug list)
Depends on:
Blocks: associate
  Show dependency treegraph
 
Reported: 2019-03-10 02:15 UTC by Ian Harvey
Modified: 2024-03-15 07:07 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2019-03-10 00:00:00


Attachments
Deadend fix for this PR (5.45 KB, patch)
2023-12-03 10:40 UTC, Paul Thomas
Details | Diff
An experimental patch for two pass compilation of contained procedures with failures (4.10 KB, patch)
2023-12-16 10:39 UTC, Paul Thomas
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Ian Harvey 2019-03-10 02:15:40 UTC
The module:

  MODULE m
    IMPLICIT NONE
    TYPE :: t
      INTEGER :: comp
    END TYPE t
  CONTAINS
    SUBROUTINE s
      ASSOCIATE(b => fun())
        PRINT *, b%comp
      END ASSOCIATE
    END SUBROUTINE s
    
    FUNCTION fun() RESULT(r)
      TYPE(t) :: r
      r = t(1)
    END FUNCTION fun
  END MODULE m

when compiled with recent trunk (r269545) gives:
  
      9 |       PRINT *, b%comp
        |                 1
  Error: Symbol ‘b’ at (1) has no IMPLICIT type

F2003 rules (8.1.4.2) and subsequent standards state for the associate statement that the entity identified by the associate name assumes the declared type of the selector.

(This has some similarity to pr60483, which related to structure constructors.)
Comment 1 Dominique d'Humieres 2019-03-10 08:47:22 UTC
Confirmed from at least 4.8 up to trunk (9.0).
Comment 2 Paul Thomas 2023-06-02 08:10:13 UTC
Hi Ian,

This is one of two PRs (cf. 99065) that expose a really nasty fault with gfortran parsing. If you interchange 's' and 'fun', you will find that the testcase compiles and runs.

The fixup for intrinsic types works fine but, unfortunately, the derived type is need for parsing the associate block itself.

I am girding my loins to fight the good fight.

Thanks for this posting.

Paul
Comment 3 Paul Thomas 2023-06-28 16:39:00 UTC
*** Bug 99065 has been marked as a duplicate of this bug. ***
Comment 4 Paul Thomas 2023-12-03 10:40:44 UTC
Created attachment 56775 [details]
Deadend fix for this PR

I got as far as I could with this fix up method. The testcase below fails and so I am going to set aside this approach and tackle what I should have done in the first place: two step parsing of contained procedures.

The patch contains a fix for class array function selectors that do not work, even if the selector function is parsed first. I will make a new PR for this and will break out the patch for it and post it to the list.

Paul

module m
  implicit none
  type t
    integer :: i = 0
  end type t
  integer :: i = 0
  type(t), parameter :: test_array (2) = [t(42),t(84)], &
                        test_scalar = t(99)
end module m
module class_selectors
  use m
  implicit none
  private
  public foo2
contains

  subroutine foo2()
    associate (var1 => bar3())
      if (any (var1%i .ne. test_array%i)) stop 8
      if (var1(2)%i .ne. test_array(2)%i) stop 9
!      associate (var3 => var1%i)                  ! This still fails
!         print *, "yipee"
!      end associate
      select type (x => var1)
        type is (t)
          if (any (x%i .ne. test_array%i)) stop 10
          if (x(1)%i .ne. test_array(1)%i) stop 11
        class default
          stop 12
      end select
    end associate

    select type (y => bar3 ())
      type is (t)
        print *, "yes, size of 'y' is ", size(y), y(1)
      class default
        print *, "no"
    end select
  end subroutine foo2

! Since these functions are parsed after 'foo', the symbols were not available
! for the selectors and the fixup, tested here, was necessary.

  function bar3() result(res)
    class(t), allocatable :: res(:)
    allocate (res, source = test_array)
  end
end module class_selectors

  use class_selectors
  call foo2
end
Comment 5 Paul Thomas 2023-12-16 10:39:28 UTC
Created attachment 56892 [details]
An experimental patch for two pass compilation of contained procedures with failures

I am giving up on this. Failures as follows:

FAIL: gfortran.dg/binding_label_tests_13_main.f03   -O   (test for errors, line 5)
FAIL: gfortran.dg/binding_label_tests_13_main.f03   -O   (test for errors, line 10)
Compiles without errors

FAIL: gfortran.dg/deferred_character_8.f90   -O0  (internal compiler error: Segmentation fault)
FAIL: gfortran.dg/deferred_character_8.f90   -O0  (test for excess errors)
Compiles and runs OK outside dejagnu

...repeats...
FAIL: gfortran.dg/entry_16.f90   -O0  (internal compiler error: Segmentation fault)
FAIL: gfortran.dg/entry_16.f90   -O0  (test for excess errors)
...repeats...
FAIL: gfortran.dg/entry_1.f90   -O1  (test for excess errors)
FAIL: gfortran.dg/entry_1.f90   -O2  (test for excess errors)
...repeats...
FAIL: gfortran.dg/entry_13.f90   -O0  (internal compiler error: Segmentation fault)
FAIL: gfortran.dg/entry_13.f90   -O0  (test for excess errors)
...repeats...
All three entry failures are due to:
internal compiler error: Segmentation fault
0x10941df crash_signal
	../../gcc/gcc/toplev.cc:316
0x10cf582 main_block_label
	../../gcc/gcc/tree-cfg.cc:1533
0x10cf582 cleanup_dead_labels()
	../../gcc/gcc/tree-cfg.cc:1718
0x10dd8d1 build_gimple_cfg
	../../gcc/gcc/tree-cfg.cc:241
0x10dd8d1 execute_build_cfg
	../../gcc/gcc/tree-cfg.cc:371

FAIL: gfortran.dg/host_assoc_call_3.f90   -O  (test for excess errors)
Not finding the specific, doubly contained 'putaline'

gfortran.dg/namelist_4.f90   -O   (test for errors, line 34)
FAIL: gfortran.dg/namelist_4.f90   -O  (test for excess errors)
Error: ‘f2’ at (1) is not a variable instead of { dg-error "is not a VALUE" }

FAIL: gfortran.dg/pointer_assign_12.f90   -O  (internal compiler error: gimplification failed)
FAIL: gfortran.dg/pointer_assign_12.f90   -O   (test for errors, line 10)
FAIL: gfortran.dg/pointer_assign_12.f90   -O  (test for excess errors)

   10 |       g => 1 ! { dg-error "Pointer assignment target cannot be a constant" }
      |                                                                            ^
internal compiler error: gimplification failed
0xd6cd97 gimplify_expr(tree_node**, gimple**, gimple**, bool (*)(tree_node*), int)
	../../gcc/gcc/gimplify.cc:17749

FAIL: gfortran.dg/pr87907.f90   -O  (internal compiler error: Segmentation fault)
FAIL: gfortran.dg/pr87907.f90   -O   (test for errors, line 15)
FAIL: gfortran.dg/pr87907.f90   -O   (test for errors, line 20)
FAIL: gfortran.dg/pr87907.f90   -O   (test for errors, line 22)
FAIL: gfortran.dg/pr87907.f90   -O  (test for excess errors)
f951: internal compiler error: Segmentation fault
0x10941df crash_signal
	../../gcc/gcc/toplev.cc:316
0x97fd53 gfc_match_decl_type_spec(gfc_typespec*, int)
	../../gcc/gcc/fortran/decl.cc:4281
0x9816bc gfc_match_data_decl()
	../../gcc/gcc/fortran/decl.cc:6286
0x9f7a9f match_word
	../../gcc/gcc/fortran/parse.cc:92
0x9f7a9f decode_statement
	../../gcc/gcc/fortran/parse.cc:476

FAIL: gfortran.dg/pr95690.f90   -O   (test for errors, line 5)
FAIL: gfortran.dg/pr95690.f90   -O  (test for excess errors)
Error: Function ‘erfc’ requires an argument list at (1)

FAIL: gfortran.dg/pr96102.f90   -O   (test for errors, line 13)
FAIL: gfortran.dg/pr96102.f90   -O   (test for errors, line 14)
FAIL: gfortran.dg/pr96102.f90   -O   (test for errors, line 17)
FAIL: gfortran.dg/pr96102.f90   -O   (test for errors, line 18)
FAIL: gfortran.dg/pr96102.f90   -O  (test for excess errors)
   17 |       if ( n /= 0 ) stop 1    ! { dg-error "internal procedure of the same name" }
      |              1
Error: Function ‘n’ requires an argument list at (1)

FAIL: gfortran.dg/pr96102b.f90   -O   (test for errors, line 10)
FAIL: gfortran.dg/pr96102b.f90   -O   (test for errors, line 11)
FAIL: gfortran.dg/pr96102b.f90   -O   (test for errors, line 14)
FAIL: gfortran.dg/pr96102b.f90   -O   (test for errors, line 15)
FAIL: gfortran.dg/pr96102b.f90   -O  (test for excess errors)
Error: Unexpected use of subroutine name ‘n’ at (1)

FAIL: gfortran.dg/proc_ptr_result_2.f90   -O  (test for excess errors)
   35 |     call set_fun(aux)
      |                     1
Error: Type mismatch in argument ‘y’ at (1); passed REAL(4) to UNKNOWN

XPASS: gfortran.dg/goacc/coarray.f95   -O  TODO (test for errors, line 27)
XPASS: gfortran.dg/goacc/cray-2.f95   -O  TODO (test for errors, line 49)
FAIL: gfortran.dg/goacc/cray-2.f95   -O  (test for excess errors)
XPASS: gfortran.dg/goacc/cray.f95   -O  TODO (test for errors, line 48)
FAIL: gfortran.dg/goacc/cray.f95   -O  (test for excess errors)
FAIL: gfortran.dg/goacc/declare-1.f95   -O   (test for errors, line 13)
FAIL: gfortran.dg/goacc/declare-1.f95   -O  (test for excess errors)
FAIL: gfortran.dg/goacc/loop-1-2.f95   -O  (test for excess errors)
FAIL: gfortran.dg/goacc/loop-1.f95   -O  (test for excess errors)
FAIL: gfortran.dg/goacc/nested-function-1.f90   -O   at line 49 (test for warnings, line 48)
FAIL: gfortran.dg/goacc/nested-function-1.f90   -O   at line 50 (test for warnings, line 48)
FAIL: gfortran.dg/goacc/nested-function-1.f90   -O   at line 51 (test for warnings, line 48)
FAIL: gfortran.dg/goacc/nested-function-1.f90   -O   at line 52 (test for warnings, line 48)
FAIL: gfortran.dg/goacc/nested-function-1.f90   -O   at line 53 (test for warnings, line 48)
FAIL: gfortran.dg/goacc/nested-function-1.f90   -O   at line 54 (test for warnings, line 48)
FAIL: gfortran.dg/goacc/nested-function-1.f90   -O   at line 59 (test for warnings, line 58)
FAIL: gfortran.dg/goacc/nested-function-1.f90   -O   at line 63 (test for warnings, line 62)
FAIL: gfortran.dg/goacc/nested-function-1.f90   -O   at line 72 (test for warnings, line 71)
FAIL: gfortran.dg/goacc/nested-function-1.f90   -O   at line 73 (test for warnings, line 71)
FAIL: gfortran.dg/goacc/nested-function-1.f90   -O   at line 74 (test for warnings, line 71)
FAIL: gfortran.dg/goacc/nested-function-1.f90   -O   at line 75 (test for warnings, line 71)
FAIL: gfortran.dg/goacc/nested-function-1.f90   -O   at line 76 (test for warnings, line 71)
FAIL: gfortran.dg/goacc/nested-function-1.f90   -O   at line 77 (test for warnings, line 71)
FAIL: gfortran.dg/goacc/nested-function-1.f90   -O   at line 82 (test for warnings, line 81)
FAIL: gfortran.dg/goacc/nested-function-1.f90   -O   at line 86 (test for warnings, line 85)
FAIL: gfortran.dg/goacc/nested-function-1.f90   -O   at line 104 (test for warnings, line 103)
FAIL: gfortran.dg/goacc/nested-function-1.f90   -O   at line 105 (test for warnings, line 103)
FAIL: gfortran.dg/goacc/nested-function-1.f90   -O   at line 106 (test for warnings, line 103)
FAIL: gfortran.dg/goacc/nested-function-1.f90   -O   at line 107 (test for warnings, line 103)
FAIL: gfortran.dg/goacc/nested-function-1.f90   -O   at line 108 (test for warnings, line 103)
FAIL: gfortran.dg/goacc/nested-function-1.f90   -O   at line 109 (test for warnings, line 103)
FAIL: gfortran.dg/goacc/nested-function-1.f90   -O   at line 114 (test for warnings, line 113)
FAIL: gfortran.dg/goacc/nested-function-1.f90   -O   at line 118 (test for warnings, line 117)
FAIL: gfortran.dg/goacc/nested-function-1.f90   -O   at line 127 (test for warnings, line 126)
FAIL: gfortran.dg/goacc/nested-function-1.f90   -O   at line 128 (test for warnings, line 126)
FAIL: gfortran.dg/goacc/nested-function-1.f90   -O   at line 129 (test for warnings, line 126)
FAIL: gfortran.dg/goacc/nested-function-1.f90   -O   at line 130 (test for warnings, line 126)
FAIL: gfortran.dg/goacc/nested-function-1.f90   -O   at line 131 (test for warnings, line 126)
FAIL: gfortran.dg/goacc/nested-function-1.f90   -O   at line 132 (test for warnings, line 126)
FAIL: gfortran.dg/goacc/nested-function-1.f90   -O   at line 137 (test for warnings, line 136)
FAIL: gfortran.dg/goacc/nested-function-1.f90   -O   at line 141 (test for warnings, line 140)
FAIL: gfortran.dg/goacc/nested-function-1.f90   -O  (test for excess errors)
FAIL: gfortran.dg/goacc/parameter.f95   -O  (test for excess errors)
FAIL: gfortran.dg/gomp/allocate-5.f90   -O   (test for warnings, line 48)
FAIL: gfortran.dg/gomp/allocate-5.f90   -O   (test for warnings, line 52)
FAIL: gfortran.dg/gomp/allocate-5.f90   -O   (test for warnings, line 56)
FAIL: gfortran.dg/gomp/allocate-5.f90   -O   (test for warnings, line 69)
FAIL: gfortran.dg/gomp/allocate-5.f90   -O   (test for warnings, line 77)
FAIL: gfortran.dg/gomp/allocate-5.f90   -O   (test for warnings, line 87)
FAIL: gfortran.dg/gomp/allocate-5.f90   -O  (test for excess errors)
FAIL: gfortran.dg/gomp/allocate-12.f90   -O  (internal compiler error: Aborted)
FAIL: gfortran.dg/gomp/allocate-12.f90   -O   (test for errors, line 6)
FAIL: gfortran.dg/gomp/allocate-12.f90   -O   (test for errors, line 16)
FAIL: gfortran.dg/gomp/allocate-12.f90   -O  (test for excess errors)
FAIL: gfortran.dg/gomp/allocate-13.f90   -O  (internal compiler error: Aborted)
FAIL: gfortran.dg/gomp/allocate-13.f90   -O  (test for excess errors)
FAIL: gfortran.dg/gomp/allocate-9.f90   -O  (internal compiler error: free_expr0(): Bad expr type)
FAIL: gfortran.dg/gomp/allocate-9.f90   -O   (test for errors, line 38)
FAIL: gfortran.dg/gomp/allocate-13a.f90   -O  (internal compiler error: Aborted)
FAIL: gfortran.dg/gomp/allocate-9.f90   -O   (test for errors, line 41)
FAIL: gfortran.dg/gomp/allocate-13a.f90   -O  (test for excess errors)
FAIL: gfortran.dg/gomp/allocate-9.f90   -O   at line 42 (test for errors, line 41)
FAIL: gfortran.dg/gomp/allocate-9.f90   -O   (test for errors, line 45)
FAIL: gfortran.dg/gomp/allocate-9.f90   -O   (test for errors, line 48)
FAIL: gfortran.dg/gomp/allocate-9.f90   -O   (test for errors, line 56)
FAIL: gfortran.dg/gomp/allocate-9.f90   -O   (test for errors, line 58)
FAIL: gfortran.dg/gomp/allocate-9.f90   -O   (test for errors, line 63)
FAIL: gfortran.dg/gomp/allocate-9.f90   -O   (test for errors, line 70)
FAIL: gfortran.dg/gomp/allocate-9.f90   -O   (test for errors, line 74)
FAIL: gfortran.dg/gomp/allocate-9.f90   -O   (test for errors, line 82)
FAIL: gfortran.dg/gomp/allocate-9.f90   -O   (test for errors, line 88)
FAIL: gfortran.dg/gomp/allocate-9.f90   -O   at line 103 (test for errors, line 102)
FAIL: gfortran.dg/gomp/allocate-9.f90   -O   at line 104 (test for errors, line 102)
FAIL: gfortran.dg/gomp/allocate-9.f90   -O   (test for errors, line 108)
FAIL: gfortran.dg/gomp/allocate-9.f90   -O   (test for errors, line 109)
FAIL: gfortran.dg/gomp/allocate-9.f90   -O  (test for excess errors)
FAIL: gfortran.dg/gomp/order-2.f90   -O  (test for excess errors)
FAIL: gfortran.dg/gomp/teams-4.f90   -O  (test for excess errors)
FAIL: gfortran.dg/graphite/pr42181.f90   -O  (test for excess errors)
Comment 6 GCC Commits 2024-03-15 06:53:17 UTC
The master branch has been updated by Paul Thomas <pault@gcc.gnu.org>:

https://gcc.gnu.org/g:3fd46d859cda1074125449a4cc680ce59fcebc38

commit r14-9489-g3fd46d859cda1074125449a4cc680ce59fcebc38
Author: Paul Thomas <pault@gcc.gnu.org>
Date:   Fri Mar 15 06:52:59 2024 +0000

    Fortran: Fix class/derived/complex function associate selectors [PR87477]
    
    2024-03-15  Paul Thomas  <pault@gcc.gnu.org>
    
    gcc/fortran
            PR fortran/87477
            PR fortran/89645
            PR fortran/99065
            PR fortran/114141
            PR fortran/114280
            * class.cc (gfc_change_class): New function needed for
            associate names, when rank changes or a derived type is
            produced by resolution
            * dump-parse-tree.cc (show_code_node): Make output for SELECT
            TYPE more comprehensible.
            * expr.cc (find_inquiry_ref): Do not simplify expressions of
            an inferred type.
            * gfortran.h : Add 'gfc_association_list' to structure
            'gfc_association_list'. Add prototypes for
            'gfc_find_derived_types', 'gfc_fixup_inferred_type_refs' and
            'gfc_change_class'. Add macro IS_INFERRED_TYPE.
            * match.cc (copy_ts_from_selector_to_associate): Add bolean arg
            'select_type' with default false. If this is a select type name
            and the selector is a inferred type, build the class type and
            apply it to the associate name.
            (build_associate_name): Pass true to 'select_type' in call to
            previous.
            * parse.cc (parse_associate): If the selector is inferred type
            the associate name is too. Make sure that function selector
            class and rank, if known, are passed to the associate name. If
            a function result exists, pass its typespec to the associate
            name.
            * primary.cc (resolvable_fcns): New function to check that all
            the function references are resolvable.
            (gfc_match_varspec): If a scalar derived type select type
            temporary has an array reference, match the array reference,
            treating this in the same way as an equivalence member. Do not
            set 'inquiry' if applied to an unknown type the inquiry name
            is ambiguous with the component of an accessible derived type.
            Check that resolution of the target expression is OK by testing
            if the symbol is declared or is an operator expression, then
            using 'resolvable_fcns' recursively. If all is well, resolve
            the expression. If this is an inferred type with a component
            reference, call 'gfc_find_derived_types' to find a suitable
            derived type. If there is an inquiry ref and the symbol either
            is of unknown type or is inferred to be a derived type, set the
            primary and symbol TKR appropriately.
            * resolve.cc (resolve_variable): Call new function below.
            (gfc_fixup_inferred_type_refs): New function to ensure that the
            expression references for a inferred type are consistent with
            the now fixed up selector.
            (resolve_assoc_var): Ensure that derived type or class function
            selectors transmit the correct arrayspec to the associate name.
            (resolve_select_type): If the selector is an associate name of
            inferred type and has no component references, the associate
            name should have its typespec. Simplify the conversion of a
            class array to class scalar by calling 'gfc_change_class'.
            Make sure that a class, inferred type selector with an array
            ref transfers the typespec from the symbol to the expression.
            * symbol.cc (gfc_set_default_type): If an associate name with
            unknown type has a selector expression, try resolving the expr.
            (find_derived_types, gfc_find_derived_types): New functions
            that search for a derived type with a given name.
            * trans-expr.cc (gfc_conv_variable): Some inferred type exprs
            escape resolution so call 'gfc_fixup_inferred_type_refs'.
            * trans-stmt.cc (trans_associate_var): Tidy up expression for
            'class_target'. Finalize and free class function results.
            Correctly handle selectors that are class functions and class
            array references, passed as derived types.
    
    gcc/testsuite/
            PR fortran/87477
            PR fortran/89645
            PR fortran/99065
            * gfortran.dg/associate_64.f90 : New test
            * gfortran.dg/associate_66.f90 : New test
            * gfortran.dg/associate_67.f90 : New test
    
            PR fortran/114141
            * gfortran.dg/associate_65.f90 : New test
    
            PR fortran/114280
            * gfortran.dg/associate_68.f90 : New test
Comment 7 Paul Thomas 2024-03-15 07:07:57 UTC
This mega-patch, on the scale of the importance of the problem, was required because of gfortran's one pass parsing. It might be a temporary fix because I am contemplating how an initial pass of contained procedures might be introduced.

Fixed on mainline.

Paul