From 37a661b034079a3fb8d3c9fab3a70adc7ef09f32 Mon Sep 17 00:00:00 2001 From: kiith-sa Date: Thu, 30 Aug 2012 15:38:51 +0200 Subject: [PATCH] Fixed compilation with DMD 2.058. There are some hacks to allow toHash() methods to be const nothrow @safe, these are due to Phobos problems (e.g. Variant.toHash() is not const nor nothrow). --- dyaml/constructor.d | 2 +- dyaml/exception.d | 4 +++- dyaml/node.d | 28 +++++++++++++++++++++++----- dyaml/reader.d | 2 +- dyaml/scanner.d | 2 +- dyaml/zerostring.d | 17 +++++++++++++---- 6 files changed, 42 insertions(+), 13 deletions(-) diff --git a/dyaml/constructor.d b/dyaml/constructor.d index 8a86773..ef3b532 100644 --- a/dyaml/constructor.d +++ b/dyaml/constructor.d @@ -550,7 +550,7 @@ ubyte[] constructBinary(ref Node node) throw new Exception("Unable to decode base64 value: " ~ e.msg); } } - catch(UtfException e) + catch(UTFException e) { throw new Exception("Unable to decode base64 value: " ~ e.msg); } diff --git a/dyaml/exception.d b/dyaml/exception.d index 8d0f6d9..3ec6cf4 100644 --- a/dyaml/exception.d +++ b/dyaml/exception.d @@ -24,7 +24,7 @@ class YAMLException : Exception } ///Position in a YAML stream, used for error messages. -align(1) struct Mark +struct Mark { private: ///Line number. @@ -49,6 +49,8 @@ align(1) struct Mark } } +static assert(Mark.sizeof == 4, "Unexpected Mark size"); + package: //Base class of YAML exceptions with marked positions of the problem. abstract class MarkedYAMLException : YAMLException diff --git a/dyaml/node.d b/dyaml/node.d index 2433e5b..83cfdf8 100644 --- a/dyaml/node.d +++ b/dyaml/node.d @@ -200,6 +200,9 @@ struct Node //Node collection style. Used to remember style this node was loaded with. CollectionStyle collectionStyle = CollectionStyle.Invalid; + static assert(Value.sizeof <= 24, "Unexpected YAML value size"); + static assert(Node.sizeof <= 48, "Unexpected YAML node size"); + public: /** * Construct a Node from a value. @@ -1271,17 +1274,32 @@ struct Node } ///Compare with another _node. - const int opCmp(ref const Node node) + int opCmp(ref const Node node) const { return cmp!true(node); } //Compute hash of the node. - const hash_t toHash() + hash_t toHash() const nothrow @safe { - const tagHash = tag_.isNull ? 0 : tag_.toHash(); - //Variant toHash is not const at the moment, so we need to const-cast. - return tagHash + (cast(Value)value_).toHash(); + // Hack to allow const nothrow @safe. + // Should be rewritten once std.variant is fixed. + hash_t unsafeHash() nothrow @trusted + { + const tagHash = tag_.isNull ? 0 : tag_.toHash(); + //Variant toHash is not nothrow at the moment, so we need to catch + //an exception that is never thrown. + try + { + //Variant toHash is not const at the moment, so we need to const-cast. + return tagHash + (cast(Value)value_).toHash(); + } + catch(Exception e) + { + assert(false, "Unexpected exception caught"); + } + } + return unsafeHash(); } package: diff --git a/dyaml/reader.d b/dyaml/reader.d index 17cda41..8964348 100644 --- a/dyaml/reader.d +++ b/dyaml/reader.d @@ -315,7 +315,7 @@ final class Reader void handleLoadCharsException(Exception e, ulong oldPosition) { try{throw e;} - catch(UtfException e) + catch(UTFException e) { const position = stream_.position; throw new ReaderException(format("Unicode decoding error between bytes ", diff --git a/dyaml/scanner.d b/dyaml/scanner.d index cbcbae5..ee7e417 100644 --- a/dyaml/scanner.d +++ b/dyaml/scanner.d @@ -1609,7 +1609,7 @@ final class Scanner { throw new Error("While scanning a " ~ name, startMark, e.msg, mark); } - catch(UtfException e) + catch(UTFException e) { throw new Error("While scanning a " ~ name, startMark, e.msg, mark); } diff --git a/dyaml/zerostring.d b/dyaml/zerostring.d index cee2cba..990995a 100644 --- a/dyaml/zerostring.d +++ b/dyaml/zerostring.d @@ -36,7 +36,7 @@ struct ZeroString(string TypeName) } ///Get the string. - @property string get() const + @property string get() const nothrow @trusted in{assert(!isNull());} body { @@ -51,12 +51,12 @@ struct ZeroString(string TypeName) } ///Compute a hash. - hash_t toHash() const + hash_t toHash() const nothrow @safe in{assert(!isNull);} body { auto str = get(); - return typeid(string).getHash(&str); + return getHash(str); } ///Compare with another string. @@ -68,5 +68,14 @@ struct ZeroString(string TypeName) } ///Is this string null (invalid)? - @property bool isNull() const {return str_ is null;} + @property bool isNull() const nothrow @safe {return str_ is null;} + + private: + ///Hack to allow toHash to be @safe. + // + //To remove this hack, need a typeid(string).getHash() replacement that does not take a pointer. + hash_t getHash(ref string str) const nothrow @trusted + { + return typeid(string).getHash(&str); + } }