Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 341775

Summary: [xtend2] Primitive equality generate Class Cast Error
Product: [Modeling] TMF Reporter: Alessandro Carraro <carraro.alessandro>
Component: XtextAssignee: Project Inbox <tmf.xtext-inbox>
Status: CLOSED INVALID QA Contact:
Severity: major    
Priority: P3 CC: sebastian.zarnekow, sven.efftinge
Version: unspecifiedFlags: sebastian.zarnekow: indigo+
Target Milestone: M7   
Hardware: All   
OS: All   
Whiteboard:

Description Alessandro Carraro CLA 2011-04-04 09:22:23 EDT
Build Identifier: 20110301-1815 

There is a problem in April nightly that did not appear on March, 15th's build:

assuming charProperty is a char property, the following expression generate an error in the generated class:
«IF someInstance.charProperty =='N'»

This is because ObjectExtensions.operator_equals invocation casts its argument to Object, so in generated code there is something like:

char a =..
char b =..
ObjectExtensions.operator_equals((Object)a,(Object)b);


Reproducible: Always

Steps to Reproduce:
1. Write an expression that compares 2 <place a primitive type here>
2. The generated class has a compilation error (Cannot cast from Object to <primitive>)
Comment 1 Sebastian Zarnekow CLA 2011-04-04 13:07:39 EDT
The following is perfectly valid Java 1.5 code:

int i = 1;
Object o = (Object)i;

This is valid, too:

char c1 = 'a';
char c2 = 'b';
ObjectExtensions.operator_equals((Object)c1, (Object)c2);

Since Xbase does not support character literals, an equals comparison of a char against a string(literal) will always be false.
We could add special equal semantics for chars and strings but it would be have baked. I'm inclined to close this one as 'works for me'. Did I miss your point?
Comment 2 Sven Efftinge CLA 2011-04-04 13:32:39 EDT
(In reply to comment #0)
> assuming charProperty is a char property, the following expression generate an
> error in the generated class:

... sounds like there used to be a problem with the generated code which we have fixed in the meantime.
Comment 3 Sebastian Zarnekow CLA 2011-04-04 13:37:43 EDT
Do we want an extension operator_equals(Character, String) that would allow to write 'a'.charAt(0) == 'a' 
and expect 'true' as the result?
Comment 4 Sven Efftinge CLA 2011-04-04 15:00:07 EDT
(In reply to comment #3)
> Do we want an extension operator_equals(Character, String) that would allow to
> write 'a'.charAt(0) == 'a' 
> and expect 'true' as the result?

No. 
We may inform the user somehow that this is probably not what he wanted to do.
Some kind of runtime logging or even an exception could help.
Comment 5 Sebastian Zarnekow CLA 2011-04-04 15:08:04 EDT
I think warning "boolean expression is always false" iff the == operator is bound to ObjectExtensions#operator_equals(Object, Object) would be nice.
Comment 6 Alessandro Carraro CLA 2011-04-05 05:29:07 EDT
(In reply to comment #1)
> The following is perfectly valid Java 1.5 code:
> 
> int i = 1;
> Object o = (Object)i;
> 
> This is valid, too:
> 
> char c1 = 'a';
> char c2 = 'b';
> ObjectExtensions.operator_equals((Object)c1, (Object)c2);

I'm rather perplex: javac compiles without errors, but eclipse complains. I checked tree times my java error configuration, but could not find anything related to autoboxing except the entry in 'Potential Problems' that is marked as 'Ignore'

I did not want to compare char to Strings (I agree the type should be the same for equality) and I did not know that that xbase do not support chars. What I was looking for was something able to evaluate 2 char Properties: 
expression <<a.charProperty == a.charProperty>> should be true even if xbase does not support char literals, imho

Btw, I found that the problem remains with all the primitive values
Comment 7 Sebastian Zarnekow CLA 2011-04-05 05:38:16 EDT
Please reopen if I missed something.
Comment 8 Sebastian Zarnekow CLA 2011-04-05 05:39:35 EDT
(In reply to comment #6)
> (In reply to comment #1)
> > The following is perfectly valid Java 1.5 code:
> > 
> > int i = 1;
> > Object o = (Object)i;
> > 
> > This is valid, too:
> > 
> > char c1 = 'a';
> > char c2 = 'b';
> > ObjectExtensions.operator_equals((Object)c1, (Object)c2);
> 
> I'm rather perplex: javac compiles without errors, but eclipse complains. I
> checked tree times my java error configuration, but could not find anything
> related to autoboxing except the entry in 'Potential Problems' that is marked
> as 'Ignore'
> 
> I did not want to compare char to Strings (I agree the type should be the same
> for equality) and I did not know that that xbase do not support chars. What I
> was looking for was something able to evaluate 2 char Properties: 
> expression <<a.charProperty == a.charProperty>> should be true even if xbase
> does not support char literals, imho
> 
> Btw, I found that the problem remains with all the primitive values

Sorry, I missed your last comment.
Are you sure that the compliance settings for the compiler and the used JVM is 1.5 or better?
Comment 9 Sven Efftinge CLA 2011-04-05 05:45:38 EDT
(In reply to comment #6)
> expression <<a.charProperty == a.charProperty>> should be true even if xbase
> does not support char literals, imho

It does evaluate to true.
Comment 10 Alessandro Carraro CLA 2011-04-05 05:54:41 EDT
Aaargh! Sorry to everyone I made one of the biggest mistakes of my career!
Please feel free to mark this bug as invalid and put a note to not respond ever again to me :( I feel so guilty!

The problem was that my template name was stupidly 'Object' (historical reasons... I know it is a really meaningless name). So when you cast something to 'Object' it is not casting to java.lang.Object, but to tests.test1.Object