Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix array access #832

Merged
merged 5 commits into from
Jun 29, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/tests/encore/basic/arrayAccess.enc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
active class Main
def main(): unit
val i = 4
i(0) = 3
end
end
1 change: 1 addition & 0 deletions src/tests/encore/basic/arrayAccess.fail
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Expected an array or a function call but found expression of type 'int'
65 changes: 33 additions & 32 deletions src/types/Typechecker/Typechecker.hs
Original file line number Diff line number Diff line change
Expand Up @@ -807,39 +807,40 @@ instance Checkable Expr where
doTypecheck ArrayAccess{emeta
,target = VarAccess{emeta, qname}
,index = head args}
else do
let typeParams = getTypeParameters ty
argTypes = getArgTypes ty
resultType = getResultType ty
actualLength = length args
expectedLength = length argTypes
defName = qname'{qnlocal = Name $ "_" ++ show qname ++ show (expectedLength - actualLength)}

calledName <-
if (actualLength == expectedLength)
then return qname'
else
if (isArrowType ty) then do
let typeParams = getTypeParameters ty
argTypes = getArgTypes ty
resultType = getResultType ty
actualLength = length args
expectedLength = length argTypes
defName = qname'{qnlocal = Name $ "_" ++ show qname ++ show (expectedLength - actualLength)}
calledName <-
if (actualLength == expectedLength)
then return qname'
else do
result2 <- findVar defName
case result2 of
Just (qname2, ty2) -> return defName
Nothing -> tcError $ WrongNumberOfFunctionArgumentsError
qname (length argTypes) (length args)
unless (isArrowType ty) $
tcError $ NonFunctionTypeError ty
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the thing that could go according to @albertnetymk. I agree.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch @albertnetymk and @EliasC. Fixed!

(eArgs, returnType, typeArgs) <-
if null typeArguments
then inferenceCall fcall typeParams (take actualLength argTypes) resultType
else do
result2 <- findVar defName
case result2 of
Just (qname2, ty2) -> return defName
Nothing -> tcError $ WrongNumberOfFunctionArgumentsError
qname (length argTypes) (length args)

unless (isArrowType ty) $
tcError $ NonFunctionTypeError ty

(eArgs, returnType, typeArgs) <-
if null typeArguments
then inferenceCall fcall typeParams (take actualLength argTypes) resultType
else do
unless (length typeArguments == length typeParams) $
tcError $ WrongNumberOfFunctionTypeArgumentsError qname
(length typeParams) (length typeArguments)
typecheckCall fcall typeParams (take actualLength argTypes) resultType
return $ setArrowType ty $
setType returnType fcall {args = eArgs,
qname = calledName,
typeArguments = typeArgs}
unless (length typeArguments == length typeParams) $
tcError $ WrongNumberOfFunctionTypeArgumentsError qname
(length typeParams) (length typeArguments)
typecheckCall fcall typeParams (take actualLength argTypes) resultType
return $ setArrowType ty $
setType returnType fcall {args = eArgs,
qname = calledName,
typeArguments = typeArgs}
else do
tcError $
ExpectingOtherTypeError "an array or a function call" ty

--- |- t1 .. |- tn
-- E, x1 : t1, .., xn : tn |- body : t
Expand Down