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

Error tpe obtained for type alias defined in package object #12663

Open
kpodsiad opened this issue Oct 10, 2022 · 7 comments
Open

Error tpe obtained for type alias defined in package object #12663

kpodsiad opened this issue Oct 10, 2022 · 7 comments

Comments

@kpodsiad
Copy link

Issue originally raised in Metals - scalameta/metals#4495

Reproduction steps

Scala version: 2.13.8, 2.13.9, probably earlier ones too

Here's repo with reproduction.

a/package.scala

package object a {
  type FancyInt = Int
}

a/Main.scala

package a
object Main extends App {
  def foo(n: FancyInt, k: Int): Unit = {
    n
    k
  }
}

Problem

There are no completions for type alias
Screenshot 2022-10-07 at 07 28 13
Screenshot 2022-10-07 at 07 30 32

I initially thought that this is the problem with Metals but @tgodzik created a testcase with reproduction https://github.com/scalameta/metals/compare/main...tgodzik:metals:add-test?expand=1 which shows that this is the compiler issue.

This is what Metals gets from compiler:

CompletionProvider.scala:451 typeMembers: List(
  ScopeMember(
    sym = value n,
    tpe = <error>,
    accessible = true,
    viaImport = <empty>,
    aliasInfo = None
  ),
  ScopeMember(
    sym = value k,
    tpe = TypeRef(pre = ThisType(sym = package scala), sym = class Int, args = List()),
    accessible = true,
    viaImport = <empty>,
    aliasInfo = None
  ),
...

note tpe = <error> for n whereas k has correct tpe.

@tgodzik
Copy link

tgodzik commented Oct 10, 2022

It seems that the issue only happens if we usage the symbol within the same package.

@som-snytt
Copy link

som-snytt commented Oct 11, 2022

The REPL effect:

scala> :paste -raw
// Entering paste mode (ctrl-D to finish)

package object p { type I = Int }


// Exiting paste mode, now interpreting.


scala> p.I.
I         package

I cut/pasted a pc test.

package object p {
  type I = Int
}
package p {
  object Main extends App {
    def i: I = 42
    def f = i./*!*/
  }
}
reload: Snippet.scala

askTypeCompletion at Snippet.scala(8,14)
================================================================================
[response] askTypeCompletion at (8,14)
retrieved 205 members
[inaccessible] protected def num: Fractional[Double]
[inaccessible] protected def ord: Ordering[Double]
[inaccessible] protected def unifiedPrimitiveEquals(x: Any): Boolean
[inaccessible] protected def unifiedPrimitiveHashcode: Int
[inaccessible] protected[package lang] def clone(): Object
[inaccessible] protected[package lang] def finalize(): Unit
def !=(x: Byte): Boolean
def !=(x: Char): Boolean
def !=(x: Double): Boolean
def !=(x: Float): Boolean
def !=(x: Int): Boolean
def !=(x: Long): Boolean
def !=(x: Short): Boolean

@tgodzik
Copy link

tgodzik commented Oct 11, 2022

This only seem to happen when they are located within a separate file and the input to the compiler is an actual compiled classfile.

@som-snytt
Copy link

Thanks, the pc test "just works" when splitting the package object into its own "compilation round" and indeed shows no completions. I'll take a look despite/because not knowing anything about it.

+reload: Snippet.scala
+
+askTypeCompletion at Snippet.scala(5,14)
+================================================================================
+[response] askTypeCompletion at (5,14)
+retrieved 0 members
+
+================================================================================

@alexarchambault
Copy link

Just want to mention that the impact of this can be pretty bad when developing on some projects with Metals. Lots of hover / go-to-definition / completion were not working in coursier before coursier/coursier#2900. Same right now on the Mill codebase, that relies quite a bit on package objects.

@som-snytt
Copy link

I don't remember my previous comment, but I'll take a swing this week. By coincidence, I was going to do a Mill bounty but maybe this instead. I don't think anyone else has free time to spare? Free as in beer.

I just started trying out metals with vscode and wanted to learn how to build/test. I noticed that metals uses twitter as an announcement channel, even though it doesn't conform to the Scala code of conduct.

@tgodzik
Copy link

tgodzik commented Sep 23, 2024

Let me know if you need any help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants