Skip to content

Commit 4299355

Browse files
author
Paul Thomas
committed
Fortran: Add error for subroutine passed to a variable dummy [PR106999]
2024-04-02 Paul Thomas <pault@gcc.gnu.org> gcc/fortran PR fortran/106999 * interface.cc (gfc_compare_interfaces): Add error for a subroutine proc pointer passed to a variable formal. (compare_parameter): If a procedure pointer is being passed to a non-procedure formal arg, and there is an an interface, use gfc_compare_interfaces to check and provide a more useful error message. gcc/testsuite/ PR fortran/106999 * gfortran.dg/pr106999.f90: New test. (cherry picked from commit a7aa945)
1 parent f598a1c commit 4299355

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

gcc/fortran/interface.cc

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1752,6 +1752,14 @@ gfc_compare_interfaces (gfc_symbol *s1, gfc_symbol *s2, const char *name2,
17521752
return false;
17531753
}
17541754

1755+
if (s2->attr.subroutine && s1->attr.flavor == FL_VARIABLE)
1756+
{
1757+
if (errmsg != NULL)
1758+
snprintf (errmsg, err_len, "subroutine proc pointer '%s' passed "
1759+
"to dummy variable '%s'", name2, s1->name);
1760+
return false;
1761+
}
1762+
17551763
/* Do strict checks on all characteristics
17561764
(for dummy procedures and procedure pointer assignments). */
17571765
if (!generic_flag && strict_flag)
@@ -2388,12 +2396,22 @@ compare_parameter (gfc_symbol *formal, gfc_expr *actual,
23882396
{
23892397
gfc_symbol *act_sym = actual->symtree->n.sym;
23902398

2391-
if (formal->attr.flavor != FL_PROCEDURE)
2399+
if (formal->attr.flavor != FL_PROCEDURE && !act_sym->ts.interface)
23922400
{
23932401
if (where)
23942402
gfc_error ("Invalid procedure argument at %L", &actual->where);
23952403
return false;
23962404
}
2405+
else if (act_sym->ts.interface
2406+
&& !gfc_compare_interfaces (formal, act_sym->ts.interface,
2407+
act_sym->name, 0, 1, err,
2408+
sizeof(err),NULL, NULL))
2409+
{
2410+
if (where)
2411+
gfc_error_opt (0, "Interface mismatch in dummy procedure %qs at %L:"
2412+
" %s", formal->name, &actual->where, err);
2413+
return false;
2414+
}
23972415

23982416
if (!gfc_compare_interfaces (formal, act_sym, act_sym->name, 0, 1, err,
23992417
sizeof(err), NULL, NULL))
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
! { dg-do compile }
2+
! Test the fix for PR106999
3+
! Contributed by Gerhard Steinmetz <gscfq@t-online.de>
4+
program p
5+
type t
6+
integer :: i
7+
procedure(g), pointer :: f
8+
end type
9+
class(t), allocatable :: y, z
10+
procedure(g), pointer :: ff
11+
allocate (z)
12+
z%i = 42
13+
z%f => g
14+
ff => g
15+
call r(z%f)
16+
call s(z%f) ! { dg-error "Interface mismatch in dummy procedure" }
17+
call s(ff) ! { dg-error "Interface mismatch in dummy procedure" }
18+
contains
19+
subroutine g(x)
20+
class(t) :: x
21+
x%i = 84
22+
end
23+
subroutine r(x)
24+
procedure(g) :: x
25+
print *, "in r"
26+
allocate (y)
27+
call x(y)
28+
print *, y%i
29+
end
30+
subroutine s(x)
31+
class(*) :: x
32+
end subroutine
33+
end

0 commit comments

Comments
 (0)