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

Refactor CrystalPath::Error #9359

Merged

Conversation

straight-shoota
Copy link
Member

@straight-shoota straight-shoota commented May 26, 2020

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)

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).
spec/compiler/semantic/require_spec.cr Outdated Show resolved Hide resolved
spec/compiler/semantic/require_spec.cr Outdated Show resolved Hide resolved
Thanks @Sija

Co-authored-by: Sijawusz Pur Rahnama <sija@sija.pl>
@straight-shoota straight-shoota added this to the 0.35.0 milestone May 27, 2020
@bcardiff bcardiff requested a review from asterite May 28, 2020 17:29
@@ -68,6 +68,24 @@ abstract class Crystal::SemanticVisitor < Crystal::Visitor
node.expanded = expanded
node.bind_to(expanded)
false
rescue ex : CrystalPath::NotFoundError
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

@asterite asterite left a 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.

@bcardiff bcardiff merged commit 9d84d37 into crystal-lang:master May 29, 2020
Refactor compiler errors automation moved this from In progress to Done May 29, 2020
@straight-shoota straight-shoota deleted the refactor/crystal-path-error branch May 29, 2020 18:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

None yet

5 participants