Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile error from backend gcc when generic int type is defined #19051

Closed
demotomohiro opened this issue Oct 25, 2021 · 4 comments
Closed

Compile error from backend gcc when generic int type is defined #19051

demotomohiro opened this issue Oct 25, 2021 · 4 comments

Comments

@demotomohiro
Copy link
Contributor

demotomohiro commented Oct 25, 2021

Following code causes compile error on backend gcc:

type GInt[T] = int

var a = 1
echo -a

Compile log:

f:\temp>nim c -r -f test.nim
Hint: used config file 'f:\project\Nim\config\nim.cfg' [Conf]
Hint: used config file 'f:\project\Nim\config\config.nims' [Conf]
Hint: used config file 'C:\Users\xyzw\AppData\Roaming\nim\nim.cfg' [Conf]
.........................................................
f:\temp\test.nim(1, 6) Hint: 'GInt' is declared but not used [XDeclaredButNotUsed]
CC: stdlib_digitsutils.nim
CC: stdlib_assertions.nim
CC: stdlib_dollars.nim
CC: stdlib_io.nim
CC: stdlib_system.nim
CC: test.nim
f:\temp\nimcache\test\debug\@mtest.nim.c: In function 'NimMainModule':
f:\temp\nimcache\test\debug\@mtest.nim.c:143:41: error: expected ')' before '-' token
  143 |  T1_[0] = dollar___systemZdollars_3(((NI-24)-(a__test_3)));
      |                                      ~  ^
      |                                         )
Error: execution of an external compiler program 'gcc.exe -c  -w -fmax-errors=3 -mno-ms-bitfields -fmax-errors=3   -If:\project\Nim\lib -If:\t
emp -o f:\temp\nimcache\test\debug\@mtest.nim.c.o f:\temp\nimcache\test\debug\@mtest.nim.c' failed with exit code: 1

Following code compiles successfully.
It seems defining generic int type and -a cause this problem.

Without GInt type:

var a = 1
echo -a

Without generics:

type GInt = int

var a = 1
echo -a

Use bool instead of int for GInt[T]:

type GInt[T] = bool

var a = 1
echo -a

Echo a as is:

type GInt[T] = int

var a = 1
echo a

Multiply -1 instead of -a:

type GInt[T] = int

var a = 1
echo a * -1

distinct int type:

type GInt[T] = distinct int

var a = 1
echo -a

Tested on devel Nim:

nim -v
Nim Compiler Version 1.7.1 [Windows: amd64]
Compiled at 2021-10-25
Copyright (c) 2006-2021 by Andreas Rumpf

git hash: 582468da1cc266d0dd922ec5c620713bbc45d02a
active boot switches: -d:release
@Araq
Copy link
Member

Araq commented Oct 25, 2021

What the ...? This is the weirdest bug I've ever seen.

@demotomohiro
Copy link
Contributor Author

I have tested same code on Nim playground.
It compiles successfully on v0.19.6 but similar compile error comes from gcc in newer version of Nim.
https://play.nim-lang.org/#ix=3CRo

@EliteTK
Copy link
Contributor

EliteTK commented Oct 29, 2021

It took a bit of effort to bisect this but it looks like this was introduced in 745f164

@EliteTK
Copy link
Contributor

EliteTK commented Oct 29, 2021

So the buggy code is emitted in ccgexprs in unaryArithOverflow in the mUnaryMinusI case. getSize returns a negative value which gets multiplied by 8. Looks like prior to the bisected version computeSizeAux which was used to implement getSize via computeSize returned conf.target.intSize based on typ.kind if typ.size was negative (which appears to be the case for GInt in this case).

I'm still trying to figure out what exactly the right behaviour is here. If anyone has any ideas that would help.

Araq added a commit that referenced this issue Nov 12, 2021
@Araq Araq closed this as completed in c6fc3b2 Nov 12, 2021
narimiran pushed a commit that referenced this issue Nov 17, 2021
(cherry picked from commit c6fc3b2)
PMunch pushed a commit to PMunch/Nim that referenced this issue Mar 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants