-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Confusion in type error when two types have same simple name #18678
Comments
It would be good to see more cases where this fails. In particular, with wildcard imports, and with other types. |
It seems that as soon as the user's package foo
class String
val s: String = "hello" Details-- [E007] Type Mismatch Error: t/Test.scala:3:16 -------------------------------
3 |val s: String = "hello"
| ^^^^^^^
| Found: ("hello" : String)
| Required: foo.String
|-----------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| Tree: "hello"
| I tried to show that
| ("hello" : String)
| conforms to
| foo.String
| but the comparison trace ended with `false`:
|
| ==> ("hello" : String) <: foo.String
| ==> String <: foo.String (left is approximated)
| <== String <: foo.String (left is approximated) = false
| <== ("hello" : String) <: foo.String = false
|
| The tests were made under the empty constraint
----------------------------------------------------------------------------- package foo {
class String
}
import foo.String
val s: String = "hello" Details-- [E007] Type Mismatch Error: t/Test.scala:5:16 -------------------------------
5 |val s: String = "hello"
| ^^^^^^^
| Found: ("hello" : String)
| Required: foo.String
|-----------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| Tree: "hello"
| I tried to show that
| ("hello" : String)
| conforms to
| foo.String
| but the comparison trace ended with `false`:
|
| ==> ("hello" : String) <: foo.String
| ==> String <: foo.String (left is approximated)
| <== String <: foo.String (left is approximated) = false
| <== ("hello" : String) <: foo.String = false
|
| The tests were made under the empty constraint
----------------------------------------------------------------------------- |
With package foo
class Int
val s: Int = 1 Details-- [E007] Type Mismatch Error: t/Test.scala:5:13 -------------------------------
5 |val s: Int = 1
| ^
| Found: (1 : Int)
| Required: foo.Int²
|
| where: Int is a class in package scala
| Int² is a class in package foo
|-----------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| Tree: 1
| I tried to show that
| (1 : Int)
| conforms to
| foo.Int
| but the comparison trace ended with `false`:
|
| ==> (1 : Int) <: foo.Int
| ==> Int <: foo.Int (left is approximated)
| <== Int <: foo.Int (left is approximated) = false
| <== (1 : Int) <: foo.Int = false
|
| The tests were made under the empty constraint
-----------------------------------------------------------------------------
</details> |
With class Int
val s: Int = 1 Details-- [E007] Type Mismatch Error: t/Test.scala:2:13 -------------------------------
2 |val s: Int = 1
| ^
| Found: (1 : Int)
| Required: Int²
|
| where: Int is a class in package scala
| Int² is a class
|-----------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| Tree: 1
| I tried to show that
| (1 : Int)
| conforms to
| Int
| but the comparison trace ended with `false`:
|
| ==> (1 : Int) <: Int
| ==> Int <: Int (left is approximated)
| <== Int <: Int (left is approximated) = false
| <== (1 : Int) <: Int = false
|
| The tests were made under the empty constraint
----------------------------------------------------------------------------- |
It is more problematic for class String
val s: String = "hello" : java.lang.String Details-- [E007] Type Mismatch Error: t/Test.scala:2:16 -------------------------------
2 |val s: String = ??? : java.lang.String
| ^^^^^^^^^^^^^^^^^^^^^^
| Found: String
| Required: String
|-----------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| Tree: ??? :java.lang.String
| I tried to show that
| String
| conforms to
| String
| but the comparison trace ended with `false`:
|
| ==> String <: String
| <== String <: String = false
|
| The tests were made under the empty constraint
----------------------------------------------------------------------------- |
Tuples can also lead to confusion class Tuple2[A, B]
val s: Tuple2[Int, Int] = (1, 3) Details-- [E007] Type Mismatch Error: t/Test.scala:2:26 -------------------------------
2 |val s: Tuple2[Int, Int] = (1, 3)
| ^^^^^^
| Found: (Int, Int)
| Required: Tuple2[Int, Int]
|-----------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| Tree: Tuple2.apply[Int, Int](1, 3)
| I tried to show that
| (Int, Int)
| conforms to
| Tuple2[Int, Int]
| but the comparison trace ended with `false`:
|
| ==> (Int, Int) <: Tuple2[Int, Int]
| <== (Int, Int) <: Tuple2[Int, Int] = false
|
| The tests were made under the empty constraint
----------------------------------------------------------------------------- |
The issue is in general between types defined in the root package, scala package, or the java lang package. None of these types show their prefix which can cause confusion. |
In #18748 there is an improvement in the main message when dealing with primitive types. |
Yes, but I have the impression this is only for |
Types in the scala package also have this issue but it manifests in a slightly different way.
Unfortunately, this is the case in the REPL which makes it less marginal. |
If it happens in the REPL it's important to fix, I agree. |
originally reported by @hnrklssn in https://stackoverflow.com/questions/68146374/why-cant-a-string-literal-be-assigned-to-variable-of-type-string/68146434, #17842, and https://contributors.scala-lang.org/t/use-fqcn-in-type-error-message-if-two-or-more-types-have-the-same-name/5166
Compiler version
3.3.1
Minimized example
Output Error/Warning message
Why this Error/Warning was not helpful
The message was unhelpful because it claims that
String
is notString
, If I was unaware of a shadowed import then I get zero help here to suggest that could be the reason.Suggested improvement
It could be made more helpful by:
The text was updated successfully, but these errors were encountered: