Skip to content

When summoning from inline match, things go wrong if a type parameter is not bound correctly #7250

Closed
@anatoliykmetyuk

Description

@anatoliykmetyuk

minimized code

import compiletime.erasedValue

sealed trait Foo
trait B[H] extends Foo

inline given f[T <: Foo]: T = inline erasedValue[T] match
  case _: B[a] => summon[a].asInstanceOf[T]

@main def Test = println(f[B[Int]])
Error
-- Error: ../pg/Main.scala:9:26 ------------------------------------------------
9 |@main def Test = println(f[B[Int]])
  |                         ^^^^^^^^^
  |                       Maximal number of successive inlines (32) exceeded,
  |                       Maybe this is caused by a recursive inline method?
  |                       You can use -Xmax-inlines to change the limit.
  | This location is in code that was inlined at Main.scala:7
  | This location is in code that was inlined at Main.scala:7
  | This location is in code that was inlined at Main.scala:7
  | This location is in code that was inlined at Main.scala:7
  | This location is in code that was inlined at Main.scala:7
  | This location is in code that was inlined at Main.scala:7
  | This location is in code that was inlined at Main.scala:7
  | This location is in code that was inlined at Main.scala:7
  | This location is in code that was inlined at Main.scala:7
  | This location is in code that was inlined at Main.scala:7
  | This location is in code that was inlined at Main.scala:7
  | This location is in code that was inlined at Main.scala:7
  | This location is in code that was inlined at Main.scala:7
  | This location is in code that was inlined at Main.scala:7
  | This location is in code that was inlined at Main.scala:7
  | This location is in code that was inlined at Main.scala:7
  | This location is in code that was inlined at Main.scala:7
  | This location is in code that was inlined at Main.scala:7
  | This location is in code that was inlined at Main.scala:7
  | This location is in code that was inlined at Main.scala:7
  | This location is in code that was inlined at Main.scala:7
  | This location is in code that was inlined at Main.scala:7
  | This location is in code that was inlined at Main.scala:7
  | This location is in code that was inlined at Main.scala:7
  | This location is in code that was inlined at Main.scala:7
  | This location is in code that was inlined at Main.scala:7
  | This location is in code that was inlined at Main.scala:7
  | This location is in code that was inlined at Main.scala:7
  | This location is in code that was inlined at Main.scala:7
  | This location is in code that was inlined at Main.scala:7
  | This location is in code that was inlined at Main.scala:7
  | This location is in code that was inlined at Main.scala:9
one error found
[error] Nonzero exit code returned from runner: 1
[error] (dotty-compiler / Compile / runMain) Nonzero exit code returned from runner: 1
[error] Total time: 9 s, completed Sep 18, 2019 4:01:59 PM

Also:

import compiletime.erasedValue

sealed trait Foo
trait A extends Foo
trait B[H] extends Foo

inline given f[T <: Foo]: T = inline erasedValue[T] match
  case _: A => new A{}.asInstanceOf[T]
  case _: B[a] => summon[a].asInstanceOf[T]

@main def Test = println(f[B[Int]])

Produces:

Exception in thread "main" java.lang.ClassCastException: Cannot cast to scala.Nothing
	at Main$package$.Test(Main.scala:8)
	at Test.main(Main.scala:11)

Line 8 is case _: A => new A{}.asInstanceOf[T].

It looks like a is bound to Nothing and the summoning of it goes into the first given definition it sees.

Possibly related: #6018.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions