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

Improve error messages for the Focus macro. #1413

Merged
merged 5 commits into from
Dec 21, 2023

Conversation

yilinwei
Copy link
Collaborator

@yilinwei yilinwei commented Dec 19, 2023

A few random changes which are (mostly) forced by compiler changes.

  1. The inferred type for inline lambas is different, so we get this awfully confusing error message currently:
Cannot generate Lens for field 'foo', because '_$1' is not a case class
  1. We can now report a position error within a macro so the language servers should be able to point to the right part of the code.

I think I'll change the hierarchy of the FocusError a little, because some of the errors are internal i.e. 5xx and some are 4xx; mixing them is quite confusing.

 - Correctly widen inferred singleton types for the error message.
 - Propagate the position information for user errors.
def getFieldSymbol(fromTypeSymbol: Symbol): Symbol = {
// We need to do this to support tuples, because even though they conform as case classes in other respects,
// for some reason their field names (_1, _2, etc) have a space at the end, ie `_1 `.
val f: String => String =
Copy link
Collaborator Author

@yilinwei yilinwei Dec 20, 2023

Choose a reason for hiding this comment

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

This fixes a silent error if we have something like

case class Foo(x: Int, `x `: Int)

since identifiers with leading/trailing spaces are not equivalent.

def errorMessage(error: FocusError): String = error match {
case FocusError.NotACaseClass(fromClass, fieldName) =>
s"Cannot generate Lens for field '$fieldName', because '$fromClass' is not a case class"
def errorReport(error: FocusError): (String, Option[Position]) = error match {
Copy link
Collaborator Author

@yilinwei yilinwei Dec 20, 2023

Choose a reason for hiding this comment

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

The dotty macro API has better support for errors now.

As a tangent, I need to see whether we can use the -explain flag which would be helpful to expand internal errors.

@julien-truffaut
Copy link
Member

Thanks @yilinwei, it looks good to me!

@yilinwei yilinwei merged commit 79db870 into optics-dev:master Dec 21, 2023
11 checks passed
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.

2 participants