nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.44k stars 1.47k forks source link

C-compiler failed, "error: redefinition of 'fmtRes'" #21956

Closed al6x closed 1 year ago

al6x commented 1 year ago

Description

Nim successfully compiles, then C-compiler crashes. Unfortunately can't isolate bug in short Nim code, but here's C snippet that should help to fix this bug, as you see the NimStringDesc* fmtRes; line happens twice and cause problem.

The Nim code fix was that I changed from el(PMessage, (text: fmt"Not found", top: true)) to el(PMessage, (text: "Not found", top: true)).

The C snippet, let me know if you need to see full C-code.

...
{   result = NIM_NIL;
    nimln_(26, "/alex/projects/nim/keep/ui/app_view.nim");
    nimZeroMem((void*)(&found), sizeof(tyObject_Option__25DWiFBjq7lBQljc2XmK5w));
    get__coreZdbm_5224(db__coreZdbm_4, sid, did, (&found));
    nimln_(27, "/alex/projects/nim/keep/ui/app_view.nim");
    {
        NIM_BOOL T3_;
        tyObject_ElcolonObjectType___oRQMvBBGUDhyxs3eDZnSIg* eX60gensym16_;
        tyObject_ElcolonObjectType___oRQMvBBGUDhyxs3eDZnSIg* elX60gensym13_;
        tyTuple__dPNkpxgceTc63bP5seHZGw T6_;
        NimStringDesc* fmtRes;
        tyTuple__dPNkpxgceTc63bP5seHZGw T7_;
        NimStringDesc* fmtRes;
        T3_ = (NIM_BOOL)0;
        T3_ = isNone__uiZapp95view_312(found);
        if (!T3_) goto LA4_;
        nimln_(256, "/alex/projects/nim/ext/html.nim");
        elX60gensym13_ = NIM_NIL;
        nimln_(114, "/alex/projects/nim/mono/core/tmpl.nim");
        nimZeroMem((void*)(&T6_), sizeof(tyTuple__dPNkpxgceTc63bP5seHZGw));
        nimln_(583, "/usr/local/Cellar/nim/1.6.12/nim/lib/pure/strformat.nim");
        nimln_(27, "/alex/projects/nim/keep/ui/app_view.nim");
        fmtRes = rawNewString(((NI) 9));
        fmtRes = resizeString(fmtRes, 9);
...

The C stack trace

nim r keep/main.nim && say ok || say failed
/Users/alex/.cache/nim/main_d/@mui@sapp_view.nim.c:1079:18: error: redefinition of 'fmtRes'
                NimStringDesc* fmtRes;
                               ^
/Users/alex/.cache/nim/main_d/@mui@sapp_view.nim.c:1077:18: note: previous definition is here
                NimStringDesc* fmtRes;
                               ^
1 error generated.
Error: execution of an external compiler program 'clang -c  -w -ferror-limit=3   -I/usr/local/Cellar/nim/1.6.12/nim/lib -I/alex/projects/nim/keep -o /Users/alex/.cache/nim/main_d/@mui@sapp_view.nim.c.o /Users/alex/.cache/nim/main_d/@mui@sapp_view.nim.c' failed with exit code: 1

P.S.

It would be much better if Nim just removed fmt and make it language built-in string interpolated. As the fmt causes lots of problems. Sometimes it just doesn't work, sometimes it can't work inside of some template, sometimes it doesn't work if you combine templates in some way. The reason for that I think is that fmt is very frequently used, and so when you write template code, especially if it's multi-level template combination, when you use fmt it just inflates that complex template code even more, producing all sort of tricky edge cases.

Nim Version

Nim Compiler Version 1.6.12 [MacOSX: amd64] Compiled at 2023-03-10 Copyright (c) 2006-2023 by Andreas Rumpf

active boot switches: -d:release -d:nimUseLinenoise

Current Output

No response

Expected Output

No response

Possible Solution

No response

Additional Information

No response

heterodoxic commented 1 year ago

Would you mind sharing some of your Nim code? Maybe you could trim it down to a point where both your IP is still safe and yet this double declaration still happens. I might have a look at this alleged codegen problem, but having insight into both the Nim and the C code realms would help me a lot!

Regarding having every string interpolated: that would mean that the Nim compiler would have to analyze every string for interpolation then. Not sure how much that would affect compile times, but I reckon it wouldn't be pretty for larger builds...

ringabout commented 1 year ago

Please try the code with the devel compiler, which is probably fixed like https://github.com/nim-lang/Nim/issues/20381

al6x commented 1 year ago

@heterodoxic about the Nim code to reproduce it, the code commit 2794581a18891d428b7ac2b16f9b8001a1f2281a with this bug, could be reproduced by running it as nim r keep/main.nim, please note that there's nim.cfg, so it's better to run this command from the root of the repo so nim.cfg will be picked up.

@ringabout thanks, yes could be that this bug is fixed, closing this issue for now. I can't build Nim compiler from sources, some issues on my machine, going to investigate it a bit later, then try to re-build Nim dev version.

al6x commented 1 year ago

Regarding having every string interpolated: that would mean that the Nim compiler would have to analyze every string for interpolation then.

Hmm, I think it should be fast, as Nim would only to scan string bytes for presence of { character, and only if it present then do the actual interpolation analysis that takes time.

ringabout commented 1 year ago

Either using choosenim update devel or installing from https://github.com/nim-lang/nightlies/releases/tag/2023-05-29-devel-8c55e2999b27e3f088fca2e61739b33498fb07ef is quite convenient.