misc bugfixes [backport:1.2] (#19203)

This commit is contained in:
Andreas Rumpf 2021-12-04 07:42:34 +01:00 • committed by GitHub
commit 23c117a950
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 17 deletions

View file

@ -366,7 +366,7 @@ proc defaultConstructionError(c: PContext, t: PType, info: TLineInfo) =
if objType.kind == tyObject: if objType.kind == tyObject:
var constrCtx = initConstrContext(objType, newNodeI(nkObjConstr, info)) var constrCtx = initConstrContext(objType, newNodeI(nkObjConstr, info))
let initResult = semConstructTypeAux(c, constrCtx, {}) let initResult = semConstructTypeAux(c, constrCtx, {})
assert constrCtx.missingFields.len > 0 if constrCtx.missingFields.len > 0:
localError(c.config, info, localError(c.config, info,
"The $1 type doesn't have a default value. The following fields must be initialized: $2." % [typeToString(t), listSymbolNames(constrCtx.missingFields)]) "The $1 type doesn't have a default value. The following fields must be initialized: $2." % [typeToString(t), listSymbolNames(constrCtx.missingFields)])
elif objType.kind == tyDistinct: elif objType.kind == tyDistinct:

View file

@ -246,11 +246,6 @@ Also, C code requires you to specify a forward declaration for functions or
the compiler will assume certain types for the return value and parameters the compiler will assume certain types for the return value and parameters
which will likely make your program crash at runtime. which will likely make your program crash at runtime.
The Nim compiler can generate a C interface header through the `--header`:option:
command-line switch. The generated header will contain all the exported
symbols and the `NimMain` proc which you need to call before any other
Nim code.
Nim invocation example from C Nim invocation example from C
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -269,9 +264,10 @@ Create a ``maths.c`` file with the following content:
.. code-block:: c .. code-block:: c
#include "fib.h"
#include <stdio.h> #include <stdio.h>
extern int fib(int a);
int main(void) int main(void)
{ {
NimMain(); NimMain();
@ -286,13 +282,12 @@ program:
.. code:: cmd .. code:: cmd
nim c --noMain --noLinking --header:fib.h fib.nim nim c --noMain --noLinking fib.nim
gcc -o m -I$HOME/.cache/nim/fib_d -Ipath/to/nim/lib $HOME/.cache/nim/fib_d/*.c maths.c gcc -o m -I$HOME/.cache/nim/fib_d -Ipath/to/nim/lib $HOME/.cache/nim/fib_d/*.c maths.c
The first command runs the Nim compiler with three special options to avoid The first command runs the Nim compiler with three special options to avoid
generating a `main()`:c: function in the generated files, avoid linking the generating a `main()`:c: function in the generated files and to avoid linking the
object files into a final binary, and explicitly generate a header file for C object files into a final binary. All the generated files are placed into the ``nimcache``
integration. All the generated files are placed into the ``nimcache``
directory. That's why the next command compiles the ``maths.c`` source plus directory. That's why the next command compiles the ``maths.c`` source plus
all the ``.c`` files from ``nimcache``. In addition to this path, you also all the ``.c`` files from ``nimcache``. In addition to this path, you also
have to tell the C compiler where to find Nim's ``nimbase.h`` header file. have to tell the C compiler where to find Nim's ``nimbase.h`` header file.
@ -302,7 +297,7 @@ also ask the Nim compiler to generate a statically linked library:
.. code:: cmd .. code:: cmd
nim c --app:staticLib --noMain --header fib.nim nim c --app:staticLib --noMain fib.nim
gcc -o m -Inimcache -Ipath/to/nim/lib libfib.nim.a maths.c gcc -o m -Inimcache -Ipath/to/nim/lib libfib.nim.a maths.c
The Nim compiler will handle linking the source files generated in the The Nim compiler will handle linking the source files generated in the