From cd9c47140e13d2b55676dc489076f800151cddd1 Mon Sep 17 00:00:00 2001 From: Laylie <202595611+laylie527@users.noreply.github.com> Date: Mon, 10 Mar 2025 22:47:03 +0800 Subject: [PATCH] Fix scanTuple undeclared identifier 'scanf' (#24759) Without this fix, trying to use `scanTuple` in a generic proc imported from a different module fails to compile (`undeclared identifier: 'scanf'`): ```nim # module.nim import std/strscans proc scan*[T](s: string): (bool, string) = s.scanTuple("$+") ``` ```nim # main.nim import ./module echo scan[int]("foo") ``` Workaround is to `export scanf` in `module.nim` or `import std/strscans` in `main.nim`. (cherry picked from commit f8294ce06e61752d15624e0422c5296292ef5467) --- lib/pure/strscans.nim | 2 +- tests/stdlib/mstrscans_undecl_scanf.nim | 4 ++++ tests/stdlib/tstrscans_undecl_scanf.nim | 3 +++ 3 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 tests/stdlib/mstrscans_undecl_scanf.nim create mode 100644 tests/stdlib/tstrscans_undecl_scanf.nim diff --git a/lib/pure/strscans.nim b/lib/pure/strscans.nim index 45ba9a469c..cbb1124420 100644 --- a/lib/pure/strscans.nim +++ b/lib/pure/strscans.nim @@ -512,7 +512,7 @@ macro scanTuple*(input: untyped; pattern: static[string]; matcherTypes: varargs[ inc userMatches else: discard inc p - result.add nnkTupleConstr.newTree(newCall(ident("scanf"), input, newStrLitNode(pattern))) + result.add nnkTupleConstr.newTree(newCall(bindSym("scanf"), input, newStrLitNode(pattern))) for arg in arguments: result[^1][0].add arg result[^1].add arg diff --git a/tests/stdlib/mstrscans_undecl_scanf.nim b/tests/stdlib/mstrscans_undecl_scanf.nim new file mode 100644 index 0000000000..24ce23ee6d --- /dev/null +++ b/tests/stdlib/mstrscans_undecl_scanf.nim @@ -0,0 +1,4 @@ +import std/strscans + +proc scan*[T](s: string): (bool, string) = + s.scanTuple("$+") diff --git a/tests/stdlib/tstrscans_undecl_scanf.nim b/tests/stdlib/tstrscans_undecl_scanf.nim new file mode 100644 index 0000000000..a2b6e8386f --- /dev/null +++ b/tests/stdlib/tstrscans_undecl_scanf.nim @@ -0,0 +1,3 @@ +import std/assertions +import ./mstrscans_undecl_scanf +doAssert scan[int]("foo") == (true, "foo")