Bug 79072 - ICE with class(*) pointer function result and character value
Summary: ICE with class(*) pointer function result and character value
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 7.0
: P3 normal
Target Milestone: ---
Assignee: Paul Thomas
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-01-12 16:30 UTC by Neil Carlson
Modified: 2018-02-21 15:12 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work: 7.3.0, 8.0
Known to fail: 4.7.4, 4.8.5, 4.9.4, 5.4.0, 6.3.0, 7.0, 7.1.0
Last reconfirmed: 2017-01-13 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Neil Carlson 2017-01-12 16:30:20 UTC
This example gives an ICE with the current 7.0 trunk and all 6.x releases:

function foo()
  class(*), pointer :: foo
  character(3), target :: string = 'foo'
  foo => string
  select type (foo)
  type is (character(*))
    !print *, foo
  end select
end function

The ICE disappears if either:
1) type is (character(*)) is replaced with integer, for example;
2) the return variable is specified in a result(...) clause.

In the original code that exhibited the compiler bug, the function
returns correct values except for character values, so I expect the
underlying problem involves the unlimited polymorphic pointer
assignment statement and character dynamic types.  I introduced
the select type (which produced the ICE) when debugging. 

Here's the output from the compiler:

$ gfortran -c code.f90 
code.f90:1:0:

 function foo()
 
internal compiler error: tree check: expected record_type or union_type or qual_union_type, have function_type in gfc_class_len_get, at fortran/trans-expr.c:226
0xe75f1c tree_check_failed(tree_node const*, char const*, int, char const*, ...)
	../../gcc/tree.c:9814
0x6f03c5 tree_check3(tree_node*, char const*, int, char const*, tree_code, tree_code, tree_code)
	../../gcc/tree.h:3100
0x6f03c5 gfc_class_len_get(tree_node*)
	../../gcc/fortran/trans-expr.c:226
0x745cbc trans_associate_var
	../../gcc/fortran/trans-stmt.c:1757
0x745cbc gfc_trans_block_construct(gfc_code*)
	../../gcc/fortran/trans-stmt.c:1810
0x6ba467 trans_code
	../../gcc/fortran/trans.c:1913
0x747c1e gfc_trans_select_type_cases
	../../gcc/fortran/trans-stmt.c:2401
0x747c1e gfc_trans_select_type(gfc_code*)
	../../gcc/fortran/trans-stmt.c:3112
0x6ba3d7 trans_code
	../../gcc/fortran/trans.c:1933
0x745638 gfc_trans_block_construct(gfc_code*)
	../../gcc/fortran/trans-stmt.c:1803
0x6ba467 trans_code
	../../gcc/fortran/trans.c:1913
0x6eb598 gfc_generate_function_code(gfc_namespace*)
	../../gcc/fortran/trans-decl.c:6292
0x672f76 translate_all_program_units
	../../gcc/fortran/parse.c:6008
0x672f76 gfc_parse_file()
	../../gcc/fortran/parse.c:6208
0x6b6512 gfc_be_parse_file
	../../gcc/fortran/f95-lang.c:202
Please submit a full bug report,
Comment 1 Martin Liška 2017-01-13 13:25:00 UTC
All releases starting from 4.8.0 ICE. Before that:

  4.7.4 (ae10eb82fe34c186)(12 Jun 2014 12:08): [took: 0.019s] result: FAILED
pr79072.f90:2.10:

  class(*), pointer :: foo
          1
Fatal Error: Unlimited polymorphism at (1) not yet supported
Comment 2 Dominique d'Humieres 2017-01-13 13:36:37 UTC
IMO bugs in new features are not regressions.
Comment 3 Neil Carlson 2017-01-13 14:05:59 UTC
Why is this tagged with 'ice-on-invalid-code'?  What is invalid about the code?
Comment 4 Martin Liška 2017-01-13 14:12:59 UTC
Sorry, bug description fixed.
Comment 5 Neil Carlson 2017-01-13 14:30:49 UTC
Here's a more complete example that avoids the ICE.

It gives correct results with 6.3:
           5 fubar
           5 fubar

But incorrect results with 7.0:
           5 fubar
           0 


program main

  class(*), pointer :: x, y
  allocate(x, source='fubar')
  
  y => foobar(x)
  
  select type (y)
  type is (character(*))
    print *, len(y), y
  end select

contains

  function foobar(bar) result(foo)
    class(*), pointer :: foo, bar
    foo => bar
    select type (foo)
    type is (character(*))
      print *, len(foo), foo
    end select
  end function

end program
Comment 6 Neil Carlson 2017-05-07 12:50:42 UTC
Comment 1 code example segfaults with the 7.1.0 release (and earlier).

Comment 5 code example gives incorrect results with the 7.1.0 release,
but correct results with 6.3 and 5.2 -- a regression.
Comment 7 Dominique d'Humieres 2017-05-07 15:02:10 UTC
> Comment 5 code example gives incorrect results with the 7.1.0 release,
> but correct results with 6.3 and 5.2 -- a regression.

Likely caused by revision r241439. AFAICT this could be two different PRs.
Comment 8 Eric Gallager 2017-08-17 15:51:54 UTC
Redoing https://gcc.gnu.org/ml/gcc-bugs/2017-08/msg01290.html which was lost:

--- Comment #8 from neil.n.carlson at gmail dot com ---
Ping.  Is there any interest in fixing this regression?
Comment 9 Jerry DeLisle 2017-08-18 18:16:10 UTC
(In reply to Eric Gallager from comment #8)
> Redoing https://gcc.gnu.org/ml/gcc-bugs/2017-08/msg01290.html which was lost:
> 
> --- Comment #8 from neil.n.carlson at gmail dot com ---
> Ping.  Is there any interest in fixing this regression?

Yes, anything that is a failure on valid code we want to fix. We are simply very short handed. What is the pain level on this one?
Comment 10 Paul Thomas 2017-11-18 16:59:29 UTC
I happened upon this bug in the course of fixing PR78990. It looked at first sight to be similar but is not.... of course. However, the fix was real easy and I'll submit it a bit later on.

Thanks for the report and sorry that it is taken so long.

Paul
Comment 11 Neil Carlson 2017-11-19 00:27:54 UTC
Paul, I'm organizing all my bug report examples, and ran across these two test cases from September that I can't find I ever reported.  They are VERY similar to the original example I posted, except the ICE occurs in a different file, so I thought I'd add them here.  I can submit a separate PR if you think it more appropriate.

The first compiles on 6.4.1, 7.2.1, and 8.0.0 (20171028). But produces an incorrect result:

b="" (expect "foo")

character(3), target :: a = 'foo'
class(*), pointer :: b
b => ptr()
select type (b)
type is (character(*))
  print '(3a)', 'b="', b, '" (expect "foo")'
end select
contains
  function ptr()
    class(*), pointer :: ptr
    ptr => a
  end function
end
Comment 12 Neil Carlson 2017-11-19 00:33:15 UTC
The second adds a select case and print to get at the result value before its handed back.  This produces an ICE with 6.4.1, 7.2.1, and 8.0.0 (20171028)

character(3), target :: a = 'foo'
class(*), pointer :: b
b => ptr()
select type (b)
type is (character(*))
  print '(3a)', 'b="', b, '" (expect "foo")'
end select
contains
  function ptr()
    class(*), pointer :: ptr
    ptr => a
    select type (ptr)
    type is (character(*))
    end select
  end function
end

Here's the traceback on 7.2.1:

gfortran-bug-20170812b.f90:46:0:

 character(3), target :: a = 'foo'
 
internal compiler error: in gfc_advance_chain, at fortran/trans.c:58
0x65d137 gfc_advance_chain(tree_node*, int)
	../../gcc/fortran/trans.c:58
0x685eba gfc_class_len_get(tree_node*)
	../../gcc/fortran/trans-expr.c:226
0x6c6364 trans_associate_var
	../../gcc/fortran/trans-stmt.c:1778
0x6c6364 gfc_trans_block_construct(gfc_code*)
	../../gcc/fortran/trans-stmt.c:1831
0x65d6b7 trans_code
	../../gcc/fortran/trans.c:1913
0x6c77d9 gfc_trans_select_type_cases
	../../gcc/fortran/trans-stmt.c:2422
0x6c77d9 gfc_trans_select_type(gfc_code*)
	../../gcc/fortran/trans-stmt.c:3133
0x65d747 trans_code
	../../gcc/fortran/trans.c:1933
0x6c5e38 gfc_trans_block_construct(gfc_code*)
	../../gcc/fortran/trans-stmt.c:1824
0x65d6b7 trans_code
	../../gcc/fortran/trans.c:1913
0x682847 gfc_generate_function_code(gfc_namespace*)
	../../gcc/fortran/trans-decl.c:6332
0x682634 gfc_generate_contained_functions
	../../gcc/fortran/trans-decl.c:5327
0x682634 gfc_generate_function_code(gfc_namespace*)
	../../gcc/fortran/trans-decl.c:6261
0x616eb6 translate_all_program_units
	../../gcc/fortran/parse.c:6074
0x616eb6 gfc_parse_file()
	../../gcc/fortran/parse.c:6274
0x65a02f gfc_be_parse_file
	../../gcc/fortran/f95-lang.c:204
Comment 13 Neil Carlson 2017-11-19 00:34:58 UTC
Correction to Comment 11.  That example gives the *correct* result on 6.4.1
Comment 14 Dominique d'Humieres 2017-11-19 01:16:26 UTC
The patch at https://gcc.gnu.org/ml/fortran/2017-11/msg00103.html fixes gfortran for the tests in comment 0, 5 and 11, but not for the test in comment 12:

internal compiler error: tree check: expected record_type or union_type or qual_union_type, have function_type in gfc_class_len_get, at fortran/trans-expr.c:226
Comment 15 Paul Thomas 2017-11-20 19:10:05 UTC
Author: pault
Date: Mon Nov 20 19:09:34 2017
New Revision: 254966

URL: https://gcc.gnu.org/viewcvs?rev=254966&root=gcc&view=rev
Log:
2017-11-20  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/79072
	* trans-expr.c (trans_class_vptr_len_assignment): Set from_len
	if the temporary is unlimited polymorphic.
	* trans-stmt.c (trans_associate_var): Use the fake result decl
	to obtain the 'len' field from an explicit function result when
	in that function scope.

2017-11-20  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/79072
	* gfortran.dg/class_result_5.f90: New test.


Added:
    trunk/gcc/testsuite/gfortran.dg/class_result_5.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-expr.c
    trunk/gcc/fortran/trans-stmt.c
    trunk/gcc/testsuite/ChangeLog
Comment 16 Neil Carlson 2017-11-20 19:58:42 UTC
I've confirmed Dominique's findings: Code in comments 0, 5, 11 are working now with Paul's commit (Thanks!), but comment 12 code still gives an ICE.

Should I create a new PR for that example, or is it fine leaving this PR open?
Comment 17 Paul Thomas 2017-11-21 17:33:59 UTC
(In reply to neil.n.carlson from comment #16)
> I've confirmed Dominique's findings: Code in comments 0, 5, 11 are working
> now with Paul's commit (Thanks!), but comment 12 code still gives an ICE.
> 
> Should I create a new PR for that example, or is it fine leaving this PR
> open?

Leave it open, thanks. Dominique, reminded me of the comment.

Paul
Comment 18 Paul Thomas 2017-11-22 11:37:48 UTC
Author: pault
Date: Wed Nov 22 11:37:16 2017
New Revision: 255053

URL: https://gcc.gnu.org/viewcvs?rev=255053&root=gcc&view=rev
Log:
2017-11-22  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/79072
	* trans-stmt.c (trans_associate_var): Weaken the over strong
	condition for using the fake result decl.

2017-11-22  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/79072
	* gfortran.dg/class_result_6.f90: New test for comment 10 of
	the PR.


Added:
    trunk/gcc/testsuite/gfortran.dg/class_result_6.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-stmt.c
    trunk/gcc/testsuite/ChangeLog
Comment 19 Neil Carlson 2017-11-22 19:42:27 UTC
This fixes the code of comment 12 for me.  All the other test cases continue to work as expected.  This can be closed as "fixed" as far as I'm concerned. Thanks Paul!
Comment 20 Paul Thomas 2017-11-23 21:03:20 UTC
Author: pault
Date: Thu Nov 23 21:02:48 2017
New Revision: 255120

URL: https://gcc.gnu.org/viewcvs?rev=255120&root=gcc&view=rev
Log:
2017-11-23  Paul Thomas  <pault@gcc.gnu.org>


	Backported from trunk
	PR fortran/79072
	* trans-expr.c (trans_class_vptr_len_assignment): Set from_len
	if the temporary is unlimited polymorphic.
	* trans-stmt.c (trans_associate_var): Use the fake result decl
	to obtain the 'len' field from an explicit function result when
	in that function scope.

2017-11-23  Paul Thomas  <pault@gcc.gnu.org>

	Backported from trunk
	PR fortran/79072
	* gfortran.dg/class_result_5.f90: New test.
	* gfortran.dg/class_result_6.f90: New test.


Added:
    branches/gcc-7-branch/gcc/testsuite/gfortran.dg/class_result_5.f90
    branches/gcc-7-branch/gcc/testsuite/gfortran.dg/class_result_6.f90
Modified:
    branches/gcc-7-branch/gcc/fortran/ChangeLog
    branches/gcc-7-branch/gcc/fortran/trans-expr.c
    branches/gcc-7-branch/gcc/fortran/trans-stmt.c
    branches/gcc-7-branch/gcc/testsuite/ChangeLog
Comment 21 Dominique d'Humieres 2018-02-20 16:08:59 UTC
Shouldn't this PR be closed?
Comment 22 Neil Carlson 2018-02-21 13:29:59 UTC
I just verified with 8.0 trunk (r257868) that all three of my examples continue to work as expected.
Comment 23 Thomas Koenig 2018-02-21 15:12:27 UTC
Let's close it then.

Thanks for the bug report!