Skip to content

inner class of enum companion not visible from the enum, unless companion is declared after #13554

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

Closed
yuriy-yarosh opened this issue Sep 17, 2021 · 4 comments · Fixed by #13952
Milestone

Comments

@yuriy-yarosh
Copy link

yuriy-yarosh commented Sep 17, 2021

Compiler version

3.0.2 and 3.1.0-RC2

Minimized code

object StatusCode:
  enum Matcher:
    case ServerError extends Matcher
  end Matcher
end StatusCode

enum StatusCode(code: Int, m: StatusCode.Matcher):
  case InternalServerError extends StatusCode(500, StatusCode.Matcher.ServerError)
end StatusCode

object Main {
  def main(args: Array[String]): Unit = {
    println(StatusCode.InternalServerError)
  }
}

Output

-- Error:
7 |enum StatusCode(code: Int, m: StatusCode.Matcher):
  |                              ^^^^^^^^^^^^^^^^^^
  |                         type Matcher is not a member of object StatusCode

Expectation

When putting the object declaration after the enum declaration it does compile, like so

enum StatusCode(code: Int, m: StatusCode.Matcher):
  case InternalServerError extends StatusCode(500, StatusCode.Matcher.ServerError)
end StatusCode

object StatusCode:
  enum Matcher:
    case ServerError extends Matcher
  end Matcher
end StatusCode

object Main {
  def main(args: Array[String]): Unit = {
    println(StatusCode.InternalServerError)
  }
}

Which is really frustrating.

@bishabosha bishabosha changed the title Accessing Object fields from the Enum declaration depends on the order of declaration inner enum of enum companion not visible from the enum, unless companion is declared after Sep 18, 2021
@yuriy-yarosh
Copy link
Author

thanks for the more concise naming @bishabosha, glad that it got some attention.

@bishabosha bishabosha changed the title inner enum of enum companion not visible from the enum, unless companion is declared after inner class of enum companion not visible from the enum, unless companion is declared after Sep 19, 2021
@bishabosha
Copy link
Member

bishabosha commented Sep 19, 2021

minimised to just an inner class causing the issue:

object StatusCode:                                                                                               
  class Matcher

enum StatusCode(m: StatusCode.Matcher):
  case InternalServerError extends StatusCode(???)

again it is fixed by changing the order:

enum StatusCode(m: StatusCode.Matcher):
  case InternalServerError extends StatusCode(???)

object StatusCode:                                                                                               
  class Matcher

@anatoliykmetyuk
Copy link
Contributor

I traced it to this line (not the root cause):

https://github.com/lampepfl/dotty/blob/ee137ff4bd06139b343034221ef81dffb0fc9825/compiler/src/dotty/tools/dotc/core/SymDenotations.scala#L2042

info appears to be the type of the object StatusCode, and info.decls, the list of the declaration in that object, doesn't contain class Matcher. Probably caused by earlier incorrect typing of that companion object.

@odersky
Copy link
Contributor

odersky commented Oct 1, 2021

Yes, that describes the symptom, but not the cause.

odersky added a commit to dotty-staging/dotty that referenced this issue Oct 1, 2021
odersky added a commit to dotty-staging/dotty that referenced this issue Oct 1, 2021
bishabosha pushed a commit to dotty-staging/dotty that referenced this issue Nov 12, 2021
@Kordyjan Kordyjan added this to the 3.1.2 milestone Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants