Skip to content

Bad error message when writing to a data structure within read-only object #7335

@genotrance

Description

@genotrance
import sets

type
  parent = object
    hash: HashSet[string]

proc getParent(): parent =
  result.hash.init()

let a = getParent()
a.hash.incl("Hello")

Here a is read-only so Nim should tell you that. Instead you see the following error which is hard to figure out.

ctest.nim(11, 7) Error: type mismatch: got <HashSet[system.string], string>
but expected one of:
proc incl[T](x: var set[T]; y: T)
  first type mismatch at position: 1
  required type: var set[T]
  but expression 'a.hash' is of type: HashSet[system.string]
template incl[T](s: var set[T]; flags: set[T])
  first type mismatch at position: 1
  required type: var set[T]
  but expression 'a.hash' is of type: HashSet[system.string]
proc incl[A](s: var HashSet[A]; key: A)
  first type mismatch at position: 1
  required type: var HashSet[incl.A]
  but expression 'a.hash' is of type: HashSet[system.string]
proc incl[A](s: var HashSet[A]; other: OrderedSet[A])
  first type mismatch at position: 1
  required type: var HashSet[incl.A]
  but expression 'a.hash' is of type: HashSet[system.string]
proc incl[A](s: var OrderedSet[A]; key: A)
  first type mismatch at position: 1
  required type: var OrderedSet[incl.A]
  but expression 'a.hash' is of type: HashSet[system.string]
proc incl[A](s: var HashSet[A]; other: HashSet[A])
  first type mismatch at position: 1
  required type: var HashSet[incl.A]
  but expression 'a.hash' is of type: HashSet[system.string]

expression: incl(a.hash, "Hello")

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions