Ticket #7351 (closed bug: fixed)

Opened 7 months ago

Last modified 6 months ago

showRichTokenStream has an off-by one error on starting col

Reported by: alanz Owned by: simonmar
Priority: normal Milestone: 7.6.2
Component: GHC API Version: 7.6.1
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Difficulty: Unknown
Test Case: Blocked By:
Blocking: Related Tickets:

Description (last modified by simonmar) (diff)

When using showRichTokenStream to re-create a source file, all lines after the first one start one column from the left.

The code for advancing to a new line has an off-by one bug where it assumes zero based columns.

I have marked what I assume to be a fix in the pasted code below.

showRichTokenStream ts = go startLoc ts ""
    where sourceFile = getFile $ map (getLoc . fst) ts
          getFile [] = panic "showRichTokenStream: No source file found"
          getFile (UnhelpfulSpan _ : xs) = getFile xs
          getFile (RealSrcSpan s : _) = srcSpanFile s
          startLoc = mkRealSrcLoc sourceFile 1 1
          go _ [] = id
          go loc ((L span _, str):ts)
              = case span of
                UnhelpfulSpan _ -> go loc ts
                RealSrcSpan s
                 | locLine == tokLine -> ((replicate (tokCol - locCol) ' ') ++)
                                       . (str ++)
                                       . go tokEnd ts
                 | otherwise -> ((replicate (tokLine - locLine) '\n') ++)
                              . ((replicate (tokCol - 1) ' ') ++) -- AZ: updated line
                              . (str ++)
                              . go tokEnd ts
                  where (locLine, locCol) = (srcLocLine loc, srcLocCol loc)
                        (tokLine, tokCol) = (srcSpanStartLine s, srcSpanStartCol s)
                        tokEnd = realSrcSpanEnd s

Change History

Changed 7 months ago by simonmar

  • difficulty set to Unknown
  • description modified (diff)

Changed 7 months ago by simonmar

  • owner set to simonmar
  • milestone set to 7.6.2

Thanks, I'll commit.

Changed 7 months ago by marlowsd@…

commit cdf1389865885c190d4b2f0eb81fc659b375fb6f

Author: Simon Marlow <marlowsd@gmail.com>
Date:   Wed Oct 24 13:23:08 2012 +0100

    fix off-by-one-column in showRichTokenStream (#7351)

 compiler/main/GHC.hs |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

Changed 7 months ago by simonmar

  • status changed from new to merge

Changed 6 months ago by igloo

  • status changed from merge to closed
  • resolution set to fixed

I think we're better off not merging this: if we do, then any workarounds people implement for 7.6.1 will mean that they-re off-by-one in the other direction with 7.6.2.

Note: See TracTickets for help on using tickets.