TD-24725/TD-24889/TD-24895: [TD 7.2.1] Compiler changed? Errors on params using derived classes

Report bugs and possible workarounds.
Dave Rabelink
Founder/Site Admin
Founder/Site Admin
Netherlands
Posts: 1358
Joined: 24 Feb 2017, 09:12
Location: Gouda, The Netherlands

TD-24725/TD-24889/TD-24895: [TD 7.2.1] Compiler changed? Errors on params using derived classes

Post by Dave Rabelink » 06 Nov 2019, 10:46

I have found a difference between TD 7.2.0 and TD 7.2.1:
(Use testcase).

TD 7.2.0 compiler accepts objects as parameters where base types are cast to derived class type
But at runtime you get a runtime error: "Assignment of different types".

TD 7.2.1 compiler does not accept it, giving this compile error: "Function argument 1 does not match declared data type. Check for possible name conflict."

Though I like that the compiler gives errors which would occur on runtime, I do not like the fact that this change was not documented in the release notes or list of changes.

So I wonder if this change was intentional or not.
CompileErr_DerivedClassParams.zip
You do not have the required permissions to view the files attached to this post.
Regards,
Dave Rabelink

Image
Articles and information on Team Developer Tips & Tricks Wiki
Download samples, documents and resources from TD Sample Vault
Videos on TDWiki YouTube Channel

Dave Rabelink
Founder/Site Admin
Founder/Site Admin
Netherlands
Posts: 1358
Joined: 24 Feb 2017, 09:12
Location: Gouda, The Netherlands

Re: TD-24725: [TD 7.2.1] Compiler changed? Errors on params using derived classes

Post by Dave Rabelink » 08 Nov 2019, 10:34

Response from Gupta dev
I would like to let you know that It is the correct behavior to report the error at compile time instead of runtime. This behavior is expected and fixed by TD-24725.

We will highlight this point to our engineering team and we will document it.
TD-24725 is not mentioned in the fixlist.

This is a nice change. Older TD versions accepted these constructions but made the runtime unstable and resulted in crashes.
Now the compiler will detect this and report a compile error.

In fact, TD 7.2.1 has detected a few issues on our projects which we actually missed and could now be corrected.
Regards,
Dave Rabelink

Image
Articles and information on Team Developer Tips & Tricks Wiki
Download samples, documents and resources from TD Sample Vault
Videos on TDWiki YouTube Channel

mlueling
Germany
Posts: 5
Joined: 03 Jul 2017, 11:57
Location: Wetter Germany

Re: TD-24725: [TD 7.2.1] Compiler changed? Errors on params using derived classes

Post by mlueling » 08 Nov 2019, 13:38

Hi Dave, I have the opposite problem:

TD 7.2.0 compiler accepts passing base classes as parameter having derived class defined. The passed object is if course of the derived type. This was working without runtime errors and made it possible to have a kind of generic interfaces in TD.
TD 7.2.1 compiler does not accept it, giving this compile error: "Function argument 1 does not match declared data type. Check for possible name conflict."

Is this a bug? I think so. I can give an examle if you need.

Dave Rabelink
Founder/Site Admin
Founder/Site Admin
Netherlands
Posts: 1358
Joined: 24 Feb 2017, 09:12
Location: Gouda, The Netherlands

Re: TD-24725: [TD 7.2.1] Compiler changed? Errors on params using derived classes

Post by Dave Rabelink » 08 Nov 2019, 13:59

mlueling wrote:
08 Nov 2019, 13:38
I can give an examle if you need.
Yes please.
Regards,
Dave Rabelink

Image
Articles and information on Team Developer Tips & Tricks Wiki
Download samples, documents and resources from TD Sample Vault
Videos on TDWiki YouTube Channel

mlueling
Germany
Posts: 5
Joined: 03 Jul 2017, 11:57
Location: Wetter Germany

Re: TD-24725: [TD 7.2.1] Compiler changed? Errors on params using derived classes

Post by mlueling » 11 Nov 2019, 08:16

Here it is. Just run the apt's with 7.2.1 and pre-7.2.1. This is completely breaking polymorphism.
You do not have the required permissions to view the files attached to this post.

thomas.uttendorfer
Site Admin
Site Admin
Germany
Posts: 180
Joined: 05 Mar 2017, 17:19
Location: Munich Germany

Re: TD-24725: [TD 7.2.1] Compiler changed? Errors on params using derived classes

Post by thomas.uttendorfer » 11 Nov 2019, 10:22

Hi mlueling,

I can confirm your case.
I found a workaround - you can cast the base class to the derived class before calling SalCreateWindow(…, p_oMail).
The example is in the attachment
Message7.2.1.zip
.

Regards Thomas
You do not have the required permissions to view the files attached to this post.
Thomas Uttendorfer
[ frevel & fey ] Software-System GmbH
https://thomasuttendorfer.wordpress.com/

mlueling
Germany
Posts: 5
Joined: 03 Jul 2017, 11:57
Location: Wetter Germany

Re: TD-24725: [TD 7.2.1] Compiler changed? Errors on params using derived classes

Post by mlueling » 11 Nov 2019, 10:57

Thanks for the workarround. I still hope that Gupta will fix this issue.

Dave Rabelink
Founder/Site Admin
Founder/Site Admin
Netherlands
Posts: 1358
Joined: 24 Feb 2017, 09:12
Location: Gouda, The Netherlands

Re: TD-24725: [TD 7.2.1] Compiler changed? Errors on params using derived classes

Post by Dave Rabelink » 11 Nov 2019, 11:37

I wonder...

My feeling is that the TD runtime never supported casting base classed to derived classes.
Maybe I'm mistaken, but even on the older ANSI TD versions, it could lead to crashes because the TD runtime was unable to map the internal memory correctly between base and derived classes.

To see if I could make TD 7.2.0 crash using the sample, I tried to pass the base class object to the derived class parameter of the form. And indeed, at runtime, TD runtime will present a memory error.

Use the adapted testcase in TD 7.2.0

Message7.1.0_RuntimeError.zip

It passes the base object to the display function.

It really depends on how old code was implemented. When you really make the object instances match the usage within the code, the TD runtime will not have issues.
But only when you know what you are doing.
Just assuming the TD runtime will handle any base object to derived object casting without problems is incorrect. The TD runtime does not support it. It is as designed.

The issue we have here is historical.
The old TD compiler and runtime accepted everything. No checks at all. TD just maps the memory structure from one to another. And if there was no crash, it seems it was all ok.
But that is just luck. Memory differences between objects could be identical, so no crash. But one instance variable added or removed, made the application crash.

Then the TD compiler was changed to not allow casting of objects to any other. But the casting between base and derived objects was not checked at compile time.
So it was just luck to have a working application. Only at runtime we could discover the use of this unsupported feature.

Now TD 7.2.1 has eliminated the luck factor and has made it explicit: the runtime does not support it and will give you the compile error which should have been there from the beginning.

I agree that it now eliminates the use of this unsupported feature even when you know what you are doing. It is not possible anymore to use this casting.

My advice to Gupta would be to make this a compiler option: enable/disable the compiler check on casting base to derived classes.
This would keep old code which worked being compiled. And it will give TD developers an optional way to detect unsupported casting which would result in crashes only visible at runtime.

I will create an OpenText defect/feature request.
You do not have the required permissions to view the files attached to this post.
Regards,
Dave Rabelink

Image
Articles and information on Team Developer Tips & Tricks Wiki
Download samples, documents and resources from TD Sample Vault
Videos on TDWiki YouTube Channel

Dave Rabelink
Founder/Site Admin
Founder/Site Admin
Netherlands
Posts: 1358
Joined: 24 Feb 2017, 09:12
Location: Gouda, The Netherlands

Re: TD-24725: [TD 7.2.1] Compiler changed? Errors on params using derived classes

Post by Dave Rabelink » 11 Nov 2019, 11:54

I have created this case:

Ticket # 4305391: TD 7.2.1: optional compiler setting to check for base to derived objects casting

Let's see the reaction from Gupta devs.
Regards,
Dave Rabelink

Image
Articles and information on Team Developer Tips & Tricks Wiki
Download samples, documents and resources from TD Sample Vault
Videos on TDWiki YouTube Channel

Dave Rabelink
Founder/Site Admin
Founder/Site Admin
Netherlands
Posts: 1358
Joined: 24 Feb 2017, 09:12
Location: Gouda, The Netherlands

Re: TD-24725: [TD 7.2.1] Compiler changed? Errors on params using derived classes

Post by Dave Rabelink » 11 Nov 2019, 12:21

So, to test my assumption I ported and adapted the sample to TD 1.5.1.

The compiler accepts everything.
I can pass an object of a completely different class to the function.

The TD runtime just maps the object structure and assumes the structure is identical.
Because the classes have the same structure (number, string, string) there is no runtime error or crash.

But when I swap the string instance variables in one class (so the sequence is different), the outcome at runtime is changed. Still no crash, but the fields on the form show swapped data.

When I reshuffle the order so that the structure really changes, like

Class 1
string, number, string

Class 2
number, string, string

You get crash at runtime.

So, it seems the assumption is correct. The runtime never supported it and having no crashes was pure luck or specially designed.


See testcase (TD 1.5.1)
Message1.5_RuntimeError.zip
You do not have the required permissions to view the files attached to this post.
Regards,
Dave Rabelink

Image
Articles and information on Team Developer Tips & Tricks Wiki
Download samples, documents and resources from TD Sample Vault
Videos on TDWiki YouTube Channel

Uwe van der Horst
Site Admin
Site Admin
Germany
Posts: 148
Joined: 05 Mar 2017, 14:21
Location: Wetter (Ruhr), Germany

Re: TD-24725: [TD 7.2.1] Compiler changed? Errors on params using derived classes

Post by Uwe van der Horst » 11 Nov 2019, 20:04

Dave, as always a fantastic work here. Without this forum and your support and the great voluntary work of many others here, everything would be much more complicated. Unfortunately, such valuable information is not disclosed in the official release notes. So any forum user opening support tickets should publish it here.
Best regards,
Uwe van der Horst
Advo-web GmbH

Dave Rabelink
Founder/Site Admin
Founder/Site Admin
Netherlands
Posts: 1358
Joined: 24 Feb 2017, 09:12
Location: Gouda, The Netherlands

Re: TD-24725: [TD 7.2.1] Compiler changed? Errors on params using derived classes

Post by Dave Rabelink » 11 Nov 2019, 21:08

Thanks Uwe. As community we all have to make our lives somewhat easier :D


To follow up on this topic:

I forgot the technical name for this OOP feature but now remember it:

It is called upcasting and downcasting.

Most (maybe all?) OOP languages support upcasting automatically. So meaning that you can cast a derived object to a base type. And so does TD.

But downcasting an object from base to derived class is not explicitly supported, even on popular languages like Java and c#.
You have to instantiate using the derived class type and assign the object type to the base class.

TD does not explicitly support it either. To my knowledge, the only way to do it is using the implementation of Thomas above.

As example: you can cast a dog to animal, because dog IS AN animal.
You can not cast an animal to dog, because an animal IS NOT A dog.
Regards,
Dave Rabelink

Image
Articles and information on Team Developer Tips & Tricks Wiki
Download samples, documents and resources from TD Sample Vault
Videos on TDWiki YouTube Channel

Dave Rabelink
Founder/Site Admin
Founder/Site Admin
Netherlands
Posts: 1358
Joined: 24 Feb 2017, 09:12
Location: Gouda, The Netherlands

Re: TD-24725: [TD 7.2.1] Compiler changed? Errors on params using derived classes

Post by Dave Rabelink » 12 Nov 2019, 09:52

Another way to downcast is using a function which has the base type object as input parameter and returns it as object having the derived type.
Or even better, use the object itself as the return object (using this keyword)

This does not involve creating new objects and assignments and can be kept in the class itself.
Create for each cast you need a separate function.

So, in the message class example it would be:

DowncastFunc.png

And here how to use it:

DowncastUsage.png

The adapted sample below:
Message7.2.1_DownCastFunc.zip

BUT beware of the warning:
Warning.png
You do not have the required permissions to view the files attached to this post.
Regards,
Dave Rabelink

Image
Articles and information on Team Developer Tips & Tricks Wiki
Download samples, documents and resources from TD Sample Vault
Videos on TDWiki YouTube Channel

Uwe van der Horst
Site Admin
Site Admin
Germany
Posts: 148
Joined: 05 Mar 2017, 14:21
Location: Wetter (Ruhr), Germany

Re: TD-24725: [TD 7.2.1] Compiler changed? Errors on params using derived classes

Post by Uwe van der Horst » 12 Nov 2019, 11:51

I have opened a ticket for my colleague Matthias (mlueling) and have attached his testcases.

TD-24889: "Function argument does not match declared data type" when passing an object to a function with the proper derived type

Comments from support:
In this case a FRM template with a parameters of a clsEmailMessage deriving from a base class clsMessage.
The template being created SalCreateWindow() with an object instance of type clsMessage which should be a valid type ( repro works with 6.3 / 7.0 / 7.2).
This must be related to TD-24725 which now at design time checks data types and this is welcomed as if passing wrong type you would get a runtime error, now at least the compiler warns be forehands .
This issue may fix with a compilation setting to enable or disable that check so at least what is valid compiles.
Best regards,
Uwe van der Horst
Advo-web GmbH

Dave Rabelink
Founder/Site Admin
Founder/Site Admin
Netherlands
Posts: 1358
Joined: 24 Feb 2017, 09:12
Location: Gouda, The Netherlands

Re: TD-24725/TD-24889: [TD 7.2.1] Compiler changed? Errors on params using derived classes

Post by Dave Rabelink » 12 Nov 2019, 15:25

All comes together ;)

A feature request has been submitted:

TD-24895: Provide an optional compiler setting to check for base to derived objects casting
Regards,
Dave Rabelink

Image
Articles and information on Team Developer Tips & Tricks Wiki
Download samples, documents and resources from TD Sample Vault
Videos on TDWiki YouTube Channel

Return to “Bug Reports”

Who is online

Users browsing this forum: No registered users and 0 guests