[Issue 5069] New: No missing extern(C) error
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sun Oct 17 05:22:01 PDT 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5069
Summary: No missing extern(C) error
Product: D
Version: D2
Platform: x86
OS/Version: Windows
Status: NEW
Keywords: accepts-invalid, diagnostic
Severity: normal
Priority: P2
Component: DMD
AssignedTo: nobody at puremagic.com
ReportedBy: bearophile_hugs at eml.cc
--- Comment #0 from bearophile_hugs at eml.cc 2010-10-17 05:21:22 PDT ---
This is a wrong D2 program:
import std.c.stdlib: qsort;
import std.c.stdio: printf;
int compare(const void *a, const void *b) {
return *cast(int*)a - *cast(int*)b;
}
void main () {
int[] values = [40, 10, 100, 90, 20, 25];
for (int n = 0; n < 6; n++)
printf ("%d ", values[n]);
printf("\n");
qsort(values.ptr, 6, int.sizeof, &compare);
for (int n = 0; n < 6; n++)
printf ("%d ", values[n]);
printf("\n");
}
With DMD 2.049 it compiles with no errors, and then sefaults at runtime.
To fix it the compare() must have a C calling convention:
extern(C) int compare(const void *a, const void *b) {
DMD 1.026 avoids that runtime error with a useful compile-time error message
that I'd like in DMD2 too:
Line 15: function std.c.stdlib.qsort (void*,uint,uint,int(C *)(void*, void*))
does not match parameter types (int*,int,uint,int(*)(void*, void*))
Line 15: Error: cannot implicitly convert expression (& compare) of type
int(*)(void*, void*) to int(C *)(void*, void*)
A better single error message for D2 may say "extern(C)" too somewhere in the
error message, instead of just saying a less clear "expression (& compare) of
type int(*)(void*, void*) to int(C *)(void*, void*)". Something like:
Line 15: Error: cannot implicitly convert expression (& compare) of type int
function(const void*, const void*) to extern(C) int function(const void*, const
void*)
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list