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

Compiler wrongly identify value classes during desugaring (dual of #21918) #21943

Open
hamzaremmal opened this issue Nov 13, 2024 · 2 comments
Open
Assignees
Labels
area:desugar Desugaring happens after parsing but before typing, see desugar.scala area:value-classes Issues tied to value classes. itype:bug Spree Suitable for a future Spree

Comments

@hamzaremmal
Copy link
Member

Compiler version

bed0e86

Minimized code

class AnyVal
class Foo extends AnyVal

Output

During typer, the desugaring assumes we are implementing a value class and generates the companion object for Foo

[[syntax trees at end of                     typer]] // t.scala
package <empty> {
  class AnyVal() extends Object() {}
  class Foo() extends AnyVal() {}
  final lazy module val Foo: Foo = new Foo()
  final module class Foo() extends AnyRef() { this: Foo.type =>}
}

Expectation

Should behave the same way as extending any other reference class:

[[syntax trees at end of                     typer]] // t.scala
package <empty> {
  class Anyval() extends Object() {}
  class Foo() extends Anyval() {}
}

## Note

This is has the same root problem as #21918. In #21918, it was a case where isAnyVal wrongly unidentify value classes (false negative) while this issue is the case where the same check wrongly identify value classes (false positive).

def isAnyVal(tree: Tree): Boolean = tree match {
case Ident(tpnme.AnyVal) => true
case Select(qual, tpnme.AnyVal) => isScala(qual)
case _ => false
}

@hamzaremmal hamzaremmal added itype:bug area:desugar Desugaring happens after parsing but before typing, see desugar.scala Spree Suitable for a future Spree area:value-classes Issues tied to value classes. labels Nov 13, 2024
@noti0na1
Copy link
Member

I think a class extending AnyVal with no parameter should be illegal: https://docs.scala-lang.org/sips/value-classes.html

@hamzaremmal
Copy link
Member Author

I think a class extending AnyVal with no parameter should be illegal: https://docs.scala-lang.org/sips/value-classes.html

It is illegal, the point of the issue is that dusagring wrongly identifies value classes and generates unnecessary extra code.

@hamzaremmal hamzaremmal self-assigned this Nov 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:desugar Desugaring happens after parsing but before typing, see desugar.scala area:value-classes Issues tied to value classes. itype:bug Spree Suitable for a future Spree
Projects
None yet
Development

No branches or pull requests

2 participants