Open
Description
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.