2022-09-23 16:43:18 +03:00
|
|
|
module Typecheck.Negative where
|
2022-03-29 20:01:19 +03:00
|
|
|
|
|
|
|
import Base
|
2023-11-30 17:03:54 +03:00
|
|
|
import Juvix.Compiler.Internal.Translation.FromInternal.Analysis.Positivity.Error
|
2022-08-03 14:20:40 +03:00
|
|
|
import Juvix.Compiler.Internal.Translation.FromInternal.Analysis.TypeChecking.Error
|
2022-03-29 20:01:19 +03:00
|
|
|
|
|
|
|
type FailMsg = String
|
|
|
|
|
|
|
|
data NegTest = NegTest
|
2022-04-07 19:10:53 +03:00
|
|
|
{ _name :: String,
|
2023-11-28 20:24:03 +03:00
|
|
|
_dir :: Path Abs Dir,
|
|
|
|
_file :: Path Abs File,
|
2022-04-22 11:06:34 +03:00
|
|
|
_checkErr :: TypeCheckerError -> Maybe FailMsg
|
2022-04-05 20:57:21 +03:00
|
|
|
}
|
2022-04-01 14:00:15 +03:00
|
|
|
|
2023-11-28 20:24:03 +03:00
|
|
|
makeLenses ''NegTest
|
|
|
|
|
2022-03-29 20:01:19 +03:00
|
|
|
testDescr :: NegTest -> TestDescr
|
2022-04-05 20:57:21 +03:00
|
|
|
testDescr NegTest {..} =
|
2023-11-28 20:24:03 +03:00
|
|
|
let tRoot = _dir
|
|
|
|
file' = _file
|
2022-04-07 19:10:53 +03:00
|
|
|
in TestDescr
|
|
|
|
{ _testName = _name,
|
|
|
|
_testRoot = tRoot,
|
|
|
|
_testAssertion = Single $ do
|
2023-12-06 20:24:59 +03:00
|
|
|
entryPoint <- testDefaultEntryPointIO tRoot file'
|
2023-12-30 22:15:35 +03:00
|
|
|
result <- testRunIOEither entryPoint upToInternalTyped
|
2022-07-08 14:59:45 +03:00
|
|
|
case mapLeft fromJuvixError result of
|
2022-05-26 18:52:08 +03:00
|
|
|
Left (Just tyError) -> whenJust (_checkErr tyError) assertFailure
|
2024-01-08 15:27:18 +03:00
|
|
|
Left Nothing -> assertFailure "An error occurred but it was not in the type checker."
|
2023-01-17 11:41:07 +03:00
|
|
|
Right _ -> assertFailure "The type checker did not find an error."
|
2022-04-07 19:10:53 +03:00
|
|
|
}
|
2022-03-29 20:01:19 +03:00
|
|
|
|
|
|
|
allTests :: TestTree
|
2022-04-05 20:57:21 +03:00
|
|
|
allTests =
|
|
|
|
testGroup
|
2022-07-23 10:27:12 +03:00
|
|
|
"Typecheck negative tests"
|
|
|
|
[ testGroup
|
|
|
|
"General typechecking errors"
|
|
|
|
(map (mkTest . testDescr) tests),
|
|
|
|
testGroup
|
|
|
|
"Non-strictly positive data types"
|
|
|
|
(map (mkTest . testDescr) negPositivityTests)
|
|
|
|
]
|
2022-03-29 20:01:19 +03:00
|
|
|
|
2022-12-20 15:05:40 +03:00
|
|
|
root :: Path Abs Dir
|
|
|
|
root = relToProject $(mkRelDir "tests/negative")
|
2022-03-29 20:01:19 +03:00
|
|
|
|
2023-11-28 20:24:03 +03:00
|
|
|
negTest :: String -> Path Rel Dir -> Path Rel File -> (TypeCheckerError -> Maybe FailMsg) -> NegTest
|
|
|
|
negTest _name rdir rfile _checkErr =
|
|
|
|
let _dir = root <//> rdir
|
|
|
|
in NegTest
|
|
|
|
{ _file = _dir <//> rfile,
|
|
|
|
_name,
|
|
|
|
_dir,
|
|
|
|
_checkErr
|
|
|
|
}
|
|
|
|
|
2022-03-29 20:01:19 +03:00
|
|
|
wrongError :: Maybe FailMsg
|
|
|
|
wrongError = Just "Incorrect error"
|
|
|
|
|
|
|
|
tests :: [NegTest]
|
2022-04-05 20:57:21 +03:00
|
|
|
tests =
|
2023-11-28 20:24:03 +03:00
|
|
|
[ negTest
|
2022-04-05 20:57:21 +03:00
|
|
|
"Constructor in pattern type error"
|
2022-12-20 15:05:40 +03:00
|
|
|
$(mkRelDir "Internal")
|
|
|
|
$(mkRelFile "PatternConstructor.juvix")
|
2022-04-05 20:57:21 +03:00
|
|
|
$ \case
|
2022-04-22 11:06:34 +03:00
|
|
|
ErrWrongConstructorType {} -> Nothing
|
2022-04-05 20:57:21 +03:00
|
|
|
_ -> wrongError,
|
2023-11-28 20:24:03 +03:00
|
|
|
negTest
|
2022-07-15 18:57:04 +03:00
|
|
|
"Check pattern with hole type"
|
2022-12-20 15:05:40 +03:00
|
|
|
$(mkRelDir "265")
|
|
|
|
$(mkRelFile "M.juvix")
|
2022-07-15 18:57:04 +03:00
|
|
|
$ \case
|
|
|
|
ErrWrongConstructorType {} -> Nothing
|
|
|
|
_ -> wrongError,
|
2023-11-28 20:24:03 +03:00
|
|
|
negTest
|
2022-04-05 20:57:21 +03:00
|
|
|
"Type vs inferred type mismatch"
|
2022-12-20 15:05:40 +03:00
|
|
|
$(mkRelDir "Internal")
|
|
|
|
$(mkRelFile "WrongType.juvix")
|
2022-04-05 20:57:21 +03:00
|
|
|
$ \case
|
2022-04-22 11:06:34 +03:00
|
|
|
ErrWrongType {} -> Nothing
|
2022-04-05 20:57:21 +03:00
|
|
|
_ -> wrongError,
|
2023-11-28 20:24:03 +03:00
|
|
|
negTest
|
2022-04-05 20:57:21 +03:00
|
|
|
"Function application with non-function type"
|
2022-12-20 15:05:40 +03:00
|
|
|
$(mkRelDir "Internal")
|
|
|
|
$(mkRelFile "ExpectedFunctionType.juvix")
|
2022-04-05 20:57:21 +03:00
|
|
|
$ \case
|
2022-04-22 11:06:34 +03:00
|
|
|
ErrExpectedFunctionType {} -> Nothing
|
2022-04-05 20:57:21 +03:00
|
|
|
_ -> wrongError,
|
2023-11-28 20:24:03 +03:00
|
|
|
negTest
|
2022-06-02 14:02:07 +03:00
|
|
|
"Unsolved hole"
|
2022-12-20 15:05:40 +03:00
|
|
|
$(mkRelDir "Internal")
|
|
|
|
$(mkRelFile "UnsolvedMeta.juvix")
|
2022-06-02 14:02:07 +03:00
|
|
|
$ \case
|
|
|
|
ErrUnsolvedMeta {} -> Nothing
|
|
|
|
_ -> wrongError,
|
2023-11-28 20:24:03 +03:00
|
|
|
negTest
|
2022-04-05 20:57:21 +03:00
|
|
|
"Multiple type errors are captured"
|
2022-12-20 15:05:40 +03:00
|
|
|
$(mkRelDir "Internal")
|
|
|
|
$(mkRelFile "MultiWrongType.juvix")
|
2022-04-05 20:57:21 +03:00
|
|
|
$ \case
|
2022-04-22 11:06:34 +03:00
|
|
|
ErrWrongType {} -> Nothing
|
2022-06-21 18:53:35 +03:00
|
|
|
_ -> wrongError,
|
2023-11-28 20:24:03 +03:00
|
|
|
negTest
|
2022-07-20 11:33:52 +03:00
|
|
|
"Unexpected braces in pattern"
|
2022-12-20 15:05:40 +03:00
|
|
|
$(mkRelDir "issue1337")
|
|
|
|
$(mkRelFile "Braces.juvix")
|
2022-07-20 11:33:52 +03:00
|
|
|
$ \case
|
2023-11-28 20:24:03 +03:00
|
|
|
ErrArityCheckerError (ErrWrongPatternIsImplicit {}) -> Nothing
|
2022-07-20 11:33:52 +03:00
|
|
|
_ -> wrongError,
|
2023-11-28 20:24:03 +03:00
|
|
|
negTest
|
2023-09-13 13:16:05 +03:00
|
|
|
"Unexpected double braces in pattern"
|
|
|
|
$(mkRelDir "issue1337")
|
|
|
|
$(mkRelFile "DoubleBraces.juvix")
|
|
|
|
$ \case
|
2023-11-28 20:24:03 +03:00
|
|
|
ErrArityCheckerError (ErrWrongPatternIsImplicit {}) -> Nothing
|
2023-09-13 13:16:05 +03:00
|
|
|
_ -> wrongError,
|
2023-11-28 20:24:03 +03:00
|
|
|
negTest
|
2022-06-21 18:53:35 +03:00
|
|
|
"Wrong return type name for a constructor of a simple data type"
|
2022-12-20 15:05:40 +03:00
|
|
|
$(mkRelDir "Internal")
|
|
|
|
$(mkRelFile "WrongReturnType.juvix")
|
2022-06-21 18:53:35 +03:00
|
|
|
$ \case
|
|
|
|
ErrWrongReturnType {} -> Nothing
|
|
|
|
_ -> wrongError,
|
2023-11-28 20:24:03 +03:00
|
|
|
negTest
|
2022-06-22 12:42:59 +03:00
|
|
|
"Too few arguments for the return type of a constructor"
|
2022-12-20 15:05:40 +03:00
|
|
|
$(mkRelDir "Internal")
|
|
|
|
$(mkRelFile "WrongReturnTypeTooFewArguments.juvix")
|
2022-06-21 18:53:35 +03:00
|
|
|
$ \case
|
2022-07-25 13:30:18 +03:00
|
|
|
ErrWrongType {} -> Nothing
|
2022-06-22 12:42:59 +03:00
|
|
|
_ -> wrongError,
|
2023-11-28 20:24:03 +03:00
|
|
|
negTest
|
2023-01-09 20:56:28 +03:00
|
|
|
"Ambiguous hole"
|
|
|
|
$(mkRelDir "Internal")
|
|
|
|
$(mkRelFile "IdenFunctionArgsNoExplicit.juvix")
|
2023-01-17 15:28:38 +03:00
|
|
|
$ \case
|
|
|
|
ErrUnsolvedMeta {} -> Nothing
|
|
|
|
_ -> wrongError,
|
2023-11-28 20:24:03 +03:00
|
|
|
negTest
|
2023-01-17 15:28:38 +03:00
|
|
|
"Cycle in hole"
|
|
|
|
$(mkRelDir "issue1700")
|
|
|
|
$(mkRelFile "SelfApplication.juvix")
|
2023-01-09 20:56:28 +03:00
|
|
|
$ \case
|
|
|
|
ErrUnsolvedMeta {} -> Nothing
|
2023-04-13 10:16:49 +03:00
|
|
|
_ -> wrongError,
|
2023-11-28 20:24:03 +03:00
|
|
|
negTest
|
2023-04-13 10:16:49 +03:00
|
|
|
"Negative integer literal cannot be used as a Nat"
|
|
|
|
$(mkRelDir "Internal")
|
|
|
|
$(mkRelFile "LiteralInteger.juvix")
|
|
|
|
$ \case
|
2023-11-03 12:01:03 +03:00
|
|
|
ErrNoInstance {} -> Nothing
|
2023-04-13 10:16:49 +03:00
|
|
|
_ -> wrongError,
|
2023-11-28 20:24:03 +03:00
|
|
|
negTest
|
2023-04-13 10:16:49 +03:00
|
|
|
"Integer literal cannot be used as a String"
|
|
|
|
$(mkRelDir "Internal")
|
|
|
|
$(mkRelFile "LiteralIntegerString.juvix")
|
|
|
|
$ \case
|
2023-11-03 12:01:03 +03:00
|
|
|
ErrNoInstance {} -> Nothing
|
2023-08-23 14:18:57 +03:00
|
|
|
_ -> wrongError,
|
2023-11-28 20:24:03 +03:00
|
|
|
negTest
|
2023-08-23 14:18:57 +03:00
|
|
|
"Unsupported type function"
|
|
|
|
$(mkRelDir "Internal")
|
|
|
|
$(mkRelFile "UnsupportedTypeFunction.juvix")
|
|
|
|
$ \case
|
|
|
|
ErrUnsupportedTypeFunction {} -> Nothing
|
2023-09-08 13:16:43 +03:00
|
|
|
_ -> wrongError,
|
2023-11-28 20:24:03 +03:00
|
|
|
negTest
|
2023-09-08 13:16:43 +03:00
|
|
|
"Instance target not a trait"
|
|
|
|
$(mkRelDir "Internal")
|
|
|
|
$(mkRelFile "TargetNotATrait.juvix")
|
|
|
|
$ \case
|
|
|
|
ErrTargetNotATrait {} -> Nothing
|
|
|
|
_ -> wrongError,
|
2023-11-28 20:24:03 +03:00
|
|
|
negTest
|
2023-09-08 13:16:43 +03:00
|
|
|
"Not a trait"
|
|
|
|
$(mkRelDir "Internal")
|
|
|
|
$(mkRelFile "NotATrait.juvix")
|
|
|
|
$ \case
|
|
|
|
ErrNotATrait {} -> Nothing
|
|
|
|
_ -> wrongError,
|
2023-11-28 20:24:03 +03:00
|
|
|
negTest
|
2023-09-08 13:16:43 +03:00
|
|
|
"No instance"
|
|
|
|
$(mkRelDir "Internal")
|
|
|
|
$(mkRelFile "NoInstance.juvix")
|
|
|
|
$ \case
|
|
|
|
ErrNoInstance {} -> Nothing
|
|
|
|
_ -> wrongError,
|
2023-11-28 20:24:03 +03:00
|
|
|
negTest
|
2023-09-08 13:16:43 +03:00
|
|
|
"Ambiguous instances"
|
|
|
|
$(mkRelDir "Internal")
|
|
|
|
$(mkRelFile "AmbiguousInstances.juvix")
|
|
|
|
$ \case
|
|
|
|
ErrAmbiguousInstances {} -> Nothing
|
|
|
|
_ -> wrongError,
|
2023-11-28 20:24:03 +03:00
|
|
|
negTest
|
2023-09-21 13:12:49 +03:00
|
|
|
"Subsumed instance"
|
|
|
|
$(mkRelDir "Internal")
|
|
|
|
$(mkRelFile "SubsumedInstance.juvix")
|
|
|
|
$ \case
|
|
|
|
ErrSubsumedInstance {} -> Nothing
|
|
|
|
_ -> wrongError,
|
2023-11-28 20:24:03 +03:00
|
|
|
negTest
|
2023-09-08 13:16:43 +03:00
|
|
|
"Explicit instance argument"
|
|
|
|
$(mkRelDir "Internal")
|
|
|
|
$(mkRelFile "ExplicitInstanceArgument.juvix")
|
|
|
|
$ \case
|
|
|
|
ErrExplicitInstanceArgument {} -> Nothing
|
|
|
|
_ -> wrongError,
|
2023-11-28 20:24:03 +03:00
|
|
|
negTest
|
2023-09-08 13:16:43 +03:00
|
|
|
"Instance termination"
|
|
|
|
$(mkRelDir "Internal")
|
|
|
|
$(mkRelFile "InstanceTermination.juvix")
|
|
|
|
$ \case
|
|
|
|
ErrTraitNotTerminating {} -> Nothing
|
2023-10-11 00:28:06 +03:00
|
|
|
_ -> wrongError,
|
2023-11-28 20:24:03 +03:00
|
|
|
negTest
|
2023-10-11 00:28:06 +03:00
|
|
|
"Default value wrong type"
|
|
|
|
$(mkRelDir "Internal")
|
|
|
|
$(mkRelFile "DefaultTypeError.juvix")
|
|
|
|
$ \case
|
|
|
|
ErrWrongType {} -> Nothing
|
2023-10-19 17:00:31 +03:00
|
|
|
_ -> wrongError,
|
2023-11-28 20:24:03 +03:00
|
|
|
negTest
|
2023-10-19 17:00:31 +03:00
|
|
|
"Coercion target not a trait"
|
|
|
|
$(mkRelDir "Internal")
|
|
|
|
$(mkRelFile "CoercionTargetNotATrait.juvix")
|
|
|
|
$ \case
|
|
|
|
ErrTargetNotATrait {} -> Nothing
|
|
|
|
_ -> wrongError,
|
2023-11-28 20:24:03 +03:00
|
|
|
negTest
|
2023-10-19 17:00:31 +03:00
|
|
|
"Invalid coercion type"
|
|
|
|
$(mkRelDir "Internal")
|
|
|
|
$(mkRelFile "InvalidCoercionType.juvix")
|
|
|
|
$ \case
|
|
|
|
ErrInvalidCoercionType {} -> Nothing
|
|
|
|
_ -> wrongError,
|
2023-11-28 20:24:03 +03:00
|
|
|
negTest
|
2023-10-19 17:00:31 +03:00
|
|
|
"Wrong coercion argument"
|
|
|
|
$(mkRelDir "Internal")
|
|
|
|
$(mkRelFile "WrongCoercionArgument.juvix")
|
|
|
|
$ \case
|
|
|
|
ErrWrongCoercionArgument {} -> Nothing
|
|
|
|
_ -> wrongError,
|
2023-11-28 20:24:03 +03:00
|
|
|
negTest
|
2023-10-19 17:00:31 +03:00
|
|
|
"Ambiguous coercions"
|
|
|
|
$(mkRelDir "Internal")
|
|
|
|
$(mkRelFile "AmbiguousCoercions.juvix")
|
|
|
|
$ \case
|
|
|
|
ErrAmbiguousInstances {} -> Nothing
|
|
|
|
_ -> wrongError,
|
2023-11-28 20:24:03 +03:00
|
|
|
negTest
|
2023-10-19 17:00:31 +03:00
|
|
|
"Coercion cycles"
|
|
|
|
$(mkRelDir "Internal")
|
|
|
|
$(mkRelFile "LoopingCoercion.juvix")
|
|
|
|
$ \case
|
|
|
|
ErrCoercionCycles {} -> Nothing
|
2022-04-05 20:57:21 +03:00
|
|
|
_ -> wrongError
|
|
|
|
]
|
2022-07-23 10:27:12 +03:00
|
|
|
|
|
|
|
negPositivityTests :: [NegTest]
|
|
|
|
negPositivityTests =
|
2023-11-28 20:24:03 +03:00
|
|
|
[ negTest "E1" $(mkRelDir "Internal/Positivity") $(mkRelFile "E1.juvix") $
|
2022-07-23 10:27:12 +03:00
|
|
|
\case
|
2023-11-30 17:03:54 +03:00
|
|
|
ErrNonStrictlyPositive ErrTypeInNegativePosition {} -> Nothing
|
2022-07-23 10:27:12 +03:00
|
|
|
_ -> wrongError,
|
2023-11-28 20:24:03 +03:00
|
|
|
negTest "E2" $(mkRelDir "Internal/Positivity") $(mkRelFile "E2.juvix") $
|
2022-07-23 10:27:12 +03:00
|
|
|
\case
|
2023-11-30 17:03:54 +03:00
|
|
|
ErrNonStrictlyPositive (ErrTypeInNegativePosition {}) -> Nothing
|
2022-07-23 10:27:12 +03:00
|
|
|
_ -> wrongError,
|
2023-11-28 20:24:03 +03:00
|
|
|
negTest "E3" $(mkRelDir "Internal/Positivity") $(mkRelFile "E3.juvix") $
|
2022-07-23 10:27:12 +03:00
|
|
|
\case
|
2023-11-30 17:03:54 +03:00
|
|
|
ErrNonStrictlyPositive (ErrTypeInNegativePosition {}) -> Nothing
|
2022-07-23 10:27:12 +03:00
|
|
|
_ -> wrongError,
|
2023-11-28 20:24:03 +03:00
|
|
|
negTest "E4" $(mkRelDir "Internal/Positivity") $(mkRelFile "E4.juvix") $
|
2022-07-23 10:27:12 +03:00
|
|
|
\case
|
2023-11-30 17:03:54 +03:00
|
|
|
ErrNonStrictlyPositive (ErrTypeInNegativePosition {}) -> Nothing
|
2022-07-23 10:27:12 +03:00
|
|
|
_ -> wrongError,
|
2023-11-28 20:24:03 +03:00
|
|
|
negTest "E5" $(mkRelDir "Internal/Positivity") $(mkRelFile "E5.juvix") $
|
2022-07-23 10:27:12 +03:00
|
|
|
\case
|
2023-11-30 17:03:54 +03:00
|
|
|
ErrNonStrictlyPositive (ErrTypeInNegativePosition {}) -> Nothing
|
2022-07-23 10:27:12 +03:00
|
|
|
_ -> wrongError,
|
2023-11-28 20:24:03 +03:00
|
|
|
negTest "E6" $(mkRelDir "Internal/Positivity") $(mkRelFile "E6.juvix") $
|
2022-07-23 10:27:12 +03:00
|
|
|
\case
|
2023-11-30 17:03:54 +03:00
|
|
|
ErrNonStrictlyPositive (ErrTypeInNegativePosition {}) -> Nothing
|
2022-07-23 10:27:12 +03:00
|
|
|
_ -> wrongError,
|
2023-11-28 20:24:03 +03:00
|
|
|
negTest "E7" $(mkRelDir "Internal/Positivity") $(mkRelFile "E7.juvix") $
|
2022-07-23 10:27:12 +03:00
|
|
|
\case
|
2023-11-30 17:03:54 +03:00
|
|
|
ErrNonStrictlyPositive (ErrTypeInNegativePosition {}) -> Nothing
|
2022-07-23 10:27:12 +03:00
|
|
|
_ -> wrongError,
|
2023-11-28 20:24:03 +03:00
|
|
|
negTest "E8" $(mkRelDir "Internal/Positivity") $(mkRelFile "E8.juvix") $
|
2022-07-23 10:27:12 +03:00
|
|
|
\case
|
2023-11-30 17:03:54 +03:00
|
|
|
ErrNonStrictlyPositive (ErrTypeInNegativePosition {}) -> Nothing
|
2022-07-23 10:27:12 +03:00
|
|
|
_ -> wrongError,
|
2023-11-28 20:24:03 +03:00
|
|
|
negTest "E9" $(mkRelDir "Internal/Positivity") $(mkRelFile "E9.juvix") $
|
2022-07-23 10:27:12 +03:00
|
|
|
\case
|
2023-11-30 17:03:54 +03:00
|
|
|
ErrNonStrictlyPositive (ErrTypeInNegativePosition {}) -> Nothing
|
|
|
|
_ -> wrongError,
|
|
|
|
negTest "E10 uses type synonym" $(mkRelDir "Internal/Positivity") $(mkRelFile "E10.juvix") $
|
|
|
|
\case
|
|
|
|
ErrNonStrictlyPositive (ErrTypeInNegativePosition {}) -> Nothing
|
|
|
|
_ -> wrongError,
|
|
|
|
negTest "E11 uses type synonym" $(mkRelDir "Internal/Positivity") $(mkRelFile "E11.juvix") $
|
|
|
|
\case
|
|
|
|
ErrNonStrictlyPositive (ErrTypeInNegativePosition {}) -> Nothing
|
2022-07-23 10:27:12 +03:00
|
|
|
_ -> wrongError
|
|
|
|
]
|