scala / scala3

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

Nonsensical imports treated cause weird error messages #19413

Open keynmol opened 10 months ago

keynmol commented 10 months ago

Compiler version

3.3.1

Minimized code


import hello.bla.*

@main def hello = 
  test(1)

object kla:
  def ra = ???

import kla.bla.ra1
@main def test = 
  ra1()

Output

Compiling project (Scala 3.3.1, JVM (17))
[error] ./test.scala:10:12
[error] Cyclic reference involving val <import>
[error] import kla.bla.ra1
[error]            ^
Error compiling project (Scala 3.3.1, JVM (17))

Expectation

I would expect hello.bla import highlighted first, as it makes no sense - import from a function But if you comment out everything after the hello function, you get only "test not found" error.

The "Cyclic reference" error is also confusing.

som-snytt commented 10 months ago

Currently, the error is completely anonymous.

This is clean build at Scala compiler version 3.4.0-RC1-bin-SNAPSHOT-git-8039426 -- Copyright 2002-2024, LAMP/EPFL

[[syntax trees at end of                     typer]] // i19413.scala
package <empty> {
  import hello.bla.*
  final lazy module val kla: kla = new kla()
  final module class kla() extends Object() { this: kla.type =>
    def ra: <error unspecified error> = <error unspecified error>#bla.???
  }
  import kla.bla.ra1
  final lazy module val i19413$package: i19413$package = new i19413$package()
  final module class i19413$package() extends Object() { this: i19413$package.type =>
    @main def hello: <error unspecified error> = test()
    @main def test: <error unspecified error> = <error Cyclic reference involving val <import>>#ra1()
  }
}

[typer  in 586ms]
1 error found

Full disclosure

$ ~/projects/dotty/bin/scalac i19413.scala
1 error found
bishabosha commented 10 months ago

in 3.4.0-RC1 repl I get this error:

-- [E008] Not Found Error: -----------------------------------------------------
10 |import kla.bla.ra1
   |       ^^^^^^^
   |       value bla is not a member of object kla
1 error found

if I remove the import and incorrect call to test now I have another error which also makes sense, but its different, it looks like it tried to apply an extension method?

scala> import hello.bla.*
     | 
     | @main def hello = 
     |   ???
     | 
     | object kla:
     |   def ra = ???
     | 
     | //import kla.bla.ra1
     | @main def test = 
     |   ???
     | 
-- [E007] Type Mismatch Error: -------------------------------------------------
1 |import hello.bla.*
  |       ^^^^^
  |Found:    (hello : => Nothing)
  |Required: ?{ bla: ? }
  |Note that implicit conversions were not tried because the result of an implicit conversion
  |must be more specific than ?{ bla: a type that can be selected or applied }
  |
  | longer explanation available when compiling with `-explain`
1 error found
som-snytt commented 8 months ago

On the command line now,

➜  snips ~/projects/dotty/bin/scalac -version
Scala compiler version 3.4.2-RC1-bin-SNAPSHOT-git-4859415 -- Copyright 2002-2024, LAMP/EPFL
➜  snips ~/projects/dotty/bin/scalac -d /tmp/sandbox i19413.scala
1 error found
➜  snips ~/projects/dotty/bin/scalac -Vprint:typer -d /tmp/sandbox i19413.scala
[[syntax trees at end of                     typer]] // i19413.scala
package <empty> {
  import hello.bla.*
  final lazy module val kla: kla = new kla()
  final module class kla() extends Object() { this: kla.type =>
    def ra: <error unspecified error> = <error unspecified error>#bla.???
  }
  import kla.bla.ra1
  final lazy module val i19413$package: i19413$package = new i19413$package()
  final module class i19413$package() extends Object() { this: i19413$package.type =>
    @main def hello: <error unspecified error> = test()
    @main def test: <error unspecified error> =
      <error Cyclic reference involving val <import>

 Run with -explain-cyclic for more details.>#ra1()
  }
}

1 error found
➜  snips ~/projects/dotty/bin/scalac -explain-cyclic -Vprint:typer -d /tmp/sandbox i19413.scala
[[syntax trees at end of                     typer]] // i19413.scala
package <empty> {
  import hello.bla.*
  final lazy module val kla: kla = new kla()
  final module class kla() extends Object() { this: kla.type =>
    def ra: <error unspecified error> = <error unspecified error>#bla.???
  }
  import kla.bla.ra1
  final lazy module val i19413$package: i19413$package = new i19413$package()
  final module class i19413$package() extends Object() { this: i19413$package.type =>
    @main def hello: <error unspecified error> = test()
    @main def test: <error unspecified error> =

        <error Cyclic reference involving val <import>

The error occurred while trying to compute the signature of import hello.bla
  which required to compute the signature of method hello
  which required to compute the signature of method test
  which required to compute the signature of import kla.bla
  which required to compute the signature of import hello.bla

 Run with both -explain-cyclic and -Ydebug-cyclic to see full stack trace.>
        #ra1()
  }
}

1 error found
som-snytt commented 8 months ago

if the def is not Nothing

[[syntax trees at end of                    parser]] // i19413b.scala
package <empty> {
  class C {
    import hello.bla.*
    def hello: String = ???
  }
}

-- [E008] Not Found Error: i19413b.scala:3:15 --------------------------------------------------------------------------
3 |  import hello.bla.*
  |         ^^^^^^^^^
  |         value bla is not a member of String
[[syntax trees at end of                     typer]] // i19413b.scala
package <empty> {
  class C() extends Object() {
    import this.hello.bla.*
    def hello: String = <error value bla is not a member of String>#???
  }
}

1 error found

as opposed to

[[syntax trees at end of                    parser]] // i19413b.scala
package <empty> {
  class C {
    import hello.bla.*
    def hello = ???
  }
}

-- [E007] Type Mismatch Error: i19413b.scala:3:9 -----------------------------------------------------------------------
3 |  import hello.bla.*
  |         ^^^^^
  |         Found:    (C.this.hello : => Nothing)
  |         Required: ?{ bla: ? }
  |         Note that implicit conversions were not tried because the result of an implicit conversion
  |         must be more specific than ?{ bla: a type that can be selected or applied }
  |
  | longer explanation available when compiling with `-explain`
[[syntax trees at end of                     typer]] // i19413b.scala
package <empty> {
  class C() extends Object() {
    import this.hello.*
    def hello: Nothing = ???
  }
}

1 error found

On the happy unhappy path, it notices the problem

[[syntax trees at end of                    parser]] // i19413b.scala
package <empty> {
  class C {
    import hello.length
    def hello: String = ???
  }
}

-- [E083] Type Error: i19413b.scala:4:9 --------------------------------------------------------------------------------
4 |  import hello.length
  |         ^^^^^
  |         (C.this.hello : => String) is not a valid import prefix, since it is not an immutable path
  |
  | longer explanation available when compiling with `-explain`
[[syntax trees at end of                     typer]] // i19413b.scala
package <empty> {
  class C() extends Object() {
    import this.hello.length
    def hello: String = ???
  }
}

1 error found

I don't even know what it means for a path to be immutable, as opposed to stable.