首页 > 解决方案 > 使用 scalatest MustMatchers 和 Await 改进的 Scala 2.11 导致编译器错误“尝试执行类型变量的 lub/glb ?F[?T,?B]”

问题描述

我一直在尝试使用精炼的 scalatest 并且在“typer”阶段遇到编译器错误:trying to do lub/glb of typevar ?F[?T, ?B]

这是我使用独立的菊石脚本对问题进行极简再现的最佳尝试:

import $ivy.`eu.timepit::refined:0.9.0`
import $ivy.`org.scalatestplus.play::scalatestplus-play:3.1.2`

import org.scalatest.{MustMatchers, WordSpec}

import scala.concurrent.duration._
import scala.concurrent.{Await, Future}

import eu.timepit.refined.auto.autoInfer

class RefinedSpec extends WordSpec with MustMatchers {
  val duration = 500.millis
  val fut = Future.successful("123")
  Await.result(fut, atMost = duration)
}

如果您对上述内容进行以下任何一项更改,它将成功编译:


澄清一下,这是一个编译错误,而不是运行时错误。在 sbt 中运行时,最初的错误发生在 play 应用程序(scala 2.11.11)test:compile中,但使用 ammonite 脚本可能更容易重现它。

我使用的菊石版本提供了版本信息:

Welcome to the Ammonite Repl 1.1.2                                                                                    
(Scala 2.11.12 Java 1.8.0_25)

安装使用:

sudo sh -c '(echo "#!/usr/bin/env sh" && curl -L https://github.com/lihaoyi/Ammonite/releases/download/1.1.2/2.11-1.1.2) > /usr/local/bin/amm && chmod +x /usr/local/bin/amm' && amm

ammonite 示例中的更多错误详细信息:

scala.reflect.internal.FatalError:
  trying to do lub/glb of typevar ?F[?T, ?B]
     while compiling: fail.sc
        during phase: typer
     library version: version 2.11.12
    compiler version: version 2.11.12
  reconstructed args: -nowarn -Yresolve-term-conflict:object

  last tree to typer: Ident(<argument>)
       tree position: line 15 of fail.sc
            tree tpe: String
              symbol: <none>
   symbol definition: <none> (a NoSymbol)
      symbol package: <none>
       symbol owners:
           call site: class RefinedSpec in object fail in package $file

从 play sbttest:compile我也得到这种输出:

[error]   last tree to typer: Ident(<argument>)
[error]        tree position: line 13 of ...../RefinedSpec.scala
[error]             tree tpe: String
[error]               symbol: <none>
[error]    symbol definition: <none> (a NoSymbol)
[error]       symbol package: <none>
[error]        symbol owners: 
[error]            call site: class RefinedSpec in package foo in package foo
[error] 
[error] == Source file context for tree position ==
[error] 
[error]     10   val duration = 500.millis
[error]     11   val fut = Future.successful("123")
[error]     12   Await.result(fut, atMost = duration)
[error]     13 }

这不是一个严重的问题,因为我可以删除autoInfer导入,因为我实际上并没有使用它。但它会绊倒人们,因为他们倾向于这样做:

import eu.timepit.refined.auto._

从编译时常量自动转换为精炼类型,当他们可能只是逃避:

import eu.timepit.refined.auto.autoRefineV

标签: scalatestscala-macrosscala-2.11scala-compilerrefined

解决方案


推荐阅读