Bug 1603 – String literals bind to pointer types

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
All
Creation time
2007-10-20T12:12:00Z
Last change time
2015-06-09T05:15:17Z
Keywords
spec
Assigned to
bugzilla
Creator
andrei

Comments

Comment #0 by andrei — 2007-10-20T12:12:18Z
The following code does not compile: module A; extern(C) void foo(in char*) {} // end of module module B; void foo(string) {} // end of module import A, B; main() { foo("string"); } The call is ambiguous although it should make it to B.foo.
Comment #1 by smjg — 2007-10-21T08:09:05Z
If it's ambiguous, it should error. This kind of hijacking is exactly why overload sets are defined the way they are.
Comment #2 by andrei — 2007-10-21T11:20:19Z
Literal strings should not bind to const char *. The overload sets corresponding to the two functions are disjoint.
Comment #3 by smjg — 2007-10-21T12:28:31Z
(In reply to comment #2) > Literal strings should not bind to const char *. By my experiments, string literals are simultaneously of all of these types: invariant(char)* invariant(wchar)* invariant(dchar)* invariant(char)[] invariant(wchar)[] invariant(dchar)[] and therefore to the const versions of these types. But there doesn't seem to be any documentation to the fact that they bind to the pointer types (unless I haven't found it). So the issue has nothing to do with overload sets. Try this and see. ---------- extern(C) void foo(in char*) {} void main() { foo("string"); } invariant(char)* cp = "string"; invariant(wchar)* wp = "string"; invariant(dchar)* dp = "string"; invariant(char)[] ca = "string"; invariant(wchar)[] wa = "string"; invariant(dchar)[] da = "string"; ---------- And why is this issue set to version 2.007? That version isn't even out yet.
Comment #4 by andrei — 2007-10-21T12:33:31Z
(In reply to comment #3) > (In reply to comment #2) > > Literal strings should not bind to const char *. > > By my experiments, string literals are simultaneously of all of these types: > > invariant(char)* > invariant(wchar)* > invariant(dchar)* > invariant(char)[] > invariant(wchar)[] > invariant(dchar)[] > > and therefore to the const versions of these types. But there doesn't seem to > be any documentation to the fact that they bind to the pointer types (unless I > haven't found it). > > So the issue has nothing to do with overload sets. You are right. I suggest we leave this bug opened with the new title until a formal resolution is found. Although accepting conversion of string literals to invariant char* is sound, in my opinion that's a bit too friendly towards C functions, and it creates unnecessary ambiguity. It's not too hard to add a .ptr when calling a C function. (I've set the version to unspecified.)
Comment #5 by bugzilla — 2009-03-11T14:56:15Z
Fixed dmd 2.026