Skip to content

Commit

Permalink
Derive Eq and Ord instance for SrcLoc and RealSrcLoc
Browse files Browse the repository at this point in the history
The Eq and Ord instance were previously hand-written and this change
updates them to be automatically derived by the compiler.  The derived
behavior should be equivalent to the original.

Reviewers: hvr, austin, bgamari

Reviewed By: bgamari

Subscribers: thomie

Differential Revision: https://phabricator.haskell.org/D1913
  • Loading branch information
Gabriella439 authored and bgamari committed Feb 17, 2016
1 parent a615215 commit 67d2226
Showing 1 changed file with 2 additions and 28 deletions.
30 changes: 2 additions & 28 deletions compiler/basicTypes/SrcLoc.hs
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,12 @@ data RealSrcLoc
= SrcLoc FastString -- A precise location (file name)
{-# UNPACK #-} !Int -- line number, begins at 1
{-# UNPACK #-} !Int -- column number, begins at 1
deriving (Eq, Ord)

data SrcLoc
= RealSrcLoc {-# UNPACK #-}!RealSrcLoc
| UnhelpfulLoc FastString -- Just a general indication
deriving Show
deriving (Eq, Ord, Show)

{-
************************************************************************
Expand Down Expand Up @@ -164,36 +165,9 @@ advanceSrcLoc (SrcLoc f l c) _ = SrcLoc f l (c + 1)
************************************************************************
-}

-- SrcLoc is an instance of Ord so that we can sort error messages easily
instance Eq SrcLoc where
loc1 == loc2 = case loc1 `cmpSrcLoc` loc2 of
EQ -> True
_other -> False

instance Eq RealSrcLoc where
loc1 == loc2 = case loc1 `cmpRealSrcLoc` loc2 of
EQ -> True
_other -> False

instance Ord SrcLoc where
compare = cmpSrcLoc

instance Ord RealSrcLoc where
compare = cmpRealSrcLoc

sortLocated :: [Located a] -> [Located a]
sortLocated things = sortBy (comparing getLoc) things

cmpSrcLoc :: SrcLoc -> SrcLoc -> Ordering
cmpSrcLoc (UnhelpfulLoc s1) (UnhelpfulLoc s2) = s1 `compare` s2
cmpSrcLoc (UnhelpfulLoc _) (RealSrcLoc _) = GT
cmpSrcLoc (RealSrcLoc _) (UnhelpfulLoc _) = LT
cmpSrcLoc (RealSrcLoc l1) (RealSrcLoc l2) = (l1 `compare` l2)

cmpRealSrcLoc :: RealSrcLoc -> RealSrcLoc -> Ordering
cmpRealSrcLoc (SrcLoc s1 l1 c1) (SrcLoc s2 l2 c2)
= (s1 `compare` s2) `thenCmp` (l1 `compare` l2) `thenCmp` (c1 `compare` c2)

instance Outputable RealSrcLoc where
ppr (SrcLoc src_path src_line src_col)
= hcat [ pprFastFilePath src_path <> colon
Expand Down

0 comments on commit 67d2226

Please sign in to comment.