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

#define Val_some #5154

Closed
vicuna opened this issue Sep 23, 2010 · 9 comments · Fixed by #9569 or ocaml-opam/ocaml-mccs#30
Closed

#define Val_some #5154

vicuna opened this issue Sep 23, 2010 · 9 comments · Fixed by #9569 or ocaml-opam/ocaml-mccs#30

Comments

@vicuna
Copy link

vicuna commented Sep 23, 2010

Original bug ID: 5154
Reporter: @ygrek
Assigned to: @damiendoligez
Status: assigned (set by @mshinwell on 2016-12-08T08:46:17Z)
Resolution: open
Priority: normal
Severity: feature
Version: 3.11.2
Category: runtime system and C interface
Tags: patch
Monitored by: @hcarty

Bug description

OCaml C interface defines many macros to construct/access primitive ocaml values from C code. One thing that I often find missing and have to copy-paste myself in almost every binding is code to handle option type.

Additional information

#define Val_none Val_int(0)

value Val_some(value v)
{
CAMLparam1(v);
CAMLlocal1(some);
some = caml_alloc_small(1, 0);
Field(some, 0) = v;
CAMLreturn(some);
}

#define Some_val(v) Field(v,0)

@vicuna
Copy link
Author

vicuna commented Dec 8, 2016

Comment author: @mshinwell

@doligez I find the lack of this functionality to be a bit of a nuisance as well; what do you think?

@github-actions
Copy link

This issue has been open one year with no activity. Consequently, it is being marked with the "stale" label. What this means is that the issue will be automatically closed in 30 days unless more comments are added or the "stale" label is removed. Comments that provide new information on the issue are especially welcome: is it still reproducible? did it appear in other contexts? how critical is it? etc.

@github-actions github-actions bot added the Stale label May 15, 2020
@ygrek
Copy link
Contributor

ygrek commented May 15, 2020

Val_some(activity)

@yallop
Copy link
Member

yallop commented May 15, 2020

@ygrek: would you consider submitting a PR?

@nojb
Copy link
Contributor

nojb commented May 15, 2020

I took the liberty of making a PR with his patch #9569

@MisterDA
Copy link
Contributor

MisterDA commented Jun 15, 2020

Just for reference, some projects had already defined some of the macros. For instance, see ocaml-mccs. Although they share the same definition, some compilers raise warnings when redefining macros (e.g. MSVC 2019). In some setups, warnings are turned to errors and the build of a package fails (e.g. building Opam…).

I’m submitting patches to fix that, I think that checking OCAML_VERSION is the best way:

#if OCAML_VERSION < 41200
#define Val_none Val_int(0)
#define Some_val(v)  Field(v, 0)
#endif

EDIT: changed <= 41100 to < 41200.

@nojb
Copy link
Contributor

nojb commented Jun 15, 2020

Thanks for the heads-up @MisterDA! Yes, checking OCAML_VERSION seems a good fix.

@MisterDA
Copy link
Contributor

But which version? If the commit won’t be included before 4.12, then < 41200 is a better choice in case there’s a 4.11.x.

@nojb
Copy link
Contributor

nojb commented Jun 15, 2020

Indeed, the patch in question will only be included in 4.12, and not in the 4.11.x releases.

MisterDA added a commit to MisterDA/ocaml-mccs that referenced this issue Jun 15, 2020
Macros `Val_none` and `Some_val` are part of the standard library
starting with OCaml 4.12.

In some environments, redefining a macro triggers a compiler warning.
When warnings are treated as errors, the build fails.

Issue discussing the introduction of the macros, the problem, and the
fix: ocaml/ocaml#5154

Commit introducing the macros in the standard library:
ocaml/ocaml@973eeb1
MisterDA added a commit to MisterDA/ocaml that referenced this issue Jul 2, 2020
If an OCaml C library already defines some of the new `Val_none`,
`Some_val`, `Is_none`, `Is_some`, `caml_alloc_some`, or `Tag_some`
macros; then the C compiler will likely warn for macro redefinition,
even if the macro definition are identical. In some setups that always
turn warnings to errors, this will block the compilation of the
library.

This problem happens in [ocaml-mccs][1].

The proposed [fix][2] for libraries is to only define the macros when
compiling with OCaml strictly older than 4.12, i.e.:

    #if OCAML_VERSION < 41200
    #define ...
    #endif

[1]: ocaml-opam/ocaml-mccs#30
[2]: ocaml#5154 (comment)
MisterDA added a commit to MisterDA/ocaml that referenced this issue Jul 17, 2020
If an OCaml C library already defines some of the new `Val_none`,
`Some_val`, `Is_none`, `Is_some`, `caml_alloc_some`, or `Tag_some`
macros; then the C compiler will likely warn for macro redefinition,
even if the macro definition are identical.

This problem happens in [ocaml-mccs][1].

The proposed [fix][2] for libraries is to only define the macros when
compiling with OCaml strictly older than 4.12, i.e.:

    #if OCAML_VERSION < 41200
    #define ...
    #endif

[1]: ocaml-opam/ocaml-mccs#30
[2]: ocaml#5154 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants