Skip to content

StackOverflowError in an infinitely looping Conversion #22668

Open
@kubukoz

Description

@kubukoz

Compiler version

3.6.3

Minimized code

sealed trait ContextValue

trait Encoder[A] {
  def apply(a: A): Json
}

case class Json()

given Encoder[Int] = _ => Json()
given Encoder[Json] = a => a

extension [A: Encoder](a: A) def asJson: Json = summon[Encoder[A]](a)

object ContextValue:
  given foo[T: Encoder]: Conversion[T, ContextValue] = _.asJson

@main def demo = println(42: ContextValue)

Output

Exception in thread "main" java.lang.StackOverflowError
	at main$package$.asJson(main.scala:12)
	at ContextValue$.ContextValue$$$_$foo$$anonfun$1(main.scala:15)
	at ContextValue$$anon$1.apply(main.scala:15)
	at ContextValue$$anon$1.apply(main.scala:15)
	at ContextValue$.ContextValue$$$_$foo$$anonfun$1(main.scala:15)
	at ContextValue$$anon$1.apply(main.scala:15)
	at ContextValue$$anon$1.apply(main.scala:15)
	at ContextValue$.ContextValue$$$_$foo$$anonfun$1(main.scala:15)
	at ContextValue$$anon$1.apply(main.scala:15)
	at ContextValue$$anon$1.apply(main.scala:15)
	at ContextValue$.ContextValue$$$_$foo$$anonfun$1(main.scala:15)
	at ContextValue$$anon$1.apply(main.scala:15)
	at ContextValue$$anon$1.apply(main.scala:15)
	...

Expectation

Either compilation failure or a warning. I understand that the conversions internally compiles to

((_$2: T) => ContextValue.foo[Json](given_Encoder_Json).apply(main$package.asJson[T](_$2)(evidence$1)))

so there's an outer call to the same conversion, making it easy to detect... but it could be that it'd cause false positives. I leave it up to you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:reportingError reporting including formatting, implicit suggestions, etcitype:bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions