The following module is, at least according to the discussion on fortran-lang.discourse (https://fortran-lang.discourse.group/t/meaning-of-the-intent-for-pointer-dummy-arguments/2328/11) is standard complying, but gfortran stops with an error message when compiling it. Someone in that topic also posted a gcc-patch, which may fix the issue. Code: module m contains subroutine s1(a) real, pointer, intent(in) :: a call s2(a ) !<-- Ok with gfortran call random_number(a) !<-- but not this end subroutine subroutine s2(x) real, intent(out) :: x call random_number(x) end subroutine end module Error message: bug3.f90:6:25: 6 | call random_number(a) !<-- but not this | 1 Error: ‘harvest’ argument of ‘random_number’ intrinsic at (1) cannot be INTENT(IN)
Confirmed. As a side note: Intel does not like it, either. But NAG and Nvidia do.
The nearly obvious fix: diff --git a/gcc/fortran/check.c b/gcc/fortran/check.c index 837eb0912c0..3859e18c6c3 100644 --- a/gcc/fortran/check.c +++ b/gcc/fortran/check.c @@ -1031,7 +1031,7 @@ variable_check (gfc_expr *e, int n, bool allow_proc) break; } - if (!ref) + if (!ref && !pointer) { gfc_error ("%qs argument of %qs intrinsic at %L cannot be " "INTENT(IN)", gfc_current_intrinsic_arg[n]->name, regresses for gfortran.dg/move_alloc_8.f90, thus needs additional investigation.
(In reply to anlauf from comment #2) > The nearly obvious fix: > > diff --git a/gcc/fortran/check.c b/gcc/fortran/check.c > index 837eb0912c0..3859e18c6c3 100644 > --- a/gcc/fortran/check.c > +++ b/gcc/fortran/check.c > @@ -1031,7 +1031,7 @@ variable_check (gfc_expr *e, int n, bool allow_proc) > break; > } > > - if (!ref) > + if (!ref && !pointer) > { > gfc_error ("%qs argument of %qs intrinsic at %L cannot be " > "INTENT(IN)", gfc_current_intrinsic_arg[n]->name, > > regresses for gfortran.dg/move_alloc_8.f90, thus needs additional > investigation. Did you try the patch posted in Fortran Discourse?
(In reply to kargl from comment #3) > (In reply to anlauf from comment #2) > > The nearly obvious fix: > > > > diff --git a/gcc/fortran/check.c b/gcc/fortran/check.c > > index 837eb0912c0..3859e18c6c3 100644 > > --- a/gcc/fortran/check.c > > +++ b/gcc/fortran/check.c > > @@ -1031,7 +1031,7 @@ variable_check (gfc_expr *e, int n, bool allow_proc) > > break; > > } > > > > - if (!ref) > > + if (!ref && !pointer) > > { > > gfc_error ("%qs argument of %qs intrinsic at %L cannot be " > > "INTENT(IN)", gfc_current_intrinsic_arg[n]->name, > > > > regresses for gfortran.dg/move_alloc_8.f90, thus needs additional > > investigation. > > Did you try the patch posted in Fortran Discourse? No. I'm afraid I also missed it on the usual channels where patches for gcc are posted.
On Thu, Nov 25, 2021 at 09:02:34PM +0000, anlauf at gcc dot gnu.org wrote: > (In reply to kargl from comment #3) > > (In reply to anlauf from comment #2) > > > The nearly obvious fix: > > > > > > diff --git a/gcc/fortran/check.c b/gcc/fortran/check.c > > > index 837eb0912c0..3859e18c6c3 100644 > > > --- a/gcc/fortran/check.c > > > +++ b/gcc/fortran/check.c > > > @@ -1031,7 +1031,7 @@ variable_check (gfc_expr *e, int n, bool allow_proc) > > > break; > > > } > > > > > > - if (!ref) > > > + if (!ref && !pointer) > > > { > > > gfc_error ("%qs argument of %qs intrinsic at %L cannot be " > > > "INTENT(IN)", gfc_current_intrinsic_arg[n]->name, > > > > > > regresses for gfortran.dg/move_alloc_8.f90, thus needs additional > > > investigation. > > > > Did you try the patch posted in Fortran Discourse? > > No. > > I'm afraid I also missed it on the usual channels where patches for gcc > are posted. > As explained on FD, I don't report problems found be other people who post them in FD, stackoverflow, or c.l.f. I encourage those people to report the problems themselves. That said, you found the right location to patch. The code looks convoluted to deal with CLASS, which messes up an array with the pointer attribute. diff --git a/gcc/fortran/check.c b/gcc/fortran/check.c index 6ea6e136d4f..e96bcdb1b44 100644 --- a/gcc/fortran/check.c +++ b/gcc/fortran/check.c @@ -1031,7 +1031,7 @@ variable_check (gfc_expr *e, int n, bool allow_proc) break; } - if (!ref) + if (!ref && !(pointer && e->ref && e->ref->type == REF_ARRAY)) { gfc_error ("%qs argument of %qs intrinsic at %L cannot be " "INTENT(IN)", gfc_current_intrinsic_arg[n]->name, @@ -1062,7 +1062,8 @@ variable_check (gfc_expr *e, int n, bool allow_proc) return true; gfc_error ("%qs argument of %qs intrinsic at %L must be a variable", - gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic, &e->where); + gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic, + &e->where); return false; }
Unfortunately the patch in comment#5 does not work for me. :-( Interestingly, the Intel compiler fails on the testcase, too.
On Thu, Nov 25, 2021 at 10:10:32PM +0000, anlauf at gcc dot gnu.org wrote: > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103418 > > --- Comment #6 from anlauf at gcc dot gnu.org --- > Unfortunately the patch in comment#5 does not work for me. :-( > > Interestingly, the Intel compiler fails on the testcase, too. > Hmmm. I did have a number of other patches in my tree. I wonder if one of those helped. Unfortunately, I updated my git repository, where I cleared out all patch, and it takes a long time to rebuild gcc on my laptop.
On Thu, Nov 25, 2021 at 02:18:46PM -0800, Steve Kargl wrote: > On Thu, Nov 25, 2021 at 10:10:32PM +0000, anlauf at gcc dot gnu.org wrote: > > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103418 > > > > --- Comment #6 from anlauf at gcc dot gnu.org --- > > Unfortunately the patch in comment#5 does not work for me. :-( > > > > Interestingly, the Intel compiler fails on the testcase, too. > > > > Hmmm. I did have a number of other patches in my tree. I > wonder if one of those helped. Unfortunately, I updated > my git repository, where I cleared out all patch, and it > takes a long time to rebuild gcc on my laptop. > For the record, module test implicit none contains subroutine change_pointer_target(ptr) real, pointer, intent(in) :: ptr(:) call random_number(ptr) ptr(:) = ptr + 1.0 end subroutine change_pointer_target end module test program foo use test implicit none real, pointer :: a(:), b allocate(a(4), b) call random_number(b) call random_number(a) print '(5F8.5)', b, a end program foo % gfcx -o z a.f90 && ./z 0.65287 0.82614 0.77541 0.61923 0.52961
On Thu, Nov 25, 2021 at 10:10:32PM +0000, anlauf at gcc dot gnu.org wrote: > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103418 > > --- Comment #6 from anlauf at gcc dot gnu.org --- > Unfortunately the patch in comment#5 does not work for me. :-( > > Interestingly, the Intel compiler fails on the testcase, too. > "does not work for me" isn't too descriptive. I just bootstrap gcc on x86_64-*-freebsd from clean source (i.e., no patches at all). There are failures with reshape_shape_2.f90 and vector_subscript_1.f90. If I apply my simply patch, % git status On branch master Your branch is up to date with 'origin/master'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: gcc/fortran/check.c no changes added to commit (use "git add" and/or "git commit -a") % git diff gcc/fortran/check.c | cat diff --git a/gcc/fortran/check.c b/gcc/fortran/check.c index 5a5aca10ebe..a0870fc87f1 100644 --- a/gcc/fortran/check.c +++ b/gcc/fortran/check.c @@ -1031,7 +1031,7 @@ variable_check (gfc_expr *e, int n, bool allow_proc) break; } - if (!ref) + if (!ref && !(pointer && e->ref && e->ref->type == REF_ARRAY)) { gfc_error ("%qs argument of %qs intrinsic at %L cannot be " "INTENT(IN)", gfc_current_intrinsic_arg[n]->name, then the code compiles and produces % gfcx -o z a.f90 % ./z 0.76536 0.09307 0.55571 0.28371 0.79951
(In reply to Steve Kargl from comment #9) > "does not work for me" isn't too descriptive. Well, you fixed a related issue, but not the problem in comment#0. Try your patch on: module m contains subroutine s1 (a, b) real, pointer, intent(in) :: a, b(:) call random_number (a) call random_number (b) end subroutine s1 end module
On Fri, Nov 26, 2021 at 08:13:05PM +0000, anlauf at gcc dot gnu.org wrote: > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103418 > > --- Comment #10 from anlauf at gcc dot gnu.org --- > (In reply to Steve Kargl from comment #9) > > "does not work for me" isn't too descriptive. > > Well, you fixed a related issue, but not the problem in comment#0. > > Try your patch on: > > module m > contains > subroutine s1 (a, b) > real, pointer, intent(in) :: a, b(:) > call random_number (a) > call random_number (b) > end subroutine s1 > end module > That's at least sufficient to fix your issue. diff --git a/gcc/fortran/check.c b/gcc/fortran/check.c index 5a5aca10ebe..49bb9d350f0 100644 --- a/gcc/fortran/check.c +++ b/gcc/fortran/check.c @@ -1031,7 +1031,9 @@ variable_check (gfc_expr *e, int n, bool allow_proc) break; } - if (!ref) + if (!ref + && !(pointer + && ((e->ref && e->ref->type == REF_ARRAY) || e->rank == 0))) { gfc_error ("%qs argument of %qs intrinsic at %L cannot be " "INTENT(IN)", gfc_current_intrinsic_arg[n]->name, @@ -1062,7 +1064,8 @@ variable_check (gfc_expr *e, int n, bool allow_proc) return true; gfc_error ("%qs argument of %qs intrinsic at %L must be a variable", - gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic, &e->where); + gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic, + &e->where); return false; }
Packaged: https://gcc.gnu.org/pipermail/fortran/2021-December/057125.html
The master branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>: https://gcc.gnu.org/g:bb6a1ebb8585b85879735d0d6df9535885fad165 commit r12-5900-gbb6a1ebb8585b85879735d0d6df9535885fad165 Author: Harald Anlauf <anlauf@gmx.de> Date: Thu Dec 9 22:57:13 2021 +0100 Fortran: fix check for pointer dummy arguments with INTENT(IN) gcc/fortran/ChangeLog: PR fortran/103418 * check.c (variable_check): Replace previous check of procedure dummy arguments with INTENT(IN) attribute when passed to intrinsic procedures by gfc_check_vardef_context. * expr.c (gfc_check_vardef_context): Correct check of INTENT(IN) dummy arguments for the case of sub-components of a CLASS pointer. gcc/testsuite/ChangeLog: PR fortran/103418 * gfortran.dg/move_alloc_8.f90: Adjust error messages. * gfortran.dg/pointer_intent_9.f90: New test.
The releases/gcc-11 branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>: https://gcc.gnu.org/g:57b51b8bae4c2ecec3281929f57e4a6a0e6f9ea0 commit r11-9404-g57b51b8bae4c2ecec3281929f57e4a6a0e6f9ea0 Author: Harald Anlauf <anlauf@gmx.de> Date: Thu Dec 9 22:57:13 2021 +0100 Fortran: fix check for pointer dummy arguments with INTENT(IN) gcc/fortran/ChangeLog: PR fortran/103418 * check.c (variable_check): Replace previous check of procedure dummy arguments with INTENT(IN) attribute when passed to intrinsic procedures by gfc_check_vardef_context. * expr.c (gfc_check_vardef_context): Correct check of INTENT(IN) dummy arguments for the case of sub-components of a CLASS pointer. gcc/testsuite/ChangeLog: PR fortran/103418 * gfortran.dg/move_alloc_8.f90: Adjust error messages. * gfortran.dg/pointer_intent_9.f90: New test. (cherry picked from commit bb6a1ebb8585b85879735d0d6df9535885fad165)
The releases/gcc-10 branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>: https://gcc.gnu.org/g:f8486c9fd71c08f30273e1a98b86e35a679ca229 commit r10-10364-gf8486c9fd71c08f30273e1a98b86e35a679ca229 Author: Harald Anlauf <anlauf@gmx.de> Date: Thu Dec 9 22:57:13 2021 +0100 Fortran: fix check for pointer dummy arguments with INTENT(IN) gcc/fortran/ChangeLog: PR fortran/103418 * check.c (variable_check): Replace previous check of procedure dummy arguments with INTENT(IN) attribute when passed to intrinsic procedures by gfc_check_vardef_context. * expr.c (gfc_check_vardef_context): Correct check of INTENT(IN) dummy arguments for the case of sub-components of a CLASS pointer. gcc/testsuite/ChangeLog: PR fortran/103418 * gfortran.dg/move_alloc_8.f90: Adjust error messages. * gfortran.dg/pointer_intent_9.f90: New test. (cherry picked from commit bb6a1ebb8585b85879735d0d6df9535885fad165)
Fixed on mainline for gcc-12, and on 11- and 10-branch. Closing. Thanks for the report!