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

Conversation

kikofernandez
Copy link
Contributor

This commit fixes accesses to an array when the type is different from an array; closes #823 and #831

This test case used to crash the compiler

active class Main
  def main(): unit
    val i = 4
    i(0) = 3
  end
end

With this commit, the compiler informs you that it expects an array of a function call and you gave it an int type.

Copy link
Contributor

@EliasC EliasC left a comment

Choose a reason for hiding this comment

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

Looks straightforward enough (especially when reviewed with ?w=1). I only have a minor comment on the error message, but I would be fine with merging it anyway. Will merge this afternoon unless anyone objects.

typeArguments = typeArgs}
else do
tcError $
ExpectingOtherTypeError "an array access or a function call" ty
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor: Should this be "an array or a function"? This way the error message becomes

Expected an array or a function but found expression of type 'int'

reflecting that the type error is not the whole expression, but only the sub-expression that is being called.

@kikofernandez
Copy link
Contributor Author

thanks! I have remove the access word from the error message

@EliasC
Copy link
Contributor

EliasC commented Jun 29, 2017

Have you also pushed?

@@ -0,0 +1 @@
Expected an array access or a function call but found expression of type 'int'
Copy link
Contributor

Choose a reason for hiding this comment

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

This test needs to be updated as well

@albertnetymk
Copy link
Contributor

It seems that unless (isArrowType ty) is guarded by an if with the same condition.

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!

@albertnetymk
Copy link
Contributor

OK, then it makes sense to restructure the flow to be sth like the following to "return early" for abnormal case, IMO.

   unless (isArrowType ty) $
    error
   -- normal case follows

@kikofernandez
Copy link
Contributor Author

@albertnetymk I thought about it and considered that the majority of the time the programmer knows that an array is an array, so I was favouring the other pattern, where the developer is correct in most occasions. I have no problems going for your suggestion though.

(@albertnetymk it would be great if your comments are actually part of the review or marked in the appropriate lines, as the ones from @EliasC)

@EliasC
Copy link
Contributor

EliasC commented Jun 29, 2017

I agree with Kiko. Having the failing case last makes sense to me.

@albertnetymk
Copy link
Contributor

By "return early", a level of indentation is removed, which is considered a decrease in certain complexity metrics. Surely, this is very subjective; the author should make the judgement call.

@EliasC
Copy link
Contributor

EliasC commented Jun 29, 2017

By "return early", a level of indentation is removed

The same "complexity reduction" could be achieved by writing

else if ... then
  ...

instead of

else
  if ... then
    ...

@albertnetymk
Copy link
Contributor

The same "complexity reduction" could be achieved by writing

I don't think that's true in Haskell, because body needs to be indented further than if.

@kikofernandez
Copy link
Contributor Author

This is a matter of taste and style. We all have provided good reasons for choosing one option or the other. Can we please move past the if-else?

@EliasC EliasC merged commit 4bf943a into parapluu:development Jun 29, 2017
@EliasC
Copy link
Contributor

EliasC commented Jun 29, 2017

I took an executive decision and merged this =)

@albertnetymk
Copy link
Contributor

The "early return" is about a taste of preference, but the indentation discussion is not.

My previous statement, "body needs to be indented further than if", is incorrect. The following is perfectly valid Haskell code:

f =
  if True then
 1
  else if True then
 2
  else
 3

@kikofernandez
Copy link
Contributor Author

Perfect valid Haskell code ≠ accepted code in Encore compiler

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Array access operator applied to string results in internal compiler error
3 participants