December 06, 2011 [Issue 7069] New: Variant Doesn't Handle Const or Immutable Contents | ||||
---|---|---|---|---|
| ||||
http://d.puremagic.com/issues/show_bug.cgi?id=7069 Summary: Variant Doesn't Handle Const or Immutable Contents Product: D Version: D2 Platform: All OS/Version: All Status: NEW Severity: normal Priority: P2 Component: Phobos AssignedTo: nobody@puremagic.com ReportedBy: debio264@gmail.com --- Comment #0 from Andrew Wiley <debio264@gmail.com> 2011-12-05 21:52:07 PST --- Example code: -------- import std.variant; class Bob {} void main() { immutable(Bob) bob = new immutable(Bob)(); Variant v = bob; immutable(Bob) bob2 = v.get!(immutable(Bob))(); } -------- Runtime Error: core.exception.AssertError@C:\D\dmd2\windows\bin\..\..\src\phobos\std\variant.d(286): immutable(Bob) This comes from here in std.variant: -------- static if (is(typeof(*cast(T*) target = *src))) { auto zat = cast(T*) target; if (src) { assert(target, "target must be non-null"); *zat = *src; } } else { // type is not assignable if (src) assert(false, A.stringof); // <-- line 286 } -------- In this case, T is of type immutable(Bob). The check for assignability fails for any immutable type (or any type that contains immutable values) even though technically this code is initializing an immutable value and should be legal. One way to make this work is to rewrite the above code to this: -------- static if (is(typeof(*cast(T*) target = *src))) { auto zat = cast(T*) target; if (src) { assert(target, "target must be non-null"); *zat = *src; } } else static if (is(T V == const(U), U) || is(T V == immutable(U), U)) { auto zat = cast(U*) target; if (src) { assert(target, "target must be non-null"); *zat = *(cast(U*) (src)); } } else { // type is not assignable if (src) assert(false, A.stringof); } -------- Which is basically casting away immutability to copy the reference. This sort of situation makes a compelling argument for the idea of a tail const reference, or a mutable reference to const or immutable data. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
December 07, 2011 [Issue 7069] Variant Doesn't Handle Const or Immutable Contents | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrew Wiley | http://d.puremagic.com/issues/show_bug.cgi?id=7069 Rob Jacques <sandford@jhu.edu> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |sandford@jhu.edu --- Comment #1 from Rob Jacques <sandford@jhu.edu> 2011-12-06 16:32:22 PST --- I've added this to my test suite for improving variant. It causes compile-time errors in my current code base, but I will work on a fix. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
Copyright © 1999-2021 by the D Language Foundation