public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Fortran: procedures with BIND(C) attribute require explicit interface [PR85877]
@ 2023-03-17 21:36 Harald Anlauf
  2023-03-18 18:52 ` Thomas Koenig
  0 siblings, 1 reply; 7+ messages in thread
From: Harald Anlauf @ 2023-03-17 21:36 UTC (permalink / raw)
  To: fortran, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 363 bytes --]

Dear all,

the Fortran standard requires an explicit procedure interface in certain
situations, such as when they have a BIND(C) attribute (F2018:15.4.2.2).
The attached patch adds a check for this.

Regtested on x86_64-pc-linux-gnu.  OK for mainline?

The PR marks this as a long-time regression, so it might be backported
to all open branches.

Thanks,
Harald


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: pr85877.diff --]
[-- Type: text/x-patch, Size: 1895 bytes --]

From c48c670ff0ce4f0d2ffb1d43aca2ec1bed1fa2ef Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Fri, 17 Mar 2023 22:24:49 +0100
Subject: [PATCH] Fortran: procedures with BIND(C) attribute require explicit
 interface [PR85877]

gcc/fortran/ChangeLog:

	PR fortran/85877
	* resolve.cc (resolve_fl_procedure): Check for an explicit interface
	of procedures with the BIND(C) attribute (F2018:15.4.2.2).

gcc/testsuite/ChangeLog:

	PR fortran/85877
	* gfortran.dg/pr85877.f90: New test.
---
 gcc/fortran/resolve.cc                | 10 ++++++++++
 gcc/testsuite/gfortran.dg/pr85877.f90 | 17 +++++++++++++++++
 2 files changed, 27 insertions(+)
 create mode 100644 gcc/testsuite/gfortran.dg/pr85877.f90

diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc
index 46585879ddc..7ec65f16240 100644
--- a/gcc/fortran/resolve.cc
+++ b/gcc/fortran/resolve.cc
@@ -13661,6 +13661,16 @@ check_formal:
 	    }
 	}
     }
+
+  /* F2018:15.4.2.2 requires an explicit interface for procedures with the
+     BIND(C) attribute.  */
+  if (sym->attr.is_bind_c && sym->attr.if_source == IFSRC_UNKNOWN)
+    {
+      gfc_error ("Interface of %qs at %L must be explicit",
+		 sym->name, &sym->declared_at);
+      return false;
+    }
+
   return true;
 }

diff --git a/gcc/testsuite/gfortran.dg/pr85877.f90 b/gcc/testsuite/gfortran.dg/pr85877.f90
new file mode 100644
index 00000000000..675faac0027
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr85877.f90
@@ -0,0 +1,17 @@
+! { dg-do compile }
+! PR fortran/85877
+! A procedure with the bind(c) attribute shall have an explicit interface
+! Contributed by G. Steinmetz
+
+subroutine p
+  bind(c) f     ! { dg-error "must be explicit" }
+  x = f()
+end
+
+subroutine s
+  interface
+     function g() bind(c)
+     end function g
+  end interface
+  x = g()
+end
--
2.35.3


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Fortran: procedures with BIND(C) attribute require explicit interface [PR85877]
  2023-03-17 21:36 [PATCH] Fortran: procedures with BIND(C) attribute require explicit interface [PR85877] Harald Anlauf
@ 2023-03-18 18:52 ` Thomas Koenig
  2023-03-18 20:59   ` Harald Anlauf
  0 siblings, 1 reply; 7+ messages in thread
From: Thomas Koenig @ 2023-03-18 18:52 UTC (permalink / raw)
  To: Harald Anlauf, fortran, gcc-patches

Hi Harald,

> the Fortran standard requires an explicit procedure interface in certain
> situations, such as when they have a BIND(C) attribute (F2018:15.4.2.2).
> The attached patch adds a check for this.
> 
> Regtested on x86_64-pc-linux-gnu.  OK for mainline?

While this fixes the ICE, it misses

function f() bind(c)
   f = 42.
end

subroutine p
   bind(c) f     ! { dg-error "must be explicit" }
   x = f()
end

subroutine s
   interface
      function g() bind(c)
      end function g
   end interface
   x = g()
end

where the interface is picked up via a global symbol.  This code
may not be valid; nagfor rejects it, but I cannot find a
constraint at least in the F2022 draft that prohibits it.

Hm... might it be better to check for attr->module_procedure ||
attr->if_source == IFSRC_IFBODY?

Best regards

	Thomas


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Fortran: procedures with BIND(C) attribute require explicit interface [PR85877]
  2023-03-18 18:52 ` Thomas Koenig
@ 2023-03-18 20:59   ` Harald Anlauf
  2023-03-18 20:59     ` Harald Anlauf
  2023-03-19  7:34     ` Thomas Koenig
  0 siblings, 2 replies; 7+ messages in thread
From: Harald Anlauf @ 2023-03-18 20:59 UTC (permalink / raw)
  To: Thomas Koenig, fortran, gcc-patches

Hi Thomas,

Am 18.03.23 um 19:52 schrieb Thomas Koenig via Gcc-patches:
> Hi Harald,
>
>> the Fortran standard requires an explicit procedure interface in certain
>> situations, such as when they have a BIND(C) attribute (F2018:15.4.2.2).
>> The attached patch adds a check for this.
>>
>> Regtested on x86_64-pc-linux-gnu.  OK for mainline?
>
> While this fixes the ICE, it misses
>
> function f() bind(c)
>    f = 42.
> end
>
> subroutine p
>    bind(c) f     ! { dg-error "must be explicit" }
>    x = f()
> end

what do you mean by "it misses"?  I do not see an explicit interface
here, only a global symbol.  All compiler I tried with the above
code reject it one way or the other, e g. Cray:

   x = f()
   ^
ftn-954 crayftn: ERROR P, File = pr85877-2.f90, Line = 12, Column = 3
   Procedure "f", referenced at line 6 (pr85877-2.f90) must have an
explicit interface because one or more arguments have the BIND attribute.


> subroutine s
>    interface
>       function g() bind(c)
>       end function g
>    end interface
>    x = g()
> end
>
> where the interface is picked up via a global symbol.

Is it really true that this is in the spirit of the standard?
Is the global declaration above really equivalent to an explicit
interface?

I would expect legal code to be like:

function g() bind(c)
   g = 42.
end

subroutine s
   interface
      function g() bind(c)
      end function g
   end interface
   x = g()
end

unless you do it in the context of a module and in the right way.

> This code
> may not be valid; nagfor rejects it, but I cannot find a
> constraint at least in the F2022 draft that prohibits it.

I thought that F2018:15.4.2.2 and F2023:15.4.2.2(7) are rather
clear and "explicit" on this.  IMHO the case of "f" above seems
to be excluded and thus not conforming.

> Hm... might it be better to check for attr->module_procedure ||
> attr->if_source == IFSRC_IFBODY?

Maybe we should find a set of legal and illegal cases that we
can agree upon.

Thanks,
Harald

> Best regards
>
>      Thomas
>
>


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Fortran: procedures with BIND(C) attribute require explicit interface [PR85877]
  2023-03-18 20:59   ` Harald Anlauf
@ 2023-03-18 20:59     ` Harald Anlauf
  2023-03-19  7:34     ` Thomas Koenig
  1 sibling, 0 replies; 7+ messages in thread
From: Harald Anlauf @ 2023-03-18 20:59 UTC (permalink / raw)
  To: gcc-patches; +Cc: fortran

Hi Thomas,

Am 18.03.23 um 19:52 schrieb Thomas Koenig via Gcc-patches:
> Hi Harald,
> 
>> the Fortran standard requires an explicit procedure interface in certain
>> situations, such as when they have a BIND(C) attribute (F2018:15.4.2.2).
>> The attached patch adds a check for this.
>>
>> Regtested on x86_64-pc-linux-gnu.  OK for mainline?
> 
> While this fixes the ICE, it misses
> 
> function f() bind(c)
>    f = 42.
> end
> 
> subroutine p
>    bind(c) f     ! { dg-error "must be explicit" }
>    x = f()
> end

what do you mean by "it misses"?  I do not see an explicit interface
here, only a global symbol.  All compiler I tried with the above
code reject it one way or the other, e g. Cray:

   x = f()
   ^
ftn-954 crayftn: ERROR P, File = pr85877-2.f90, Line = 12, Column = 3
   Procedure "f", referenced at line 6 (pr85877-2.f90) must have an 
explicit interface because one or more arguments have the BIND attribute.


> subroutine s
>    interface
>       function g() bind(c)
>       end function g
>    end interface
>    x = g()
> end
> 
> where the interface is picked up via a global symbol.

Is it really true that this is in the spirit of the standard?
Is the global declaration above really equivalent to an explicit
interface?

I would expect legal code to be like:

function g() bind(c)
   g = 42.
end

subroutine s
   interface
      function g() bind(c)
      end function g
   end interface
   x = g()
end

unless you do it in the context of a module and in the right way.

> This code
> may not be valid; nagfor rejects it, but I cannot find a
> constraint at least in the F2022 draft that prohibits it.

I thought that F2018:15.4.2.2 and F2023:15.4.2.2(7) are rather
clear and "explicit" on this.  IMHO the case of "f" above seems
to be excluded and thus not conforming.

> Hm... might it be better to check for attr->module_procedure ||
> attr->if_source == IFSRC_IFBODY?

Maybe we should find a set of legal and illegal cases that we
can agree upon.

Thanks,
Harald

> Best regards
> 
>      Thomas
> 
> 



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Fortran: procedures with BIND(C) attribute require explicit interface [PR85877]
  2023-03-18 20:59   ` Harald Anlauf
  2023-03-18 20:59     ` Harald Anlauf
@ 2023-03-19  7:34     ` Thomas Koenig
  2023-03-19 19:38       ` Harald Anlauf
  1 sibling, 1 reply; 7+ messages in thread
From: Thomas Koenig @ 2023-03-19  7:34 UTC (permalink / raw)
  To: Harald Anlauf, fortran, gcc-patches

Hi Harald,

> Am 18.03.23 um 19:52 schrieb Thomas Koenig via Gcc-patches:
>> Hi Harald,
>>
>>> the Fortran standard requires an explicit procedure interface in certain
>>> situations, such as when they have a BIND(C) attribute (F2018:15.4.2.2).
>>> The attached patch adds a check for this.
>>>
>>> Regtested on x86_64-pc-linux-gnu.  OK for mainline?
>>
>> While this fixes the ICE, it misses
>>
>> function f() bind(c)
>>    f = 42.
>> end
>>
>> subroutine p
>>    bind(c) f     ! { dg-error "must be explicit" }
>>    x = f()
>> end
> 
> what do you mean by "it misses"?

Sorry, that was caused by confusion on my part (and it is better
to test an assumption of what the compiler actually does :-)

Patch is OK, also for backport.  Maybe you can also include the
test above, just to make sure.

Best regards

	Thomas


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Fortran: procedures with BIND(C) attribute require explicit interface [PR85877]
  2023-03-19  7:34     ` Thomas Koenig
@ 2023-03-19 19:38       ` Harald Anlauf
  2023-03-19 19:38         ` Harald Anlauf
  0 siblings, 1 reply; 7+ messages in thread
From: Harald Anlauf @ 2023-03-19 19:38 UTC (permalink / raw)
  To: Thomas Koenig, fortran, gcc-patches

Hi Thomas,

Am 19.03.23 um 08:34 schrieb Thomas Koenig via Gcc-patches:
> Hi Harald,
>
>> Am 18.03.23 um 19:52 schrieb Thomas Koenig via Gcc-patches:
>>> Hi Harald,
>>>
>>>> the Fortran standard requires an explicit procedure interface in
>>>> certain
>>>> situations, such as when they have a BIND(C) attribute
>>>> (F2018:15.4.2.2).
>>>> The attached patch adds a check for this.
>>>>
>>>> Regtested on x86_64-pc-linux-gnu.  OK for mainline?
>>>
>>> While this fixes the ICE, it misses
>>>
>>> function f() bind(c)
>>>    f = 42.
>>> end
>>>
>>> subroutine p
>>>    bind(c) f     ! { dg-error "must be explicit" }
>>>    x = f()
>>> end
>>
>> what do you mean by "it misses"?
>
> Sorry, that was caused by confusion on my part (and it is better
> to test an assumption of what the compiler actually does :-)
>
> Patch is OK, also for backport.  Maybe you can also include the
> test above, just to make sure.

I've added your suggestion to the testcase.  Pushed as:

https://gcc.gnu.org/g:5426ab34643d9e6502f3ee572891a03471fa33ed

> Best regards
>
>      Thomas
>
>

Thanks,
Harald


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Fortran: procedures with BIND(C) attribute require explicit interface [PR85877]
  2023-03-19 19:38       ` Harald Anlauf
@ 2023-03-19 19:38         ` Harald Anlauf
  0 siblings, 0 replies; 7+ messages in thread
From: Harald Anlauf @ 2023-03-19 19:38 UTC (permalink / raw)
  To: gcc-patches; +Cc: fortran

Hi Thomas,

Am 19.03.23 um 08:34 schrieb Thomas Koenig via Gcc-patches:
> Hi Harald,
> 
>> Am 18.03.23 um 19:52 schrieb Thomas Koenig via Gcc-patches:
>>> Hi Harald,
>>>
>>>> the Fortran standard requires an explicit procedure interface in 
>>>> certain
>>>> situations, such as when they have a BIND(C) attribute 
>>>> (F2018:15.4.2.2).
>>>> The attached patch adds a check for this.
>>>>
>>>> Regtested on x86_64-pc-linux-gnu.  OK for mainline?
>>>
>>> While this fixes the ICE, it misses
>>>
>>> function f() bind(c)
>>>    f = 42.
>>> end
>>>
>>> subroutine p
>>>    bind(c) f     ! { dg-error "must be explicit" }
>>>    x = f()
>>> end
>>
>> what do you mean by "it misses"?
> 
> Sorry, that was caused by confusion on my part (and it is better
> to test an assumption of what the compiler actually does :-)
> 
> Patch is OK, also for backport.  Maybe you can also include the
> test above, just to make sure.

I've added your suggestion to the testcase.  Pushed as:

https://gcc.gnu.org/g:5426ab34643d9e6502f3ee572891a03471fa33ed

> Best regards
> 
>      Thomas
> 
> 

Thanks,
Harald



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-03-19 19:38 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-17 21:36 [PATCH] Fortran: procedures with BIND(C) attribute require explicit interface [PR85877] Harald Anlauf
2023-03-18 18:52 ` Thomas Koenig
2023-03-18 20:59   ` Harald Anlauf
2023-03-18 20:59     ` Harald Anlauf
2023-03-19  7:34     ` Thomas Koenig
2023-03-19 19:38       ` Harald Anlauf
2023-03-19 19:38         ` Harald Anlauf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).