[project @ 1998-04-07 16:40:08 by simonpj]
[ghc-hetmet.git] / ghc / compiler / rename / RnExpr.lhs
index 73b1c44..87ac92d 100644 (file)
@@ -10,39 +10,38 @@ general, all of these functions return a renamed thing, and a set of
 free variables.
 
 \begin{code}
-#include "HsVersions.h"
-
 module RnExpr (
        rnMatch, rnGRHSsAndBinds, rnPat,
        checkPrecMatch
    ) where
 
-IMP_Ubiq()
-IMPORT_DELOOPER(RnLoop)                -- break the RnPass/RnExpr/RnBinds loops
+#include "HsVersions.h"
+
+import {-# SOURCE #-} RnBinds 
+import {-# SOURCE #-} RnSource ( rnHsSigType )
 
 import HsSyn
 import RdrHsSyn
 import RnHsSyn
 import RnMonad
 import RnEnv
-import PrelInfo                ( numClass_RDR, fractionalClass_RDR, eqClass_RDR, ccallableClass_RDR,
-                         creturnableClass_RDR, monadZeroClass_RDR, enumClass_RDR,
-                         negate_RDR
+import CmdLineOpts     ( opt_GlasgowExts )
+import BasicTypes      ( Fixity(..), FixityDirection(..), IfaceFlavour(..) )
+import PrelInfo                ( numClass_RDR, fractionalClass_RDR, eqClass_RDR, 
+                         ccallableClass_RDR, creturnableClass_RDR, 
+                         monadZeroClass_RDR, enumClass_RDR, ordClass_RDR,
+                         ratioDataCon_RDR, negate_RDR, assertErr_RDR,
+                         ioDataCon_RDR, ioOkDataCon_RDR
                        )
 import TysPrim         ( charPrimTyCon, addrPrimTyCon, intPrimTyCon, 
                          floatPrimTyCon, doublePrimTyCon
                        )
-import TyCon           ( TyCon )
-import ErrUtils                ( addErrLoc, addShortErrLocLine )
 import Name
-import Pretty
-import UniqFM          ( lookupUFM{-, ufmToList ToDo:rm-} )
-import UniqSet         ( emptyUniqSet, unitUniqSet,
-                         unionUniqSets, unionManyUniqSets,
-                         SYN_IE(UniqSet)
-                       )
-import PprStyle                ( PprStyle(..) )
-import Util            ( Ord3(..), removeDups, panic, pprPanic, assertPanic )
+import UniqFM          ( isNullUFM )
+import UniqSet         ( emptyUniqSet, unionManyUniqSets, UniqSet )
+import Unique          ( assertIdKey )
+import Util            ( removeDups )
+import Outputable
 \end{code}
 
 
@@ -58,7 +57,7 @@ rnPat :: RdrNamePat -> RnMS s RenamedPat
 rnPat WildPatIn = returnRn WildPatIn
 
 rnPat (VarPatIn name)
-  = lookupRn name      `thenRn` \ vname ->
+  = lookupBndrRn  name                 `thenRn` \ vname ->
     returnRn (VarPatIn vname)
 
 rnPat (LitPatIn lit) 
@@ -72,17 +71,17 @@ rnPat (LazyPatIn pat)
 
 rnPat (AsPatIn name pat)
   = rnPat pat          `thenRn` \ pat' ->
-    lookupRn name      `thenRn` \ vname ->
+    lookupBndrRn name  `thenRn` \ vname ->
     returnRn (AsPatIn vname pat')
 
 rnPat (ConPatIn con pats)
-  = lookupRn con       `thenRn` \ con' ->
+  = lookupOccRn con    `thenRn` \ con' ->
     mapRn rnPat pats   `thenRn` \ patslist ->
     returnRn (ConPatIn con' patslist)
 
 rnPat (ConOpPatIn pat1 con _ pat2)
   = rnPat pat1         `thenRn` \ pat1' ->
-    lookupRn con       `thenRn` \ con' ->
+    lookupOccRn con    `thenRn` \ con' ->
     lookupFixity con   `thenRn` \ fixity ->
     rnPat pat2         `thenRn` \ pat2' ->
     mkConOpPatRn pat1' con' fixity pat2'
@@ -105,6 +104,12 @@ rnPat (ParPatIn pat)
   = rnPat pat          `thenRn` \ pat' ->
     returnRn (ParPatIn pat')
 
+rnPat (NPlusKPatIn name lit)
+  = litOccurrence lit                  `thenRn_`
+    lookupImplicitOccRn ordClass_RDR   `thenRn_`
+    lookupBndrRn name                  `thenRn` \ name' ->
+    returnRn (NPlusKPatIn name' lit)
+
 rnPat (ListPatIn pats)
   = addImplicitOccRn listType_name     `thenRn_` 
     mapRn rnPat pats                   `thenRn` \ patslist ->
@@ -116,7 +121,7 @@ rnPat (TuplePatIn pats)
     returnRn (TuplePatIn patslist)
 
 rnPat (RecPatIn con rpats)
-  = lookupRn con       `thenRn` \ con' ->
+  = lookupOccRn con    `thenRn` \ con' ->
     rnRpats rpats      `thenRn` \ rpats' ->
     returnRn (RecPatIn con' rpats')
 \end{code}
@@ -128,17 +133,35 @@ rnPat (RecPatIn con rpats)
 ************************************************************************
 
 \begin{code}
-rnMatch :: RdrNameMatch -> RnMS s (RenamedMatch, FreeVars)
+rnMatch, rnMatch1 :: RdrNameMatch -> RnMS s (RenamedMatch, FreeVars)
 
-rnMatch (PatMatch pat match)
-  = bindLocalsRn "pattern" binders     $ \ new_binders ->
-    rnPat pat                          `thenRn` \ pat' ->
-    rnMatch match                      `thenRn` \ (match', fvMatch) ->
-    returnRn (PatMatch pat' match', fvMatch `minusNameSet` mkNameSet new_binders)
+-- The only tricky bit here is that we want to do a single
+-- bindLocalsRn for all the matches together, so that we spot
+-- the repeated variable in
+--     f x x = 1
+
+rnMatch match
+  = pushSrcLocRn (getMatchLoc match) $
+    bindLocalsRn "pattern" (get_binders        match)  $ \ new_binders ->
+    rnMatch1 match                             `thenRn` \ (match', fvs) ->
+    let
+       binder_set     = mkNameSet new_binders
+       unused_binders = binder_set `minusNameSet` fvs
+       net_fvs        = fvs `minusNameSet` binder_set
+    in
+    warnUnusedMatches unused_binders           `thenRn_`
+    
+    returnRn (match', net_fvs)
  where
-    binders = collectPatBinders pat
+    get_binders (GRHSMatch _)       = []
+    get_binders (PatMatch pat match) = collectPatBinders pat ++ get_binders match
+
+rnMatch1 (PatMatch pat match)
+  = rnPat pat                          `thenRn` \ pat' ->
+    rnMatch1 match                     `thenRn` \ (match', fvs) ->
+    returnRn (PatMatch pat' match', fvs)
 
-rnMatch (GRHSMatch grhss_and_binds)
+rnMatch1 (GRHSMatch grhss_and_binds)
   = rnGRHSsAndBinds grhss_and_binds `thenRn` \ (grhss_and_binds', fvs) ->
     returnRn (GRHSMatch grhss_and_binds', fvs)
 \end{code}
@@ -166,14 +189,26 @@ rnGRHSsAndBinds (GRHSsAndBindsIn grhss binds)
 
     rnGRHS (GRHS guard expr locn)
       = pushSrcLocRn locn $                
-       rnExpr guard    `thenRn` \ (guard', fvsg) ->
-       rnExpr expr     `thenRn` \ (expr',  fvse) ->
-       returnRn (GRHS guard' expr' locn, fvsg `unionNameSets` fvse)
+       (if not (opt_GlasgowExts || is_standard_guard guard) then
+               addWarnRn (nonStdGuardErr guard)
+        else
+               returnRn ()
+       )               `thenRn_`
+
+       (rnStmts rnExpr guard   $ \ guard' ->
+               -- This nested thing deals with scope and
+               -- the free vars of the guard, and knocking off the
+               -- free vars of the rhs that are bound by the guard
 
-    rnGRHS (OtherwiseGRHS expr locn)
-      = pushSrcLocRn locn $
-       rnExpr expr     `thenRn` \ (expr', fvs) ->
-       returnRn (OtherwiseGRHS expr' locn, fvs)
+       rnExpr expr     `thenRn` \ (expr',  fvse) ->
+       returnRn (GRHS guard' expr' locn, fvse))
+
+       -- Standard Haskell 1.4 guards are just a single boolean
+       -- expression, rather than a list of qualifiers as in the
+       -- Glasgow extension
+    is_standard_guard []             = True
+    is_standard_guard [GuardStmt _ _] = True
+    is_standard_guard other          = False
 \end{code}
 
 %************************************************************************
@@ -184,13 +219,23 @@ rnGRHSsAndBinds (GRHSsAndBindsIn grhss binds)
 
 \begin{code}
 rnExprs :: [RdrNameHsExpr] -> RnMS s ([RenamedHsExpr], FreeVars)
+rnExprs ls = rnExprs' ls emptyUniqSet
+ where
+  rnExprs' [] acc = returnRn ([], acc)
+  rnExprs' (expr:exprs) acc
+   = rnExpr expr               `thenRn` \ (expr', fvExpr) ->
 
-rnExprs [] = returnRn ([], emptyNameSet)
+       -- Now we do a "seq" on the free vars because typically it's small
+       -- or empty, especially in very long lists of constants
+    let
+       acc' = acc `unionNameSets` fvExpr
+    in
+    (grubby_seqNameSet acc' rnExprs') exprs acc'       `thenRn` \ (exprs', fvExprs) ->
+    returnRn (expr':exprs', fvExprs)
 
-rnExprs (expr:exprs)
-  = rnExpr expr        `thenRn` \ (expr', fvExpr) ->
-    rnExprs exprs      `thenRn` \ (exprs', fvExprs) ->
-    returnRn (expr':exprs', fvExpr `unionNameSets` fvExprs)
+-- Grubby little function to do "seq" on namesets; replace by proper seq when GHC can do seq
+grubby_seqNameSet ns result | isNullUFM ns = result
+                           | otherwise    = result
 \end{code}
 
 Variables. We look up the variable and return the resulting name.  The
@@ -204,10 +249,16 @@ free-var set iff if it's a LocallyDefined Name.
 rnExpr :: RdrNameHsExpr -> RnMS s (RenamedHsExpr, FreeVars)
 
 rnExpr (HsVar v)
-  = lookupOccRn v      `thenRn` \ vname ->
-    returnRn (HsVar vname, if isLocallyDefined vname
-                          then unitNameSet vname
-                          else emptyUniqSet)
+  = lookupOccRn v      `thenRn` \ name ->
+    if nameUnique name == assertIdKey then
+       -- We expand it to (GHCerr.assert__ location)
+        mkAssertExpr  `thenRn` \ expr ->
+       returnRn (expr, emptyUniqSet)
+    else
+        -- The normal case
+       returnRn (HsVar name, if isLocallyDefined name
+                            then unitNameSet name
+                            else emptyUniqSet)
 
 rnExpr (HsLit lit) 
   = litOccurrence lit          `thenRn_`
@@ -227,12 +278,15 @@ rnExpr (OpApp e1 op@(HsVar op_name) _ e2)
     rnExpr e2                          `thenRn` \ (e2', fv_e2) ->
     rnExpr op                          `thenRn` \ (op', fv_op) ->
 
-       -- Deal wth fixity
+       -- Deal with fixity
+       -- When renaming code synthesised from "deriving" declarations
+       -- we're in Interface mode, and we should ignore fixity; assume
+       -- that the deriving code generator got the association correct
     lookupFixity op_name               `thenRn` \ fixity ->
     getModeRn                          `thenRn` \ mode -> 
     (case mode of
-       SourceMode    -> mkOpAppRn e1' op' fixity e2'
-       InterfaceMode -> returnRn (OpApp e1' op' fixity e2')
+       SourceMode        -> mkOpAppRn e1' op' fixity e2'
+       InterfaceMode _ _ -> returnRn (OpApp e1' op' fixity e2')
     )                                  `thenRn` \ final_e -> 
 
     returnRn (final_e,
@@ -241,8 +295,7 @@ rnExpr (OpApp e1 op@(HsVar op_name) _ e2)
 rnExpr (NegApp e n)
   = rnExpr e                           `thenRn` \ (e', fv_e) ->
     lookupImplicitOccRn negate_RDR     `thenRn` \ neg ->
-    getModeRn                          `thenRn` \ mode -> 
-    mkNegAppRn mode e' (HsVar neg)     `thenRn` \ final_e ->
+    mkNegAppRn e' (HsVar neg)          `thenRn` \ final_e ->
     returnRn (final_e, fv_e)
 
 rnExpr (HsPar e)
@@ -260,8 +313,11 @@ rnExpr (SectionR op expr)
     returnRn (SectionR op' expr', fvs_op `unionNameSets` fvs_expr)
 
 rnExpr (CCall fun args may_gc is_casm fake_result_ty)
+       -- Check out the comment on RnIfaces.getNonWiredDataDecl about ccalls
   = lookupImplicitOccRn ccallableClass_RDR     `thenRn_`
     lookupImplicitOccRn creturnableClass_RDR   `thenRn_`
+    lookupImplicitOccRn ioDataCon_RDR          `thenRn_`
+    lookupImplicitOccRn ioOkDataCon_RDR                `thenRn_`
     rnExprs args                               `thenRn` \ (args', fvs_args) ->
     returnRn (CCall fun args' may_gc is_casm fake_result_ty, fvs_args)
 
@@ -280,16 +336,11 @@ rnExpr (HsLet binds expr)
     rnExpr expr                         `thenRn` \ (expr',fvExpr) ->
     returnRn (HsLet binds' expr', fvExpr)
 
-rnExpr (HsDo stmts src_loc)
+rnExpr (HsDo do_or_lc stmts src_loc)
   = pushSrcLocRn src_loc $
     lookupImplicitOccRn monadZeroClass_RDR     `thenRn_`       -- Forces Monad to come too
-    rnStmts stmts                              `thenRn` \ (stmts', fvStmts) ->
-    returnRn (HsDo stmts' src_loc, fvStmts)
-
-rnExpr (ListComp expr quals)
-  = addImplicitOccRn listType_name     `thenRn_` 
-    rnQuals expr quals                         `thenRn` \ ((expr', quals'), fvs) ->
-    returnRn (ListComp expr' quals', fvs)
+    (rnStmts rnExpr stmts                      $ \ stmts' ->
+    returnRn (HsDo do_or_lc stmts' src_loc, emptyNameSet))
 
 rnExpr (ExplicitList exps)
   = addImplicitOccRn listType_name     `thenRn_` 
@@ -301,10 +352,10 @@ rnExpr (ExplicitTuple exps)
     rnExprs exps                                       `thenRn` \ (exps', fvExps) ->
     returnRn (ExplicitTuple exps', fvExps)
 
-rnExpr (RecordCon (HsVar con) rbinds)
-  = lookupOccRn con                    `thenRn` \ conname ->
+rnExpr (RecordCon con_id _ rbinds)
+  = lookupOccRn con_id                         `thenRn` \ conname ->
     rnRbinds "construction" rbinds     `thenRn` \ (rbinds', fvRbinds) ->
-    returnRn (RecordCon (HsVar conname) rbinds', fvRbinds)
+    returnRn (RecordCon conname (error "rnExpr:RecordCon") rbinds', fvRbinds)
 
 rnExpr (RecordUpd expr rbinds)
   = rnExpr expr                        `thenRn` \ (expr', fvExpr) ->
@@ -312,8 +363,8 @@ rnExpr (RecordUpd expr rbinds)
     returnRn (RecordUpd expr' rbinds', fvExpr `unionNameSets` fvRbinds)
 
 rnExpr (ExprWithTySig expr pty)
-  = rnExpr expr                                `thenRn` \ (expr', fvExpr) ->
-    rnHsType pty                       `thenRn` \ pty' ->
+  = rnExpr expr                                        `thenRn` \ (expr', fvExpr) ->
+    rnHsSigType (text "an expression") pty     `thenRn` \ pty' ->
     returnRn (ExprWithTySig expr' pty', fvExpr)
 
 rnExpr (HsIf p b1 b2 src_loc)
@@ -362,12 +413,12 @@ rnRbinds str rbinds
     mapAndUnzipRn rn_rbind rbinds      `thenRn` \ (rbinds', fvRbind_s) ->
     returnRn (rbinds', unionManyNameSets fvRbind_s)
   where
-    (_, dup_fields) = removeDups cmp [ f | (f,_,_) <- rbinds ]
+    (_, dup_fields) = removeDups compare [ f | (f,_,_) <- rbinds ]
 
     field_dup_err dups = addErrRn (dupFieldErr str dups)
 
     rn_rbind (field, expr, pun)
-      = lookupOccRn field      `thenRn` \ fieldname ->
+      = lookupGlobalOccRn field        `thenRn` \ fieldname ->
        rnExpr expr             `thenRn` \ (expr', fvExpr) ->
        returnRn ((fieldname, expr', pun), fvExpr)
 
@@ -375,19 +426,19 @@ rnRpats rpats
   = mapRn field_dup_err dup_fields     `thenRn_`
     mapRn rn_rpat rpats
   where
-    (_, dup_fields) = removeDups cmp [ f | (f,_,_) <- rpats ]
+    (_, dup_fields) = removeDups compare [ f | (f,_,_) <- rpats ]
 
     field_dup_err dups = addErrRn (dupFieldErr "pattern" dups)
 
     rn_rpat (field, pat, pun)
-      = lookupOccRn field      `thenRn` \ fieldname ->
+      = lookupGlobalOccRn field        `thenRn` \ fieldname ->
        rnPat pat               `thenRn` \ pat' ->
        returnRn (fieldname, pat', pun)
 \end{code}
 
 %************************************************************************
 %*                                                                     *
-\subsubsection{@Qualifier@s: in list comprehensions}
+\subsubsection{@Stmt@s: in @do@ expressions}
 %*                                                                     *
 %************************************************************************
 
@@ -400,72 +451,27 @@ be @{r}@, and the free var set for the entire Quals will be @{r}@. This
 Quals.
 
 \begin{code}
-rnQuals :: RdrNameHsExpr -> [RdrNameQual]
-        -> RnMS s ((RenamedHsExpr, [RenamedQual]), FreeVars)
-
-rnQuals expr [qual]                            -- must be at least one qual
-  = rnQual qual                        $ \ new_qual ->
-    rnExpr expr                                `thenRn` \ (expr', fvs) ->
-    returnRn ((expr', [new_qual]), fvs)
-
-rnQuals expr (qual: quals)
-  = rnQual qual                        $ \ qual' ->
-    rnQuals expr quals         `thenRn` \ ((expr', quals'), fv_quals) ->
-    returnRn ((expr', qual' : quals'), fv_quals)
-
-
--- rnQual :: RdrNameQual
---        -> (RenamedQual -> RnMS s (a,FreeVars))
---        -> RnMS s (a,FreeVars)
--- Because of mutual recursion the actual type is a bit less general than this [Haskell 1.2]
-
-rnQual (GeneratorQual pat expr) thing_inside
-  = rnExpr expr                                                        `thenRn` \ (expr', fv_expr) ->
-    bindLocalsRn "pattern in list comprehension" binders       $ \ new_binders ->
-    rnPat pat                                                  `thenRn` \ pat' ->
-
-    thing_inside (GeneratorQual pat' expr')            `thenRn` \ (result, fvs) ->     
-    returnRn (result, fv_expr `unionNameSets` (fvs `minusNameSet` mkNameSet new_binders))
-  where
-    binders = collectPatBinders pat
-
-rnQual (FilterQual expr) thing_inside
-  = rnExpr expr                                `thenRn` \ (expr', fv_expr) ->
-    thing_inside (FilterQual expr')    `thenRn` \ (result, fvs) ->
-    returnRn (result, fv_expr `unionNameSets` fvs)
-
-rnQual (LetQual binds) thing_inside
-  = rnBinds binds                      $ \ binds' ->
-    thing_inside (LetQual binds')
-\end{code}
-
-
-%************************************************************************
-%*                                                                     *
-\subsubsection{@Stmt@s: in @do@ expressions}
-%*                                                                     *
-%************************************************************************
-
-\begin{code}
-rnStmts :: [RdrNameStmt] -> RnMS s ([RenamedStmt], FreeVars)
+type RnExprTy s = RdrNameHsExpr -> RnMS s (RenamedHsExpr, FreeVars)
 
-rnStmts [stmt@(ExprStmt expr src_loc)]         -- last stmt must be ExprStmt
-  = pushSrcLocRn src_loc $
-    rnExpr expr                                `thenRn` \ (expr', fv_expr) ->
-    returnRn ([ExprStmt expr' src_loc], fv_expr)
+rnStmts :: RnExprTy s
+       -> [RdrNameStmt] 
+       -> ([RenamedStmt] -> RnMS s (a, FreeVars))
+       -> RnMS s (a, FreeVars)
 
-rnStmts (stmt:stmts)
-  = rnStmt stmt                                $ \ stmt' ->
-    rnStmts stmts                      `thenRn` \ (stmts', fv_stmts) ->
-    returnRn (stmt':stmts', fv_stmts)
+rnStmts rn_expr [] thing_inside 
+  = thing_inside []
 
+rnStmts rn_expr (stmt:stmts) thing_inside
+  = rnStmt rn_expr stmt                                $ \ stmt' ->
+    rnStmts rn_expr stmts                      $ \ stmts' ->
+    thing_inside (stmt' : stmts')
 
--- rnStmt :: RdrNameStmt -> (RenamedStmt -> RnMS s (a, FreeVars)) -> RnMS s (a, FreeVars)
--- Because of mutual recursion the actual type is a bit less general than this [Haskell 1.2]
+rnStmt :: RnExprTy s -> RdrNameStmt -> (RenamedStmt -> RnMS s (a, FreeVars)) -> RnMS s (a, FreeVars)
+-- Because of mutual recursion we have to pass in rnExpr.
 
-rnStmt (BindStmt pat expr src_loc) thing_inside
+rnStmt rn_expr (BindStmt pat expr src_loc) thing_inside
   = pushSrcLocRn src_loc $
-    rnExpr expr                                                `thenRn` \ (expr', fv_expr) ->
+    rn_expr expr                                       `thenRn` \ (expr', fv_expr) ->
     bindLocalsRn "pattern in do binding" binders       $ \ new_binders ->
     rnPat pat                                          `thenRn` \ pat' ->
 
@@ -474,13 +480,24 @@ rnStmt (BindStmt pat expr src_loc) thing_inside
   where
     binders = collectPatBinders pat
 
-rnStmt (ExprStmt expr src_loc) thing_inside
+rnStmt rn_expr (ExprStmt expr src_loc) thing_inside
   = pushSrcLocRn src_loc $
-    rnExpr expr                                        `thenRn` \ (expr', fv_expr) ->
+    rn_expr expr                               `thenRn` \ (expr', fv_expr) ->
     thing_inside (ExprStmt expr' src_loc)      `thenRn` \ (result, fvs) ->
     returnRn (result, fv_expr `unionNameSets` fvs)
 
-rnStmt (LetStmt binds) thing_inside
+rnStmt rn_expr (GuardStmt expr src_loc) thing_inside
+  = pushSrcLocRn src_loc $
+    rn_expr expr                               `thenRn` \ (expr', fv_expr) ->
+    thing_inside (GuardStmt expr' src_loc)     `thenRn` \ (result, fvs) ->
+    returnRn (result, fv_expr `unionNameSets` fvs)
+
+rnStmt rn_expr (ReturnStmt expr) thing_inside
+  = rn_expr expr                               `thenRn` \ (expr', fv_expr) ->
+    thing_inside (ReturnStmt expr')            `thenRn` \ (result, fvs) ->
+    returnRn (result, fv_expr `unionNameSets` fvs)
+
+rnStmt rn_expr (LetStmt binds) thing_inside
   = rnBinds binds              $ \ binds' ->
     thing_inside (LetStmt binds')
 \end{code}
@@ -515,20 +532,30 @@ mkOpAppRn e1@(OpApp e11 op1 fix1 e12)
     returnRn (OpApp e11 op1 fix1 new_e)
   where
     (nofix_error, rearrange_me) = compareFixity fix1 fix2
-    get (HsVar n) = n
 
-mkOpAppRn e1@(NegApp neg_arg neg_id) 
+mkOpAppRn e1@(NegApp neg_arg neg_op) 
          op2 
          fix2@(Fixity prec2 dir2)
          e2
-  | prec2 > 6  -- Precedence of unary - is wired in as 6!
+  | nofix_error
+  = addErrRn (precParseErr (get neg_op,fix_neg) (get op2,fix2))        `thenRn_`
+    returnRn (OpApp e1 op2 fix2 e2)
+
+  | rearrange_me
   = mkOpAppRn neg_arg op2 fix2 e2      `thenRn` \ new_e ->
-    returnRn (NegApp new_e neg_id)
+    returnRn (NegApp new_e neg_op)
+  where
+    fix_neg = Fixity 6 InfixL          -- Precedence of unary negate is wired in as infixl 6!
+    (nofix_error, rearrange_me) = compareFixity fix_neg fix2
 
 mkOpAppRn e1 op fix e2                         -- Default case, no rearrangment
-  = ASSERT( right_op_ok fix e2 )
+  = ASSERT( if right_op_ok fix e2 then True
+           else pprPanic "mkOpAppRn" (vcat [ppr e1, text "---", ppr op, text "---", ppr fix, text "---", ppr e2])
+    )
     returnRn (OpApp e1 op fix e2)
 
+get (HsVar n) = n
+
 -- Parser left-associates everything, but 
 -- derived instances may have correctly-associated things to
 -- in the right operarand.  So we just check that the right operand is OK
@@ -540,9 +567,13 @@ right_op_ok fix1 other
   = True
 
 -- Parser initially makes negation bind more tightly than any other operator
-mkNegAppRn mode neg_arg neg_id
-  = ASSERT( not_op_app mode neg_arg )
-    returnRn (NegApp neg_arg neg_id)
+mkNegAppRn neg_arg neg_op
+  = 
+#ifdef DEBUG
+    getModeRn                  `thenRn` \ mode ->
+    ASSERT( not_op_app mode neg_arg )
+#endif
+    returnRn (NegApp neg_arg neg_op)
 
 not_op_app SourceMode (OpApp _ _ _ _) = False
 not_op_app mode other                = True
@@ -626,10 +657,10 @@ compareFixity :: Fixity -> Fixity
              -> (Bool,         -- Error please
                  Bool)         -- Associate to the right: a op1 (b op2 c)
 compareFixity (Fixity prec1 dir1) (Fixity prec2 dir2)
-  = case prec1 `cmp` prec2 of
-       GT_ -> left
-       LT_ -> right
-       EQ_ -> case (dir1, dir2) of
+  = case prec1 `compare` prec2 of
+       GT -> left
+       LT -> right
+       EQ -> case (dir1, dir2) of
                        (InfixR, InfixR) -> right
                        (InfixL, InfixL) -> left
                        _                -> error_please
@@ -663,13 +694,17 @@ litOccurrence (HsStringPrim _)
   = addImplicitOccRn (getName addrPrimTyCon)
 
 litOccurrence (HsInt _)
-  = lookupImplicitOccRn numClass_RDR   `thenRn_`       -- Int and Integer are forced in by Num
-    returnRn ()
+  = lookupImplicitOccRn numClass_RDR                   -- Int and Integer are forced in by Num
 
 litOccurrence (HsFrac _)
-  = lookupImplicitOccRn fractionalClass_RDR    `thenRn_`       -- ... similarly Rational
-    returnRn ()
-
+  = lookupImplicitOccRn fractionalClass_RDR    `thenRn_`
+    lookupImplicitOccRn ratioDataCon_RDR
+       -- We have to make sure that the Ratio type is imported with
+       -- its constructor, because literals of type Ratio t are
+       -- built with that constructor.
+       -- The Rational type is needed too, but that will come in
+       -- when fractionalClass does.
+    
 litOccurrence (HsIntPrim _)
   = addImplicitOccRn (getName intPrimTyCon)
 
@@ -680,32 +715,62 @@ litOccurrence (HsDoublePrim _)
   = addImplicitOccRn (getName doublePrimTyCon)
 
 litOccurrence (HsLitLit _)
-  = lookupImplicitOccRn ccallableClass_RDR     `thenRn_`
-    returnRn ()
+  = lookupImplicitOccRn ccallableClass_RDR
 \end{code}
 
-
 %************************************************************************
 %*                                                                     *
-\subsubsection{Errors}
+\subsubsection{Assertion utils}
 %*                                                                     *
 %************************************************************************
 
 \begin{code}
-dupFieldErr str (dup:rest) sty
-  = ppBesides [ppStr "duplicate field name `", ppr sty dup, ppStr "' in record ", ppStr str]
-
-negPatErr pat  sty
-  = ppSep [ppStr "prefix `-' not applied to literal in pattern", ppr sty pat]
+mkAssertExpr :: RnMS s RenamedHsExpr
+mkAssertExpr =
+  newImportedGlobalName mod occ HiFile `thenRn` \ name ->
+  addOccurrenceName name              `thenRn_`
+  getSrcLocRn                          `thenRn` \ sloc ->
+  let
+   expr = HsApp (HsVar name)
+               (HsLit (HsString (_PK_ (showSDoc (ppr sloc)))))
+  in
+  returnRn expr
 
-precParseNegPatErr op sty 
-  = ppHang (ppStr "precedence parsing error")
-      4 (ppBesides [ppStr "prefix `-' has lower precedence than ", pp_op sty op, ppStr " in pattern"])
+  where
+   mod = rdrNameModule assertErr_RDR
+   occ = rdrNameOcc assertErr_RDR
+\end{code}
 
-precParseErr op1 op2  sty
-  = ppHang (ppStr "precedence parsing error")
-      4 (ppBesides [ppStr "cannot mix ", pp_op sty op1, ppStr " and ", pp_op sty op2,
-                   ppStr " in the same infix expression"])
+%************************************************************************
+%*                                                                     *
+\subsubsection{Errors}
+%*                                                                     *
+%************************************************************************
 
-pp_op sty (op, fix) = ppBesides [pprSym sty op, ppLparen, ppr sty fix, ppRparen]
+\begin{code}
+dupFieldErr str (dup:rest)
+  = hsep [ptext SLIT("duplicate field name"), 
+          quotes (ppr dup),
+         ptext SLIT("in record"), text str]
+
+negPatErr pat 
+  = sep [ptext SLIT("prefix `-' not applied to literal in pattern"), quotes (ppr pat)]
+
+precParseNegPatErr op 
+  = hang (ptext SLIT("precedence parsing error"))
+      4 (hsep [ptext SLIT("prefix `-' has lower precedence than"), 
+              quotes (pp_op op), 
+              ptext SLIT("in pattern")])
+
+precParseErr op1 op2 
+  = hang (ptext SLIT("precedence parsing error"))
+      4 (hsep [ptext SLIT("cannot mix"), quotes (pp_op op1), ptext SLIT("and"), 
+              quotes (pp_op op2),
+              ptext SLIT("in the same infix expression")])
+
+nonStdGuardErr guard
+  = hang (ptext SLIT("accepting non-standard pattern guards (-fglasgow-exts to suppress this message)"))
+      4 (ppr guard)
+
+pp_op (op, fix) = hcat [ppr op, space, parens (ppr fix)]
 \end{code}