Commit adcb9dbc authored by glguy's avatar glguy Committed by Austin Seipp

Add support for LINE pragma in template-haskell

Summary:
Provide a way to generate {-# LINE #-} pragmas when generating
Decs in Template Haskell. This allows more meaningful line
numbers to be reported in compile-time errors for dynamically
generated code.

Test Plan: Run test suite

Reviewers: austin, hvr

Reviewed By: austin

Subscribers: hvr, simonmar, ezyang, carter, thomie

Differential Revision: https://phabricator.haskell.org/D299
parent 2ee25278
This diff is collapsed.
......@@ -101,7 +101,8 @@
<itemizedlist>
<listitem>
<para>
TODO FIXME
Added support for generating LINE pragma declarations
(<xref linkend="line-pragma"/>).
</para>
</listitem>
</itemizedlist>
......
......@@ -10500,6 +10500,11 @@ happen.
42 in the original. GHC will adjust its error messages to refer
to the line/file named in the <literal>LINE</literal>
pragma.</para>
<para><literal>LINE</literal> pragmas generated from Template Haskell set
the file and line position for the duration of the splice and are limited
to the splice. Note that because Template Haskell splices abstract syntax,
the file positions are not automatically advanced.</para>
</sect2>
<sect2 id="rules">
......
......@@ -137,6 +137,7 @@ module Language.Haskell.TH(
-- **** Pragmas
ruleVar, typedRuleVar,
pragInlD, pragSpecD, pragSpecInlD, pragSpecInstD, pragRuleD, pragAnnD,
pragLineD,
-- * Pretty-printer
Ppr(..), pprint, pprExp, pprLit, pprPat, pprParendType
......
......@@ -413,6 +413,9 @@ pragAnnD target expr
exp1 <- expr
return $ PragmaD $ AnnP target exp1
pragLineD :: Int -> String -> DecQ
pragLineD line file = return $ PragmaD $ LineP line file
familyNoKindD :: FamFlavour -> Name -> [TyVarBndr] -> DecQ
familyNoKindD flav tc tvs = return $ FamilyD flav tc tvs Nothing
......
......@@ -417,6 +417,8 @@ instance Ppr Pragma where
where target1 ModuleAnnotation = text "module"
target1 (TypeAnnotation t) = text "type" <+> ppr t
target1 (ValueAnnotation v) = ppr v
ppr (LineP line file)
= text "{-# LINE" <+> int line <+> text (show file) <+> text "#-}"
------------------------------
instance Ppr Inline where
......
......@@ -1321,6 +1321,7 @@ data Pragma = InlineP Name Inline RuleMatch Phases
| SpecialiseInstP Type
| RuleP String [RuleBndr] Exp Exp Phases
| AnnP AnnTarget Exp
| LineP Int String
deriving( Show, Eq, Data, Typeable )
data Inline = NoInline
......
{-# LANGUAGE TemplateHaskell #-}
-- Test that LINE pragmas influence type error messages correctly
module ShouldFail where
import Language.Haskell.TH
$( do p <- pragLineD 42 "virtual file"
ds <- [d| x = $(varE (mkName "doesntExist")) |]
return (p:ds) )
virtual file:42:1: Not in scope: ‘doesntExist’
......@@ -131,6 +131,8 @@ test('TH_runIO', normal, compile_fail, ['-v0'])
test('TH_ghci1', normal, ghci_script, ['TH_ghci1.script'])
test('TH_linePragma', normal, compile_fail, ['-v0'])
test('TH_scope', normal, compile, [''])
test('T2632', normal, compile, [''])
test('T2700', normal, compile, ['-v0'])
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment