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

Fix pkg obj prefix of opaque tp ext meth #21527

Merged
merged 2 commits into from
Nov 12, 2024

Conversation

dwijnand
Copy link
Member

@dwijnand dwijnand commented Sep 1, 2024

TypeOps.makePackageObjPrefixExplicit is a part of accessibleType,
which is called on the result of findRef in typedIdent. But in
tryExtension it's not. We could fix it in the usage of the results in
tryExtension, but I thought perhaps we could fix it for all call
sites, by handling it within findRef.

Fixes #18097

@dwijnand dwijnand marked this pull request as ready for review September 2, 2024 01:08
@dwijnand dwijnand requested a review from odersky September 2, 2024 01:08
Copy link
Contributor

@odersky odersky left a comment

Choose a reason for hiding this comment

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

I think this is fine but could profit from a refactoring.

  • Move makePackageObjPrefixExplciit from TypeOps to TypeUtils and make it an extension method on Type
  • Drop the extension method in findRef.

@odersky odersky assigned dwijnand and unassigned odersky Sep 23, 2024
@dwijnand dwijnand force-pushed the fix-opaque-ext-meth branch from d53c798 to 3b06cad Compare November 6, 2024 15:35
There is use of `makePackageObjPrefixExplicit` within `accessibleType`,
which is called on the result of findRef in typedIdent.  But in
`tryExtension` it's no such use.  We could fix it in the usage of the
results in `tryExtension`, but I thought perhaps we could fix it for all
call sites, by handling it within findRef.
@dwijnand dwijnand force-pushed the fix-opaque-ext-meth branch from 3b06cad to 7db83c5 Compare November 12, 2024 11:22
@dwijnand dwijnand merged commit 896965c into scala:main Nov 12, 2024
29 checks passed
@dwijnand dwijnand deleted the fix-opaque-ext-meth branch November 12, 2024 14:10
@WojciechMazur
Copy link
Contributor

@dwijnand
2 projects in OpenCB started to fail after including these changes.
It seems like previously opaque type rhs type could have been interpreted as opaque type.
I think it now works as expected, but please confirm it based on the following reproducers. Probably it would be useful to include these cases in the compiler tests to prevent restoring old behaviour in the future.

zainab-ali/aquascape

Build logs

abstract class Ref[F[_], A]

trait FunctorOps[F[_], A]:
  def map[B](f: A => B): F[B] = ???

type Branch = String
type Label = String
opaque type Stack[F[_]] = Ref[F, Map[Branch, List[Label]]]
extension [F[_]](stack: Stack[F])
  def bracketF[A](branch: Branch, child: Label)(fa: F[A]): F[A] = ???

trait Pen[F[_], E]:
  def bracket[A](branch: Branch, child: Label)(fa: F[A]): F[A]

object Pen {
  def apply[F[_], E]: F[Pen[F, E]] =
    null
    .asInstanceOf[FunctorOps[F, Ref[F, Map[Branch, List[Label]]]]] // fails
    // .asInstanceOf[FunctorOps[F, Stack[F]]] // works
    .map { stack =>
      new Pen[F, E] {
        def bracket[A](branch: Branch, child: Label)(fa: F[A]): F[A] = stack.bracketF(branch, child)(fa)
      }
    }
}

yields

[error] ./test.scala:22:72
[error] value bracketF is not a member of Ref[F, Map[Branch, List[Label]]].
[error] An extension method was tried, but could not be fully constructed:
[error] 
[error]     bracketF[F](stack)
[error] 
[error]     failed with:
[error] 
[error]         Found:    (stack : Ref[F, Map[Branch, List[Label]]])
[error]         Required: Stack[F²]
[error]         
[error]         where:    F  is a type in method apply with bounds <: [_] =>> Any
[error]                   F² is a type variable with constraint <: [_²] =>> Any
[error]         
[error] 
[error] where:    F is a type in method apply with bounds <: [_] =>> Any
[error]         def bracket[A](branch: Branch, child: Label)(fa: F[A]): F[A] = stack.bracketF(branch, child)(fa)
[error]      

bilal-fazlani/zio-maelstrom

Build logs

// defs.scala
opaque type NodeId = String
opaque type MessageSource = NodeId
extension (s: MessageSource)
  def nodeId: NodeId = s
// usage.scala
private def suspend(remote: NodeId) = remote.nodeId
class Logic:
  private def suspend(remote: NodeId) = remote.nodeId

yields

-- [E008] Not Found Error: /Users/wmazur/projects/scala/sandbox/.zio-mealstrom/usage.scala:3:47 -----------------------------------------------------------------------------------------------
3 |  private def suspend(remote: NodeId) = remote.nodeId
  |                                        ^^^^^^^^^^^^^
  |                                        value nodeId is not a member of NodeId.
  |                                        An extension method was tried, but could not be fully constructed:
  |
  |                                            nodeId(remote)
  |
  |                                            failed with:
  |
  |                                                Found:    (remote : NodeId)
  |                                                Required: MessageSource
-- [E008] Not Found Error: /Users/wmazur/projects/scala/sandbox/.zio-mealstrom/usage.scala:1:45 -----------------------------------------------------------------------------------------------
1 |private def suspend(remote: NodeId) = remote.nodeId
  |                                      ^^^^^^^^^^^^^
  |                                      value nodeId is not a member of NodeId.
  |                                      An extension method was tried, but could not be fully constructed:
  |
  |                                          nodeId(remote)
  |
  |                                          failed with:
  |
  |                                              Found:    (remote : NodeId)
  |                                              Required: MessageSource

@dwijnand
Copy link
Member Author

Yes, that all looks correct. I'll PR the cases, thank you!

@WojciechMazur
Copy link
Contributor

FYI @zainab-ali @bilal-fazlani

@WojciechMazur WojciechMazur added this to the 3.6.3 milestone Nov 25, 2024
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.

top level extension method dealiases opaque type in result type
3 participants