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

StackOverflowError in an infinitely looping Conversion #22668

Open
kubukoz opened this issue Feb 25, 2025 · 0 comments
Open

StackOverflowError in an infinitely looping Conversion #22668

kubukoz opened this issue Feb 25, 2025 · 0 comments
Labels
area:reporting Error reporting including formatting, implicit suggestions, etc itype:bug

Comments

@kubukoz
Copy link
Contributor

kubukoz commented Feb 25, 2025

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.

@kubukoz kubukoz added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Feb 25, 2025
@Gedochao Gedochao added area:reporting Error reporting including formatting, implicit suggestions, etc and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Feb 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:reporting Error reporting including formatting, implicit suggestions, etc itype:bug
Projects
None yet
Development

No branches or pull requests

2 participants