Bug 84924 - Erroneous error in C_F_POINTER
Summary: Erroneous error in C_F_POINTER
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 7.3.0
: P4 normal
Target Milestone: ---
Assignee: Dominique d'Humieres
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-03-17 15:48 UTC by Seth Johnson
Modified: 2018-03-25 14:13 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2018-03-23 00:00:00


Attachments
Module exemplifying failure (281 bytes, text/plain)
2018-03-17 15:48 UTC, Seth Johnson
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Seth Johnson 2018-03-17 15:48:49 UTC
Created attachment 43691 [details]
Module exemplifying failure

The gfortran compiler mistakenly prohibits calls to `c_f_pointer` where `fptr` is a noninteroperable *scalar* derived type. The standard only prohibits this for *arrays* of the derived type (and this restriction is lifted in TS29113).

The compiler correctly allows `c_loc(fptr)` on the same pointer object; and according to the standard the ability to do `c_loc` and `c_f_pointer` should be symmetric.

I have attached a minimal test case:
```
$ gfortran-mp-7 -save-temps -std=f2003 -c -Wall -Wextra ftest.f90
ftest.f90:27:38:

         call c_f_pointer(cptr=p, fptr=handle)
                                      1
Error: TS 29113/TS 18508: Noninteroperable array FPTR at (1) to C_F_POINTER: Expression is a noninteroperable derived type
```

I think the code in `gfc_check_c_f_pointer` in `gcc/fortran/check.c` simply needs to be replaced by similar code in `gfc_check_c_loc`:
```
if (!is_c_interoperable (fptr, &msg, false, true))
```
should become
```
if (fptr->rank > 0 && !is_c_interoperable (fptr, &msg, false, true))
```
Comment 1 Seth Johnson 2018-03-17 15:54:54 UTC
- Correction in test case: `call dellocate(` should be replaced by `deallocate(` (but this doesn't affect the behavior or test outcome
- Also note that the Fortran 2003 standard itself includes a structurally identical example in "C.10.2.4 Example of opaque communication between C and Fortran", if further evidence is needed that this is allowable with `-std=f2003` and `-std=f2008`.
Comment 2 Harald Anlauf 2018-03-17 17:18:36 UTC
There is no error for -std=f2008ts or -std=2018.
Comment 3 Seth Johnson 2018-03-17 17:34:25 UTC
That's correct, because those standards include TS 29113 which allows arrays to be referenced. `-std=f2008ts` also works. The problem is that the usage described is valid Fortran 2003 but is marked otherwise.

This is like the "const" qualifier raising an error with `gcc -std=c89` but allowing it with `gcc -std=c99`. It's valid with C99, sure, but the feature is allowed in the C89 standard as well.
Comment 4 Dominique d'Humieres 2018-03-23 12:20:15 UTC
Is the following patch OK?

       return false;
     }
 
-  if (!is_c_interoperable (fptr, &msg, false, true))
+  if (fptr->rank > 0 && !is_c_interoperable (fptr, &msg, false, true))
     return gfc_notify_std (GFC_STD_F2008_TS, "Noninteroperable array FPTR "
 			   "at %L to C_F_POINTER: %s", &fptr->where, msg);
Comment 5 Seth Johnson 2018-03-23 13:24:00 UTC
That looks perfect, thank you for looking into this.

(In reply to Dominique d'Humieres from comment #4)
> Is the following patch OK?
> 
>        return false;
>      }
>  
> -  if (!is_c_interoperable (fptr, &msg, false, true))
> +  if (fptr->rank > 0 && !is_c_interoperable (fptr, &msg, false, true))
>      return gfc_notify_std (GFC_STD_F2008_TS, "Noninteroperable array FPTR "
>  			   "at %L to C_F_POINTER: %s", &fptr->where, msg);
Comment 6 Dominique d'Humieres 2018-03-23 13:37:44 UTC
> That looks perfect, thank you for looking into this.

So taking the PR. I'll do the packaging and submit it.
Comment 7 dominiq 2018-03-25 11:31:08 UTC
Author: dominiq
Date: Sun Mar 25 11:30:24 2018
New Revision: 258843

URL: https://gcc.gnu.org/viewcvs?rev=258843&root=gcc&view=rev
Log:
2018-03-25  Seth Johnson <johnsonsr@ornl.gov>
	Dominique d'Humieres  <dominiq@gcc.gnu.org>

	PR fortran/84924
	* check.c (gfc_check_c_f_pointer): Allow scalar noninteroperable
	scalar derived type with -std=f2003 and -std=f2008.

2018-03-25  Seth Johnson <johnsonsr@ornl.gov>
	Dominique d'Humieres  <dominiq@gcc.gnu.org>

	PR fortran/84924
	* gfortran.dg/scalar_pointer_1.f90: New test.


Added:
    trunk/gcc/testsuite/gfortran.dg/scalar_pointer_1.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/check.c
    trunk/gcc/testsuite/ChangeLog
Comment 8 Dominique d'Humieres 2018-03-25 11:33:54 UTC
Closing as FIXED.
Comment 9 Seth Johnson 2018-03-25 14:13:58 UTC
Thanks guys for the fast fix and review!