Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

this can be passed to super constructor leading to further unexpected errors #135

Closed
Leko1705 opened this issue Jul 7, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@Leko1705
Copy link

Leko1705 commented Jul 7, 2024

Description

An InternalError occures with the given message:
"internal interpreter error; t.value.b is undefined"
This Error occures if a Class inherits from a Primitive Wrapper class, such as Integer.
If now 'this' is passed to the super constructor of that Primitive type, unexpected behaviour in the
following code might happen, as the error-message shows.
This probably happens, because with 'this' a reference is passed to super, even if an actual value is expected.
This mismatch then is not detected/checked leading to further unexpected errors.

To Reproduce

Example code:

class FakeInteger: Integer {
	public: constructor(): super(this) {}
}

var x = FakeInteger();
print(x); // InternalError occures!

What should happen instead / What I expected to happen instead:

IMO it should not be possible to inherit from primitive Types such as Integer or String. For that I would reccomend to
introduce/implement the existing keyword 'const', which currently exist, but has no effect/throws syntax errors.
E.g.:
const class X {} // OK
class Y: X() // Syntax Error

const x = 3; // OK
x = 4; // Syntax Error

I also reccomend to not allow passing this to the super constructor, since the super-type-content must be initialized first
before using this. This also applies to static functions.
E.g.:

class FakeInteger: Integer {
	public: constructor(): super(foo()) {} // This seems to work fine!
        public: static function foo(){ return 1; }
}
@Leko1705 Leko1705 added the bug Something isn't working label Jul 7, 2024
TGlas pushed a commit that referenced this issue Jul 7, 2024
TGlas pushed a commit that referenced this issue Jul 7, 2024
@TGlas
Copy link
Owner

TGlas commented Jul 7, 2024

Thanks for bringing this up. The behavior is clearly a bug, although I wonder how you even managed to stumble across such a weird corner case.

I agree that inheriting from built-in types is rarely useful. However, I'd like to avoid a distinction between primitive and class types at the language level. Declaring built-in types final (or const) would do the job indeed. However, that would introduce a whole new concept only for fixing a very unlikely edge case.

Disallowing the use of this in super calls also seems too restrictive. After all, in TScript, all member variables are fully constructed (to null) and only need initialization. Therefore, using this should be safe in most cases. The situation is different for super classes of built-in types though, and that's what caused this issue.

Since the error can only occur in very few corner cases where an instance is (or a few instances are) in the process of being constructed, I feel that this issue should not be solved with overly restrictive measures affecting inheritance as a whole. Instead, I added minimal checks catching the error, and a corresponding error message.

@TGlas TGlas closed this as completed Jul 7, 2024
@Leko1705
Copy link
Author

Leko1705 commented Jul 7, 2024

[...] although I wonder how you even managed to stumble across such a weird corner case.

just look at my github-profile. Then you will understand...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants