mirror of
https://github.com/anoma/juvix.git
synced 2024-12-01 00:04:58 +03:00
Check all the type parameter names are different when declaring an inductive type (#1377)
* Fix #1334 * Rename error type
This commit is contained in:
parent
753c5d5b0b
commit
d11605ab1e
@ -32,6 +32,7 @@ data ScoperError
|
||||
| ErrMultipleCompileBlockSameName MultipleCompileBlockSameName
|
||||
| ErrMultipleCompileRuleSameBackend MultipleCompileRuleSameBackend
|
||||
| ErrWrongKindExpressionCompileBlock WrongKindExpressionCompileBlock
|
||||
| ErrDuplicateInductiveParameterName DuplicateInductiveParameterName
|
||||
deriving stock (Show)
|
||||
|
||||
instance ToGenericError ScoperError where
|
||||
@ -58,3 +59,4 @@ instance ToGenericError ScoperError where
|
||||
ErrMultipleCompileBlockSameName e -> genericError e
|
||||
ErrMultipleCompileRuleSameBackend e -> genericError e
|
||||
ErrWrongKindExpressionCompileBlock e -> genericError e
|
||||
ErrDuplicateInductiveParameterName e -> genericError e
|
||||
|
@ -522,3 +522,27 @@ ambiguousMessage n es =
|
||||
<> "It could be any of:"
|
||||
<> line
|
||||
<> indent' (vsep (map ppCode es))
|
||||
|
||||
newtype DuplicateInductiveParameterName = DuplicateInductiveParameterName
|
||||
{ _duplicateInductiveParameterName :: Symbol
|
||||
}
|
||||
deriving stock (Show)
|
||||
|
||||
makeLenses ''DuplicateInductiveParameterName
|
||||
|
||||
instance ToGenericError DuplicateInductiveParameterName where
|
||||
genericError e =
|
||||
GenericError
|
||||
{ _genericErrorLoc = i,
|
||||
_genericErrorMessage = prettyError msg,
|
||||
_genericErrorIntervals = [i]
|
||||
}
|
||||
where
|
||||
param = e ^. duplicateInductiveParameterName
|
||||
i = getLoc param
|
||||
msg =
|
||||
"Invalid name"
|
||||
<+> ppCode param
|
||||
<> "."
|
||||
<> line
|
||||
<> "Inductive parameter names can not be repeated."
|
||||
|
@ -429,24 +429,31 @@ withParams ::
|
||||
[InductiveParameter 'Parsed] ->
|
||||
([InductiveParameter 'Scoped] -> Sem r a) ->
|
||||
Sem r a
|
||||
withParams xs a = go [] xs
|
||||
withParams xs a = go [] [] xs
|
||||
where
|
||||
go :: [InductiveParameter 'Scoped] -> [InductiveParameter 'Parsed] -> Sem r a
|
||||
go inductiveParameters' params =
|
||||
go :: [InductiveParameter 'Scoped] -> [Symbol] -> [InductiveParameter 'Parsed] -> Sem r a
|
||||
go inductiveParameters' usedNames params =
|
||||
case params of
|
||||
-- All params have been checked
|
||||
[] -> a inductiveParameters'
|
||||
-- More params to check
|
||||
(InductiveParameter {..} : ps) -> do
|
||||
inductiveParameterType' <- checkParseExpressionAtoms _inductiveParameterType
|
||||
inductiveParameterName' <- freshVariable _inductiveParameterName
|
||||
let param' =
|
||||
InductiveParameter
|
||||
{ _inductiveParameterType = inductiveParameterType',
|
||||
_inductiveParameterName = inductiveParameterName'
|
||||
}
|
||||
withBindLocalVariable (LocalVariable inductiveParameterName') $
|
||||
go (inductiveParameters' ++ [param']) ps
|
||||
-- All params have been checked
|
||||
[] -> a inductiveParameters'
|
||||
if
|
||||
| _inductiveParameterName `elem` usedNames ->
|
||||
throw
|
||||
( ErrDuplicateInductiveParameterName
|
||||
(DuplicateInductiveParameterName _inductiveParameterName)
|
||||
)
|
||||
| otherwise -> do
|
||||
inductiveParameterName' <- freshVariable _inductiveParameterName
|
||||
let param' =
|
||||
InductiveParameter
|
||||
{ _inductiveParameterType = inductiveParameterType',
|
||||
_inductiveParameterName = inductiveParameterName'
|
||||
}
|
||||
withBindLocalVariable (LocalVariable inductiveParameterName') $
|
||||
go (inductiveParameters' ++ [param']) (_inductiveParameterName : usedNames) ps
|
||||
|
||||
checkInductiveDef ::
|
||||
forall r.
|
||||
|
@ -211,6 +211,13 @@ scoperErrorTests =
|
||||
"WrongKindExpressionCompileBlock.juvix"
|
||||
$ \case
|
||||
ErrWrongKindExpressionCompileBlock {} -> Nothing
|
||||
_ -> wrongError,
|
||||
NegTest
|
||||
"A type parameter name occurs twice when declaring an inductive type"
|
||||
"."
|
||||
"DuplicateInductiveParameterName.juvix"
|
||||
$ \case
|
||||
ErrDuplicateInductiveParameterName {} -> Nothing
|
||||
_ -> wrongError
|
||||
]
|
||||
|
||||
|
5
tests/negative/DuplicateInductiveParameterName.juvix
Normal file
5
tests/negative/DuplicateInductiveParameterName.juvix
Normal file
@ -0,0 +1,5 @@
|
||||
module DuplicateInductiveParameterName;
|
||||
|
||||
inductive T (A : Type) (B : Type) (A : Type) {};
|
||||
|
||||
end;
|
Loading…
Reference in New Issue
Block a user