www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Untyped string variable fails silently. No compiler warning given.

reply "Gary Willoughby" <dev nomad.so> writes:
Rather nasty bug that silently fails. Linux DMD64 D Compiler 
v2.065

import std.stdio;
import std.array;
import std.conv;
import std.digest.crc;
import std.random;

class Foo
{
	public string generateHash()
	{
		// The following variable is untyped
		// yet no compiler error is thrown.
		text = Random(unpredictableSeed).front.to!(string);

		// text is null here.
		return hexDigest!(CRC32)(text).array.to!(string);
	}
}

void main(string[] args)
{
	auto foo = new Foo();

	writefln("%s", foo.generateHash());
}

Reported: https://d.puremagic.com/issues/show_bug.cgi?id=12357
Mar 13 2014
next sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Thursday, 13 March 2014 at 13:57:49 UTC, Gary Willoughby wrote:
 		text = Random(unpredictableSeed).front.to!(string);

This is actually calling the function std.conv.text.... this line is rewritten to: std.conv.text(Random(unpredictableSeed).front.to!(string)); The text function converts all is arguments to string and returns them all concated together. It thinks you are trying to use a property setter.
 		// text is null here.
 		return hexDigest!(CRC32)(text).array.to!(string);

And here, it is calling the text function with no arguments and no parens (similarly to a property getter). With no arguments, it just returns null. So this isn't actually a bug, D is working as designed, but it is a better example than the common "writeln = 1;" thing given in most property setter arguments...
Mar 13 2014
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 3/13/14, Gary Willoughby <dev nomad.so> wrote:
 Rather nasty bug that silently fails. Linux DMD64 D Compiler
 v2.065

Unfortunately it's not a variable, it's the std.conv.text function being called. I've actually ran into this nasty "bug" once already. It all boils down to being allowed to call functions via "foo = bar;" ----- void text(string x) { } void main() { text = ""; } ----- However I think this will be finally fixed once property enforcement is properly implemented.
Mar 13 2014
prev sibling parent "Gary Willoughby" <dev nomad.so> writes:
On Thursday, 13 March 2014 at 14:07:22 UTC, Adam D. Ruppe wrote:
 On Thursday, 13 March 2014 at 13:57:49 UTC, Gary Willoughby 
 wrote:
 		text = Random(unpredictableSeed).front.to!(string);

This is actually calling the function std.conv.text.... this line is rewritten to: std.conv.text(Random(unpredictableSeed).front.to!(string)); The text function converts all is arguments to string and returns them all concated together. It thinks you are trying to use a property setter.
 		// text is null here.
 		return hexDigest!(CRC32)(text).array.to!(string);

And here, it is calling the text function with no arguments and no parens (similarly to a property getter). With no arguments, it just returns null. So this isn't actually a bug, D is working as designed, but it is a better example than the common "writeln = 1;" thing given in most property setter arguments...

Ah yes of course!
Mar 13 2014