Skip to content

Commit 5253493

Browse files
Fix "Hooked sorts may not have subsorts" error (#2177)
1 parent 7caeea1 commit 5253493

File tree

5 files changed

+96
-8
lines changed

5 files changed

+96
-8
lines changed

kore/app/share/GlobalMain.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ import Kore.IndexedModule.IndexedModule
118118
( VerifiedModule
119119
)
120120
import Kore.Log as Log
121+
import Kore.Log.ErrorParse
122+
( errorParse
123+
)
121124
import Kore.Parser
122125
( ParsedPattern
123126
, parseKoreDefinition
@@ -506,7 +509,7 @@ mainParse parser fileName = do
506509
parseResult <-
507510
clockSomething "Parsing the file" (parser fileName contents)
508511
case parseResult of
509-
Left err -> error err
512+
Left err -> errorParse err
510513
Right definition -> return definition
511514

512515
type LoadedModule = VerifiedModule Attribute.Symbol

kore/src/Kore/ASTVerifier/AttributesVerifier.hs

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ module Kore.ASTVerifier.AttributesVerifier
1919
) where
2020

2121
import qualified Control.Lens as Lens
22+
import Data.Foldable
23+
( find
24+
, for_
25+
)
2226
import Data.Generics.Product
2327
import Prelude.Kore
2428

@@ -28,6 +32,7 @@ import qualified Data.Functor.Foldable as Recursive
2832
import Kore.ASTVerifier.Error
2933
import qualified Kore.Attribute.Axiom as Attribute
3034
( Axiom
35+
, sourceLocation
3136
)
3237
import Kore.Attribute.Hook
3338
import Kore.Attribute.Overload
@@ -37,6 +42,9 @@ import Kore.Attribute.Sort
3742
import Kore.Attribute.Sort.HasDomainValues
3843
import Kore.Attribute.Subsort as Subsort
3944

45+
import Kore.AST.AstWithLocation
46+
( locationFromAst
47+
)
4048
import qualified Kore.Attribute.Parser as Attribute.Parser
4149
import qualified Kore.Attribute.Symbol as Attribute
4250
import Kore.Error
@@ -50,11 +58,18 @@ import Kore.Syntax.Application
5058
( SymbolOrAlias (..)
5159
)
5260
import Kore.Syntax.Definition
61+
import Kore.Syntax.Id
62+
( prettyPrintAstLocation
63+
)
5364
import Kore.Syntax.Pattern
5465
import Kore.Syntax.Variable
5566
( VariableName (..)
5667
)
68+
import Kore.Unparser
69+
( unparse
70+
)
5771
import qualified Kore.Verified as Verified
72+
import Pretty
5873

5974
parseAttributes :: MonadError (Error VerifyError) m => Attributes -> m Hook
6075
parseAttributes = Attribute.Parser.liftParser . Attribute.Parser.parseAttributes
@@ -138,11 +153,29 @@ verifyNoHookAttribute attributes = do
138153

139154
verifyNoHookedSupersort
140155
:: MonadError (Error VerifyError) error
141-
=> [Kore.Attribute.Sort.Sort]
156+
=> IndexedModule Verified.Pattern Attribute.Symbol attrs
157+
-> Attribute.Axiom SymbolOrAlias VariableName
158+
-> [Subsort.Subsort]
142159
-> error ()
143-
verifyNoHookedSupersort supersortsAtts = do
144-
let isHooked = getHasDomainValues . hasDomainValues <$> supersortsAtts
145-
when (or isHooked) $ koreFail "Hooked sorts may not have subsorts."
160+
verifyNoHookedSupersort indexedModule axiom subsorts = do
161+
let isHooked =
162+
getHasDomainValues . hasDomainValues
163+
. getSortAttributes indexedModule
164+
. Subsort.supersort
165+
hookedSubsort = find isHooked subsorts
166+
for_ hookedSubsort $ \sort ->
167+
koreFail . unlines $
168+
[ "Hooked sorts may not have subsorts."
169+
, "Hooked sort:"
170+
, show . unparse $ Subsort.supersort sort
171+
, "Its subsort:"
172+
, show . unparse $ Subsort.subsort sort
173+
, "Location in the Kore file:"
174+
, show . prettyPrintAstLocation
175+
$ locationFromAst (Subsort.supersort sort)
176+
, "Location in the original K file: "
177+
, show . pretty $ Attribute.sourceLocation axiom
178+
]
146179

147180
verifyAxiomAttributes
148181
:: forall error attrs
@@ -152,9 +185,8 @@ verifyAxiomAttributes
152185
-> error (Attribute.Axiom Internal.Symbol.Symbol VariableName)
153186
verifyAxiomAttributes indexedModule axiom = do
154187
let overload = axiom Lens.^. field @"overload"
155-
supersorts = Subsort.supersort <$> getSubsorts (axiom Lens.^. field @"subsorts")
156-
supersortsAtts = getSortAttributes indexedModule <$> supersorts
157-
verifyNoHookedSupersort supersortsAtts
188+
subsorts = getSubsorts (axiom Lens.^. field @"subsorts")
189+
verifyNoHookedSupersort indexedModule axiom subsorts
158190
case getOverload overload of
159191
Nothing -> do
160192
let newOverload = Overload Nothing

kore/src/Kore/Log/ErrorParse.hs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{- |
2+
Copyright : (c) Runtime Verification, 2020
3+
License : NCSA
4+
5+
-}
6+
7+
module Kore.Log.ErrorParse
8+
( ErrorParse (..)
9+
, errorParse
10+
) where
11+
12+
import Prelude.Kore
13+
14+
import Control.Monad.Catch
15+
( Exception (..)
16+
, MonadThrow
17+
, throwM
18+
)
19+
import Pretty
20+
21+
import Log
22+
23+
newtype ErrorParse = ErrorParse { message :: String }
24+
deriving Show
25+
26+
instance Exception ErrorParse where
27+
toException = toException . SomeEntry
28+
fromException exn =
29+
fromException exn >>= fromEntry
30+
31+
instance Pretty ErrorParse where
32+
pretty ErrorParse { message } =
33+
Pretty.pretty message
34+
35+
instance Entry ErrorParse where
36+
entrySeverity _ = Error
37+
38+
errorParse :: MonadThrow log => String -> log a
39+
errorParse message =
40+
throwM ErrorParse { message }

kore/src/Kore/Log/Registry.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ import Kore.Log.ErrorDecidePredicateUnknown
7171
import Kore.Log.ErrorException
7272
( ErrorException
7373
)
74+
import Kore.Log.ErrorParse
75+
( ErrorParse
76+
)
7477
import Kore.Log.ErrorRewriteLoop
7578
( ErrorRewriteLoop
7679
)
@@ -151,6 +154,7 @@ entryHelpDocs :: [Pretty.Doc ()]
151154
, mk $ Proxy @DebugSubstitutionSimplifier
152155
, mk $ Proxy @ErrorBottomTotalFunction
153156
, mk $ Proxy @ErrorDecidePredicateUnknown
157+
, mk $ Proxy @ErrorParse
154158
, mk $ Proxy @WarnFunctionWithoutEvaluators
155159
, mk $ Proxy @WarnSymbolSMTRepresentation
156160
, mk $ Proxy @WarnStuckClaimState

test/no-injection-into-hooked/test.k.verify.out.golden

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,12 @@ Error:
22
module 'TEST':
33
axiom declaration:
44
Hooked sorts may not have subsorts.
5+
Hooked sort:
6+
SortInt{}
7+
Its subsort:
8+
SortS{}
9+
Location in the Kore file:
10+
"./test-kompiled/definition.kore 209:136"
11+
Location in the original K file:
12+
13+

0 commit comments

Comments
 (0)