D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6632 - toUTFz sometimes does not work with const parameters
Summary: toUTFz sometimes does not work with const parameters
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: Other Windows
: P2 major
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-09-08 17:47 UTC by Andrej Mitrovic
Modified: 2012-01-04 21:56 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Andrej Mitrovic 2011-09-08 17:47:46 UTC
import std.utf;

void main()
{
    const char[] c = "";
    auto res1 = toUTFz!(const(char)*)(c);  // doesn't work
    auto res2 = toUTFz!(const(wchar)*)(c);  // works ok
}

D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\utf.d(1481): Error: variable std.utf.toUTFz!(const(char)*,const(char[])).toUTFz.str cannot modify const

test.d(8): Error: template instance std.utf.toUTFz!(const(char)*,const(char[])) error instantiating

Funny thing is it works fine with immutable:

immutable char[] c = "";
auto res1 = toUTFz!(const(char)*)(c);  // ok
Comment 1 zeljkog 2011-09-09 06:36:01 UTC
It is compiler issue:

import std.stdio;

void f(S)(S str){
	writeln(str);
}

alias f!(string) fc;
alias f!(wstring) fc;

void main(){
	fc("foo");      // L11

//~ 	fc("foo"c);	 // works
//~ 	auto s = "foo";  
//~  	fc(s);       // works  
}

//~ Compilation breaks with message:

//~ bug.d(11): Error: function alias bug.f called with argument types:
//~ 	((string))
//~ matches both:
//~ 	bug.f!(string).f(string str)
//~ and:
//~ 	bug.f!(immutable(wchar)[]).f(immutable(wchar)[] str)
Comment 2 zeljkog 2011-09-09 06:44:55 UTC
Sorry, it's another issue.
Comment 3 Andrej Mitrovic 2011-11-09 09:58:23 UTC
import std.utf;

const wchar[] foo = "foo";

void main()
{
    auto wptr = toUTFz!(const(wchar)*)(foo);  // NG
}

It would be great if toUTFz worked in *all* cases.
Comment 4 Jonathan M Davis 2011-11-09 10:19:23 UTC
Well obviously, the fact that it doesn't is a bug. I expect that I'll have it fixed before the next release. I probably should have had it fixed for the last one but have been busy and didn't get around to it.
Comment 5 Andrej Mitrovic 2011-11-09 10:51:57 UTC
Thanks Jonathan.

This kind of call is common when passing wstring constants to Win API functions.
Comment 6 Jonathan M Davis 2011-11-12 11:00:47 UTC
By the way, in case you didn't notice, I should point out that thanks to the discussion on the newsgroup about it, toUTF16z is no longer scheduled for deprecation. It's still using its old implementation internally, but should be switched over to using toUTFz by the next release (which will allow it to take all of the string types). So, if your use case of toUTFz matches toUTF16z, then you can use that.
Comment 7 Andrej Mitrovic 2011-11-12 11:24:01 UTC
Ok good to know, this will help porting older code. Thanks.
Comment 8 Jonathan M Davis 2011-11-20 04:52:50 UTC
https://github.com/D-Programming-Language/phobos/pull/279
Comment 9 Andrej Mitrovic 2012-01-04 06:57:43 UTC
Fixed in 2.057, thanks Jonathan.
Comment 10 Jonathan M Davis 2012-01-04 21:56:09 UTC
LOL. My fix actually hasn't been checked in yet. The changes to IFTI made it completely unnecessary (and is why it works in 2.057). In fact, I just removed my fix from the pull request for adding toUTF, since there's no reason to have it anymore. The change to how IFTI deals with const and immutable arrays is moste definitely a welcome one.