Hi all!
Proposed patch to:
Bug 94104 - Request for diagnostic improvement
Patch tested only on x86_64-pc-linux-gnu.
Error message improvement. In Fortran 2008 actual arguments to
procedures having a pointer, with intent attribute in, formal argument
can also have the target attribute not just pointer.
Thank you very much.
Best regards,
José Rui
Fortran: error message improvement.
PR fortran/94104
gcc/fortran/ChangeLog:
* interface.c (gfc_compare_actual_formal): improve error message.
gcc/testsuite/ChangeLog:
* gfortran.dg/parens_2.f90: update regex.
* gfortran.dg/PR94104a.f90: New test.
* gfortran.dg/PR94104b.f90: New test.
diff --git a/gcc/fortran/interface.c b/gcc/fortran/interface.c
index 9e3e8aa..4cc0708 100644
--- a/gcc/fortran/interface.c
+++ b/gcc/fortran/interface.c
@@ -3329,26 +3329,38 @@ gfc_compare_actual_formal (gfc_actual_arglist **ap, gfc_formal_arglist *formal,
return false;
}
- if (a->expr->expr_type != EXPR_NULL
- && compare_pointer (f->sym, a->expr) == 0)
+ if (a->expr->expr_type != EXPR_NULL)
{
- if (where)
- gfc_error ("Actual argument for %qs must be a pointer at %L",
- f->sym->name, &a->expr->where);
- return false;
- }
+ int cmp = compare_pointer (f->sym, a->expr);
+ bool pre2008 = ((gfc_option.allow_std & GFC_STD_F2008) == 0);
+
+ if (pre2008 && cmp == 0)
+ {
+ if (where)
+ gfc_error ("Actual argument for %qs at %L must be a pointer.",
+ f->sym->name, &a->expr->where);
+ return false;
+ }
+
+ if (pre2008 && cmp == 2)
+ {
+ if (where)
+ gfc_error ("Fortran 2008: Non-pointer actual argument at %L to "
+ "pointer dummy %qs", &a->expr->where,f->sym->name);
+ return false;
+ }
- if (a->expr->expr_type != EXPR_NULL
- && (gfc_option.allow_std & GFC_STD_F2008) == 0
- && compare_pointer (f->sym, a->expr) == 2)
- {
- if (where)
- gfc_error ("Fortran 2008: Non-pointer actual argument at %L to "
- "pointer dummy %qs", &a->expr->where,f->sym->name);
- return false;
+ if (!pre2008 && cmp == 0)
+ {
+ if (where)
+ gfc_error ("Actual argument for %qs at %L must be a pointer "
+ "or a valid target for the dummy pointer in a "
+ "pointer assignment statement.",
+ f->sym->name, &a->expr->where);
+ return false;
+ }
}
-
/* Fortran 2008, C1242. */
if (f->sym->attr.pointer && gfc_is_coindexed (a->expr))
{
diff --git a/gcc/testsuite/gfortran.dg/PR94104a.f90 b/gcc/testsuite/gfortran.dg/PR94104a.f90
new file mode 100644
index 0000000..acde7fe
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/PR94104a.f90
@@ -0,0 +1,35 @@
+! { dg-do compile }
+! { dg-options "-std=f2003" }
+! { dg-shouldfail "Actual argument" }
+!
+! PR fortran/94104
+!
+
+program diag_p
+
+ implicit none
+
+ integer, parameter :: n = 7
+
+ integer :: a(n)
+ integer, target :: b(n)
+
+ a = 1
+ print *, sumf(a) ! { dg-error "Actual argument for 'a' at .1. must be a pointer\\." }
+ print *, sumf(b) ! { dg-error "Fortran 2008: Non-pointer actual argument at .1. to pointer dummy 'a'" }
+ stop
+
+contains
+
+ function sumf(a) result(s)
+ integer, pointer, intent(in) :: a(:)
+
+ integer :: s
+
+ s = sum(a)
+ return
+ end function sumf
+
+end program diag_p
+
+
diff --git a/gcc/testsuite/gfortran.dg/PR94104b.f90 b/gcc/testsuite/gfortran.dg/PR94104b.f90
new file mode 100644
index 0000000..5018da9
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/PR94104b.f90
@@ -0,0 +1,35 @@
+! { dg-do compile }
+! { dg-options "-std=f2008" }
+! { dg-shouldfail "Actual argument" }
+!
+! PR fortran/94104
+!
+
+program diag_p
+
+ implicit none
+
+ integer, parameter :: n = 7
+
+ integer :: a(n)
+ integer, target :: b(n)
+
+ a = 1
+ print *, sumf(a) ! { dg-error "Actual argument for 'a' at .1. must be a pointer or a valid target" }
+ print *, sumf(b)
+ stop
+
+contains
+
+ function sumf(a) result(s)
+ integer, pointer, intent(in) :: a(:)
+
+ integer :: s
+
+ s = sum(a)
+ return
+ end function sumf
+
+end program diag_p
+
+
diff --git a/gcc/testsuite/gfortran.dg/parens_2.f90 b/gcc/testsuite/gfortran.dg/parens_2.f90
index bc2acd8..dc5965d 100644
--- a/gcc/testsuite/gfortran.dg/parens_2.f90
+++ b/gcc/testsuite/gfortran.dg/parens_2.f90
@@ -2,7 +2,7 @@
! { dg-do compile }
! Originally contributed by Joost VandeVondele
INTEGER, POINTER :: I
-CALL S1((I)) ! { dg-error "Actual argument for .i. must be a pointer" }
+CALL S1((I)) ! { dg-error "Actual argument for .i. at .1. must be a pointer or a valid target" }
CONTAINS
SUBROUTINE S1(I)
INTEGER, POINTER ::I