Add const attribute more strictly

This commit is contained in:
k-hara 2012-07-11 01:02:59 +09:00
parent 6f10e60b88
commit d6ffbd1328
8 changed files with 62 additions and 54 deletions

View file

@ -120,7 +120,7 @@ unittest
{ {
int x; int x;
this(int y) { x = y; } this(int y) { x = y; }
override string toString() { return .to!string(x); } override string toString() const { return .to!string(x); }
} }
auto c = array([new C(1), new C(2)][]); auto c = array([new C(1), new C(2)][]);
//writeln(c); //writeln(c);

View file

@ -991,7 +991,7 @@ unittest
class A class A
{ {
override string toString() { return "an A"; } override string toString() const { return "an A"; }
} }
A a; A a;
assert(to!string(a) == "null"); assert(to!string(a) == "null");
@ -999,7 +999,7 @@ unittest
assert(to!string(a) == "an A"); assert(to!string(a) == "an A");
// Bug 7660 // Bug 7660
class C { override string toString() { return "C"; } } class C { override string toString() const { return "C"; } }
struct S { C c; alias c this; } struct S { C c; alias c this; }
S s; s.c = new C(); S s; s.c = new C();
assert(to!string(s) == "C"); assert(to!string(s) == "C");

View file

@ -124,7 +124,7 @@ class CSVException : Exception
this.col = col; this.col = col;
} }
override string toString() { override string toString() const {
return "(Row: " ~ to!string(row) ~ return "(Row: " ~ to!string(row) ~
", Col: " ~ to!string(col) ~ ") " ~ msg; ", Col: " ~ to!string(col) ~ ") " ~ msg;
} }

View file

@ -1219,14 +1219,15 @@ unittest
class C1 { bool val; alias val this; this(bool v){ val = v; } } class C1 { bool val; alias val this; this(bool v){ val = v; } }
class C2 { bool val; alias val this; this(bool v){ val = v; } class C2 { bool val; alias val this; this(bool v){ val = v; }
override string toString(){ return "C"; } } override string toString() const { return "C"; } }
formatTest( new C1(false), "false" ); formatTest( new C1(false), "false" );
formatTest( new C1(true), "true" ); formatTest( new C1(true), "true" );
formatTest( new C2(false), "C" ); formatTest( new C2(false), "C" );
formatTest( new C2(true), "C" ); formatTest( new C2(true), "C" );
struct S1 { bool val; alias val this; } struct S1 { bool val; alias val this; }
struct S2 { bool val; alias val this; string toString(){ return "S"; } } struct S2 { bool val; alias val this;
string toString() const { return "S"; } }
formatTest( S1(false), "false" ); formatTest( S1(false), "false" );
formatTest( S1(true), "true" ); formatTest( S1(true), "true" );
formatTest( S2(false), "S" ); formatTest( S2(false), "S" );
@ -1417,12 +1418,13 @@ unittest
class C1 { long val; alias val this; this(long v){ val = v; } } class C1 { long val; alias val this; this(long v){ val = v; } }
class C2 { long val; alias val this; this(long v){ val = v; } class C2 { long val; alias val this; this(long v){ val = v; }
override string toString(){ return "C"; } } override string toString() const { return "C"; } }
formatTest( new C1(10), "10" ); formatTest( new C1(10), "10" );
formatTest( new C2(10), "C" ); formatTest( new C2(10), "C" );
struct S1 { long val; alias val this; } struct S1 { long val; alias val this; }
struct S2 { long val; alias val this; string toString(){ return "S"; } } struct S2 { long val; alias val this;
string toString() const { return "S"; } }
formatTest( S1(10), "10" ); formatTest( S1(10), "10" );
formatTest( S2(10), "S" ); formatTest( S2(10), "S" );
} }
@ -1502,12 +1504,13 @@ unittest
class C1 { double val; alias val this; this(double v){ val = v; } } class C1 { double val; alias val this; this(double v){ val = v; } }
class C2 { double val; alias val this; this(double v){ val = v; } class C2 { double val; alias val this; this(double v){ val = v; }
override string toString(){ return "C"; } } override string toString() const { return "C"; } }
formatTest( new C1(2.25), "2.25" ); formatTest( new C1(2.25), "2.25" );
formatTest( new C2(2.25), "C" ); formatTest( new C2(2.25), "C" );
struct S1 { double val; alias val this; } struct S1 { double val; alias val this; }
struct S2 { double val; alias val this; string toString(){ return "S"; } } struct S2 { double val; alias val this;
string toString() const { return "S"; } }
formatTest( S1(2.25), "2.25" ); formatTest( S1(2.25), "2.25" );
formatTest( S2(2.25), "S" ); formatTest( S2(2.25), "S" );
} }
@ -1542,12 +1545,13 @@ unittest
class C1 { cdouble val; alias val this; this(cdouble v){ val = v; } } class C1 { cdouble val; alias val this; this(cdouble v){ val = v; } }
class C2 { cdouble val; alias val this; this(cdouble v){ val = v; } class C2 { cdouble val; alias val this; this(cdouble v){ val = v; }
override string toString(){ return "C"; } } override string toString() const { return "C"; } }
formatTest( new C1(3+2.25i), "3+2.25i" ); formatTest( new C1(3+2.25i), "3+2.25i" );
formatTest( new C2(3+2.25i), "C" ); formatTest( new C2(3+2.25i), "C" );
struct S1 { cdouble val; alias val this; } struct S1 { cdouble val; alias val this; }
struct S2 { cdouble val; alias val this; string toString(){ return "S"; } } struct S2 { cdouble val; alias val this;
string toString() const { return "S"; } }
formatTest( S1(3+2.25i), "3+2.25i" ); formatTest( S1(3+2.25i), "3+2.25i" );
formatTest( S2(3+2.25i), "S" ); formatTest( S2(3+2.25i), "S" );
} }
@ -1580,12 +1584,13 @@ unittest
class C1 { idouble val; alias val this; this(idouble v){ val = v; } } class C1 { idouble val; alias val this; this(idouble v){ val = v; } }
class C2 { idouble val; alias val this; this(idouble v){ val = v; } class C2 { idouble val; alias val this; this(idouble v){ val = v; }
override string toString(){ return "C"; } } override string toString() const { return "C"; } }
formatTest( new C1(2.25i), "2.25i" ); formatTest( new C1(2.25i), "2.25i" );
formatTest( new C2(2.25i), "C" ); formatTest( new C2(2.25i), "C" );
struct S1 { idouble val; alias val this; } struct S1 { idouble val; alias val this; }
struct S2 { idouble val; alias val this; string toString(){ return "S"; } } struct S2 { idouble val; alias val this;
string toString() const { return "S"; } }
formatTest( S1(2.25i), "2.25i" ); formatTest( S1(2.25i), "2.25i" );
formatTest( S2(2.25i), "S" ); formatTest( S2(2.25i), "S" );
} }
@ -1616,12 +1621,13 @@ unittest
class C1 { char val; alias val this; this(char v){ val = v; } } class C1 { char val; alias val this; this(char v){ val = v; } }
class C2 { char val; alias val this; this(char v){ val = v; } class C2 { char val; alias val this; this(char v){ val = v; }
override string toString(){ return "C"; } } override string toString() const { return "C"; } }
formatTest( new C1('c'), "c" ); formatTest( new C1('c'), "c" );
formatTest( new C2('c'), "C" ); formatTest( new C2('c'), "C" );
struct S1 { char val; alias val this; } struct S1 { char val; alias val this; }
struct S2 { char val; alias val this; string toString(){ return "S"; } } struct S2 { char val; alias val this;
string toString() const { return "S"; } }
formatTest( S1('c'), "c" ); formatTest( S1('c'), "c" );
formatTest( S2('c'), "S" ); formatTest( S2('c'), "S" );
} }
@ -1659,10 +1665,11 @@ unittest
unittest unittest
{ {
class C3 { string val; alias val this; this(string s){ val = s; } class C3 { string val; alias val this; this(string s){ val = s; }
override string toString(){ return "C"; } } override string toString() const { return "C"; } }
formatTest( new C3("c3"), "C" ); formatTest( new C3("c3"), "C" );
struct S3 { string val; alias val this; string toString(){ return "S"; } } struct S3 { string val; alias val this;
string toString() const { return "S"; } }
formatTest( S3("s3"), "S" ); formatTest( S3("s3"), "S" );
} }
@ -1731,7 +1738,7 @@ unittest
} }
static if (flags & 4) static if (flags & 4)
string toString(){ return "S"; } string toString() const { return "S"; }
} }
formatTest(S!0b000([0, 1, 2]), "S!(0)([0, 1, 2])"); formatTest(S!0b000([0, 1, 2]), "S!(0)([0, 1, 2])");
formatTest(S!0b001([0, 1, 2]), "[0, 1, 2]"); // Test for bug 7628 formatTest(S!0b001([0, 1, 2]), "[0, 1, 2]"); // Test for bug 7628
@ -1758,7 +1765,7 @@ unittest
} }
static if (flags & 4) static if (flags & 4)
override string toString(){ return "C"; } override string toString() const { return "C"; }
} }
formatTest(new C!0b000([0, 1, 2]), (new C!0b000([])).toString()); formatTest(new C!0b000([0, 1, 2]), (new C!0b000([])).toString());
formatTest(new C!0b001([0, 1, 2]), "[0, 1, 2]"); // Test for bug 7628 formatTest(new C!0b001([0, 1, 2]), "[0, 1, 2]"); // Test for bug 7628
@ -2244,12 +2251,13 @@ unittest
{ {
class C1 { int[char] val; alias val this; this(int[char] v){ val = v; } } class C1 { int[char] val; alias val this; this(int[char] v){ val = v; } }
class C2 { int[char] val; alias val this; this(int[char] v){ val = v; } class C2 { int[char] val; alias val this; this(int[char] v){ val = v; }
override string toString(){ return "C"; } } override string toString() const { return "C"; } }
formatTest( new C1(['c':1, 'd':2]), `['c':1, 'd':2]` ); formatTest( new C1(['c':1, 'd':2]), `['c':1, 'd':2]` );
formatTest( new C2(['c':1, 'd':2]), "C" ); formatTest( new C2(['c':1, 'd':2]), "C" );
struct S1 { int[char] val; alias val this; } struct S1 { int[char] val; alias val this; }
struct S2 { int[char] val; alias val this; string toString(){ return "S"; } } struct S2 { int[char] val; alias val this;
string toString() const { return "S"; } }
formatTest( S1(['c':1, 'd':2]), `['c':1, 'd':2]` ); formatTest( S1(['c':1, 'd':2]), `['c':1, 'd':2]` );
formatTest( S2(['c':1, 'd':2]), "S" ); formatTest( S2(['c':1, 'd':2]), "S" );
} }
@ -2417,7 +2425,7 @@ unittest
class C4 class C4
{ {
mixin(inputRangeCode); mixin(inputRangeCode);
override string toString() { return "[012]"; } override string toString() const { return "[012]"; }
} }
class C5 class C5
{ {
@ -2467,7 +2475,7 @@ unittest
interface Whatever {} interface Whatever {}
class C : Whatever class C : Whatever
{ {
override @property string toString() { return "ab"; } override @property string toString() const { return "ab"; }
} }
Whatever val = new C; Whatever val = new C;
formatTest( val, "ab" ); formatTest( val, "ab" );
@ -2525,9 +2533,9 @@ if ((is(T == struct) || is(T == union)) && (hasToString!(T, Char) || !isBuiltinT
unittest unittest
{ {
// bug 4638 // bug 4638
struct U8 { string toString() { return "blah"; } } struct U8 { string toString() const { return "blah"; } }
struct U16 { wstring toString() { return "blah"; } } struct U16 { wstring toString() const { return "blah"; } }
struct U32 { dstring toString() { return "blah"; } } struct U32 { dstring toString() const { return "blah"; } }
formatTest( U8(), "blah" ); formatTest( U8(), "blah" );
formatTest( U16(), "blah" ); formatTest( U16(), "blah" );
formatTest( U32(), "blah" ); formatTest( U32(), "blah" );
@ -2558,7 +2566,7 @@ unittest
{ {
int n; int n;
string s; string s;
string toString(){ return s; } string toString() const { return s; }
} }
U2 u2; U2 u2;
u2.s = "hello"; u2.s = "hello";
@ -2705,7 +2713,7 @@ unittest
// Test for issue 7869 // Test for issue 7869
struct S struct S
{ {
string toString(){ return ""; } string toString() const { return ""; }
} }
S* p = null; S* p = null;
formatTest( p, "null" ); formatTest( p, "null" );

View file

@ -242,7 +242,7 @@ class OutBuffer
* Convert internal buffer to array of chars. * Convert internal buffer to array of chars.
*/ */
override string toString() override string toString() const
{ {
//printf("OutBuffer.toString()\n"); //printf("OutBuffer.toString()\n");
return cast(string) data[0 .. offset].idup; return cast(string) data[0 .. offset].idup;

View file

@ -132,7 +132,7 @@ class SocketStream: Stream
* Does not return the entire stream because that would * Does not return the entire stream because that would
* require the remote connection to be closed. * require the remote connection to be closed.
*/ */
override string toString() override string toString() const
{ {
return sock.toString(); return sock.toString();
} }

View file

@ -72,7 +72,7 @@ class UTFException : Exception
} }
override string toString() override string toString() const
{ {
import std.string; import std.string;
if(len == 0) if(len == 0)

View file

@ -824,7 +824,7 @@ class Element : Item
* if (e1 == e2) { } * if (e1 == e2) { }
* -------------- * --------------
*/ */
override bool opEquals(const Object o) override const bool opEquals(const Object o)
{ {
const element = toType!(const Element)(o); const element = toType!(const Element)(o);
auto len = items.length; auto len = items.length;
@ -848,7 +848,7 @@ class Element : Item
* if (e1 < e2) { } * if (e1 < e2) { }
* -------------- * --------------
*/ */
override int opCmp(const Object o) override const int opCmp(const Object o)
{ {
const element = toType!(const Element)(o); const element = toType!(const Element)(o);
for (uint i=0; ; ++i) for (uint i=0; ; ++i)
@ -867,7 +867,7 @@ class Element : Item
* You should rarely need to call this function. It exists so that Elements * You should rarely need to call this function. It exists so that Elements
* can be used as associative array keys. * can be used as associative array keys.
*/ */
override hash_t toHash() override const hash_t toHash()
{ {
hash_t hash = tag.toHash(); hash_t hash = tag.toHash();
foreach(item;items) hash += item.toHash(); foreach(item;items) hash += item.toHash();
@ -1233,7 +1233,7 @@ class Comment : Item
* if (item1 == item2) { } * if (item1 == item2) { }
* -------------- * --------------
*/ */
override bool opEquals(const Object o) override const bool opEquals(const Object o)
{ {
const item = toType!(const Item)(o); const item = toType!(const Item)(o);
const t = cast(Comment)item; const t = cast(Comment)item;
@ -1252,7 +1252,7 @@ class Comment : Item
* if (item1 < item2) { } * if (item1 < item2) { }
* -------------- * --------------
*/ */
override int opCmp(const Object o) override const int opCmp(const Object o)
{ {
const item = toType!(const Item)(o); const item = toType!(const Item)(o);
const t = cast(Comment)item; const t = cast(Comment)item;
@ -1266,7 +1266,7 @@ class Comment : Item
* You should rarely need to call this function. It exists so that Comments * You should rarely need to call this function. It exists so that Comments
* can be used as associative array keys. * can be used as associative array keys.
*/ */
override hash_t toHash() { return hash(content); } override const hash_t toHash() { return hash(content); }
/** /**
* Returns a string representation of this comment * Returns a string representation of this comment
@ -1312,7 +1312,7 @@ class CData : Item
* if (item1 == item2) { } * if (item1 == item2) { }
* -------------- * --------------
*/ */
override bool opEquals(const Object o) override const bool opEquals(const Object o)
{ {
const item = toType!(const Item)(o); const item = toType!(const Item)(o);
const t = cast(CData)item; const t = cast(CData)item;
@ -1331,7 +1331,7 @@ class CData : Item
* if (item1 < item2) { } * if (item1 < item2) { }
* -------------- * --------------
*/ */
override int opCmp(const Object o) override const int opCmp(const Object o)
{ {
const item = toType!(const Item)(o); const item = toType!(const Item)(o);
const t = cast(CData)item; const t = cast(CData)item;
@ -1345,7 +1345,7 @@ class CData : Item
* You should rarely need to call this function. It exists so that CDatas * You should rarely need to call this function. It exists so that CDatas
* can be used as associative array keys. * can be used as associative array keys.
*/ */
override hash_t toHash() { return hash(content); } override const hash_t toHash() { return hash(content); }
/** /**
* Returns a string representation of this CData section * Returns a string representation of this CData section
@ -1389,7 +1389,7 @@ class Text : Item
* if (item1 == item2) { } * if (item1 == item2) { }
* -------------- * --------------
*/ */
override bool opEquals(const Object o) override const bool opEquals(const Object o)
{ {
const item = toType!(const Item)(o); const item = toType!(const Item)(o);
const t = cast(Text)item; const t = cast(Text)item;
@ -1408,7 +1408,7 @@ class Text : Item
* if (item1 < item2) { } * if (item1 < item2) { }
* -------------- * --------------
*/ */
override int opCmp(const Object o) override const int opCmp(const Object o)
{ {
const item = toType!(const Item)(o); const item = toType!(const Item)(o);
const t = cast(Text)item; const t = cast(Text)item;
@ -1422,7 +1422,7 @@ class Text : Item
* You should rarely need to call this function. It exists so that Texts * You should rarely need to call this function. It exists so that Texts
* can be used as associative array keys. * can be used as associative array keys.
*/ */
override hash_t toHash() { return hash(content); } override const hash_t toHash() { return hash(content); }
/** /**
* Returns a string representation of this Text section * Returns a string representation of this Text section
@ -1471,7 +1471,7 @@ class XMLInstruction : Item
* if (item1 == item2) { } * if (item1 == item2) { }
* -------------- * --------------
*/ */
override bool opEquals(const Object o) override const bool opEquals(const Object o)
{ {
const item = toType!(const Item)(o); const item = toType!(const Item)(o);
const t = cast(XMLInstruction)item; const t = cast(XMLInstruction)item;
@ -1490,7 +1490,7 @@ class XMLInstruction : Item
* if (item1 < item2) { } * if (item1 < item2) { }
* -------------- * --------------
*/ */
override int opCmp(const Object o) override const int opCmp(const Object o)
{ {
const item = toType!(const Item)(o); const item = toType!(const Item)(o);
const t = cast(XMLInstruction)item; const t = cast(XMLInstruction)item;
@ -1504,7 +1504,7 @@ class XMLInstruction : Item
* You should rarely need to call this function. It exists so that * You should rarely need to call this function. It exists so that
* XmlInstructions can be used as associative array keys. * XmlInstructions can be used as associative array keys.
*/ */
override hash_t toHash() { return hash(content); } override const hash_t toHash() { return hash(content); }
/** /**
* Returns a string representation of this XmlInstruction * Returns a string representation of this XmlInstruction
@ -1550,7 +1550,7 @@ class ProcessingInstruction : Item
* if (item1 == item2) { } * if (item1 == item2) { }
* -------------- * --------------
*/ */
override bool opEquals(const Object o) override const bool opEquals(const Object o)
{ {
const item = toType!(const Item)(o); const item = toType!(const Item)(o);
const t = cast(ProcessingInstruction)item; const t = cast(ProcessingInstruction)item;
@ -1569,7 +1569,7 @@ class ProcessingInstruction : Item
* if (item1 < item2) { } * if (item1 < item2) { }
* -------------- * --------------
*/ */
override int opCmp(const Object o) override const int opCmp(const Object o)
{ {
const item = toType!(const Item)(o); const item = toType!(const Item)(o);
const t = cast(ProcessingInstruction)item; const t = cast(ProcessingInstruction)item;
@ -1583,7 +1583,7 @@ class ProcessingInstruction : Item
* You should rarely need to call this function. It exists so that * You should rarely need to call this function. It exists so that
* ProcessingInstructions can be used as associative array keys. * ProcessingInstructions can be used as associative array keys.
*/ */
override hash_t toHash() { return hash(content); } override const hash_t toHash() { return hash(content); }
/** /**
* Returns a string representation of this ProcessingInstruction * Returns a string representation of this ProcessingInstruction
@ -1599,13 +1599,13 @@ class ProcessingInstruction : Item
abstract class Item abstract class Item
{ {
/// Compares with another Item of same type for equality /// Compares with another Item of same type for equality
abstract override bool opEquals(const Object o); abstract override const bool opEquals(const Object o);
/// Compares with another Item of same type /// Compares with another Item of same type
abstract override int opCmp(const Object o); abstract override const int opCmp(const Object o);
/// Returns the hash of this item /// Returns the hash of this item
abstract override hash_t toHash(); abstract override const hash_t toHash();
/// Returns a string representation of this item /// Returns a string representation of this item
abstract override const string toString(); abstract override const string toString();
@ -2070,7 +2070,7 @@ class ElementParser
/** /**
* Returns that part of the element which has already been parsed * Returns that part of the element which has already been parsed
*/ */
const override string toString() override const string toString()
{ {
assert(elementStart.length >= s.length); assert(elementStart.length >= s.length);
return elementStart[0 .. elementStart.length - s.length]; return elementStart[0 .. elementStart.length - s.length];