Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

to!string with inout enum argument fails to compile #10568

Open
dlangBugzillaToGithub opened this issue Nov 25, 2024 · 1 comment
Open

to!string with inout enum argument fails to compile #10568

dlangBugzillaToGithub opened this issue Nov 25, 2024 · 1 comment

Comments

@dlangBugzillaToGithub
Copy link

snarwin+bugzilla (@pbackus) reported this on 2024-11-25T15:46:38Z

Transfered from https://issues.dlang.org/show_bug.cgi?id=24880

Description

As of DMD 2.109.1, the following program fails to compile:

---
enum E { One }

inout(E) fun(inout(E) e)
{
    import std.conv;
    auto s = e.to!string;
    return e;
}
---

The error message is:

---
/usr/include/dmd/phobos/std/conv.d(1227): Error: variable `std.conv.value` - only parameters or stack-based variables can be `inout`
/usr/include/dmd/phobos/std/conv.d(210): Error: template instance `std.conv.toImpl!(string, inout(E))` error instantiating
bug.d(6):        instantiated from here: `to!(inout(E))`
---

This error occurs because toImpl attempts to pass an inout(E) value as an argument to the template enumRep. The instantiation fails because inout values cannot be used as template arguments:

---
enum E { One }
enum example(T, T value) = true;
alias test = example!(inout(E), E.One);
// Error: only parameters or stack-based variables can be `inout
---
@dlangBugzillaToGithub
Copy link
Author

dfj1esp02 commented on 2024-11-26T18:45:25Z

private template enumRep(T, S, S value)
if (is (T == immutable) && isExactSomeString!T && is(S == enum))
{
    static T enumRep = toStr!T(value);
}

If I mark the argument as immutable, then it compiles:
private template enumRep(T, S, immutable S value)

Presumably for enums it should be fine.

Invoking with enumRep!(immutable(T), immutable(S), member) works too.

@LightBender LightBender removed the P1 label Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants