Bug 36283 - When a class method, pointed by id, returns float, it returns wrong value
Summary: When a class method, pointed by id, returns float, it returns wrong value
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: objc (show other bugs)
Version: 3.4.5
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-05-20 20:44 UTC by JongAm Park
Modified: 2008-12-28 21:38 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description JongAm Park 2008-05-20 20:44:47 UTC
(I don't know if this happens only to gcc 3.4.5 from the MingW and if it was solved already in newer versions.)

I made two classes inherited from a common parent class in Objective-C.
The two children classes have methods, "- (float) data" and "- (int) data" respectively. They are for returning data value contained in each classes, or more accurately, their class instances.

I would like to show a code example.
@implementation GFloat : Generics
- (id) add:(id) aVal
{
    float value, value2;
    int value3;
    
    value = [aVal data]; // This can't return a float value in "aVal" correctly.
    
    value2 = returnFloat(); // C function returns a float value correctly.
    value3 = returnInt();   // C function returns a int value correctly.
    
    printf("%f, %f and %d\n", value, value2, value3);
    
   ....

The method, "add", takes the aVal as an id type.
By sending "data" message to aVal, it calls GFloat's data method successfully.
So, inside of the data method/message, correct data value is fetched and returned. However, from the caller's line, which is shown above, the returned value is something other than what the "data" message returned.

When the aVal is casted to (GFloat *), its returned value is correct.

Is it weird because it returns wrong value although correct method is called.

I would like to test the code at home with my MacOS X machine.
Comment 1 Andrew Pinski 2008-05-20 21:02:58 UTC
Actually I don't think this is a bug.  What is happening is the id is a generic class type so the compiler needs to decide at compile time what the type is being returned.  In this case it decides on the one the user did not want.  The cast will chose the correct one.  Now at runtime is selects the correct type but of course by the time you get there it is already messed up.

-- Pinski
Comment 2 Andrew Pinski 2008-05-20 21:05:51 UTC
Try using -Wstrict-selector-match and see what you get.
Comment 3 JongAm Park 2008-05-21 02:57:09 UTC
(In reply to comment #2)
> Try using -Wstrict-selector-match and see what you get.
> 

I will try that. Thanks.
Comment 4 JongAm Park 2008-05-21 03:00:20 UTC
(In reply to comment #1)
> Actually I don't think this is a bug.  What is happening is the id is a generic
> class type so the compiler needs to decide at compile time what the type is
> being returned.  In this case it decides on the one the user did not want.  The
> cast will chose the correct one.  Now at runtime is selects the correct type
> but of course by the time you get there it is already messed up.
> 
> -- Pinski
> 

That was my first idea. So, that is why I tested if correct method is called, and it returns things correctly. Once it knows what value to return and through which register, I guess it means just the interpretation of the returned data.
So, I tried casting the returned value to float. But it didn't fix the problem. The only way to fix it is to cast the pointer to the object of which method is called.

Comment 5 JongAm Park 2008-05-21 04:40:30 UTC
I tried it on my MacBook with 4.0.1 version of gcc. ( provided by Apple Inc. )

- (void)add:(id)valObj
{
	float temp_value;

// If I cast this way	
	temp_value = (float)[valObj value]; // I changed "data" to "value", to see if I had misfortune in choosing message name.

// it displays a message, "error:pointer value used where a floating point value was expected.
It seems to me that it means the return value by sending message "value" is recognized as a pointer.

// If I cast this way
temp_value = [(GFloat *)valObj value];

It compiles without any problem and returns correct value.

So, does it mean that we can't rely on the returned value when using id?

By the way, the Objective-C programming language manual from Apple describes :

Return and Argument Types
In general, methods in different classes that have the same selector (the same name) must also share the same return and argument types. This constraint is imposed by the compiler to allow dynamic binding. Because the class of a message receiver (and therefore class-specific details about the method it’s asked to perform), can’t be known at compile time, the compiler must treat all methods with the same name alike. When it prepares information on method return and argument types for the runtime system, it creates just one method description for each method selector.

Then... wouldn't it be better to present error message in the case of my test code?
Because the return type of the same message of GInt, and GFloat are different, i.e. int and float, respectively, it violates the explanation above. Instead of allowing the compilation, it would be good to prevent compilation or somewhat better error message.
Comment 6 JongAm Park 2008-05-21 14:57:00 UTC
(In reply to comment #2)
> Try using -Wstrict-selector-match and see what you get.
> 

The gcc 3.4.5 came from the MingW doesn't recognize the flag.
And even without it, it warns multiply declared methods.
Comment 7 Andrew Pinski 2008-12-28 21:38:19 UTC
THis is not a bug as explained, the compiler needs to decide what kind of method needs to be called including the return value.  So the compiler is guess it returns an int instead of the float one.