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

Explain unresolvable references better #20477

Merged
merged 4 commits into from
May 28, 2024
Merged

Conversation

odersky
Copy link
Contributor

@odersky odersky commented May 27, 2024

We run into problems when referring to a member of a self type of a class that it not also a member of the class from outside via an asSeenFrom.

One example is in 11226.scala where we see:

trait ManagedActorClassification { this: ActorEventBus =>
  def unsubscribe(subscriber: Subscriber): Unit
}

class Unsubscriber(bus: ManagedActorClassification) {
  def test(a: ActorRef): Unit = bus.unsubscribe(a) // error
}

The problem is that unsubscribe refers to the type Subscriber which is not resolvable as a member of bus. one idea could be to rule out type signatures like unsubscribe, similar how we rule out public signatures referring to private members. But this could rule out existing valid programs. For instance, the unsubscribe signature is unproblematic if it gets only called with prefixes that inherit ActorEventBus. You could say that the problem was instead that the type of bus was not specific enough.

In the long term, maybe restricting the signature is the right move. But for now, we just try to give better error messages in the case of existing failures.

Fixes #11226

@odersky odersky marked this pull request as ready for review May 27, 2024 11:43
@odersky odersky requested a review from smarter May 27, 2024 11:45
@odersky odersky added the area:reporting Error reporting including formatting, implicit suggestions, etc label May 27, 2024
@odersky
Copy link
Contributor Author

odersky commented May 27, 2024

@jchyb I had to add another test to the BestEffortCompilation blacklist. Here the compiler survives a MissingType exception in Typer but continuing leads to the same exception being thrown without a handler that reports an error.

@smarter smarter assigned odersky and unassigned smarter May 28, 2024
odersky and others added 4 commits May 28, 2024 18:24
We run into problems when referring to a member of a self type of a class
that it not also a member of the class from outside via an asSeenFrom.

One example is in 11226.scala where we see:
```scala
trait ManagedActorClassification { this: ActorEventBus =>
  def unsubscribe(subscriber: Subscriber): Unit
}

class Unsubscriber(bus: ManagedActorClassification) {
  def test(a: ActorRef): Unit = bus.unsubscribe(a) // error
}
```
The problem is that `unsubscribe` refers to the type `Subscriber` which
is not resolvable as a member of `bus`. one idea could be to rule out
type signatures like `unsubscribe`, similar how we rule out public signatures
referring to private members. But this could rule out existing valid programs.
For instance, the `unsubscribe` signature is unproblematic if it gets only
called with prefixes that inherit `ActorEventBus`. You could say that the
problem was instead that the type of `bus` was not specific enough.

In the long term, maybe restructing the signature is the right move. But for
now, we just try to give better error messages in the case of existing failures.

Fixes scala#11226
@odersky odersky merged commit d1b51dc into scala:main May 28, 2024
19 checks passed
@odersky odersky deleted the fix-11226 branch May 28, 2024 20:20
@Kordyjan Kordyjan added this to the 3.5.1 milestone Jul 3, 2024
WojciechMazur added a commit that referenced this pull request Jul 9, 2024
Backports #20477 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:reporting Error reporting including formatting, implicit suggestions, etc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"cannot resolve reference to type" when type member of self-type appears in method type
3 participants