Thread overview
The @@@BUG@@@ the size of China - std.conv.d - Target parse(Target, Source)(ref Source s, uint radix)
Dec 07, 2015
John Carter
Dec 07, 2015
anonymous
Dec 08, 2015
tsbockman
Dec 08, 2015
John Carter
Dec 08, 2015
tsbockman
December 07, 2015
So whilst attempt to convert from a hex string (without the 0x) to int I bumped into the @@@BUG@@@ the size of China....

https://github.com/D-Programming-Language/phobos/blob/master/std/conv.d#L2270

Is there a bugzilla issue number tracking this?

Searching for conv and parse  in the issue tracker didn't turn it up....

Is this a phobos bug or a compiler bug?

I followed the example in the unit test to get a workaround.... but I don't understand why the workaround works!
December 07, 2015
On 07.12.2015 21:56, John Carter wrote:
> So whilst attempt to convert from a hex string (without the 0x) to int I
> bumped into the @@@BUG@@@ the size of China....
>
> https://github.com/D-Programming-Language/phobos/blob/master/std/conv.d#L2270
>
>
> Is there a bugzilla issue number tracking this?
>
> Searching for conv and parse  in the issue tracker didn't turn it up....
>
> Is this a phobos bug or a compiler bug?
>
> I followed the example in the unit test to get a workaround.... but I
> don't understand why the workaround works!

I'm not sure if there's a bug. `parse` takes the string via `ref`, but string literals are not lvalues so they cannot be passed that way. This should also make it clear why the workaround works: A string literal is not an lvalue, but a variable is.

Maybe whoever added that note thinks that string literals should be lvalues. That would make it a compiler bug. I think that would be a controversial viewpoint, though.

Or the author thinks that `parse` should work with non-lvalues. That would make it a phobos issue. We have std.conv.to for that, though. So weakening the requirements on `parse` isn't exactly necessary. Just use `to` when you don't care about popping the input.
December 07, 2015
On 12/7/15 5:32 PM, anonymous wrote:
> On 07.12.2015 21:56, John Carter wrote:
>> So whilst attempt to convert from a hex string (without the 0x) to int I
>> bumped into the @@@BUG@@@ the size of China....
>>
>> https://github.com/D-Programming-Language/phobos/blob/master/std/conv.d#L2270
>>
>>
>>
>> Is there a bugzilla issue number tracking this?
>>
>> Searching for conv and parse  in the issue tracker didn't turn it up....
>>
>> Is this a phobos bug or a compiler bug?
>>
>> I followed the example in the unit test to get a workaround.... but I
>> don't understand why the workaround works!
>
> I'm not sure if there's a bug. `parse` takes the string via `ref`, but
> string literals are not lvalues so they cannot be passed that way. This
> should also make it clear why the workaround works: A string literal is
> not an lvalue, but a variable is.
>
> Maybe whoever added that note thinks that string literals should be
> lvalues. That would make it a compiler bug. I think that would be a
> controversial viewpoint, though.

https://github.com/D-Programming-Language/phobos/commit/406bbbb9b485c01eb3afeada056ffb46924ee06b

I doubt Andrei thinks string literals should be lvalues.

It's an old bug, if it still exists. But in any case, the description is terrible. A real bug report should be filed.

-Steve
December 08, 2015
On Monday, 7 December 2015 at 22:38:03 UTC, Steven Schveighoffer wrote:
> It's an old bug, if it still exists. But in any case, the description is terrible. A real bug report should be filed.
>
> -Steve

Filed (https://issues.dlang.org/show_bug.cgi?id=15419) and fixed (https://github.com/D-Programming-Language/phobos/pull/3861).

Someone still needs to review the PR, though.

December 08, 2015
On Monday, 7 December 2015 at 20:56:24 UTC, John Carter wrote:
> So whilst attempt to convert from a hex string (without the 0x) to int I bumped into the @@@BUG@@@ the size of China....
>
> https://github.com/D-Programming-Language/phobos/blob/master/std/conv.d#L2270
>
> Is there a bugzilla issue number tracking this?
>
> Searching for conv and parse  in the issue tracker didn't turn it up....
>
> Is this a phobos bug or a compiler bug?
>
> I followed the example in the unit test to get a workaround.... but I don't understand why the workaround works!

Despite the note in the unittest, it's actually not a bug after all - parse is just designed for a different use case than yours.

What you want is T std.conv.to(T, S)(S value, uint radix). For example, this works fine:

import std.conv;
foreach (i; 2..37)
{
    assert(to!int("0", i) == 0);
    assert(to!int("1", i) == 1);
    assert(to!byte("10", i) == i);
}

Or using UFCS, if you prefer:

import std.conv;
foreach (i; 2..37)
{
    assert("0".to!int(i) == 0);
    assert("1".to!int(i) == 1);
    assert("10".to!byte(i) == i);
}

The technical reason that the compiler won't let you use parse that way, is that its first argument is annotated with ref and cannot accept rvalues for memory safety reasons.
December 08, 2015
On Tuesday, 8 December 2015 at 00:40:29 UTC, tsbockman wrote:

> Someone still needs to review the PR, though.

Thanks! Looks like it's been merged already.

It was a double problem... I failed to read the bit about advancing the ref and then the old big @@@BUG@@@ comment in the unit test made me think it was a bug.

Thanks! Great response, I appreciate it!