Skip to content

Confusion in type error when two types have same simple name #18678

Closed
@bishabosha

Description

@bishabosha

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

class String // could be wildcard imported

val s: String = "hello"

Output Error/Warning message

-- [E007] Type Mismatch Error: -------------------------------------------------
3 |val s: String = "hello"
  |                ^^^^^^^
  |                Found:    ("hello" : String)
  |                Required: String
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  |
  | Tree: "hello"
  | I tried to show that
  |   ("hello" : String)
  | conforms to
  |   String
  | but the comparison trace ended with `false`:
  |
  |   ==> ("hello" : String)  <:  String
  |     ==> String  <:  String (left is approximated)
  |     <== String  <:  String (left is approximated) = false
  |   <== ("hello" : String)  <:  String = false
  |
  | The tests were made under the empty constraint
   -----------------------------------------------------------------------------
1 error found

Why this Error/Warning was not helpful

The message was unhelpful because it claims that String is not String, 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:

  • printing fully qualified name of the type
  • otherwise signalling the origin of each type

Metadata

Metadata

Assignees

No one assigned

    Labels

    SpreeSuitable for a future Spreearea:reportingError reporting including formatting, implicit suggestions, etcbetter-errorsIssues concerned with improving confusing/unhelpful diagnostic messagesitype:enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions