(was: Re: [r12-4457 Regression] FAIL: gfortran.dg/deferred_type_param_6.f90 -Os execution test on Linux/x86_64)

Message ID bd183830-0e3f-a1c9-9c6c-97005cced372@codesourcery.com
State New
Headers
Series (was: Re: [r12-4457 Regression] FAIL: gfortran.dg/deferred_type_param_6.f90 -Os execution test on Linux/x86_64) |

Commit Message

Tobias Burnus Oct. 16, 2021, 6:48 p.m. UTC
  Hi Honza,

On 16.10.21 20:23, Jan Hubicka via Gcc-patches wrote:
>> FAIL: gfortran.dg/deferred_type_param_6.f90   -O1  execution test
>> FAIL: gfortran.dg/deferred_type_param_6.f90   -Os  execution test
> Sorry for the breakage.  This time it seems like bug in Fortran FE
> which was previously latent:
>
> __attribute__((fn spec (". . R ")))
> void subfunc (character(kind=1)[1:..__result] * & __result, integer(kind=8) * .__result)

...

Fortran has for a long time 'character(len=5), allocatable" or
"character(len=*)". In the first case, the "5" can be ignored as both
caller and callee know the length. In the second case, the length is
determined by the argument, but it cannot be changed.

Since a not-that-short while, 'len=:' together with allocatable/pointer
is supported.

In the latter case, the value can be change when the array
association/allocation is changed.

I attached a patch, which was not tested. I am not quite sure whether
the pointer address can actually escape or not - I think cannot but I
played safe.

Tobias
-----------------
Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht München, HRB 106955
  

Comments

Jan Hubicka Oct. 16, 2021, 6:54 p.m. UTC | #1
> 
> Fortran has for a long time 'character(len=5), allocatable" or
> "character(len=*)". In the first case, the "5" can be ignored as both
> caller and callee know the length. In the second case, the length is
> determined by the argument, but it cannot be changed.
> 
> Since a not-that-short while, 'len=:' together with allocatable/pointer
> is supported.
> 
> In the latter case, the value can be change when the array
> association/allocation is changed.
> 
> I attached a patch, which was not tested. I am not quite sure whether
> the pointer address can actually escape or not - I think cannot but I
> played safe.
> 
> Tobias
> -----------------
> Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht München, HRB 106955

> Fortran: Fix fn spec for character-returning functions
> 
> gcc/fortran/ChangeLog
> 	* trans-types.c (create_fn_spec): For character-returning functions,
> 	set the hidden string-length argument to 'R' only when the "len=:",
> 	i.e. deferred length which goes alongside with allocatable/pointer.
> 
> diff --git a/gcc/fortran/trans-types.c b/gcc/fortran/trans-types.c
> index 220976babb8..637d2c71d01 100644
> --- a/gcc/fortran/trans-types.c
> +++ b/gcc/fortran/trans-types.c
> @@ -3008,7 +3008,14 @@ create_fn_spec (gfc_symbol *sym, tree fntype)
>  	}
>        if (sym->ts.type == BT_CHARACTER)
>  	{
> -	  spec[spec_len++] = 'R';
> +	  if (!sym->ts.u.cl->length
> +	      && ((sym->attr.allocatable && sym->attr.target)
> +		  || sym->attr.pointer))
> +	    spec[spec_len++] = '.';
> +	  if (!sym->ts.u.cl->length && sym->attr.allocatable)
> +	    spec[spec_len++] = 'w';
> +	  else
> +	    spec[spec_len++] = 'R';
>  	  spec[spec_len++] = ' ';

Thanks a lot! I was just looking into that function and was quite
confused on what is going there. Are you going to commit the patch?
Also escaping is quite important bit of information so it would be
good to figure out if it really can escape rather than playing safe.

Honza
>  	}
>      }
  

Patch

Fortran: Fix fn spec for character-returning functions

gcc/fortran/ChangeLog
	* trans-types.c (create_fn_spec): For character-returning functions,
	set the hidden string-length argument to 'R' only when the "len=:",
	i.e. deferred length which goes alongside with allocatable/pointer.

diff --git a/gcc/fortran/trans-types.c b/gcc/fortran/trans-types.c
index 220976babb8..637d2c71d01 100644
--- a/gcc/fortran/trans-types.c
+++ b/gcc/fortran/trans-types.c
@@ -3008,7 +3008,14 @@  create_fn_spec (gfc_symbol *sym, tree fntype)
 	}
       if (sym->ts.type == BT_CHARACTER)
 	{
-	  spec[spec_len++] = 'R';
+	  if (!sym->ts.u.cl->length
+	      && ((sym->attr.allocatable && sym->attr.target)
+		  || sym->attr.pointer))
+	    spec[spec_len++] = '.';
+	  if (!sym->ts.u.cl->length && sym->attr.allocatable)
+	    spec[spec_len++] = 'w';
+	  else
+	    spec[spec_len++] = 'R';
 	  spec[spec_len++] = ' ';
 	}
     }