Bug 5307 – Using to!() should not allow removal of qualifiers

Status
RESOLVED
Resolution
DUPLICATE
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-12-01T22:06:00Z
Last change time
2011-10-09T03:47:01Z
Keywords
patch
Assigned to
andrei
Creator
Jesse.K.Phillips+D

Comments

Comment #0 by Jesse.K.Phillips+D — 2010-12-01T22:06:55Z
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))); } /**
Comment #1 by k.hara.pg — 2011-10-09T03:47:01Z
This issue is a duplication of issue 6288, and it was already resolved. *** This issue has been marked as a duplicate of issue 6288 ***