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
Refactor CrystalPath::Error #9359
Refactor CrystalPath::Error #9359
Conversation
Introduces a dedicated error type for path lookup fails which contains information about the path being looked up. This separates creating the actual compiler error which then happens in SemanticVisitor#visit(Require).
@@ -68,6 +68,24 @@ abstract class Crystal::SemanticVisitor < Crystal::Visitor | |||
node.expanded = expanded | |||
node.bind_to(expanded) | |||
false | |||
rescue ex : CrystalPath::NotFoundError |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move this rescue just around find_in_path
because that's where the error can happen? This makes it much clearer when the exception is expected to happen.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I considered that, but the problem is that the exception raised there would subsequently be rescued in line 89. I don't think it makes sense to make that rescue handler work around that.
A slightly less confusing option could be to create the error in a rescue around find_in_path
, assign it to a local variable and only raise in an else
rescue handler. Not sure if that's worth it, though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I see. Yeah, let's leave it like that for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That said, I don't understand why CrystalPath::NotFoundError
is handled in a special way here, instead of just being a regular Crystal::Exception and having a sensible to_s
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea was to have CrystalPath::NotFoundError
as a plain internal error type, not related to any user code interpretation. CrystalPath
should not care wheter it was invoked for a require
or something else. It also doesn't know the code location, so it can't really create a proper user-facing error.
NotFoundError
just communicates that finding a path failed. What that means depends on the context. In the context of interpreting a Require
node it means the require has failed and an appropriate error is raised for that specific situation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. I left an optional suggestion.
Introduces a dedicated error type
CrystalPath::NotFoundError
for path lookup fails which contains information about the path being looked up.This separates creating the actual compiler error which then happens in
SemanticVisitor#visit(Require)
.Also refactors the specs to not use absolute paths which could compromise the spec effectiveness.
This PR is part of a series on refactoring compiler errors #8410 (comment)