| |
| Posted by Jesse Phillips | PermalinkReply |
|
Jesse Phillips
| http://d.puremagic.com/issues/show_bug.cgi?id=5307
Summary: Using to!() should not allow removal of qualifiers
Product: D
Version: D2
Platform: All
OS/Version: All
Status: NEW
Keywords: patch
Severity: enhancement
Priority: P2
Component: Phobos
AssignedTo: nobody@puremagic.com
ReportedBy: Jesse.K.Phillips+D@gmail.com
CC: Jesse.K.Phillips+D@gmail.com
--- Comment #0 from Jesse Phillips <Jesse.K.Phillips+D@gmail.com> 2010-12-01 22:06:55 PST ---
Currently you can call 'to' on a const/immutable/shared object and have it converted to a mutable type. I think 'to' should provide safer conversions. In terms of pointers this is already true.
The patch below does a few things.
* Will not conflict with the implicitlyConverts version of to.
* Objects can be cast to types of the same qualifier
* Qualifiers can be changed based on the implicit rules ( immutable -> const)
* Casts are prevent for unsafe operations such as mutable <-> immutable
See assertions for more details.
Index: conv.d ===================================================================
--- conv.d (revision 2204)
+++ conv.d (working copy)
@@ -623,7 +623,13 @@
Object-to-object conversions throw exception when the source is
non-null and the target is null.
*/
-T toImpl(T, S)(S value) if (is(S : Object) && is(T : Object))
+T toImpl(T, S)(S value) if (is(S == class) && is(T == class)
+ && !implicitlyConverts!(S,T) &&
+ ((is(Unqual!S == S) && is(Unqual!T == T)) ||
+ (is(S U == const U) && is(T V == const V)) ||
+ (is(S U == immutable U) && (!is(Unqual!T == T))) ||
+ (is(S U == shared U) && !is(T V == const V) && !is(Unqual!T == T)) ||
+ (is(S U == shared(const U)) && is(T V == shared(const V)))))
{
auto result = cast(T) value;
if (!result && value)
@@ -643,6 +649,9 @@
A a1 = new A, a2 = new B, a3 = new C;
assert(to!(B)(a2) is a2);
assert(to!(C)(a3) is a3);
+ assert(__traits(compiles, to!(const A)(a1)));
+ assert(!__traits(compiles, to!(immutable A)(a1)));
+ assert(!__traits(compiles, to!(shared A)(a1)));
try
{
to!(B)(a3);
@@ -652,6 +661,18 @@
{
//writeln(e);
}
+
+ const A a4 = new B; immutable A a5 = new B; shared A a6 = new B;
+ assert(!__traits(compiles, to!(B)(a4)));
+ assert(!__traits(compiles, to!(B)(a5)));
+ assert(!__traits(compiles, to!(B)(a6)));
+ assert(__traits(compiles, to!(const B)(a4)));
+ assert(__traits(compiles, to!(immutable B)(a5)));
+ assert(__traits(compiles, to!(shared B)(a6)));
+ assert(!__traits(compiles, to!(shared B)(a4)));
+ assert(__traits(compiles, to!(const B)(a5)));
+ assert(__traits(compiles, to!(immutable B)(a6)));
+ assert(__traits(compiles, to!(shared B)(a5)));
}
/**
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
|