Closed
Description
Compiler version
3.1.1
Minimized code
https://scastie.scala-lang.org/Baodp0IPRQWdkjwE2SgUYg
type Amount = Amount.Type
object Amount:
opaque type Type = BigDecimal
inline def apply(inline dec: BigDecimal): Type = dec
extension (self: Type)
inline def value: BigDecimal = self
inline def +(y: Type): Type = self + y
@main def r(): Unit =
val aa: Amount = Amount(1)
val ab: Amount = Amount(2)
val ac: Amount = Amount(2)
val as1: Amount = aa + ab
val as2: Amount = aa + ab + ac
println(s"aa + ab = ${as1}")
println(s"aa + ab = ${as2}")
Output
undefined: aa.+ # -1: TermRef(TermRef(NoPrefix,val aa),+) at inlining
9
Found: (ac : Playground.Amount)
Required: BigDecimal
Expectation
Either successfully compile or warn about recursive definition.
I am not sure the correct way to write
inline def +(y: Type): Type = self + y
is instead
inline def +(y: Type): Type = self.value + y.value
Activity
[-]Odd compiler message using opaque type and inline recursive-like definition[/-][+]undefined: aa.+ # -1: TermRef(TermRef(NoPrefix,val aa),+) at inlining[/+][-]undefined: aa.+ # -1: TermRef(TermRef(NoPrefix,val aa),+) at inlining[/-][+]Inlining in opaque type[/+]prolativ commentedon Mar 10, 2022
I guess this is somehow related to the ordering of compilation phases. Some phases related to inlining are before
ElimOpaque
and some are after it.nicolasstucki commentedon Apr 29, 2022
Minimized
Workaround
nicolasstucki commentedon Apr 29, 2022
It seems that by having the alias type we do not detect that we need to generate a refined proxy for
x
nicolasstucki commentedon Apr 29, 2022
is inlined with an unrefined proxy
Dealias arg type to detect opaque types
Dealias arg type to detect opaque types