gpt4 book ai didi

Scala 3 方法在使用镜像时太大

转载 作者:行者123 更新时间:2023-12-05 04:38:32 26 4
gpt4 key购买 nike

当我使用scala 3 的Mirror 生成类型类列表时,出现异常。我知道这是方法大小的 jvm 的硬性限制,但我该如何规避这个问题。

ps:删除Data类的某些字段时可以,但还有其他解决方案吗?

信息

sbt: 1.6.0

斯卡拉:3.1.0

错误

scala.tools.asm.MethodTooLargeException:方法太大:parse/Main$。 ()V 编译时

堆栈跟踪

[error] scala.tools.asm.MethodTooLargeException: Method too large: parse/Main$.<clinit> ()V
[error] scala.tools.asm.MethodWriter.computeMethodInfoSize(MethodWriter.java:2087)
[error] scala.tools.asm.ClassWriter.toByteArray(ClassWriter.java:489)
[error] dotty.tools.backend.jvm.GenBCodePipeline$Worker2.getByteArray$1(GenBCode.scala:478)
[error] dotty.tools.backend.jvm.GenBCodePipeline$Worker2.addToQ3(GenBCode.scala:484)
[error] dotty.tools.backend.jvm.GenBCodePipeline$Worker2.run(GenBCode.scala:461)
[error] dotty.tools.backend.jvm.GenBCodePipeline.buildAndSendToDisk(GenBCode.scala:562)
[error] dotty.tools.backend.jvm.GenBCodePipeline.run(GenBCode.scala:525)
[error] dotty.tools.backend.jvm.GenBCode.run(GenBCode.scala:63)
[error] dotty.tools.dotc.core.Phases$Phase.runOn$$anonfun$1(Phases.scala:308)
[error] scala.collection.immutable.List.map(List.scala:246)
[error] dotty.tools.dotc.core.Phases$Phase.runOn(Phases.scala:309)
[error] dotty.tools.backend.jvm.GenBCode.runOn(GenBCode.scala:71)
[error] dotty.tools.dotc.Run.runPhases$4$$anonfun$4(Run.scala:261)
[error] scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
[error] scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
[error] scala.collection.ArrayOps$.foreach$extension(ArrayOps.scala:1323)
[error] dotty.tools.dotc.Run.runPhases$5(Run.scala:272)
[error] dotty.tools.dotc.Run.compileUnits$$anonfun$1(Run.scala:280)
[error] scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.scala:18)
[error] dotty.tools.dotc.util.Stats$.maybeMonitored(Stats.scala:68)
[error] dotty.tools.dotc.Run.compileUnits(Run.scala:289)
[error] dotty.tools.dotc.Run.compileSources(Run.scala:222)
[error] dotty.tools.dotc.Run.compile(Run.scala:206)
[error] dotty.tools.dotc.Driver.doCompile(Driver.scala:39)
[error] dotty.tools.xsbt.CompilerBridgeDriver.run(CompilerBridgeDriver.java:88)
[error] dotty.tools.xsbt.CompilerBridge.run(CompilerBridge.java:22)
[error] sbt.internal.inc.AnalyzingCompiler.compile(AnalyzingCompiler.scala:91)
[error] sbt.internal.inc.MixedAnalyzingCompiler.$anonfun$compile$7(MixedAnalyzingCompiler.scala:192)
[error] scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23)
[error] sbt.internal.inc.MixedAnalyzingCompiler.timed(MixedAnalyzingCompiler.scala:247)
[error] sbt.internal.inc.MixedAnalyzingCompiler.$anonfun$compile$4(MixedAnalyzingCompiler.scala:182)
[error] sbt.internal.inc.MixedAnalyzingCompiler.$anonfun$compile$4$adapted(MixedAnalyzingCompiler.scala:163)
[error] sbt.internal.inc.JarUtils$.withPreviousJar(JarUtils.scala:239)
[error] sbt.internal.inc.MixedAnalyzingCompiler.compileScala$1(MixedAnalyzingCompiler.scala:163)
[error] sbt.internal.inc.MixedAnalyzingCompiler.compile(MixedAnalyzingCompiler.scala:210)
[error] sbt.internal.inc.IncrementalCompilerImpl.$anonfun$compileInternal$1(IncrementalCompilerImpl.scala:528)
[error] sbt.internal.inc.IncrementalCompilerImpl.$anonfun$compileInternal$1$adapted(IncrementalCompilerImpl.scala:528)
[error] sbt.internal.inc.Incremental$.$anonfun$apply$5(Incremental.scala:177)
[error] sbt.internal.inc.Incremental$.$anonfun$apply$5$adapted(Incremental.scala:175)
[error] sbt.internal.inc.Incremental$$anon$2.run(Incremental.scala:461)
[error] sbt.internal.inc.IncrementalCommon$CycleState.next(IncrementalCommon.scala:116)
[error] sbt.internal.inc.IncrementalCommon$$anon$1.next(IncrementalCommon.scala:56)
[error] sbt.internal.inc.IncrementalCommon$$anon$1.next(IncrementalCommon.scala:52)
[error] sbt.internal.inc.IncrementalCommon.cycle(IncrementalCommon.scala:263)
[error] sbt.internal.inc.Incremental$.$anonfun$incrementalCompile$8(Incremental.scala:416)
[error] sbt.internal.inc.Incremental$.withClassfileManager(Incremental.scala:503)
[error] sbt.internal.inc.Incremental$.incrementalCompile(Incremental.scala:403)
[error] sbt.internal.inc.Incremental$.apply(Incremental.scala:169)
[error] sbt.internal.inc.IncrementalCompilerImpl.compileInternal(IncrementalCompilerImpl.scala:528)
[error] sbt.internal.inc.IncrementalCompilerImpl.$anonfun$compileIncrementally$1(IncrementalCompilerImpl.scala:482)
[error] sbt.internal.inc.IncrementalCompilerImpl.handleCompilationError(IncrementalCompilerImpl.scala:332)
[error] sbt.internal.inc.IncrementalCompilerImpl.compileIncrementally(IncrementalCompilerImpl.scala:420)
[error] sbt.internal.inc.IncrementalCompilerImpl.compile(IncrementalCompilerImpl.scala:137)
[error] sbt.Defaults$.compileIncrementalTaskImpl(Defaults.scala:2366)
[error] sbt.Defaults$.$anonfun$compileIncrementalTask$2(Defaults.scala:2316)
[error] sbt.internal.server.BspCompileTask$.$anonfun$compute$1(BspCompileTask.scala:30)
[error] sbt.internal.io.Retry$.apply(Retry.scala:46)
[error] sbt.internal.io.Retry$.apply(Retry.scala:28)
[error] sbt.internal.io.Retry$.apply(Retry.scala:23)
[error] sbt.internal.server.BspCompileTask$.compute(BspCompileTask.scala:30)
[error] sbt.Defaults$.$anonfun$compileIncrementalTask$1(Defaults.scala:2314)
[error] scala.Function1.$anonfun$compose$1(Function1.scala:49)
[error] sbt.internal.util.$tilde$greater.$anonfun$$u2219$1(TypeFunctions.scala:62)
[error] sbt.std.Transform$$anon$4.work(Transform.scala:68)
[error] sbt.Execute.$anonfun$submit$2(Execute.scala:282)
[error] sbt.internal.util.ErrorHandling$.wideConvert(ErrorHandling.scala:23)
[error] sbt.Execute.work(Execute.scala:291)
[error] sbt.Execute.$anonfun$submit$1(Execute.scala:282)
[error] sbt.ConcurrentRestrictions$$anon$4.$anonfun$submitValid$1(ConcurrentRestrictions.scala:265)
[error] sbt.CompletionService$$anon$2.call(CompletionService.scala:64)
[error] java.util.concurrent.FutureTask.run(FutureTask.java:266)
[error] java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
[error] java.util.concurrent.FutureTask.run(FutureTask.java:266)
[error] java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
[error] java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
[error] java.lang.Thread.run(Thread.java:748)
[error]
[error] stack trace is suppressed; run last Compile / compileIncremental for the full output
[error] (Compile / compileIncremental) scala.tools.asm.MethodTooLargeException: Method too large: parse/Main$.<clinit> ()V
[error] Total time: 2 s, completed 2022-1-5 14:11:19

代码

import scala.compiletime.*
import scala.deriving.Mirror

object Main extends App {

trait FromString[A] {
def convert(str: String): A
}
object FromString {
given FromString[Int] = (str) => str.toInt
given FromString[Double] = (str) => str.toDouble
given FromString[String] = (str) => str
}

inline def getTypeclassInstances[F[_], A <: Tuple]: List[F[Any]] =
inline erasedValue[A] match {
case _: EmptyTuple => Nil
case _: (head *: tail) =>
val headTypeClass =
summonInline[F[head]]
val tailTypeClasses =
getTypeclassInstances[F, tail]
headTypeClass.asInstanceOf[F[Any]] :: getTypeclassInstances[F, tail]
}

inline def summonInstancesHelper[F[_], A](using
m: Mirror.Of[A]
): List[F[Any]] =
getTypeclassInstances[F, m.MirroredElemTypes]

case class Data(
ip: String,
method: String,
uri: String,
protocal: String,
httpStatus: Int,
byteSent: Double,
reqLength: Double,
reqTime: Double,
respTime: Double,
referer: String,
device: String
)

val types =
summonInstancesHelper[FromString, Data]
println(types.mkString("\r\n"))
}

最佳答案

感谢@bishabosha,它得出结论:

引用:https://github.com/lampepfl/dotty/issues/14213

希望对其他人有帮助。

 inline def getTypeclassInstances[F[_], A <: Tuple]: List[F[Any]] =
inline erasedValue[A] match {
case _: EmptyTuple => Nil
case _: (head *: tail) =>
val headTypeClass =
summonInline[F[head]]
val tailTypeClasses =
getTypeclassInstances[F, tail]
- headTypeClass.asInstanceOf[F[Any]] :: getTypeclassInstances[F, tail]
+ headTypeClass.asInstanceOf[F[Any]] :: tailTypeClasses
}

虽然这仍然可能会因非常大的案例类而崩溃 - 也许您可以将 headTypeClass 和 tailTypeClasses 更改为 def 而不是 val。

 inline def getTypeclassInstances[F[_], A <: Tuple]: List[F[Any]] =
inline erasedValue[A] match {
case _: EmptyTuple => Nil
case _: (head *: tail) =>
//use def rather than val
def headTypeClass =
summonInline[F[head]]
def tailTypeClasses =
getTypeclassInstances[F, tail]
headTypeClass.asInstanceOf[F[Any]] :: tailTypeClasses
}

关于Scala 3 方法在使用镜像时太大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70588541/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com