Skip to content

Type inference involving getClass on private trait doesn't work #12974

Open
@palanga

Description

@palanga

Reproduction steps

On scala 2.13.13 (scala 3 infers correctly)

package style

object bug {

  private[style] trait Style

  private[style] class StyleSheet private (private val stylesByType: Map[Class[? <: Style], Style]) {
    /**
     * Add a new Style to this StyleSheet. If this StyleSheet already contains a
     * Style definition of the same type, the new value overrides the old one.
     */
    private def +(style: Style): StyleSheet = new StyleSheet(this.stylesByType + (style.getClass -> style))
  }

}

gives the following error:

[error] /bug/src/main/scala/style/bug.scala:12:98: type mismatch;
[error]  found   : (Class[?0], style.bug.Style) where type ?0
[error]  required: (Class[_ <: style.bug.Style], style.bug.Style)
[error]     private def +(style: Style): StyleSheet = new StyleSheet(this.stylesByType + (style.getClass -> style))
[error]                                                                                                  ^
[error] one error found
[error] (Compile / compileIncremental) Compilation failed

but if I remove the private[style] annotation on Style, or if I cast style.getClass as Class[Style], it compiles and works correctly.

Problem

I expect the compiler to infer the type correctly whether it is a private or non private trait. In scala 3, the compiler infers it correctly.

Metadata

Metadata

Assignees

Labels

existentialfixed in Scala 3This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/)help wantedtyper

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions