Skip to content

Commit

Permalink
add missing pattern arity check
Browse files Browse the repository at this point in the history
  • Loading branch information
dorchard committed Mar 7, 2024
1 parent 6f948b4 commit 38c1c18
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
6 changes: 4 additions & 2 deletions frontend/src/Language/Granule/Checker/Monad.hs
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ data CheckerError
| PatternTypingMismatch
{ errLoc :: Span, errPat :: Pattern (), tyExpected :: Type, tyActual :: Type }
| PatternArityError
{ errLoc :: Span, errId :: Id }
{ errLoc :: Span, errId :: Id, expectedArgs :: Int, actualArgs :: Int }
| UnboundVariableError
{ errLoc :: Span, errId :: Id }
| UnboundTypeVariable
Expand Down Expand Up @@ -886,7 +886,9 @@ instance UserMsg CheckerError where
msg PatternArityError{..}
= "Data constructor `"
<> pretty errId
<> "` is applied to too many arguments."
<> "` is applied to wrong number of arguments. Expecting "
<> show expectedArgs <> " but pattern match has "
<> show actualArgs <> " patterns."

msg UnboundVariableError{..} = "`" <> pretty errId <> "`"

Expand Down
7 changes: 6 additions & 1 deletion frontend/src/Language/Granule/Checker/Patterns.hs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,12 @@ ctxtFromTypedPattern' outerBoxTy _ pos ty p@(PConstr s _ rf dataC tyVarBindsRequ
mConstructor <- lookupDataConstructor s dataC
case mConstructor of
Nothing -> throw UnboundDataConstructor{ errLoc = s, errId = dataC }
Just (tySch@(Forall _ tyVarBinders _ _), coercions, indices) -> do
Just (tySch@(Forall _ tyVarBinders _ innerType), coercions, indices) -> do

-- Check pattern arity
if (arity innerType /= length ps)
then throw $ PatternArityError s dataC (arity innerType) (length ps)
else return ()

case outerBoxTy of
-- Hsup if you only have more than one premise (and have an enclosing grade)
Expand Down
4 changes: 4 additions & 0 deletions frontend/tests/cases/negative/polymorphism/patternArity.gr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
data Vec a = Nil a | Cons a (Vec a)

extract : Vec Int -> Int
extract (Cons x) = x
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Type checking failed: blappy.gr:
Pattern arity error: blappy.gr:4:10:
Data constructor `Cons` is applied to wrong number of arguments. Expecting 2 but pattern match has 1 patterns.

0 comments on commit 38c1c18

Please sign in to comment.