Skip to content

Compiler error when sorting array of objects with proc attributes set to nil (in compile time) #19698

@amnr

Description

@amnr

I tried to make the test case to be as small as possible (ymmv).
The code is a part of my image viewer, I kept original names.
Doing any operation on SortedFormats (see main) results in compiler error.

What works fine:

  • operations on SortedFormats when all proc callbacks in FormatInfo are not nil (see "CASE 1" in code).
  • operations on KnownFormats (an "unsorted array") instead of SortedFormats (cee "CASE 2" in code) - nil proc callback in FormatInfo work.

Example

from algorithm import Descending, sort

# Definition of object FormatInfo that causes "problems".

type
  FormatInfo* = object
    readproc*:  ReadProc
    writeproc*: WriteProc

  ReadProc* = proc (s: pointer)
  WriteProc* = proc (s: pointer)

proc dummyWrite(s: pointer) = discard

func initFormatInfo*(readproc: ReadProc, writeproc: WriteProc = nil): FormatInfo {.compileTime.} =
  result.readproc = readproc
  # CASE 1: Defining dummyWrite instead of nil fixes the error.
  result.writeproc = nil
  # result.writeproc = if writeproc != nil: writeproc else: dummyWrite

# Sample format handler.

proc readSampleImage(s: pointer) = discard

const SampleFormatInfo* = initFormatInfo(readproc = readSampleImage)

# ==============================

const KnownFormats* = [SampleFormatInfo]

func `==`(x, y: FormatInfo): bool = false

proc `<`(x, y: FormatInfo): bool = false

func sortedFormatInfos(): array[len KnownFormats, FormatInfo] {.compileTime.} =
  for i, info in KnownFormats:
    result[i] = info
  result.sort Descending

const SortedFormats* = sortedFormatInfos()

# ============================

proc main() =
  # CASE 2: Doing any operation on SortedFormats results in compiler error
  # unless I define writeProc != nil in initImageFormat.
  discard SortedFormats

when isMainModule:
  main()

Current Output

CC: stdlib_system.nim
CC: all.nim
/tmp/.nim-cache/debug/all/@mall.nim.c:56:155: error: ‘T7_’ undeclared here (not in a function)
   56 | NST tyArray__s05lElO4OmtAjWwaQQiPWQ SortedFormats__all_242 = {{{((TM__HjqD9asWHiLcPFta7z9bI9avw_2) readSampleImage__all_28),NIM_NIL}, T7_}
      |                                                                                                                                       ^~~

/tmp/.nim-cache/debug/all/@mall.nim.c: In function ‘NimMainModule’:
/tmp/.nim-cache/debug/all/@mall.nim.c:156:21: error: incompatible types when assigning to type ‘void (*)(void *, void *)’ from type ‘tyProc__uMUlmGrxGbyKfim0kpddgQ’
  156 |         T1_.ClP_0 = TM__HjqD9asWHiLcPFta7z9bI9avw_3; T1_.ClE_0 = TM__HjqD9asWHiLcPFta7z9bI9avw_3;
      |                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/tmp/.nim-cache/debug/all/@mall.nim.c:156:66: error: incompatible types when assigning to type ‘void *’ from type ‘tyProc__uMUlmGrxGbyKfim0kpddgQ’
  156 |         T1_.ClP_0 = TM__HjqD9asWHiLcPFta7z9bI9avw_3; T1_.ClE_0 = TM__HjqD9asWHiLcPFta7z9bI9avw_3;
      |                                                                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated due to -fmax-errors=3.
Error: execution of an external compiler program 'gcc -c  -w -fmax-errors=3   -I/usr/lib/nim -I/tmp/rrr/src -o /tmp/.nim-cache/debug/all/@mall.nim.c.o /tmp/.nim-cache/debug/all/@mall.nim.c' failed with exit code: 1

Expected Output

(none)

Additional Information

$ nim -v
Nim Compiler Version 1.6.4 [Linux: amd64]
Compiled at 2022-02-10
Copyright (c) 2006-2021 by Andreas Rumpf

active boot switches: -d:release

$ uname -a
Linux karnak 5.16.0-5-rt-amd64 #1 SMP PREEMPT_RT Debian 5.16.14-1 (2022-03-15) x86_64 GNU/Linux

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