Skip to content

Commit 6d3b706

Browse files
Araqnarimiran
authored andcommitted
misc bugfixes [backport:1.2] (#19203)
(cherry picked from commit 23c117a)
1 parent 0df0510 commit 6d3b706

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

compiler/semobjconstr.nim

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -358,12 +358,17 @@ proc defaultConstructionError(c: PContext, t: PType, info: TLineInfo) =
358358
while objType.kind != tyObject:
359359
objType = objType.lastSon
360360
assert objType != nil
361-
var constrCtx = initConstrContext(objType, newNodeI(nkObjConstr, info))
362-
let initResult = semConstructTypeAux(c, constrCtx, {})
363-
assert constrCtx.missingFields.len > 0
364-
localError(c.config, info,
365-
"The $1 type doesn't have a default value. The following fields must be initialized: $2.",
366-
[typeToString(t), listSymbolNames(constrCtx.missingFields)])
361+
if objType.kind == tyObject:
362+
var constrCtx = initConstrContext(objType, newNodeI(nkObjConstr, info))
363+
let initResult = semConstructTypeAux(c, constrCtx, {})
364+
if constrCtx.missingFields.len > 0:
365+
localError(c.config, info,
366+
"The $1 type doesn't have a default value. The following fields must be initialized: $2." % [typeToString(t), listSymbolNames(constrCtx.missingFields)])
367+
elif objType.kind == tyDistinct:
368+
localError(c.config, info,
369+
"The $1 distinct type doesn't have a default value." % typeToString(t))
370+
else:
371+
assert false, "Must not enter here."
367372

368373
proc semObjConstr(c: PContext, n: PNode, flags: TExprFlags): PNode =
369374
var t = semTypeNode(c, n[0], nil)

doc/backends.rst

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ The commands to compile to either C, C++ or Objective-C are:
4747
The most significant difference between these commands is that if you look
4848
into the ``nimcache`` directory you will find ``.c``, ``.cpp`` or ``.m``
4949
files, other than that all of them will produce a native binary for your
50-
project. This allows you to take the generated code and place it directly
50+
project. This allows you to take the generated code and place it directly
5151
into a project using any of these languages. Here are some typical command-
5252
line invocations::
5353

@@ -110,8 +110,8 @@ Nim code calling the backend
110110
Nim code can interface with the backend through the `Foreign function
111111
interface <manual.html#foreign-function-interface>`_ mainly through the
112112
`importc pragma <manual.html#foreign-function-interface-importc-pragma>`_.
113-
The ``importc`` pragma is the *generic* way of making backend symbols available
114-
in Nim and is available in all the target backends (JavaScript too). The C++
113+
The `importc` pragma is the *generic* way of making backend symbols available
114+
in Nim and is available in all the target backends (JavaScript too). The C++
115115
or Objective-C backends have their respective `ImportCpp
116116
<manual.html#implementation-specific-pragmas-importcpp-pragma>`_ and
117117
`ImportObjC <manual.html#implementation-specific-pragmas-importobjc-pragma>`_
@@ -232,11 +232,6 @@ Also, C code requires you to specify a forward declaration for functions or
232232
the compiler will assume certain types for the return value and parameters
233233
which will likely make your program crash at runtime.
234234

235-
The Nim compiler can generate a C interface header through the ``--header``
236-
command-line switch. The generated header will contain all the exported
237-
symbols and the ``NimMain`` proc which you need to call before any other
238-
Nim code.
239-
240235

241236
Nim invocation example from C
242237
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -255,9 +250,10 @@ Create a ``maths.c`` file with the following content:
255250

256251
.. code-block:: c
257252
258-
#include "fib.h"
259253
#include <stdio.h>
260254
255+
extern int fib(int a);
256+
261257
int main(void)
262258
{
263259
NimMain();
@@ -270,26 +266,27 @@ Now you can run the following Unix like commands to first generate C sources
270266
from the Nim code, then link them into a static binary along your main C
271267
program::
272268

273-
$ nim c --noMain --noLinking --header:fib.h fib.nim
274-
$ gcc -o m -I$HOME/.cache/nim/fib_d -Ipath/to/nim/lib $HOME/.cache/nim/fib_d/*.c maths.c
269+
.. code:: cmd
270+
271+
nim c --noMain --noLinking fib.nim
272+
gcc -o m -I$HOME/.cache/nim/fib_d -Ipath/to/nim/lib $HOME/.cache/nim/fib_d/*.c maths.c
275273
276274
The first command runs the Nim compiler with three special options to avoid
277-
generating a ``main()`` function in the generated files, avoid linking the
278-
object files into a final binary, and explicitly generate a header file for C
279-
integration. All the generated files are placed into the ``nimcache``
275+
generating a `main()`:c: function in the generated files and to avoid linking the
276+
object files into a final binary. All the generated files are placed into the ``nimcache``
280277
directory. That's why the next command compiles the ``maths.c`` source plus
281278
all the ``.c`` files from ``nimcache``. In addition to this path, you also
282279
have to tell the C compiler where to find Nim's ``nimbase.h`` header file.
283280

284281
Instead of depending on the generation of the individual ``.c`` files you can
285282
also ask the Nim compiler to generate a statically linked library::
286283

287-
$ nim c --app:staticLib --noMain --header fib.nim
288-
$ gcc -o m -Inimcache -Ipath/to/nim/lib libfib.nim.a maths.c
284+
nim c --app:staticLib --noMain fib.nim
285+
gcc -o m -Inimcache -Ipath/to/nim/lib libfib.nim.a maths.c
289286

290287
The Nim compiler will handle linking the source files generated in the
291288
``nimcache`` directory into the ``libfib.nim.a`` static library, which you can
292-
then link into your C program. Note that these commands are generic and will
289+
then link into your C program. Note that these commands are generic and will
293290
vary for each system. For instance, on Linux systems you will likely need to
294291
use ``-ldl`` too to link in required dlopen functionality.
295292

0 commit comments

Comments
 (0)