scala / scala3

The Scala 3 compiler, also known as Dotty.
https://dotty.epfl.ch
Apache License 2.0
5.83k stars 1.05k forks source link

value xx is not a member of Array #18591

Closed cjdxhjj closed 1 year ago

cjdxhjj commented 1 year ago

Compiler version

3.3.1 If you're not sure what version you're using, run print scalaVersion from sbt (if you're running scalac manually, use scalac -version instead).

Minimized code

class CreateBrandCommand(@BeanProperty val id: Long, @BeanProperty val name: String, @BeanProperty val logo: String, @BeanProperty val scope: Int, @BeanProperty val groupId: Long, @BeanProperty val orgId: Long, @BeanProperty val tertiary: Long, @BeanProperty val tertiaryType: Int, @BeanProperty val isEnable: Boolean, @BeanProperty val source: Int, @BeanProperty val creator: Long, @BeanProperty val description: String)

def addBrandBatch(brandList: Array[CreateBrandCommand])(using Trace, ExecutionContext): Task[ActionListResult[Brand]] = {
    ZIO.ifZIO(queryGateway.zioOne[Boolean](new IsOrgGroupMatchQuery(brandList.map(x => new OrgRecord(x.orgId, x.groupId)))))({
      ZIO.ifZIO(queryGateway.zioOne[Boolean](new CanCreateBrandsQuery(brandList.map(x => new BrandGroupName(x.groupId, x.name)))))({
        ZIO.ifZIO({
          val idList = brandList.map(_.id).filter(_ > 0).distinct
          if (idList.isEmpty) ZIO.succeed(false) else queryGateway.zioOne[Boolean](new IsAnyBrandExistQuery(idList))
        })(ZIO.succeed(ActionListResult(23003, List(), "部分品牌已存在")), commandGateway.zioMsg[ActionListResult[Brand]](new BatchCreateBrandCommand(brandList)))
      }, ZIO.succeed(ActionListResult(23002, List(), "品牌名称重复")))
    }, ZIO.succeed(ActionListResult(23001, List(), "集团与现有设置不匹配")))
  }

image image

Output

value orgId is not a member of Array[com.nxin.product.domain.brand.command.CreateBrandCommand]

// TODO add output here

why the complier treat x as Array[CreateBrandCommand] ?

Expectation

WojciechMazur commented 1 year ago

It this problem only visible in the InteliJ or is it only present in normal compilation using gradle/sbt/mill or other build tool? InteliJ has it's own presentation compiler, which might be bugged - might show errors for code which would compile correctly when using actual Scala compiler.

If possible try to compile it using the gradle cli to check if it's just the InteliJ issue. Also, make sure to use the latest version of InteliJ + Scala Plugin as it follows rapid development cycle and typically it's recommended to stay on their bleeding edge versions.

cjdxhjj commented 1 year ago

i'm using gradle 8.3, when i use the gradle cli, it report the same error image image image i'm wanted to upgrade from scala 2.13.11 to 3.3.1, but it alway report that error,

cjdxhjj commented 1 year ago

the issure appear half a year, i have tried scala 3.0, 3.1,3.2, jdk 11, jdk 17, zio 1.16, zio 2, i hava no idea to slove that problem, the error may cause by other mistake, but the error report is error

cjdxhjj commented 1 year ago

the project work fine on scala 2.13

cjdxhjj commented 1 year ago

my idea is 2023.2.2 and scala plugin is 2023.2.23

He-Pin commented 1 year ago

@cjdxhjj is it possible for you to minimalize it?

cjdxhjj commented 1 year ago

@He-Pin yes, i will try to provider an demo, but i'm not sure i could provider it

cjdxhjj commented 1 year ago

@He-Pin i can't provider an minimalize demo to repeat that complie error, i guess some other thing went wrong, but the compile report the wrong error scala3test.zip

cjdxhjj commented 1 year ago

image

cjdxhjj commented 1 year ago

@WojciechMazur there is any suggest, the project is write by scala 2.13 for three years age, i have upgrade the zio to 2.17, when i upgrade scala from 2 to 3, the error appear.

He-Pin commented 1 year ago

@cjdxhjj It would be better you have a simple code to demo this, you can post it with text block,better with scala-cli.

你把这个问题搞个小文件复现下,这样大家都好看。比如 scala ... 这样的代码块儿。你现在这个好多自己公司的项目中的类,别个怎么复现呢.

WojciechMazur commented 1 year ago

@cjdxhjj Thank you for publishing the code, but it also is working fine on my machine. I really can't tell what might be happening here without the working reproducer leading to compilation error. I doesn't look like issue with the Scala typer, unless there is happening something very strange. You can try to check if there is no imports/package definitions that might shadow the scala.Predef.Array in some way or you can try to use this type directly in the failing method just to be sure.

cjdxhjj commented 1 year ago

@WojciechMazur the demo work fine, but my project build error, the error report point out the code error look like correct

cjdxhjj commented 1 year ago

@He-Pin @WojciechMazur i have copy my project and delete some module, the error report again

SethTisue commented 1 year ago

@cjdxhjj your repo has about 1000 lines of Scala in it. nearly any compiler issue can be demonstrated in about 5–20 lines of code. it's very rare for a minimal reproduction to require 50, let alone 1000

for guidance on what constitutes a minimal reproduction of a bug, see https://stackoverflow.com/help/minimal-reproducible-example

cjdxhjj commented 1 year ago

@SethTisue i will try to reduce some code, but i can't figure out what code cause the problem,

cjdxhjj commented 1 year ago

@SethTisue , @WojciechMazur, @He-Pin ,after delete some unused class.

scala3p.zip

WojciechMazur commented 1 year ago

What happened here is the implicit conversion, in this case it's PreDef.futureWrapper

The following code:

import com.example.domain.shared.PreDef.*

class Foo {
  def addBrandBatch2(brandList: Array[CreateBrandCommand]): ActionListResult[Brand] = {
     brandList.map(x =>x.orgId)
     ???
  }
}

is being converted to

class Foo() extends Object() {
    def addBrandBatch2(
      brandList: Array[com.example.domain.brand.command.CreateBrandCommand]):
      com.example.domain.shared.entity.ActionListResult[
        com.example.domain.brand.entity.Brand]
     =
      {
        com.example.domain.shared.PreDef.futureWrapper[
          Array[com.example.domain.brand.command.CreateBrandCommand]](brandList)
          .map[Any](
          {
            def $anonfun(
              x: Array[com.example.domain.brand.command.CreateBrandCommand]):
              Any = x.orgId
            closure($anonfun)
          }
        )(/* missing */summon[scala.concurrent.ExecutionContext])
        ???
      }
  } 

You should build your project with -feature flag to get more info about where implicit conversion is used in your code. To get the similar implicits semantics as in Scala 2 use Conversion type class instead


  given futureWrapper[T]: Conversion[T, Future[T]] = Future.successful(_)
  // implicit def futureWrapper[T](data: => T): Future[T] = Future.successful(data)

With this change project compiles

SethTisue commented 1 year ago

if you have more questions like this in the future, https://users.scala-lang.org is a better place. https://scala-lang.org/community/ has some other suggestions for places to get help with Scala

to report a bug here in the issue tracker, you should first be reasonably certain that a compiler bug is the cause, and you should be prepared to make an argument and show evidence that's the case

cjdxhjj commented 1 year ago

thanks very much, @SethTisue @SethTisue