public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug fortran/99561] New: gfortran reports an error for a truncation that is permitted by the standard
@ 2021-03-12 13:33 michal.paszta at mobica dot com
  2021-03-12 18:39 ` [Bug fortran/99561] " kargl at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: michal.paszta at mobica dot com @ 2021-03-12 13:33 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99561

            Bug ID: 99561
           Summary: gfortran reports an error for a truncation that is
                    permitted by the standard
           Product: gcc
           Version: 10.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: michal.paszta at mobica dot com
  Target Milestone: ---

In this line of code:

INTEGER(KIND=1) :: var8 = 257_2

we try to cast an integer of kind 2 (16 bits) onto an integer of kind 1 (8
bits, value up to 256). This will result in a truncation of the value and is
allowed by the Fortran 2018 Standard, see Table 10.9, Fortran 2018 Standard.

$ ~/gcc/trunk/bin/gfortran issue.f90 
issue.f90:17:27:

   17 |   INTEGER(KIND=1) :: var8 = 257_2
      |                           1
Error: Arithmetic overflow converting INTEGER(2) to INTEGER(1) at (1). This
check can be disabled with the option ‘-fno-range-check’

We believe the operation should be allowed. It could warn the programmer about
the truncation, but should not prevent a successful compilation.

The obvious workaround is to follow the suggestion for the error message and
add '-fno-range-check' - in that case I got no warning or error.

The report originates from a flang github issue:
https://github.com/flang-compiler/flang/issues/992 and was pointed out by Bryan
Chan.

I tried gfortran 7.5.0 and 10.2.0 and both acted the same.

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

* [Bug fortran/99561] gfortran reports an error for a truncation that is permitted by the standard
  2021-03-12 13:33 [Bug fortran/99561] New: gfortran reports an error for a truncation that is permitted by the standard michal.paszta at mobica dot com
@ 2021-03-12 18:39 ` kargl at gcc dot gnu.org
  2021-03-12 18:51 ` kargl at gcc dot gnu.org
  2021-03-12 19:20 ` sgk at troutmask dot apl.washington.edu
  2 siblings, 0 replies; 4+ messages in thread
From: kargl at gcc dot gnu.org @ 2021-03-12 18:39 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99561

kargl at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |INVALID
             Status|UNCONFIRMED                 |RESOLVED
                 CC|                            |kargl at gcc dot gnu.org

--- Comment #1 from kargl at gcc dot gnu.org ---
(In reply to Michal Paszta from comment #0)
> In this line of code:
> 
> INTEGER(KIND=1) :: var8 = 257_2
> 
> we try to cast an integer of kind 2 (16 bits) onto an integer of kind 1 (8
> bits, value up to 256). This will result in a truncation of the value and is
> allowed by the Fortran 2018 Standard, see Table 10.9, Fortran 2018 Standard.
> 

The sentence preceding Table 10.9 and the table tell you 
what conversions are allowed and how the conversion is
done via a built-in intrinsic subprogram.

It does tell you anything about an out-of-range value.
In fact, an INTEGER(KIND=1) entity has a range of
[-128,127], so the value of 256 is still out-of-range.

As you have found, gfortran offers a programmer a bullet
to shoot their foot (i.e, the -fno-range-check option).
On most (all?) targets supported by gfortran, you'll get 
two's complement wrap-around semantics.  You do not get
truncation, where I assume you mean that an out-of-range 
value is truncated to -128 or 127 as the situation would
merit (e.g., var8 = 257_2 <-- huge(var8) = 127).

As to the "no warning problem", you did not ask gfortran
to generate warnings.  You can use either the -Wall option
or the -Wconversion option to get a warning when using
the -fno-range-check option.

%gfcx -o z -fno-range-check -Wall a.f90
a.f90:2:33:

    2 |   integer(1), parameter :: var8 = 257_2
      |                                 1
Warning: Conversion from 'INTEGER(2)' to 'INTEGER(1)' at (1) [-Wconversion]

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

* [Bug fortran/99561] gfortran reports an error for a truncation that is permitted by the standard
  2021-03-12 13:33 [Bug fortran/99561] New: gfortran reports an error for a truncation that is permitted by the standard michal.paszta at mobica dot com
  2021-03-12 18:39 ` [Bug fortran/99561] " kargl at gcc dot gnu.org
@ 2021-03-12 18:51 ` kargl at gcc dot gnu.org
  2021-03-12 19:20 ` sgk at troutmask dot apl.washington.edu
  2 siblings, 0 replies; 4+ messages in thread
From: kargl at gcc dot gnu.org @ 2021-03-12 18:51 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99561

--- Comment #2 from kargl at gcc dot gnu.org ---
(In reply to kargl from comment #1)
> (In reply to Michal Paszta from comment #0)
> > In this line of code:
> > 
> > INTEGER(KIND=1) :: var8 = 257_2
> > 
> > we try to cast an integer of kind 2 (16 bits) onto an integer of kind 1 (8
> > bits, value up to 256). This will result in a truncation of the value and is
> > allowed by the Fortran 2018 Standard, see Table 10.9, Fortran 2018 Standard.
> > 
> 
> The sentence preceding Table 10.9 and the table tell you 
> what conversions are allowed and how the conversion is
> done via a built-in intrinsic subprogram.
> 
> It does tell you anything about an out-of-range value.
> In fact, an INTEGER(KIND=1) entity has a range of
> [-128,127], so the value of 256 is still out-of-range.
> 
> As you have found, gfortran offers a programmer a bullet
> to shoot their foot (i.e, the -fno-range-check option).
> On most (all?) targets supported by gfortran, you'll get 
> two's complement wrap-around semantics.  You do not get
> truncation, where I assume you mean that an out-of-range 
> value is truncated to -128 or 127 as the situation would
> merit (e.g., var8 = 257_2 <-- huge(var8) = 127).
> 
> As to the "no warning problem", you did not ask gfortran
> to generate warnings.  You can use either the -Wall option
> or the -Wconversion option to get a warning when using
> the -fno-range-check option.
> 
> %gfcx -o z -fno-range-check -Wall a.f90
> a.f90:2:33:
> 
>     2 |   integer(1), parameter :: var8 = 257_2
>       |                                 1
> Warning: Conversion from 'INTEGER(2)' to 'INTEGER(1)' at (1) [-Wconversion]

I should probably not continue with this issue, but I found the
other text in the Standard.  The intrinsic assignment of

var8 = 257_2

with regards to Table 10.9 is then

var8 = int(257_2, kind=1)

But, the Fortran standard contains (18-007r1, p.339)

  A program shall not invoke an intrinsic procedure under circumstances
  where a value to be assigned to a subroutine argument or returned as
  a function result is not representable by objects of the specified
  type and type parameters.

So, int(257_2, 1) is technically not permitted by the Fortran standard,
and gfortran dutifully issues an error if you write such a conversion.

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

* [Bug fortran/99561] gfortran reports an error for a truncation that is permitted by the standard
  2021-03-12 13:33 [Bug fortran/99561] New: gfortran reports an error for a truncation that is permitted by the standard michal.paszta at mobica dot com
  2021-03-12 18:39 ` [Bug fortran/99561] " kargl at gcc dot gnu.org
  2021-03-12 18:51 ` kargl at gcc dot gnu.org
@ 2021-03-12 19:20 ` sgk at troutmask dot apl.washington.edu
  2 siblings, 0 replies; 4+ messages in thread
From: sgk at troutmask dot apl.washington.edu @ 2021-03-12 19:20 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99561

--- Comment #3 from Steve Kargl <sgk at troutmask dot apl.washington.edu> ---
On Fri, Mar 12, 2021 at 06:39:49PM +0000, kargl at gcc dot gnu.org wrote:
> --- Comment #1 from kargl at gcc dot gnu.org ---
> (In reply to Michal Paszta from comment #0)
> > In this line of code:
> > 
> > INTEGER(KIND=1) :: var8 = 257_2
> > 
> > we try to cast an integer of kind 2 (16 bits) onto an integer of kind 1 (8
> > bits, value up to 256). This will result in a truncation of the value and is
> > allowed by the Fortran 2018 Standard, see Table 10.9, Fortran 2018 Standard.
> > 
> 
> The sentence preceding Table 10.9 and the table tell you 
> what conversions are allowed and how the conversion is
> done via a built-in intrinsic subprogram.
> 
> It does tell you anything about an out-of-range value.

It does NOT tell

> In fact, an INTEGER(KIND=1) entity has a range of
> [-128,127], so the value of 256 is still out-of-range.

Sigh, "not" was a rather important omission. :(

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

end of thread, other threads:[~2021-03-12 19:20 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-12 13:33 [Bug fortran/99561] New: gfortran reports an error for a truncation that is permitted by the standard michal.paszta at mobica dot com
2021-03-12 18:39 ` [Bug fortran/99561] " kargl at gcc dot gnu.org
2021-03-12 18:51 ` kargl at gcc dot gnu.org
2021-03-12 19:20 ` sgk at troutmask dot apl.washington.edu

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).