Skip to content

Defining a proc [] that access array with a distinct int raise assert #23031

@demotomohiro

Description

@demotomohiro

Description

When compiled following code, AssertionDefect was raised.

type
  DisInt = distinct int
  
proc `[]`[N: static int](a: array[N, int]; i: DisInt): int =
  a[i.int]

proc `[]`[N: static int](a: var array[N, int]; i: DisInt): var int =
  a[i.int]

var
  a = [1, 2]
  i = 1.DisInt
echo a[i]
a[i] += 3
echo a[i]

Nim Version

Nim Compiler Version 2.1.1 [Linux: amd64]
Compiled at 2023-12-05
Copyright (c) 2006-2023 by Andreas Rumpf

git hash: 202e21d
active boot switches: -d:release

Current Output

.....................................................................
Nim/compiler/nim.nim(168) nim
Nim/compiler/nim.nim(123) handleCmdLine
Nim/compiler/main.nim(338) mainCommand
Nim/compiler/main.nim(305) compileToBackend
Nim/compiler/main.nim(142) commandCompileToC
Nim/compiler/pipelines.nim(320) compilePipelineProject
Nim/compiler/pipelines.nim(243) compilePipelineModule
Nim/compiler/pipelines.nim(185) processPipelineModule
Nim/compiler/sem.nim(881) semWithPContext
Nim/compiler/sem.nim(843) semStmtAndGenerateGenerics
Nim/compiler/semstmts.nim(2700) semStmt
Nim/compiler/semexprs.nim(1163) semExprNoType
Nim/compiler/semexprs.nim(3268) semExpr
Nim/compiler/semstmts.nim(2647) semStmtList
Nim/compiler/semexprs.nim(3167) semExpr
Nim/compiler/semexprs.nim(2549) semMagic
Nim/compiler/semexprs.nim(1144) semDirectOp
Nim/compiler/semexprs.nim(970) semOverloadedCallAnalyseEffects
Nim/compiler/semcall.nim(686) semOverloadedCall
Nim/compiler/semcall.nim(421) resolveOverloads
Nim/compiler/semcall.nim(95) pickBestCandidate
Nim/compiler/sigmatch.nim(2712) matches
Nim/compiler/sigmatch.nim(2645) matchesAux
Nim/compiler/sigmatch.nim(2439) prepareOperand
Nim/compiler/semexprs.nim(55) semOperand
Nim/compiler/semexprs.nim(3208) semExpr
Nim/compiler/semexprs.nim(1711) semArrayAccess
Nim/compiler/semexprs.nim(3166) semExpr
Nim/compiler/semexprs.nim(1144) semDirectOp
Nim/compiler/semexprs.nim(970) semOverloadedCallAnalyseEffects
Nim/compiler/semcall.nim(686) semOverloadedCall
Nim/compiler/semcall.nim(421) resolveOverloads
Nim/compiler/semcall.nim(104) pickBestCandidate
Nim/compiler/sigmatch.nim(304) cmpCandidates
Nim/compiler/sigmatch.nim(196) checkGeneric
Nim/compiler/sigmatch.nim(1237) typeRel
Nim/compiler/sigmatch.nim(1279) typeRel
Nim/compiler/ast.nim(959) inferStaticsInRange
Nim/compiler/int128.nim(69) toInt64
Nim/lib/std/assertions.nim(41) failedAssertImpl
Nim/lib/std/assertions.nim(34) raiseAssert
Error: unhandled exception: Nim/compiler/int128.nim(69, 11) `arg.sdata(3) == 0` out of range [AssertionDefect]

Expected Output

Compiles without errors

Possible Solution

No response

Additional Information

No response

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