www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Compiler bug ?

reply "Temtaime" <temtaime gmail.com> writes:
struct A {
	int opBinary(string op)(A) { k++; return 0; }
}

struct B {
	A __a;
	alias __a this;
}

void main() {
     B b;
     auto c = b * b;
}


This code doesn't compiles with an error:
Error: 'b' is not of arithmetic type, it is a B

If i remove k++, then it's OK. It seems that compiler omitting 
the function if there an error in it.
May 17 2013
next sibling parent reply "Namespace" <rswhite4 googlemail.com> writes:
I don't see where k comes from?
And it's always a bad idea to prefix a variable with '__', 
because this is reserved to the compiler.
May 17 2013
parent reply "Maxim Fomin" <maxim maxim-fomin.ru> writes:
On Friday, 17 May 2013 at 14:12:10 UTC, Namespace wrote:
 I don't see where k comes from?
I think the point is that it comes from nowhere. Compiler incorectly omits two errors from output: "Error undefined indentifier" and "template instance error instantiation". This should go to bugzilla.
 And it's always a bad idea to prefix a variable with '__', 
 because this is reserved to the compiler.
If it alwalys a bad idea, compiler should not accept such code.
May 17 2013
next sibling parent reply "Temtaime" <temtaime gmail.com> writes:
Yes, i want to see 'undeclared variable' error, but the compiler 
omits function declaration instead.

And why i shouldn't use __? I think it's ordinary identifier.
May 17 2013
next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 05/17/2013 07:40 AM, Temtaime wrote:
 Yes, i want to see 'undeclared variable' error, but the compiler omits
 function declaration instead.

 And why i shouldn't use __? I think it's ordinary identifier.
"Iden­ti­fiers start­ing with __ (two un­der­scores) are re­served." http://dlang.org/lex.html#Identifier Ali
May 17 2013
prev sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Friday, May 17, 2013 16:40:23 Temtaime wrote:
 Yes, i want to see 'undeclared variable' error, but the compiler
 omits function declaration instead.
 
 And why i shouldn't use __? I think it's ordinary identifier.
Identifiers begining with two underscores are reserved by the compiler, which is why identifiers such as __FILE__, __LINE__, __traits, and __gshared all begin with two underscores. Identifiers that the compiler generates are going to start with two underscores, and you could end up with bugs in your program if you happened to declare an identifier with a name that matched a compiler generated one. I believe that that's the case with most C-based languages. - Jonathan M Davis
May 17 2013
prev sibling next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Maxim Fomin:

 If it alwalys a bad idea, compiler should not accept such code.
Ideally you are right. But I think sometimes you want to use those reserved names... So I don't know. Bye, bearophile
May 17 2013
prev sibling next sibling parent "Namespace" <rswhite4 googlemail.com> writes:
On Friday, 17 May 2013 at 14:35:35 UTC, Maxim Fomin wrote:
 On Friday, 17 May 2013 at 14:12:10 UTC, Namespace wrote:
 I don't see where k comes from?
I think the point is that it comes from nowhere. Compiler incorectly omits two errors from output: "Error undefined indentifier" and "template instance error instantiation". This should go to bugzilla.
 And it's always a bad idea to prefix a variable with '__', 
 because this is reserved to the compiler.
If it alwalys a bad idea, compiler should not accept such code.
Ok, not always, but mostly it's a bad idea.
May 17 2013
prev sibling next sibling parent reply 1100110 <0b1100110 gmail.com> writes:
On 05/17/2013 09:35 AM, Maxim Fomin wrote:
 On Friday, 17 May 2013 at 14:12:10 UTC, Namespace wrote:
 I don't see where k comes from?
=20 I think the point is that it comes from nowhere. Compiler incorectly omits two errors from output: "Error undefined indentifier" and "template instance error instantiation". This should go to bugzilla. =20
 And it's always a bad idea to prefix a variable with '__', because
 this is reserved to the compiler.
=20 If it alwalys a bad idea, compiler should not accept such code.
It is not the D way to forbid you from shooting yourself in the foot. goto, catch(Throwable th), there are plenty more examples.
May 17 2013
parent reply "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Friday, May 17, 2013 11:59:38 1100110 wrote:
 On 05/17/2013 09:35 AM, Maxim Fomin wrote:
 If it alwalys a bad idea, compiler should not accept such code.
It is not the D way to forbid you from shooting yourself in the foot. goto, catch(Throwable th), there are plenty more examples.
D will stop you from shooting yourself in the foot if it can do so in a way that doesn't actually limit you, and it does do quite a few things to make it harder to shoot yourself in the foot, but it certainly doesn't ultimately stop you from doing so. However, if it _never_ makes sense to declare a variable beginning with two underscores, I don't know why the compiler wouldn't forbid it other than the fact that it probably inserts such variables prior to when it would do the semantic analysis to check whether variables started with underscores, in which case, catching the user's variables that start with two underscores while permitting the compiler's variables could get tricky. - Jonathan M Davis
May 17 2013
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Jonathan M Davis:

 However, if it _never_ makes sense to declare a variable
 beginning with two underscores, I don't know why the compiler 
 wouldn't forbid
 it other than the fact that it probably inserts such variables 
 prior to when
 it would do the semantic analysis to check whether variables 
 started with
 underscores, in which case, catching the user's variables that 
 start with two
 underscores while permitting the compiler's variables could get 
 tricky.
I think to answer this issue we need someone that knows more about the DMD compiler (as Kenji Hara). I think that if we want to forbid those variables in user code, then probably there is a way do to it. Bye, bearophile
May 17 2013
next sibling parent reply "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Friday, May 17, 2013 19:19:46 bearophile wrote:
 I think to answer this issue we need someone that knows more
 about the DMD compiler (as Kenji Hara). I think that if we want
 to forbid those variables in user code, then probably there is a
 way do to it.
Oh, I'm sure that it would be possible. It's just a question of how complicated that would be and whether it's worth it given that very few programmers try and declare variables which start with two underscores. - Jonathan M Davis
May 17 2013
parent 1100110 <0b1100110 gmail.com> writes:
On 05/17/2013 12:27 PM, Jonathan M Davis wrote:
 On Friday, May 17, 2013 19:19:46 bearophile wrote:
 I think to answer this issue we need someone that knows more
 about the DMD compiler (as Kenji Hara). I think that if we want
 to forbid those variables in user code, then probably there is a
 way do to it.
=20 Oh, I'm sure that it would be possible. It's just a question of how=20 complicated that would be and whether it's worth it given that very few=
=20
 programmers try and declare variables which start with two underscores.=
=20
 - Jonathan M Davis
I love how we are all basically agreeing with one another, even though we are saying such different things.
May 17 2013
prev sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 05/17/2013 07:19 PM, bearophile wrote:
 Jonathan M Davis:

 However, if it _never_ makes sense to declare a variable
 beginning with two underscores, I don't know why the compiler wouldn't
 forbid
 it other than the fact that it probably inserts such variables prior
 to when
 it would do the semantic analysis to check whether variables started with
 underscores, in which case, catching the user's variables that start
 with two
 underscores while permitting the compiler's variables could get tricky.
I think to answer this issue we need someone that knows more about the DMD compiler (as Kenji Hara). I think that if we want to forbid those variables in user code, then probably there is a way do to it. Bye, bearophile
It is trivial to catch them in the parser.
May 18 2013
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Timon Gehr:

 It is trivial to catch them in the parser.
So the question is: is it right to catch them all? :-) Bye, bearophile
May 18 2013
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 05/18/2013 11:50 AM, bearophile wrote:> Timon Gehr:
 It is trivial to catch them in the parser.
So the question is: is it right to catch them all? :-)
I don't think so. Double underscores are reserved for the "implementation", which includes the standard library. I don't think the compiler should know that it is compiling something that is part of Phobos. It could even be a mixin that has such a variable that is implemented in Phobos. There is nothing wrong in reminder each other that a name is reserved. :) Ali
May 18 2013
prev sibling parent "Maxim Fomin" <maxim maxim-fomin.ru> writes:
On Friday, 17 May 2013 at 17:08:31 UTC, Jonathan M Davis wrote:
 On Friday, May 17, 2013 11:59:38 1100110 wrote:
 On 05/17/2013 09:35 AM, Maxim Fomin wrote:
 If it alwalys a bad idea, compiler should not accept such 
 code.
It is not the D way to forbid you from shooting yourself in the foot. goto, catch(Throwable th), there are plenty more examples.
D will stop you from shooting yourself in the foot if it can do so in a way that doesn't actually limit you, and it does do quite a few things to make it harder to shoot yourself in the foot, but it certainly doesn't ultimately stop you from doing so. However, if it _never_ makes sense to declare a variable beginning with two underscores, I don't know why the compiler wouldn't forbid it other than the fact that it probably inserts such variables prior to when it would do the semantic analysis to check whether variables started with underscores, in which case, catching the user's variables that start with two underscores while permitting the compiler's variables could get tricky. - Jonathan M Davis
The only scenario when __identifiers are needed which came to my mind is case when working with C or os implementation. Low-level implementation supposed to use '__' namespace because of practice encouraged by ISO C and other docs to reserve space for user and system specific identifiers. Probably double leading underscores should be restricted to extern declarations.
May 17 2013
prev sibling parent "Jesse Phillips" <jessek.phillips+D gmail.m> writes:
On Friday, 17 May 2013 at 14:35:35 UTC, Maxim Fomin wrote:
 If it alwalys a bad idea, compiler should not accept such code.
I'm pretty sure the answer is: Compilers can have different extensions, it would be bad for a compiler to reject code which is valid by another compiler. That said, I don't know of any rules around ignoring these as there is for pragma.
May 17 2013
prev sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Temtaime:

 This code doesn't compiles with an error:
 Error: 'b' is not of arithmetic type, it is a B

 If i remove k++, then it's OK. It seems that compiler omitting 
 the function if there an error in it.
It's a bad error message, but it's correct, because k doesn't exists. This error message is so bad because opBinary is a template. In Bugzilla there is a request for an improvement related to this: http://d.puremagic.com/issues/show_bug.cgi?id=9715 Bye, bearophile
May 17 2013