Skip to content

Passing Natural to bitops.BitsRange[T] parameter in generic proc is compile error #18823

@demotomohiro

Description

@demotomohiro

Minimum code:

import std/bitops

testBit(1, 1.Natural)

Compile error message:

f:\temp\nim-1.5.1\lib\pure\bitops.nim(63, 37) Error: 'sizeof' requires '.importc' types to be '.completeStruct'

Minimum code without importing bitops:

type BitsRange[T] = range[0..sizeof(T)*8-1]

proc bar[T](a: T; b: BitsRange[T]) =
  discard

bar(1, 2.Natural)

Compile error message:

f:\temp\testbitr.nim(1, 36) Error: 'sizeof' requires '.importc' types to be '.completeStruct'

This code also produces same compile error:

type BitsRange[T] = range[0..sizeof(T)*8-1]

proc bar[T](a: T; b: BitsRange[T]) =
  discard

bar(1.Natural, 2.Natural)

Just converting Natural variable to BitsRange[int] doesn't make compile error.
Following code compiles without erorr.

type BitsRange[T] = range[0..sizeof(T)*8-1]

var x: BitsRange[int] = 0
x = 1.Natural
echo x

var y: BitsRange[Natural] = 0
y = 1.Natural
echo y
y = 2
echo y

proc foo(x: BitsRange[int]) =
  discard

foo(1)
foo(1.Natural)

proc foo2(x: BitsRange[Natural]) =
  discard

foo2(1)
foo2(1.Natural)

proc bar[T](a: T; b: BitsRange[T]) =
  discard

bar(1.Natural, 2)

Nim version:

f:\temp>nim -v
Nim Compiler Version 1.5.1 [Windows: amd64]
Compiled at 2021-09-08
Copyright (c) 2006-2021 by Andreas Rumpf

active boot switches: -d:release

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions