[Bug fortran/114141] ASSOCIATE and complex part ref when associate target is a function

2024-03-15 Thread pault at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114141

Paul Thomas  changed:

   What|Removed |Added

 Resolution|--- |FIXED
 Status|NEW |RESOLVED

--- Comment #16 from Paul Thomas  ---
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

[Bug fortran/114141] ASSOCIATE and complex part ref when associate target is a function

2024-03-15 Thread cvs-commit at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114141

--- Comment #15 from GCC Commits  ---
The master branch has been updated by Paul Thomas :

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

commit r14-9489-g3fd46d859cda1074125449a4cc680ce59fcebc38
Author: Paul Thomas 
Date:   Fri Mar 15 06:52:59 2024 +

Fortran: Fix class/derived/complex function associate selectors [PR87477]

2024-03-15  Paul Thomas  

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

[Bug fortran/114141] ASSOCIATE and complex part ref when associate target is a function

2024-03-02 Thread pault at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114141

--- Comment #14 from Paul Thomas  ---
To fix the parentheses wrinkle, this works:
diff --git a/gcc/fortran/match.cc b/gcc/fortran/match.cc
index eee569dac91..64f61c50c66 100644
--- a/gcc/fortran/match.cc
+++ b/gcc/fortran/match.cc
@@ -1963,6 +1963,20 @@ gfc_match_associate (void)
  goto assocListError;
}

+  /* If the selector expression is enclosed in parentheses and the
+expression is not a variable, throw the parentheses away.  */
+  while (newAssoc->target->expr_type == EXPR_OP
+&& newAssoc->target->value.op.op == INTRINSIC_PARENTHESES)
+   {
+ if (newAssoc->target->value.op.op1->expr_type == EXPR_VARIABLE)
+   break;
+ else
+   {
+ gfc_expr *e = gfc_copy_expr (newAssoc->target->value.op.op1);
+ gfc_replace_expr (newAssoc->target, e);
+   }
+   }
+
   /* The `variable' field is left blank for now; because the target is not

To maintain compatibility with
https://gcc.gnu.org/pipermail/fortran/2024-January/060092.html:

@@ -2220,7 +2235,8 @@ gfc_match_varspec (gfc_expr *primary, int equiv_flag,
bool sub_flag,
|| tgt_expr->symtree->n.sym->attr.if_source
== IFSRC_DECL);
   permissible = permissible
-   || (tgt_expr && tgt_expr->expr_type == EXPR_OP);
+   || (tgt_expr && (tgt_expr->expr_type == EXPR_OP
+   || (inquiry && tgt_expr->expr_type == EXPR_FUNCTION)));

   if (permissible)
{

[Bug fortran/114141] ASSOCIATE and complex part ref when associate target is a function

2024-03-01 Thread pault at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114141

--- Comment #13 from Paul Thomas  ---
(In reply to Steve Kargl from comment #11)
...snip...
> I know you had some ASSOCIATE patches in the works, and
> certainly do not want to interfere.  Do you want to
> incorporate my patch or some variation into your work?
> I'm hoping to take a stab at the issue Jerry raised 
> with parentheses this weekend.

Hi Steve,

Interference was not what I had in mind :-)

I was thinking of breaking the patch
https://gcc.gnu.org/pipermail/fortran/2024-January/060092.html in two; the
first to deal with derived type functions and the second for class functions.
Your patch for this PR would sit nicely in the first.

Cheers

Paul

[Bug fortran/114141] ASSOCIATE and complex part ref when associate target is a function

2024-03-01 Thread pault at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114141

--- Comment #12 from Paul Thomas  ---
(In reply to Steve Kargl from comment #11)
...snip...
> I know you had some ASSOCIATE patches in the works, and
> certainly do not want to interfere.  Do you want to
> incorporate my patch or some variation into your work?
> I'm hoping to take a stab at the issue Jerry raised 
> with parentheses this weekend.

Hi Steve,

Interference was not what I had in mind :-)

I was thinking of breaking the patch
https://gcc.gnu.org/pipermail/fortran/2024-January/060092.html in two; the
first to deal with derived type functions and the second for class functions.
Your patch for this PR would sit nicely in the first.

Cheers

Paul

[Bug fortran/114141] ASSOCIATE and complex part ref when associate target is a function

2024-02-29 Thread sgk at troutmask dot apl.washington.edu via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114141

--- Comment #11 from Steve Kargl  ---
On Thu, Feb 29, 2024 at 06:33:51PM +, pault at gcc dot gnu.org wrote:
> --- Comment #10 from Paul Thomas  ---
> (In reply to Jerry DeLisle from comment #9)
> > --- snip ---
> > > % gfcx -o z a.f90
> > > a.f90:5:6:
> > > 
> > > 5 |   x%im = 42
> > >   |  1
> > > Error: 'x' at (1) associated to expression cannot be used in
> > > a variable definition context (assignment)
> > > 
> > > Mikael, thanks for the feedback.  I'll see if I can fix
> > > the parentheses case this weekend.
> > 
> > This is definitely a 42 case, which is why I had three '?' in my reply.
> > 
> > And if you understand this, you are OK in my book. :)
> 
> BTW Both nagfor and ifort compile the testcase without complaint.
> 
> Your fix is closely related to my patch for not-yet-parsed function selectors.
> 

I know you had some ASSOCIATE patches in the works, and
certainly do not want to interfere.  Do you want to
incorporate my patch or some variation into your work?
I'm hoping to take a stab at the issue Jerry raised 
with parentheses this weekend.

[Bug fortran/114141] ASSOCIATE and complex part ref when associate target is a function

2024-02-29 Thread pault at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114141

Paul Thomas  changed:

   What|Removed |Added

 Ever confirmed|0   |1
 Status|UNCONFIRMED |NEW
 CC||pault at gcc dot gnu.org
   Last reconfirmed||2024-02-29

--- Comment #10 from Paul Thomas  ---
(In reply to Jerry DeLisle from comment #9)
> --- snip ---
> > % gfcx -o z a.f90
> > a.f90:5:6:
> > 
> > 5 |   x%im = 42
> >   |  1
> > Error: 'x' at (1) associated to expression cannot be used in
> > a variable definition context (assignment)
> > 
> > Mikael, thanks for the feedback.  I'll see if I can fix
> > the parentheses case this weekend.
> 
> This is definitely a 42 case, which is why I had three '?' in my reply.
> 
> And if you understand this, you are OK in my book. :)

BTW Both nagfor and ifort compile the testcase without complaint.

Your fix is closely related to my patch for not-yet-parsed function selectors.

Cheers

Paul

[Bug fortran/114141] ASSOCIATE and complex part ref when associate target is a function

2024-02-28 Thread jvdelisle at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114141

--- Comment #9 from Jerry DeLisle  ---
--- snip ---
> % gfcx -o z a.f90
> a.f90:5:6:
> 
> 5 |   x%im = 42
>   |  1
> Error: 'x' at (1) associated to expression cannot be used in
> a variable definition context (assignment)
> 
> Mikael, thanks for the feedback.  I'll see if I can fix
> the parentheses case this weekend.

This is definitely a 42 case, which is why I had three '?' in my reply.

And if you understand this, you are OK in my book. :)

[Bug fortran/114141] ASSOCIATE and complex part ref when associate target is a function

2024-02-28 Thread sgk at troutmask dot apl.washington.edu via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114141

--- Comment #8 from Steve Kargl  ---
On Wed, Feb 28, 2024 at 08:24:16PM +, sgk at troutmask dot
apl.washington.edu wrote:
> 
> Indeed.  Bit more reading of F2023, 11.1.3 agrees with you.
> 
>11.1.3.1
> 
>The ASSOCIATE construct associates named entities with expressions
>or variables during the execution of its block.  These named construct
>entities (19.4) are associating entities (19.5.1.6).  The names are
>associate names.
> 
>11.1.3.3(5) The associating entity itself is a variable, but ...
> 
> The "but ..." applies to whether the selector is a definable variable.
> 
> So, 'y = x%im' is allowed, but 'x%im = 42' is disallowed because
> the selector is not definable.  Interesting twist.  This then 
> suggests that Jerry's use of parentheses should be accepted.
> 

As a quick follow-up.

program p
   associate(x => sin(cmplx(0.5,0.5)))
  print *, x
  print *, x%im  ! <-- allowed with my patch
  x%im = 42  ! <-- this is an error
  print *, x
   end associate
end

% gfcx -o z a.f90
a.f90:5:6:

5 |   x%im = 42
  |  1
Error: 'x' at (1) associated to expression cannot be used in
a variable definition context (assignment)

Mikael, thanks for the feedback.  I'll see if I can fix
the parentheses case this weekend.

[Bug fortran/114141] ASSOCIATE and complex part ref when associate target is a function

2024-02-28 Thread sgk at troutmask dot apl.washington.edu via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114141

--- Comment #7 from Steve Kargl  ---
On Wed, Feb 28, 2024 at 07:27:24PM +, mikael at gcc dot gnu.org wrote:
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114141
> 
> --- Comment #6 from Mikael Morin  ---
> (In reply to kargl from comment #5)
> > (In reply to Mikael Morin from comment #4)
> > 
> > > (In reply to kargl from comment #3)
> > > > Yep, agreed.  I went back an re-read the section about ASSOCIATE.
> > > > Not sure how I convinced myself that a constant expression, which
> > > > reduces to a constant is okay.
> > > > 
> > > Not sure how you convinced yourself it isn't. ;-)
> > 
> > x => log(cmplx(-1,0))
> > 
> > R1104 association  is associate-name => selector
> > 
> > R1105 selector is expr
> >or variable
> > 
> > R902 variable  is designator
> >or function-reference
> > 
> > R901 designatoris object-name
> >or array-element
> >or array-section
> >or coindexed-named-object
> >or complex-part-designator
> >or structure-component
> >or substring
> > 
> > log(cmplx(-1,0)) is certainly not a designator.
> > 
> > log(cmplx(-1,0)) is a function-reference.  But this then
> > leads to
> > 
> > C902 (R902) function-reference shall have a data pointer result.
> > 
> > 
> > log(cmplx(-1,0)) violates C902, so this then means that it
> > must be an expr.
> Agreed.
> 
> >  One now needs
> > 
> > 
> > R915 complex-part-designator  is designator % RE
> >   or designator % IM
> > 
> > C922 (R915) The designator shall be of complex type.
> > 
> > which shows that expr%im is invalid; even though log(cmplx(-1,0))
> > reduces to a constant (i.e., it's not a named constant.  This
> > is likely the error [pun intended] in my ways.).
> > 
> This is about x%im, which is a different expression from log(cmplx(-1, 0)).
> x is an associate-name, and thus (I think) an object-name, and a valid
> designator, even if it's associated selector isn't.
> 

Indeed.  Bit more reading of F2023, 11.1.3 agrees with you.

   11.1.3.1

   The ASSOCIATE construct associates named entities with expressions
   or variables during the execution of its block.  These named construct
   entities (19.4) are associating entities (19.5.1.6).  The names are
   associate names.

   11.1.3.3(5) The associating entity itself is a variable, but ...

The "but ..." applies to whether the selector is a definable variable.

So, 'y = x%im' is allowed, but 'x%im = 42' is disallowed because
the selector is not definable.  Interesting twist.  This then 
suggests that Jerry's use of parentheses should be accepted.

[Bug fortran/114141] ASSOCIATE and complex part ref when associate target is a function

2024-02-28 Thread mikael at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114141

--- Comment #6 from Mikael Morin  ---
(In reply to kargl from comment #5)
> (In reply to Mikael Morin from comment #4)
> 
> > (In reply to kargl from comment #3)
> > > Yep, agreed.  I went back an re-read the section about ASSOCIATE.
> > > Not sure how I convinced myself that a constant expression, which
> > > reduces to a constant is okay.
> > > 
> > Not sure how you convinced yourself it isn't. ;-)
> 
> x => log(cmplx(-1,0))
> 
> R1104 association  is associate-name => selector
> 
> R1105 selector is expr
>or variable
> 
> R902 variable  is designator
>or function-reference
> 
> R901 designatoris object-name
>or array-element
>or array-section
>or coindexed-named-object
>or complex-part-designator
>or structure-component
>or substring
> 
> log(cmplx(-1,0)) is certainly not a designator.
> 
> log(cmplx(-1,0)) is a function-reference.  But this then
> leads to
> 
> C902 (R902) function-reference shall have a data pointer result.
> 
> 
> log(cmplx(-1,0)) violates C902, so this then means that it
> must be an expr.
Agreed.

>  One now needs
> 
> 
> R915 complex-part-designator  is designator % RE
>   or designator % IM
> 
> C922 (R915) The designator shall be of complex type.
> 
> which shows that expr%im is invalid; even though log(cmplx(-1,0))
> reduces to a constant (i.e., it's not a named constant.  This
> is likely the error [pun intended] in my ways.).
> 
This is about x%im, which is a different expression from log(cmplx(-1, 0)).
x is an associate-name, and thus (I think) an object-name, and a valid
designator, even if it's associated selector isn't.

[Bug fortran/114141] ASSOCIATE and complex part ref when associate target is a function

2024-02-28 Thread kargl at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114141

kargl at gcc dot gnu.org changed:

   What|Removed |Added

   Priority|P4  |P5
   Severity|normal  |enhancement

--- Comment #5 from kargl at gcc dot gnu.org ---
(In reply to Mikael Morin from comment #4)

> (In reply to kargl from comment #3)
> > Yep, agreed.  I went back an re-read the section about ASSOCIATE.
> > Not sure how I convinced myself that a constant expression, which
> > reduces to a constant is okay.
> > 
> Not sure how you convinced yourself it isn't. ;-)

x => log(cmplx(-1,0))

R1104 association  is associate-name => selector

R1105 selector is expr
   or variable

R902 variable  is designator
   or function-reference

R901 designatoris object-name
   or array-element
   or array-section
   or coindexed-named-object
   or complex-part-designator
   or structure-component
   or substring

log(cmplx(-1,0)) is certainly not a designator.

log(cmplx(-1,0)) is a function-reference.  But this then
leads to

C902 (R902) function-reference shall have a data pointer result.


log(cmplx(-1,0)) violates C902, so this then means that it
must be an expr.  One now needs


R915 complex-part-designator  is designator % RE
  or designator % IM

C922 (R915) The designator shall be of complex type.

which shows that expr%im is invalid; even though log(cmplx(-1,0))
reduces to a constant (i.e., it's not a named constant.  This
is likely the error [pun intended] in my ways.).

Sometimes the trees get in the way of seeing the forest.


Arguably, the error message is wrong
gfortran13 -c a.f90
a.f90:6:13:

6 |   y = x%im
  | 1
Error: Symbol 'x' at (1) has no IMPLICIT type

'x' has the type of COMPLEX.

The version of the code where Jerry wraps log(cmplx(-1,0))
in parentheses.  Generates a better error message


$ gfc pr114141.f90
pr114141.f90:6:14:

6 |   y = x%im
  |  1
Error: The RE or IM part_ref at (1) must be applied to a COMPLEX expression

but this is still wrong in that RE and IM are applied to a designator.

I'll leave the PR open has an enhancement request.

[Bug fortran/114141] ASSOCIATE and complex part ref when associate target is a function

2024-02-28 Thread mikael at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114141

Mikael Morin  changed:

   What|Removed |Added

 CC||mikael at gcc dot gnu.org

--- Comment #4 from Mikael Morin  ---
(In reply to Jerry DeLisle from comment #2)
> It looks like the 'selector' in this case is an expr.
> 
Agreed.

> The expr must be a pointer object or a 'designator'
> 
Really?  Can't find the constraint saying that.
The only associate constraint mentioning 'designator' I can see is this:

  C1105 (R1105) expr shall not be a designator of a procedure pointer or a
function reference that returns a procedure pointer.

... but it relates more to procedure pointers (and it is a 'shall NOT be'
constraint).


(In reply to kargl from comment #3)
> Yep, agreed.  I went back an re-read the section about ASSOCIATE.
> Not sure how I convinced myself that a constant expression, which
> reduces to a constant is okay.
> 
Not sure how you convinced yourself it isn't. ;-)

[Bug fortran/114141] ASSOCIATE and complex part ref when associate target is a function

2024-02-28 Thread kargl at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114141

kargl at gcc dot gnu.org changed:

   What|Removed |Added

   Priority|P3  |P4

--- Comment #3 from kargl at gcc dot gnu.org ---
(In reply to Jerry DeLisle from comment #2)
> It looks like the 'selector' in this case is an expr.
> 
> The expr must be a pointer object or a 'designator'
> 
> A designator must be:
> 
> R901
> designator
> 
> object-name
> array-element
> array-section
> coindexed-named-object
> complex-part-designator
> structure-component
> substring
> 
> I am not seeing the expr in the example as one of these listed. ???

Yep, agreed.  I went back an re-read the section about ASSOCIATE.
Not sure how I convinced myself that a constant expression, which
reduces to a constant is okay.

I suppose the question is "do we generate a better error message
or simply close the PR?"

[Bug fortran/114141] ASSOCIATE and complex part ref when associate target is a function

2024-02-27 Thread jvdelisle at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114141

--- Comment #2 from Jerry DeLisle  ---
It looks like the 'selector' in this case is an expr.

The expr must be a pointer object or a 'designator'

A designator must be:

R901
designator

object-name
array-element
array-section
coindexed-named-object
complex-part-designator
structure-component
substring

I am not seeing the expr in the example as one of these listed. ???

[Bug fortran/114141] ASSOCIATE and complex part ref when associate target is a function

2024-02-27 Thread jvdelisle at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114141

Jerry DeLisle  changed:

   What|Removed |Added

 CC||jvdelisle at gcc dot gnu.org

--- Comment #1 from Jerry DeLisle  ---
I see the fail here but I am not sure of the validity. I am going to read the
latest standard. I always thought of associate to refer associate complicated
data references to make code more readable. In this case it is referring to a
function which resolves to a constant.

Interestingly if I wrap the associate with parens one gets:

   associate (x => (log(cmplx(-1,0

$ gfc pr114141.f90 
pr114141.f90:6:14:

6 |   y = x%im
  |  1
Error: The RE or IM part_ref at (1) must be applied to a COMPLEX expression