Skip to content

Commit

Permalink
[ error ] Improve error messages for Delay &co
Browse files Browse the repository at this point in the history
  • Loading branch information
dunhamsteve committed Aug 3, 2023
1 parent b7bda5e commit bc4176d
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 5 deletions.
12 changes: 8 additions & 4 deletions src/Idris/Parser.idr
Original file line number Diff line number Diff line change
Expand Up @@ -946,16 +946,20 @@ mutual
lazy : OriginDesc -> IndentInfo -> Rule PTerm
lazy fname indents
= do tm <- bounds (decorate fname Typ (exactIdent "Lazy")
*> simpleExpr fname indents)
*> simpleExpr fname indents
<* mustFailBecause "Lazy only takes one argument" (continue indents >> simpleExpr fname indents))
pure (PDelayed (boundToFC fname tm) LLazy tm.val)
<|> do tm <- bounds (decorate fname Typ (exactIdent "Inf")
*> simpleExpr fname indents)
*> simpleExpr fname indents
<* mustFailBecause "Inf only takes one argument" (continue indents >> simpleExpr fname indents))
pure (PDelayed (boundToFC fname tm) LInf tm.val)
<|> do tm <- bounds (decorate fname Data (exactIdent "Delay")
*> simpleExpr fname indents)
*> simpleExpr fname indents
<* mustFailBecause "Delay only takes one argument" (continue indents >> simpleExpr fname indents))
pure (PDelay (boundToFC fname tm) tm.val)
<|> do tm <- bounds (decorate fname Data (exactIdent "Force")
*> simpleExpr fname indents)
*> simpleExpr fname indents
<* mustFailBecause "Force only takes one argument" (continue indents >> simpleExpr fname indents))
pure (PForce (boundToFC fname tm) tm.val)

binder : OriginDesc -> IndentInfo -> Rule PTerm
Expand Down
7 changes: 7 additions & 0 deletions src/Libraries/Text/Parser/Core.idr
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,13 @@ export %inline
bounds : Grammar state tok c ty -> Grammar state tok c (WithBounds ty)
bounds = Bounds

export
mustFailBecause :
String ->
Grammar state tok True ty -> Grammar state tok False Unit
mustFailBecause msg p
= (bounds p >>= \res => fatalLoc {c=False} res.bounds msg) <|> pure ()

export %inline
position : Grammar state tok False Bounds
position = Position
Expand Down
2 changes: 1 addition & 1 deletion tests/Main.idr
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ idrisTestsError = MkTestPool "Error messages" [] Nothing
"perror011", "perror012", "perror013", "perror014", "perror015",
"perror016", "perror017", "perror018", "perror019", "perror020",
"perror021", "perror022", "perror023", "perror024", "perror025",
"perror026", "perror027", "perror028"]
"perror026", "perror027", "perror028", "perror029"]

idrisTestsInteractive : TestPool
idrisTestsInteractive = MkTestPool "Interactive editing" [] Nothing
Expand Down
13 changes: 13 additions & 0 deletions tests/idris2/perror029/DelayParse.idr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module DelayParse

public export
data MyStream : Type where
(::) : (anA : a)
-> (nextA : a -> a)
-> Inf MyStream
-> MyStream

public export
go : a -> (a -> a) -> MyStream
go initA fn =
MyStream.(::) initA fn (Delay (fn initA) fn)
11 changes: 11 additions & 0 deletions tests/idris2/perror029/expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
1/1: Building DelayParse (DelayParse.idr)
Error: Delay only takes one argument.

DelayParse:13:44--13:46
09 |
10 | public export
11 | go : a -> (a -> a) -> MyStream
12 | go initA fn =
13 | MyStream.(::) initA fn (Delay (fn initA) fn)
^^

3 changes: 3 additions & 0 deletions tests/idris2/perror029/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
rm -rf build

$1 --no-color --console-width 0 --check DelayParse.idr

0 comments on commit bc4176d

Please sign in to comment.