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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove unsafe.Tag based type resolution for CFuncPtr.{fromScalaFunction,apply} #3270
Remove unsafe.Tag based type resolution for CFuncPtr.{fromScalaFunction,apply} #3270
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good so far, I've left some comments how we can simplify it a bit.
Regarding to problems with local publishing, it is probably due to usage of nativeLink
task in root project. I'm not sure where does it comes from, probably we could inspect the build and override it. Due to complexity of the build (we don't have typical crossVersion but use project duplicates for each Scala binary version), and the fact that there are cases where we need to either always depend on Scala 2.12 (sbtPlugin) or Scala 2.13 (scalalib for Scala 3) running tasks in root project is not recommended. To target a single project you can use one of dedicated subprojects, eg. tests2_13
, tests3
, etc.
If you want to publish all projects locally you can use a custom command publish-local-dev <scala-version>
case Apply(TypeApply(fun, tArgs), args) if CFuncPtrFromFunctionMethods.contains(fun.symbol) => | ||
val idx = CFuncPtrFromFunctionMethods.indexOf(fun.symbol) | ||
val transformed = _CFuncPtrFromFunctionMethods(idx) | ||
val tys = tArgs.map(t => widenDealiasType(t.tpe)) | ||
typer | ||
.typed { | ||
Apply(transformed, args: _*) | ||
} | ||
.updateAttachment(NonErasedTypes(tys)) | ||
.setPos(tree.pos) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this attachment is not used anywhere (at least in Scala2), we use only attachment for CFuncPtrApply
. It means to can safely remove handling fromScalaFunction
and it's erased variant _fromScalaFunction
.
It made me think about how we can achiv the same in Scala 3. I'll describe below how to get rid of this attachment for Scala3 version of this method
case Apply(TypeApply(fun, tArgs), args) if defnNir.CFuncPtr_fromScalaFunction.contains(fun.symbol) => | ||
val idx = defnNir.CFuncPtr_fromScalaFunction.indexOf(fun.symbol) | ||
val transformed = defnNir._CFuncPtr_fromScalaFunction(idx) | ||
val cls = defnNir.CFuncPtrNClass(idx) | ||
val tys = tArgs.map(t => dealiasTypeMapper(t.typeOpt)) | ||
cpy | ||
.Apply(tree)( | ||
ref(transformed), | ||
args | ||
) | ||
.withAttachment(NirDefinitions.NonErasedTypes, tys) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can add attachment to outer Apply
without transformation of inner TypeApply
. This combined with the fact that on Scala 2 it's not needed as well, would allow to eliminate additional _fromScalaFunction
method.
In Scala2 this transformation was unused, but in Scala3 we need to get original type and save it in attachment, because the target of a lambda might get replaced with adapted (boxed and erased) variant of a function. https://github.com/lampepfl/dotty/blob/abf9a25d5f27a41c11c11c11cf671ed3cb33d10d/compiler/src/dotty/tools/dotc/transform/Erasure.scala#L409-L439
case Apply(TypeApply(fun, tArgs), args) if defnNir.CFuncPtr_fromScalaFunction.contains(fun.symbol) => | |
val idx = defnNir.CFuncPtr_fromScalaFunction.indexOf(fun.symbol) | |
val transformed = defnNir._CFuncPtr_fromScalaFunction(idx) | |
val cls = defnNir.CFuncPtrNClass(idx) | |
val tys = tArgs.map(t => dealiasTypeMapper(t.typeOpt)) | |
cpy | |
.Apply(tree)( | |
ref(transformed), | |
args | |
) | |
.withAttachment(NirDefinitions.NonErasedTypes, tys) | |
case appy @ Apply(TypeApply(fun, tArgs), _) if defnNir.CFuncPtr_fromScalaFunction.contains(fun.symbol) => | |
val tys = tArgs.map(t => dealiasTypeMapper(t.typeOpt)) | |
app | |
.withAttachment(NirDefinitions.NonErasedTypes, tys) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! _fromScalaFunction
was a thing I don't like in this change and I'm glad we can get rid of that!
done cc62ca2
but in Scala3 we need to get original type and save it in attachment, because the target of a lambda might get replaced with adapted (boxed and erased) variant of a function.
Oh, that's why we didn't need this in Scala2, while Scala3 does. That makes sense, thank you for the explanation!
implicit def fromScalaFunction[${allTps}](fn: ${FunctionN})(implicit ${evidences}): ${CFuncPtrN} = intrinsic | ||
implicit def fromScalaFunction[${allTps}](fn: ${FunctionN}): ${CFuncPtrN} = intrinsic | ||
|
||
def _fromScalaFunction(fn: ${FunctionNWildcard}): ${CFuncPtrNWildcard} = intrinsic |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can get rid of these, see comments below
This reverts commit f86daba. Reverting this change for now because it's not related to the pull request that refactor scala-native not to use unsafe.Tag based type preservation.
164c42f
to
cc83f93
Compare
Regarding the auxlib, it seems like the same problem as #3265 and I do workaround by publishing locally for another version of scala 2.12 It seems like the linking problems are caused by my contaminated local environment (by |
There's no longer a special scripts like `scripts/publish-local`. ScalaNative is required to depend on multiple Scala versions e.g. Scala 2.12 (sbtPlugin) or Scala 2.13 (scalalib for Scala 3) and just running `++x.y.z publishLocal` on root project is not recommended. Instead, we can use `publish-local-dev` custom task in sbt. see: scala-native#3270 (review)
There's no longer a special scripts like `scripts/publish-local`. ScalaNative is required to depend on multiple Scala versions e.g. Scala 2.12 (sbtPlugin) or Scala 2.13 (scalalib for Scala 3) and just running `++x.y.z publishLocal` on root project is not recommended. Instead, we can use `publish-local-dev` custom task in sbt. see: scala-native#3270 (review)
There's no longer a special scripts like `scripts/publish-local`. ScalaNative is required to depend on multiple Scala versions e.g. Scala 2.12 (sbtPlugin) or Scala 2.13 (scalalib for Scala 3) and just running `++x.y.z publishLocal` on root project is not recommended. Instead, we can use `publish-local-dev` custom task in sbt. see: #3270 (review)
There's no longer a special scripts like `scripts/publish-local`. ScalaNative is required to depend on multiple Scala versions e.g. Scala 2.12 (sbtPlugin) or Scala 2.13 (scalalib for Scala 3) and just running `++x.y.z publishLocal` on root project is not recommended. Instead, we can use `publish-local-dev` custom task in sbt. see: #3270 (review) (cherry picked from commit 713593a)
There's no longer a special scripts like `scripts/publish-local`. ScalaNative is required to depend on multiple Scala versions e.g. Scala 2.12 (sbtPlugin) or Scala 2.13 (scalalib for Scala 3) and just running `++x.y.z publishLocal` on root project is not recommended. Instead, we can use `publish-local-dev` custom task in sbt. see: #3270 (review) (cherry picked from commit 713593a)
Fix #3220
This pull request refacts the handling of
CFuncPtr.{fromscalaFunction,apply}
by preserving the exact type information during the type erasure phase and referencing it in the NIR generation phase. This is the same approach assizeOf[T]
andalignmentOf[T]
#3198.Previously, we restored the type information based on the `unsafe.Tag' proof types, which is a bit buggy.
This PR includes the following changes
CFuncPtr#apply
andCFuncPtr.fromScalaFunction
whose values are their type information (parameter types and return type).NIRGenExpr.scala
), get the attached exact type information and use it to generate external forwarder methods (previously we used the type information restored based onunsafe.Tag
).NirGenUtil.unwrap{ClassTag,Tag}
and Tags related code inNirDefinitions
.nativelib/src/main/scala/scalanative/unsafe.CFuncPtr.scala.gyb
to remove unsedevidences
types.finished todo
TODOs
unwrapTag
from NirGenUtil`tests2_13 / Test
test3 / Test
test2_12 / Test
auxlib failed to build with this patchnevermind, it seems like it my local environment probem.++3.2.2 publishLocal
ok++2.12.17 publishLocal
++2.13.10 publishLocal
~ it seems like nativeLink always fail with the following error while tests pass.~ it's also my local environment problem. nevermind