Skip to content

Regression: class with no object shadows outer object #11853

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
japgolly opened this issue Mar 22, 2021 · 5 comments
Closed

Regression: class with no object shadows outer object #11853

japgolly opened this issue Mar 22, 2021 · 5 comments

Comments

@japgolly
Copy link
Contributor

Compiler version

3.0.0-RC1

Minimized code

sealed trait Children
object Children {
  sealed trait None extends Children
  sealed trait Varargs extends Children
}

sealed abstract class CtorType {
  type ChildrenType <: Children
}

object CtorType {
  final class X() extends CtorType {
    override type ChildrenType = Children.None
  }

  final class Children() extends CtorType {
    override type ChildrenType = Children.Varargs
  }
}

Output

-- Error: x.scala:13:33 --------------------------------------------------------
13 |    override type ChildrenType = Children.None
   |                                 ^^^^^^^^
   |        Reference to creator proxy for class Children in object CtorType
   |        shadows outer reference to object Children
-- Error: x.scala:17:33 --------------------------------------------------------
17 |    override type ChildrenType = Children.Varargs
   |                                 ^^^^^^^^
   |        Reference to creator proxy for class Children in object CtorType
   |        shadows outer reference to object Children
2 errors found

Expectation

It should compiles as it does in Scala 2.x.

@smarter
Copy link
Member

smarter commented Mar 22, 2021

The error message calls them creator proxy when they're now called constructor proxy so it's a bit confusing but this is a feature, not a bug: http://dotty.epfl.ch/docs/reference/other-new-features/creator-applications.html, every class gets a synthetic companion with a synthetic apply method, so the shadowing is unavoidable.

@som-snytt
Copy link
Contributor

The bug may be where the spec turns to their date in the darkened theater and whispers with menacing assurance:

and there is also no other value or method named C defined or inherited in the scope where C is defined.

@japgolly
Copy link
Contributor Author

@smarter Damn I thought the new auto-new-thingy worked by synthesising new X at call-site, not synthesising an object with an apply method like case classes. Maybe the doc was older when I last read it or maybe I'm just misremembering. np

What do you think about saying "companion object" in the error message instead? From a user's pov I understand that a case class results in a synthesised object and now I see that in Scala 3, the same thing happens for normal classes too. No point, and a bit confusing having a different name for the synthesised object. I can create a PR if you're happy with such a change?

@som-snytt
Copy link
Contributor

I also expected the new new was just new. I see it is rewritten early, says -Xprint:all.

@Jasper-M
Copy link
Contributor

Jasper-M commented Mar 23, 2021

But "constructor proxy companions cannot be used as values by themselves".

The documentation also says:

Scala case classes generate apply methods, so that values of case classes can be created using simple function application, without needing to write new.

Scala 3 generalizes this scheme to all concrete classes.

It's nice that Scala 3 thinks of the poor StackOverflow answerers who came in after all the high-reputation answers were already taken. Some nice generalizations with just enough differences to thoroughly confuse people should do the trick.

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

4 participants