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

Make PC more resilient to crashes #19488

Merged
merged 1 commit into from
Jan 19, 2024
Merged

Make PC more resilient to crashes #19488

merged 1 commit into from
Jan 19, 2024

Conversation

rochala
Copy link
Contributor

@rochala rochala commented Jan 19, 2024

When working with code that is recovered from errors, we should use typeOpt instead of tpe as it can sometimes be null. The added test cases were the exact snippet that previously failed. I took this opportunity to change all calls to tpe to typeOpt.

@rochala rochala requested a review from tgodzik January 19, 2024 13:13
Copy link
Contributor

@tgodzik tgodzik left a comment

Choose a reason for hiding this comment

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

LGTM! Would be great to set up some kind of linter to report any methods that could throw,

@tgodzik tgodzik merged commit 5fc2ef9 into scala:main Jan 19, 2024
19 checks passed
@tgodzik
Copy link
Contributor

tgodzik commented Jan 19, 2024

Could you port that to Metals also?

@som-snytt som-snytt changed the title Make PC more resiliant to crashes Make PC more resilient to crashes Jan 19, 2024
@dwijnand
Copy link
Member

dwijnand commented Jan 19, 2024

Why are these typed trees, if they don't have a type? That's half the reason to have the whole typed and untyped trees modelling, so we can define the implementation once and not have to allocate, but define APIs based on whether they're statically known to be typed or not. Some part of the code path must be casting the trees unsafely, somewhere.

But even more importantly, I don't think we should be wrapping the call to allImplicits and silenting throwing away any and all exceptions thrown. That's a recipe for big confusion in the future.

@rochala
Copy link
Contributor Author

rochala commented Jan 20, 2024

Remember that what we're working is usually trees recovered from error like:

class Wrapper(n: Int):
  extension (x: Int)
    def + (y: Int) = Wrap@@per(x) + y

with its tree after the typer:

package <empty> {
  import scala.collection.immutable.ListSet
  class Wrapper(n: Int) extends Object() {
    private[this] val n: Int
    extension (x: Int) def +(y: Int):
      <error Overloaded or recursive method + needs return type>
     = Wrapper(x).+(y)
  }
}

Even tho it is a typed tree, at cursor position, its type is null. I don't know whether this is a bug in compiler, but in my opinion change to typeOpt is something that should help in these cases.

As of adding try clause, it also slipped through my mind that this is not the best idea. As presentation compiler has raports system, we could use it to notify whether this happens.
Once again doing implicit search is something that can also happen on trees it shouldn't happen like List[Int]. Ideally we should ensure that only trees that can be completed have their completions computed and this is not so easy to achieve. I tried to handle this specific case with List[Int], as there should be no completions on such type applys, but if we call method returning a TypeApply like def asInstanceOf[T]: [T] it should still happen. I didn't succeed in distinguishing which is the case, but i think it can be done with AST analsysis.

@Kordyjan Kordyjan added this to the 3.4.1 milestone Feb 14, 2024
@tgodzik tgodzik added the area:presentation-compiler Related to the presentation compiler module used by Metals and possibly other tools label May 8, 2024
WojciechMazur added a commit that referenced this pull request Jun 28, 2024
Backports #19488 to the LTS branch.

PR submitted by the release tooling.
[skip ci]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:presentation-compiler Related to the presentation compiler module used by Metals and possibly other tools
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants