Skip to content

valueOf does not work in inlined code #6241

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

Closed
odersky opened this issue Apr 5, 2019 · 6 comments
Closed

valueOf does not work in inlined code #6241

odersky opened this issue Apr 5, 2019 · 6 comments
Assignees

Comments

@odersky
Copy link
Contributor

odersky commented Apr 5, 2019

No description provided.

@odersky odersky changed the title valueOf crashes backend in inlined code valueOf does not work in inlined code Apr 5, 2019
@odersky odersky changed the title valueOf does not work in inlined code valueOf not work in inlined code Apr 5, 2019
@odersky
Copy link
Contributor Author

odersky commented Apr 5, 2019

object Test extends App {
  inline def v[T] = valueOf[T]

  println(v[String])
}

leads to

Unexpected tree in genLoad: TypeTree[TypeRef(ThisType(TypeRef(NoPrefix,module class lang)),class String)]/class dotty.tools.dotc.ast.Trees$TypeTree at: <56..56>
java.lang.RuntimeException: Unexpected tree in genLoad: TypeTree[TypeRef(ThisType(TypeRef(NoPrefix,module class lang)),class String)]/class dotty.tools.dotc.ast.Trees$TypeTree at: <56..56>
	at dotty.tools.backend.jvm.DottyBackendInterface.abort(DottyBackendInterface.scala:390)
	at dotty.tools.backend.jvm.BCodeBodyBuilder$PlainBodyBuilder.genLoad(BCodeBodyBuilder.scala:431)

The culprit is that synthesizeValueOf will succeed also if the argument is a non-singleton TypeRef but of course that can't work. This problem is fixed in #6218.

With the fix we get:

2 |  inline def v[T] = valueOf[T]  // error
  |                              ^
  |no implicit argument of type ValueOf[T] was found for parameter vt of method valueOf in object DottyPredef

That's still unsatisfactory. valueOf should work in inlined code. Its sibling constValue does, but we should have the functionality of both in one method.

The underlying problem is that valueOf demands an implicit unconditionally, so it can't work in inlined code, where the implicit is not yet available since the type is not yet specialized.

@odersky odersky changed the title valueOf not work in inlined code valueOf does not work in inlined code Apr 5, 2019
@odersky
Copy link
Contributor Author

odersky commented Apr 5, 2019

Here's a workaround:

      inline def valueOf2 = implicit match {
        case ev: ValueOf[T] => ev.value
      }

Question: Should valueOf be defined like this?

@milessabin
Copy link
Contributor

@odersky That looks OK to me ... I'm happy to test and PR that change.

@milessabin
Copy link
Contributor

No problem with that change, though it might be better to roll it in to #6218 so that a neg test can be added.

@odersky
Copy link
Contributor Author

odersky commented Apr 5, 2019

@milessabin OK, please go ahead!

@milessabin
Copy link
Contributor

@odersky as a separate PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants