Skip to content

Commit

Permalink
dhall lint no longer sorts let bindings (#2503)
Browse files Browse the repository at this point in the history
Fixes #2480

This lint transformation was unsound and it seems tricky to fix, so the
safe thing to do is to remove the transformation for now.
  • Loading branch information
Gabriella439 authored Apr 9, 2023
1 parent 595d05b commit 1ae72a5
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 75 deletions.
53 changes: 0 additions & 53 deletions dhall/src/Dhall/Lint.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ module Dhall.Lint
) where

import Control.Applicative ((<|>))
import Data.List.NonEmpty (NonEmpty (..))

import Dhall.Syntax
( Binding (..)
Expand All @@ -38,7 +37,6 @@ import Dhall.Syntax

import qualified Data.Foldable as Foldable
import qualified Data.List.NonEmpty as NonEmpty
import qualified Data.Map as Map
import qualified Data.Text as Text
import qualified Dhall.Core as Core
import qualified Dhall.Map
Expand All @@ -63,7 +61,6 @@ lint = Dhall.Optics.rewriteOf subExpressions rewrite
<|> fixParentPath e
<|> removeLetInLet e
<|> addPreludeExtensions e
<|> sortImports e

-- | Remove unused `Let` bindings.
removeUnusedBindings :: Eq a => Expr s a -> Maybe (Expr s a)
Expand Down Expand Up @@ -212,53 +209,3 @@ useToMap (ListLit _ keyValues)
Nothing
useToMap _ =
Nothing

-- | This sorts `let` bindings to move imports to the front if doing so does not
-- change the behavior of the code.
sortImports :: Eq s => Expr s Import -> Maybe (Expr s Import)
sortImports oldExpression@(Let binding0 oldBody0)
| oldExpression == newExpression = Nothing
| otherwise = Just newExpression
where
toBool (Embed _ ) = False
toBool (Note _ e) = toBool e
toBool _ = True

process (seen, index) Binding{..} oldBody function = (pair, pairs, newBody)
where
order =
if b then index else Map.findWithDefault (0 :: Int) variable seen

b = toBool value

pair = (order, function)

~(pairs, newBody) =
label (Map.insert variable order seen, index + 1) oldBody

label state (Let binding oldBody) = (pair : pairs, newBody)
where
function = Let binding

~(pair, pairs, newBody) = process state binding oldBody function

label state (Note src (Let binding oldBody)) = (pair : pairs, newBody)
where
function e = Note src (Let binding e)

~(pair, pairs, newBody) = process state binding oldBody function

label _ body =
([], body)

~(pairs0, newBody0) = (pair :| pairs, newBody)
where
function = Let binding0

~(pair, pairs, newBody) =
process (Map.empty, 1) binding0 oldBody0 function

sortedFunctions = fmap snd (NonEmpty.sortWith fst pairs0)

newExpression = foldr id newBody0 sortedFunctions
sortImports _ = Nothing
11 changes: 0 additions & 11 deletions dhall/tests/lint/success/sortLetsA.dhall

This file was deleted.

11 changes: 0 additions & 11 deletions dhall/tests/lint/success/sortLetsB.dhall

This file was deleted.

0 comments on commit 1ae72a5

Please sign in to comment.