Skip to content

Commit

Permalink
Fix import source locations
Browse files Browse the repository at this point in the history
.. as caught by @Profpatsch in:

#812 (comment)

Before this change the location was always being reported as `(stdin):1:1`
because the `SourcedException` kept getting modified with a broader
source location in the `Note` branch of `loadWith`.

This was originally done so that alternative imports would show the entire
source span, but it mistakenly just kept bubbling up regardless of whether or
not there were alternative imports.

Instead, this includes an approximate source span for alternative imports.
The source span bounds are correct but the contents just show which imports
were alternated, even if they might have been buried in a larger expression.

For example, if the original expression had been:

```haskell
Some ./x ? None ./y
```

... then the source span for the error message would display just:

```haskell
./x ? ./y
```

... which is probably as good as it will get for now.
  • Loading branch information
Gabriella439 committed Feb 12, 2019
1 parent 98497e4 commit e951376
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions dhall/src/Dhall/Import.hs
Original file line number Diff line number Diff line change
Expand Up @@ -846,11 +846,13 @@ loadWith expr₀ = case expr₀ of
return expr
ImportAlt a b -> loadWith a `catch` handler₀
where
handler₀ (SourcedException (Src begin _ text) (MissingImports es₀)) =
handler₀ (SourcedException (Src begin _ text) (MissingImports es₀)) =
loadWith b `catch` handler₁
where
handler₁ (SourcedException (Src _ end _) (MissingImports es₁)) =
throwM (SourcedException (Src begin end text) (MissingImports (es₀ ++ es₁)))
handler₁ (SourcedException (Src _ end text₁) (MissingImports es₁)) =
throwM (SourcedException (Src begin end text₂) (MissingImports (es₀ ++ es₁)))
where
text₂ = text₀ <> " ? " <> text₁

Const a -> pure (Const a)
Var a -> pure (Var a)
Expand Down Expand Up @@ -917,14 +919,9 @@ loadWith expr₀ = case expr₀ of
Field a b -> Field <$> loadWith a <*> pure b
Project a b -> Project <$> loadWith a <*> pure b
Note a b -> do
let handler e = throwM (SourcedException a (e :: MissingImports))
let handler e = throwM (SourcedException a (e :: MissingImports))

let handler₁ (SourcedException _ e) =
throwM (SourcedException a (e :: MissingImports))

let handlers = [ Handler handler₀, Handler handler₁ ]

(Note <$> pure a <*> loadWith b) `catches` handlers
(Note <$> pure a <*> loadWith b) `catch` handler

-- | Resolve all imports within an expression
load :: Expr Src Import -> IO (Expr Src X)
Expand Down

0 comments on commit e951376

Please sign in to comment.