Zulip Chat Archive

Stream: lean4

Topic: Difference between extern and extern c?


Siddharth Bhat (Jan 13 2023 at 15:43):

Consider:

@[extern "foo_impl"]
private def foo (sz : Nat) (h : Prop) : Nat := 42

@[extern c "bar_impl"]
private def bar (sz : Nat) (h : Prop) : Nat := 42

This generates two different forward declarations, with the forward declaration of bar being incorrect (it assumes that bar has two parameters, while in the runtime, the (h: Prop) ought to have been erased):

LEAN_EXPORT lean_object* bar_impl(lean_object*, lean_object*); // wrong
...
lean_object* foo_impl(lean_object*); // right
// Lean compiler output
// Module: compile
// Imports: Init
#include <lean/lean.h>
#if defined(__clang__)
#pragma clang diagnostic ignored "-Wunused-parameter"
#pragma clang diagnostic ignored "-Wunused-label"
#elif defined(__GNUC__) && !defined(__CLANG__)
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wunused-label"
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
#endif
#ifdef __cplusplus
extern "C" {
#endif
LEAN_EXPORT lean_object* l___private_compile_0__foo___boxed(lean_object*, lean_object*);
LEAN_EXPORT lean_object* bar_impl(lean_object*, lean_object*);
LEAN_EXPORT lean_object* l___private_compile_0__bar___boxed(lean_object*, lean_object*);
lean_object* foo_impl(lean_object*);
LEAN_EXPORT lean_object* l___private_compile_0__foo___boxed(lean_object* x_1, lean_object* x_2) {
_start:
{
lean_object* x_3;
x_3 = foo_impl(x_1);
return x_3;
}
}
LEAN_EXPORT lean_object* l___private_compile_0__bar___boxed(lean_object* x_1, lean_object* x_2) {
_start:
{
lean_object* x_3;
x_3 = bar_impl(x_1);
return x_3;
}
}
lean_object* initialize_Init(uint8_t builtin, lean_object*);
static bool _G_initialized = false;
LEAN_EXPORT lean_object* initialize_compile(uint8_t builtin, lean_object* w) {
lean_object * res;
if (_G_initialized) return lean_io_result_mk_ok(lean_box(0));
_G_initialized = true;
res = initialize_Init(builtin, lean_io_mk_world());
if (lean_io_result_is_error(res)) return res;
lean_dec_ref(res);
return lean_io_result_mk_ok(lean_box(0));
}
#ifdef __cplusplus
}
#endif

So what's the precise semantic difference between @[extern ...] and @[extern c ...]?

Mario Carneiro (Jan 13 2023 at 15:49):

part of the story is described in docs4#Lean.ExternAttrData

Mario Carneiro (Jan 13 2023 at 15:51):

another part is docs4#Lean.isExternC

Siddharth Bhat (Jan 13 2023 at 15:54):

@Mario Carneiro would you consider the above as a miscompile? The file does not compile, since the number of arguments in the forward declaration for bar_impldoes not match the number of arguments bar_impl is invoked with (eg. x_3 = bar_impl(x_1);).

Mario Carneiro (Jan 13 2023 at 15:54):

yes, it's a bug

Siddharth Bhat (Jan 13 2023 at 15:54):

OK, I'll forge a patch.

Mario Carneiro (Jan 13 2023 at 15:55):

I think the buggy calculation is happening in get_extern_borrowed_info in the C++

Siddharth Bhat (Jan 13 2023 at 15:57):

Hmm, I suspect the error is at isExternC at src/Lean/Compiler/ExternAttr.lean

Siddharth Bhat (Jan 13 2023 at 15:58):

(the pattern match some { entries := [ ExternEntry.standard ``all _ ], .. } => true seems too specific, it needs to handle the other possible ways of expressing extern that's described in the docstring)


Last updated: Dec 20 2023 at 11:08 UTC