Remove a number of unsafe immutable casts (#164)

Remove a number of unsafe immutable casts
merged-on-behalf-of: BBasile <BBasile@users.noreply.github.com>
This commit is contained in:
Cameron Ross 2018-06-16 01:40:27 -03:00 committed by The Dlang Bot
parent 6a421f239b
commit 1cda3fce89
9 changed files with 45 additions and 45 deletions

View file

@ -180,7 +180,7 @@ final class Composer
{ {
if(parser_.checkEvent(EventID.Alias)) if(parser_.checkEvent(EventID.Alias))
{ {
immutable event = parser_.getEvent(); const event = parser_.getEvent();
const anchor = event.anchor; const anchor = event.anchor;
enforce((anchor in anchors_) !is null, enforce((anchor in anchors_) !is null,
new ComposerException("Found undefined alias: " ~ anchor, new ComposerException("Found undefined alias: " ~ anchor,
@ -196,7 +196,7 @@ final class Composer
return anchors_[anchor]; return anchors_[anchor];
} }
immutable event = parser_.peekEvent(); const event = parser_.peekEvent();
const anchor = event.anchor; const anchor = event.anchor;
if((anchor !is null) && (anchor in anchors_) !is null) if((anchor !is null) && (anchor in anchors_) !is null)
{ {
@ -236,7 +236,7 @@ final class Composer
///Compose a scalar node. ///Compose a scalar node.
Node composeScalarNode() @safe Node composeScalarNode() @safe
{ {
immutable event = parser_.getEvent(); const event = parser_.getEvent();
const tag = resolver_.resolve(NodeID.Scalar, event.tag, event.value, const tag = resolver_.resolve(NodeID.Scalar, event.tag, event.value,
event.implicit); event.implicit);
@ -256,7 +256,7 @@ final class Composer
ensureAppendersExist(pairAppenderLevel, nodeAppenderLevel); ensureAppendersExist(pairAppenderLevel, nodeAppenderLevel);
auto nodeAppender = &(nodeAppenders_[nodeAppenderLevel]); auto nodeAppender = &(nodeAppenders_[nodeAppenderLevel]);
immutable startEvent = parser_.getEvent(); const startEvent = parser_.getEvent();
const tag = resolver_.resolve(NodeID.Sequence, startEvent.tag, null, const tag = resolver_.resolve(NodeID.Sequence, startEvent.tag, null,
startEvent.implicit); startEvent.implicit);
@ -351,7 +351,7 @@ final class Composer
@safe @safe
{ {
ensureAppendersExist(pairAppenderLevel, nodeAppenderLevel); ensureAppendersExist(pairAppenderLevel, nodeAppenderLevel);
immutable startEvent = parser_.getEvent(); const startEvent = parser_.getEvent();
const tag = resolver_.resolve(NodeID.Mapping, startEvent.tag, null, const tag = resolver_.resolve(NodeID.Mapping, startEvent.tag, null,
startEvent.implicit); startEvent.implicit);
auto pairAppender = &(pairAppenders_[pairAppenderLevel]); auto pairAppender = &(pairAppenders_[pairAppenderLevel]);

View file

@ -266,11 +266,11 @@ struct Dumper
* *
* Throws: YAMLException if unable to emit. * Throws: YAMLException if unable to emit.
*/ */
void emit(Event[] events) @system void emit(Event[] events) @safe
{ {
try try
{ {
auto emitter = Emitter(stream_, canonical_, indent_, textWidth_, lineBreak_); auto emitter = new Emitter(stream_, canonical_, indent_, textWidth_, lineBreak_);
foreach(ref event; events) foreach(ref event; events)
{ {
emitter.emit(event); emitter.emit(event);

View file

@ -308,11 +308,11 @@ struct Loader
// Parse and return all events. Used for debugging. // Parse and return all events. Used for debugging.
immutable(Event)[] parse() @safe Event[] parse() @safe
{ {
try try
{ {
immutable(Event)[] result; Event[] result;
while(parser_.checkEvent()) while(parser_.checkEvent())
{ {
result ~= parser_.getEvent(); result ~= parser_.getEvent();

View file

@ -182,13 +182,13 @@ final class Parser
* *
* Must not be called if there are no events left. * Must not be called if there are no events left.
*/ */
immutable(Event) peekEvent() @trusted Event peekEvent() @safe
{ {
if(currentEvent_.isNull && state_ !is null) if(currentEvent_.isNull && state_ !is null)
{ {
currentEvent_ = state_(); currentEvent_ = state_();
} }
if(!currentEvent_.isNull){return cast(immutable Event)currentEvent_;} if(!currentEvent_.isNull){return currentEvent_;}
assert(false, "No event left to peek"); assert(false, "No event left to peek");
} }
@ -197,7 +197,7 @@ final class Parser
* *
* Must not be called if there are no events left. * Must not be called if there are no events left.
*/ */
immutable(Event) getEvent() @trusted Event getEvent() @safe
{ {
//Get the next event and proceed further. //Get the next event and proceed further.
if(currentEvent_.isNull && state_ !is null) if(currentEvent_.isNull && state_ !is null)
@ -207,7 +207,7 @@ final class Parser
if(!currentEvent_.isNull) if(!currentEvent_.isNull)
{ {
immutable Event result = cast(immutable Event)currentEvent_; Event result = currentEvent_;
currentEvent_.id = EventID.Invalid; currentEvent_.id = EventID.Invalid;
return result; return result;
} }
@ -336,7 +336,7 @@ final class Parser
} }
/// Process directives at the beginning of a document. /// Process directives at the beginning of a document.
TagDirective[] processDirectives() @system TagDirective[] processDirectives() @safe
{ {
// Destroy version and tag handles from previous document. // Destroy version and tag handles from previous document.
YAMLVersion_ = null; YAMLVersion_ = null;
@ -534,11 +534,11 @@ final class Parser
/// Handle escape sequences in a double quoted scalar. /// Handle escape sequences in a double quoted scalar.
/// ///
/// Moved here from scanner as it can't always be done in-place with slices. /// Moved here from scanner as it can't always be done in-place with slices.
string handleDoubleQuotedScalarEscapes(char[] tokenValue) const @system string handleDoubleQuotedScalarEscapes(char[] tokenValue) const @safe
{ {
string notInPlace; string notInPlace;
bool inEscape = false; bool inEscape = false;
auto appender = appender!(char[])(); auto appender = appender!(string)();
for(char[] oldValue = tokenValue; !oldValue.empty();) for(char[] oldValue = tokenValue; !oldValue.empty();)
{ {
const dchar c = oldValue.front(); const dchar c = oldValue.front();
@ -604,7 +604,7 @@ final class Parser
assert(false, "Scanner must handle unsupported escapes"); assert(false, "Scanner must handle unsupported escapes");
} }
return notInPlace is null ? cast(string)appender.data : notInPlace; return notInPlace is null ? appender.data : notInPlace;
} }
/** /**

View file

@ -71,7 +71,7 @@ void run(D)(string testName, D testFunction,
} }
else else
{ {
results ~= execute(testName, testFunction, cast(string[])[]); results ~= execute(testName, testFunction, string[].init);
} }
display(results); display(results);
} }

View file

@ -105,10 +105,10 @@ Node[] constructCustom() @safe
Node[] constructFloat() @safe Node[] constructFloat() @safe
{ {
return [Node([pair("canonical", cast(real)685230.15), return [Node([pair("canonical", 685230.15L),
pair("exponential", cast(real)685230.15), pair("exponential", 685230.15L),
pair("fixed", cast(real)685230.15), pair("fixed", 685230.15L),
pair("sexagesimal", cast(real)685230.15), pair("sexagesimal", 685230.15L),
pair("negative infinity", -real.infinity), pair("negative infinity", -real.infinity),
pair("not a number", real.nan)])]; pair("not a number", real.nan)])];
} }
@ -236,8 +236,8 @@ Node[] constructValue() @safe
return[Node([pair("link with", return[Node([pair("link with",
[Node("library1.dll"), Node("library2.dll")])]), [Node("library1.dll"), Node("library2.dll")])]),
Node([pair("link with", Node([pair("link with",
[Node([pair("=", "library1.dll"), pair("version", cast(real)1.2)]), [Node([pair("=", "library1.dll"), pair("version", 1.2L)]),
Node([pair("=", "library2.dll"), pair("version", cast(real)2.3)])])])]; Node([pair("=", "library2.dll"), pair("version", 2.3L)])])])];
} }
Node[] duplicateMergeKey() @safe Node[] duplicateMergeKey() @safe
@ -251,7 +251,7 @@ Node[] duplicateMergeKey() @safe
Node[] floatRepresenterBug() @safe Node[] floatRepresenterBug() @safe
{ {
return [Node([pair(cast(real)1.0, 1L), return [Node([pair(1.0L, 1L),
pair(real.infinity, 10L), pair(real.infinity, 10L),
pair(-real.infinity, -10L), pair(-real.infinity, -10L),
pair(real.nan, 100L)])]; pair(real.nan, 100L)])];
@ -264,9 +264,9 @@ Node[] invalidSingleQuoteBug() @safe
Node[] moreFloats() @safe Node[] moreFloats() @safe
{ {
return [Node([Node(cast(real)0.0), return [Node([Node(0.0L),
Node(cast(real)1.0), Node(1.0L),
Node(cast(real)-1.0), Node(-1.0L),
Node(real.infinity), Node(real.infinity),
Node(-real.infinity), Node(-real.infinity),
Node(real.nan), Node(real.nan),
@ -275,7 +275,7 @@ Node[] moreFloats() @safe
Node[] negativeFloatBug() @safe Node[] negativeFloatBug() @safe
{ {
return [Node(cast(real)-1.0)]; return [Node(-1.0L)];
} }
Node[] singleDotFloatBug() @safe Node[] singleDotFloatBug() @safe

View file

@ -28,7 +28,7 @@ import dyaml.token;
/// events2 = Second event array to compare. /// events2 = Second event array to compare.
/// ///
/// Returns: true if the events are equivalent, false otherwise. /// Returns: true if the events are equivalent, false otherwise.
bool compareEvents(Event[] events1, Event[] events2) @system bool compareEvents(Event[] events1, Event[] events2) @safe
{ {
if(events1.length != events2.length){return false;} if(events1.length != events2.length){return false;}
@ -78,11 +78,11 @@ bool compareEvents(Event[] events1, Event[] events2) @system
/// Params: dataFilename = YAML file to parse. /// Params: dataFilename = YAML file to parse.
/// canonicalFilename = Canonical YAML file used as dummy to determine /// canonicalFilename = Canonical YAML file used as dummy to determine
/// which data files to load. /// which data files to load.
void testEmitterOnData(string dataFilename, string canonicalFilename) @system void testEmitterOnData(string dataFilename, string canonicalFilename) @safe
{ {
//Must exist due to Anchor, Tags reference counts. //Must exist due to Anchor, Tags reference counts.
auto loader = Loader.fromFile(dataFilename); auto loader = Loader.fromFile(dataFilename);
auto events = cast(Event[])loader.parse(); auto events = loader.parse();
auto emitStream = new YMemoryStream; auto emitStream = new YMemoryStream;
Dumper(emitStream).emit(events); Dumper(emitStream).emit(events);
@ -90,14 +90,14 @@ void testEmitterOnData(string dataFilename, string canonicalFilename) @system
{ {
writeln(dataFilename); writeln(dataFilename);
writeln("ORIGINAL:\n", readText(dataFilename)); writeln("ORIGINAL:\n", readText(dataFilename));
writeln("OUTPUT:\n", cast(string)emitStream.data); writeln("OUTPUT:\n", cast(char[])emitStream.data);
} }
auto loader2 = Loader.fromBuffer(emitStream.data); auto loader2 = Loader.fromBuffer(emitStream.data);
loader2.name = "TEST"; loader2.name = "TEST";
loader2.constructor = new Constructor; loader2.constructor = new Constructor;
loader2.resolver = new Resolver; loader2.resolver = new Resolver;
auto newEvents = cast(Event[])loader2.parse(); auto newEvents = loader2.parse();
assert(compareEvents(events, newEvents)); assert(compareEvents(events, newEvents));
} }
@ -106,11 +106,11 @@ void testEmitterOnData(string dataFilename, string canonicalFilename) @system
/// comparing events from parsing the emitted result with originally parsed events. /// comparing events from parsing the emitted result with originally parsed events.
/// ///
/// Params: canonicalFilename = Canonical YAML file to parse. /// Params: canonicalFilename = Canonical YAML file to parse.
void testEmitterOnCanonical(string canonicalFilename) @system void testEmitterOnCanonical(string canonicalFilename) @safe
{ {
//Must exist due to Anchor, Tags reference counts. //Must exist due to Anchor, Tags reference counts.
auto loader = Loader.fromFile(canonicalFilename); auto loader = Loader.fromFile(canonicalFilename);
auto events = cast(Event[])loader.parse(); auto events = loader.parse();
foreach(canonical; [false, true]) foreach(canonical; [false, true])
{ {
auto emitStream = new YMemoryStream; auto emitStream = new YMemoryStream;
@ -120,13 +120,13 @@ void testEmitterOnCanonical(string canonicalFilename) @system
static if(verbose) static if(verbose)
{ {
writeln("OUTPUT (canonical=", canonical, "):\n", writeln("OUTPUT (canonical=", canonical, "):\n",
cast(string)emitStream.data); cast(char[])emitStream.data);
} }
auto loader2 = Loader.fromBuffer(emitStream.data); auto loader2 = Loader.fromBuffer(emitStream.data);
loader2.name = "TEST"; loader2.name = "TEST";
loader2.constructor = new Constructor; loader2.constructor = new Constructor;
loader2.resolver = new Resolver; loader2.resolver = new Resolver;
auto newEvents = cast(Event[])loader2.parse(); auto newEvents = loader2.parse();
assert(compareEvents(events, newEvents)); assert(compareEvents(events, newEvents));
} }
} }
@ -138,13 +138,13 @@ void testEmitterOnCanonical(string canonicalFilename) @system
/// Params: dataFilename = YAML file to parse. /// Params: dataFilename = YAML file to parse.
/// canonicalFilename = Canonical YAML file used as dummy to determine /// canonicalFilename = Canonical YAML file used as dummy to determine
/// which data files to load. /// which data files to load.
void testEmitterStyles(string dataFilename, string canonicalFilename) @system void testEmitterStyles(string dataFilename, string canonicalFilename) @safe
{ {
foreach(filename; [dataFilename, canonicalFilename]) foreach(filename; [dataFilename, canonicalFilename])
{ {
//must exist due to Anchor, Tags reference counts //must exist due to Anchor, Tags reference counts
auto loader = Loader.fromFile(canonicalFilename); auto loader = Loader.fromFile(canonicalFilename);
auto events = cast(Event[])loader.parse(); auto events = loader.parse();
foreach(flowStyle; [CollectionStyle.Block, CollectionStyle.Flow]) foreach(flowStyle; [CollectionStyle.Block, CollectionStyle.Flow])
{ {
foreach(style; [ScalarStyle.Literal, ScalarStyle.Folded, foreach(style; [ScalarStyle.Literal, ScalarStyle.Folded,
@ -178,20 +178,20 @@ void testEmitterStyles(string dataFilename, string canonicalFilename) @system
{ {
writeln("OUTPUT (", filename, ", ", to!string(flowStyle), ", ", writeln("OUTPUT (", filename, ", ", to!string(flowStyle), ", ",
to!string(style), ")"); to!string(style), ")");
writeln(emitStream.data); writeln(cast(char[])emitStream.data);
} }
auto loader2 = Loader.fromBuffer(emitStream.data); auto loader2 = Loader.fromBuffer(emitStream.data);
loader2.name = "TEST"; loader2.name = "TEST";
loader2.constructor = new Constructor; loader2.constructor = new Constructor;
loader2.resolver = new Resolver; loader2.resolver = new Resolver;
auto newEvents = cast(Event[])loader2.parse(); auto newEvents = loader2.parse();
assert(compareEvents(events, newEvents)); assert(compareEvents(events, newEvents));
} }
} }
} }
} }
@system unittest @safe unittest
{ {
printProgress("D:YAML Emitter unittest"); printProgress("D:YAML Emitter unittest");
run("testEmitterOnData", &testEmitterOnData, ["data", "canonical"]); run("testEmitterOnData", &testEmitterOnData, ["data", "canonical"]);

View file

@ -51,7 +51,7 @@ void testUnicodeInput(string unicodeFilename) @safe
string data = readText(unicodeFilename); string data = readText(unicodeFilename);
string expected = data.split().join(" "); string expected = data.split().join(" ");
Node output = Loader.fromBuffer(cast(ubyte[])data.to!(char[])).load(); Node output = Loader.fromString(data).load();
assert(output.as!string == expected); assert(output.as!string == expected);
foreach(buffer; [cast(ubyte[])(bom16() ~ data.to!(wchar[])), foreach(buffer; [cast(ubyte[])(bom16() ~ data.to!(wchar[])),

View file

@ -21,7 +21,7 @@ void runReader(ubyte[] fileData) @safe
{ {
try try
{ {
auto reader = new Reader(cast(ubyte[])fileData); auto reader = new Reader(fileData);
while(reader.peek() != '\0') { reader.forward(); } while(reader.peek() != '\0') { reader.forward(); }
} }
catch(ReaderException e) catch(ReaderException e)