From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 672CB3858C52; Sat, 2 Apr 2022 19:03:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 672CB3858C52 From: "kargl at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug fortran/105138] [7,8,9,10,11,12,F95] Bogus error when function name does not shadow an intrinsic when RESULT clause is used Date: Sat, 02 Apr 2022 19:03:18 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: fortran X-Bugzilla-Version: 12.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: kargl at gcc dot gnu.org X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P4 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: 13.0 X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 02 Apr 2022 19:03:18 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D105138 --- Comment #7 from kargl at gcc dot gnu.org --- It seems the problem is that gfortran does not know that a function name is local to its own scoping unit when a result-name is used. First, if the function is contained in a module it seems to work correctly. Consider, module foo implicit none contains recursive function log_gamma(z) result(res) complex,intent(in) :: z complex :: res complex x if (real(z) =3D=3D 0) then res =3D 42 else x =3D (0,1) res =3D log_gamma(x) end if end function log_gamma end module foo program bar use foo implicit none complex z z =3D log_gamma(cmplx(2.,3.)) print *, z end program bar % gfcx -Wall -o z a.f90 a.f90:4:3: 4 | recursive function log_gamma(z) result(res) | 1 Warning: 'log_gamma' declared at (1) may shadow the intrinsic of the same name. In order to call the intrinsic, explicit INTRINSIC declarations may be required. [-Wintrinsic-shadow] % ./z (42.0000000,0.00000000) Now, consider=20 recursive function log_gamma(z) result(res) complex,intent(in) :: z complex :: res complex x if (real(z) =3D=3D 0) then res =3D 42 else x =3D (0,1) res =3D log_gamma(x) end if end function log_gamma program bar implicit none complex z z =3D log_gamma(cmplx(2.,3.)) print *, z end program bar % gfcx -Wall -o z a.f90 a.f90:1:0: 1 | recursive function log_gamma(z) result(res) |=20 Warning: 'log_gamma' declared at (1) is also the name of an intrinsic. It can only be called via an explicit interface or if declared EXTERNAL. [-Wintrinsic-shadow] a.f90:9:22: 9 | res =3D log_gamma(x) | 1 Error: 'x' argument of 'log_gamma' intrinsic at (1) must be REAL a.f90:16:17: 16 | z =3D log_gamma(cmplx(2.,3.)) | 1 Error: 'x' argument of 'log_gamma' intrinsic at (1) must be REAL I believe the first error is wrong. The local function name should block the intrinsic name. The second error is correct, because the recursive function requires an explicit interface in program bar and gfortran is picking up the intrinsic function. If the function is modified to recursive function log_gamma(z) result(res) complex,intent(in) :: z complex :: res complex x if (real(z) =3D=3D 0) then res =3D 42 else block complex, external :: log_gamma x =3D (0,1) res =3D log_gamma(x) end block end if end function log_gamma then the first error message does not occur. The block...end block should not be required. The second error message remains as it should. If the program is modified to=20 program bar implicit none complex z complex, external :: log_gamma z =3D log_gamma(cmplx(2.,3.)) print *, z end program bar or=20 program bar implicit none interface recursive function log_gamma(z) result(res) complex,intent(in) :: z complex :: res end function log_gamma end interface complex z z =3D log_gamma(cmplx(2.,3.)) print *, z end program bar it compiles and runs with the block...end block modified function For completeness, if log_gamma() is a contained routine within the program it compiles and runs. program bar implicit none complex z z =3D log_gamma(cmplx(2.,3.)) print *, z contains recursive function log_gamma(z) result(res) complex,intent(in) :: z complex :: res complex x if (real(z) =3D=3D 0) then res =3D 42 else x =3D (0,1) res =3D log_gamma(x) end if end function log_gamma end program bar % gfcx -Wall -o z a.f90 a.f90:7:4: 7 | recursive function log_gamma(z) result(res) | 1 Warning: 'log_gamma' declared at (1) may shadow the intrinsic of the same name. In order to call the intrinsic, explicit INTRINSIC declarations may be required. [-Wintrinsic-shadow] %./z (42.0000000,0.00000000)=