Opened 6 years ago

Closed 3 years ago

#2245 closed bug (fixed)

Numeric literal printed wrong in error message

Reported by: guest Owned by:
Priority: normal Milestone: 7.2.1
Component: Compiler Version: 6.9
Keywords: Cc: lennart@…
Operating System: MacOS X Architecture: x86
Type of failure: None/Unknown Difficulty: Unknown
Test Case: Blocked By:
Blocking: Related Tickets:

Description

Compile the following with -Wall

default (T)

data T = T deriving (Eq, Ord, Read, Show)
instance Num T
instance Fractional T

main = interact $ show . (< 1e400) . read

The error message says

Bug.hs:7:28:
    Warning: Defaulting the following constraint(s) to type `T'
             `Fractional b'
               arising from the literal `Infinity' at Bug.hs:7:28-32

There is no literal Infinity in my program, I wrote 1e400, which could be a perfectly good literal for the type T. I don't wanted printed as Infinity.

Change History (9)

comment:1 Changed 6 years ago by simonpj

  • Difficulty set to Unknown

cf #1861, which is related.

Nevertheless this ticket is a distinct bug in GHC's error message.

Simon

comment:2 Changed 6 years ago by simonpj

  • Milestone set to 6.10 branch
  • Priority changed from normal to low

The underlying problem here is this. The lexer sees the token

1.5e-4

and from that produces a Rational. See Lexer.x, and the function readRational. In this case it'll produce something like

15 :% 100000

The trouble is that when printing it out in an error message, it's hard to reverse-engineer that into exactly what the programmer wrote. What we do at the moment (in Pretty.rational) is to convert to Double and show that. This will go wrong if there isn't an exact representation for the constant in Double -- and that's what is happening to Lennart.

The Right Thing is for a HsLit.HsFractional to contain not a Rational, but rather something like a

data FloatLit 
  = FL { fl_int :: Integer         -- Part before decimal point
       , fl_frac :: Maybe Integer  -- Part after decimal point (if any)
       , fl_exp  :: Maybe Integer  -- Exponent (if any)
       }

This would record exactly what the user wrote. Then we can convert to a Rational later, but meanwhile we can print it accurately in error messages.

Simple, but a bit fiddly, so I'm not going to do it today. The above notes just record the plan. Yell if it's important to get it done sooner.

Simon

comment:3 Changed 5 years ago by igloo

  • Milestone changed from 6.10 branch to 6.12 branch

comment:4 Changed 4 years ago by igloo

  • Milestone changed from 6.12 branch to 6.12.3

comment:5 Changed 4 years ago by igloo

  • Milestone changed from 6.12.3 to 6.14.1
  • Priority changed from low to normal
  • Type of failure set to None/Unknown

comment:6 Changed 4 years ago by igloo

See also #2911.

comment:7 Changed 3 years ago by igloo

  • Milestone changed from 7.0.1 to 7.0.2

comment:8 Changed 3 years ago by igloo

  • Milestone changed from 7.0.2 to 7.2.1

comment:9 Changed 3 years ago by batterseapower

  • Resolution set to fixed
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.