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

With const c: cstring using literal string value from template, Error: type mismatch: got 'cstring' for '""' but expected 'string' #24296

Closed
tersec opened this issue Oct 13, 2024 · 3 comments · Fixed by #24298

Comments

@tersec
Copy link
Contributor

tersec commented Oct 13, 2024

Description

# Either changing the template to `proc`/`func` or using `$""`, not a string
# literal alone, allows any version of Nim 2.x to compile this.
template g(): string = ""

# Alternatively: don't retrieve the string through g(), but directly, also
# allows compilation across Nim 2.x versions.
const d: cstring = ""
const f: cstring = $""
const b = cstring g()
const m = cstring ""
const p = cstring $""

# But this does not compile across Nim 2.x/devel.
const c: cstring = g()

Nim Version

Nim Compiler Version 2.0.10 [Linux: amd64]
Compiled at 2024-10-13
Copyright (c) 2006-2023 by Andreas Rumpf

git hash: e941ee15be775fe3c46db1bed9b4f41c7dfb1334
active boot switches: -d:release
Nim Compiler Version 2.2.0 [Linux: amd64]
Compiled at 2024-10-13
Copyright (c) 2006-2024 by Andreas Rumpf

git hash: 78983f1876726a49c69d65629ab433ea1310ece1
active boot switches: -d:release
Nim Compiler Version 2.2.1 [Linux: amd64]
Compiled at 2024-10-13
Copyright (c) 2006-2024 by Andreas Rumpf

git hash: 720d0aee5c2d56260587f9ab09220d18ab9b53f7
active boot switches: -d:release

Current Output

/tmp/u.nim(14, 21) Error: type mismatch: got 'cstring' for '""' but expected 'string'

Expected Output

Builds, or otherwise consistent behavior. Using many minor variations allows the compile-time context to trigger properly, just this one specific way of accessing it triggers this error.

Known Workarounds

No response

Additional Information

Related to #24295 but the method syntax usage seems to have accidentally worked around this through 2.0, until 2.2 (even giving the template a dummy parameter works in 2.0):

template g(x: int): string = ""
const c: cstring = 0.g()

works in 2.0 but not 2.2/devel; see linked issue for more.

This issue points to a broader case which also did not work in 2.0.x.

@ringabout
Copy link
Member

It is also a 1.6.12 -> 1.6.14 regression

@ringabout
Copy link
Member

!nim c

# Either changing the template to `proc`/`func` or using `$""`, not a string
# literal alone, allows any version of Nim 2.x to compile this.
template g(): string = ""

# But this does not compile across Nim 2.x/devel.
const c: cstring = g()

Copy link
Contributor

🐧 Linux bisect by @ringabout (member)
devel 👎 FAIL

Output


IR

Compiled filesize 0 (0 bytes)

Stats

  • Started 2024-10-13T06:54:32
  • Finished 2024-10-13T06:54:32
  • Duration

AST

nnkStmtList.newTree(
  nnkTemplateDef.newTree(
    newIdentNode("g"),
    newEmptyNode(),
    newEmptyNode(),
    nnkFormalParams.newTree(
      newIdentNode("string")
    ),
    newEmptyNode(),
    newEmptyNode(),
    nnkStmtList.newTree(
      newLit("")
    )
  ),
  nnkConstSection.newTree(
    nnkConstDef.newTree(
      newIdentNode("c"),
      newIdentNode("cstring"),
      nnkCall.newTree(
        newIdentNode("g")
      )
    )
  )
)
stable 👎 FAIL

Output


IR

Compiled filesize 0 (0 bytes)

Stats

  • Started 2024-10-13T06:54:36
  • Finished 2024-10-13T06:54:36
  • Duration

AST

nnkStmtList.newTree(
  nnkTemplateDef.newTree(
    newIdentNode("g"),
    newEmptyNode(),
    newEmptyNode(),
    nnkFormalParams.newTree(
      newIdentNode("string")
    ),
    newEmptyNode(),
    newEmptyNode(),
    nnkStmtList.newTree(
      newLit("")
    )
  ),
  nnkConstSection.newTree(
    nnkConstDef.newTree(
      newIdentNode("c"),
      newIdentNode("cstring"),
      nnkCall.newTree(
        newIdentNode("g")
      )
    )
  )
)
2.0.10 👎 FAIL

Output


IR

Compiled filesize 0 (0 bytes)

Stats

  • Started 2024-10-13T06:54:39
  • Finished 2024-10-13T06:54:39
  • Duration

AST

nnkStmtList.newTree(
  nnkTemplateDef.newTree(
    newIdentNode("g"),
    newEmptyNode(),
    newEmptyNode(),
    nnkFormalParams.newTree(
      newIdentNode("string")
    ),
    newEmptyNode(),
    newEmptyNode(),
    nnkStmtList.newTree(
      newLit("")
    )
  ),
  nnkConstSection.newTree(
    nnkConstDef.newTree(
      newIdentNode("c"),
      newIdentNode("cstring"),
      nnkCall.newTree(
        newIdentNode("g")
      )
    )
  )
)
2.0.0 👎 FAIL

Output


IR

Compiled filesize 0 (0 bytes)

Stats

  • Started 2024-10-13T06:54:42
  • Finished 2024-10-13T06:54:43
  • Duration

AST

nnkStmtList.newTree(
  nnkTemplateDef.newTree(
    newIdentNode("g"),
    newEmptyNode(),
    newEmptyNode(),
    nnkFormalParams.newTree(
      newIdentNode("string")
    ),
    newEmptyNode(),
    newEmptyNode(),
    nnkStmtList.newTree(
      newLit("")
    )
  ),
  nnkConstSection.newTree(
    nnkConstDef.newTree(
      newIdentNode("c"),
      newIdentNode("cstring"),
      nnkCall.newTree(
        newIdentNode("g")
      )
    )
  )
)
1.6.20 👎 FAIL

Output


IR

Compiled filesize 0 (0 bytes)

Stats

  • Started 2024-10-13T06:54:45
  • Finished 2024-10-13T06:54:45
  • Duration

AST

nnkStmtList.newTree(
  nnkTemplateDef.newTree(
    newIdentNode("g"),
    newEmptyNode(),
    newEmptyNode(),
    nnkFormalParams.newTree(
      newIdentNode("string")
    ),
    newEmptyNode(),
    newEmptyNode(),
    nnkStmtList.newTree(
      newLit("")
    )
  ),
  nnkConstSection.newTree(
    nnkConstDef.newTree(
      newIdentNode("c"),
      newIdentNode("cstring"),
      nnkCall.newTree(
        newIdentNode("g")
      )
    )
  )
)
1.4.8 👍 OK

Output


IR

Compiled filesize 91.16 Kb (93,344 bytes)
#include "nimbase.h"
#define nimfr_(x, y)
#define nimln_(x, y)
static N_INLINE(void, initStackBottomWith)(void* locals);
N_LIB_PRIVATE N_NOINLINE(void, nimGC_setStackBottom)(void* theStackBottom);
N_LIB_PRIVATE N_NIMCALL(void, systemDatInit000)(void);
N_LIB_PRIVATE N_NIMCALL(void, systemInit000)(void);
N_LIB_PRIVATE N_NIMCALL(void, NimMainModule)(void);
static N_INLINE(void, initStackBottomWith)(void* locals) {
 nimGC_setStackBottom(locals);
}
N_LIB_PRIVATE void PreMainInner(void) {
}
N_LIB_PRIVATE int cmdCount;
N_LIB_PRIVATE char** cmdLine;
N_LIB_PRIVATE char** gEnv;
N_LIB_PRIVATE void PreMain(void) {
 void (*volatile inner)(void);
 inner = PreMainInner;
 systemDatInit000();
 initStackBottomWith((void *)&inner);
 systemInit000();
 (*inner)();
}
N_LIB_PRIVATE N_CDECL(void, NimMainInner)(void) {
 NimMainModule();
}
N_CDECL(void, NimMain)(void) {
 void (*volatile inner)(void);
 PreMain();
 inner = NimMainInner;
 initStackBottomWith((void *)&inner);
 (*inner)();
}
int main(int argc, char** args, char** env) {
 cmdLine = args;
 cmdCount = argc;
 gEnv = env;
 NimMain();
 return nim_program_result;
}
N_LIB_PRIVATE N_NIMCALL(void, NimMainModule)(void) {
{
}
}

Stats

  • Started 2024-10-13T06:54:48
  • Finished 2024-10-13T06:54:48
  • Duration
1.2.18 👍 OK

Output


IR

Compiled filesize 86.88 Kb (88,968 bytes)
#include "nimbase.h"
#define nimfr_(x, y)
#define nimln_(x, y)
static N_INLINE(void, initStackBottomWith)(void* locals);
N_LIB_PRIVATE N_NOINLINE(void, nimGC_setStackBottom)(void* theStackBottom);
N_LIB_PRIVATE N_NIMCALL(void, systemDatInit000)(void);
N_LIB_PRIVATE N_NIMCALL(void, systemInit000)(void);
N_LIB_PRIVATE N_NIMCALL(void, NimMainModule)(void);
static N_INLINE(void, initStackBottomWith)(void* locals) {
 nimGC_setStackBottom(locals);
}
N_LIB_PRIVATE void PreMainInner(void) {
}
N_LIB_PRIVATE int cmdCount;
N_LIB_PRIVATE char** cmdLine;
N_LIB_PRIVATE char** gEnv;
N_LIB_PRIVATE void PreMain(void) {
 void (*volatile inner)(void);
 inner = PreMainInner;
 systemDatInit000();
 initStackBottomWith((void *)&inner);
 systemInit000();
 (*inner)();
}
N_LIB_PRIVATE N_CDECL(void, NimMainInner)(void) {
 NimMainModule();
}
N_CDECL(void, NimMain)(void) {
 void (*volatile inner)(void);
 PreMain();
 inner = NimMainInner;
 initStackBottomWith((void *)&inner);
 (*inner)();
}
int main(int argc, char** args, char** env) {
 cmdLine = args;
 cmdCount = argc;
 gEnv = env;
 NimMain();
 return nim_program_result;
}
N_LIB_PRIVATE N_NIMCALL(void, NimMainModule)(void) {
{
}
}

Stats

  • Started 2024-10-13T06:54:50
  • Finished 2024-10-13T06:54:51
  • Duration
1.0.10 👍 OK

Output


IR

Compiled filesize 81.98 Kb (83,952 bytes)
#include "nimbase.h"
#define nimfr_(x, y)
#define nimln_(x, y)
static N_INLINE(void, initStackBottomWith)(void* locals);
N_NOINLINE(void, nimGC_setStackBottom)(void* theStackBottom);
N_LIB_PRIVATE N_NIMCALL(void, systemDatInit000)(void);
N_LIB_PRIVATE N_NIMCALL(void, systemInit000)(void);
N_LIB_PRIVATE N_NIMCALL(void, NimMainModule)(void);
static N_INLINE(void, initStackBottomWith)(void* locals) {	nimGC_setStackBottom(locals);
}
void PreMainInner(void) {
}
int cmdCount;
char** cmdLine;
char** gEnv;
void PreMain(void) {
 void (*volatile inner)(void);
 inner = PreMainInner;
 systemDatInit000();
 initStackBottomWith((void *)&inner);
 systemInit000();
 (*inner)();
}
N_CDECL(void, NimMainInner)(void) {
 NimMainModule();
}
N_CDECL(void, NimMain)(void) {
 void (*volatile inner)(void);
 PreMain();
 inner = NimMainInner;
 initStackBottomWith((void *)&inner);
 (*inner)();
}
int main(int argc, char** args, char** env) {
 cmdLine = args;
 cmdCount = argc;
 gEnv = env;
 NimMain();
 return nim_program_result;
}
N_LIB_PRIVATE N_NIMCALL(void, NimMainModule)(void) {
{
}
}

Stats

  • Started 2024-10-13T06:54:53
  • Finished 2024-10-13T06:54:53
  • Duration
#0014b9c48 ➡️ 🐛

Diagnostics

metagn introduced a bug at 2022-08-24 08:11:41 +0300 on commit #0014b9c48 with message:

top-down type inference, implements rfc 149 (#20091)

* micro implementation of rfc 149

refs https://github.com/nim-lang/RFCs/issues/149

* number/array/seq literals, more statements

* try fix number literal alias issue

* renew expectedType with if/case/try branch types

* fix (nerf) index type handling and float typed int

* use typeAllowed

* tweaks + const test (tested locally) [skip ci]

* fill out more of the checklist

* more literals, change @ order, type conversions

Not copying the full call tree before the typedesc call check
in `semIndirectOp` is also a small performance improvement.

* disable self-conversion warning

* revert type conversions (maybe separate op later)

* deal with CI for now (seems unrelated), try enums

* workaround CI different way

* proper fix

* again

* see sizes

* lol

* overload selection, simplify int literal -> float

* range, new @ solution, try use fitNode for nil

* use new magic

* try fix ranges, new magic, deal with #20193

* add documentation, support templates

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>

The bug is in the files:

compiler/ast.nim
compiler/ccgexprs.nim
compiler/condsyms.nim
compiler/jsgen.nim
compiler/sem.nim
compiler/semdata.nim
compiler/semexprs.nim
compiler/semobjconstr.nim
compiler/semstmts.nim
compiler/sizealignoffsetimpl.nim
compiler/varpartitions.nim
compiler/vmgen.nim
doc/manual_experimental.md
lib/pure/collections/sequtils.nim
lib/system.nim
tests/stdlib/tsequtils.nim
tests/types/ttopdowninference.nim

The bug can be in the commits:

(Diagnostics sometimes off-by-one).

Stats
  • GCC 11.4.0
  • Clang 14.0.0
  • NodeJS 20.4
  • Created 2024-10-13T06:54:06Z
  • Comments 2
  • Commands nim c --run -d:nimDebug -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim

🤖 Bug found in 40 mins bisecting 4404 commits at 109 commits per second

@Araq Araq closed this as completed in 80e6b35 Oct 13, 2024
narimiran pushed a commit that referenced this issue Oct 23, 2024
#24298)

fixes #24296
fixes #24295

Templates use `expectedType` for type inference. It's justified that
when templates don't have an actual return type, i.e., `untyped` etc.

When the return type of templates is specified, we should not infer the
type

```nim
template g(): string = ""

let c: cstring = g()
```
In this example, it is not reasonable to annotate the templates
expression with the `cstring` type before the `fitNode` check with its
specified return type.

(cherry picked from commit 80e6b35)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants