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

BREAKING CHANGE: Improve null comparison #2760

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

HerrCai0907
Copy link
Member

Fixes #2662.

@HerrCai0907 HerrCai0907 changed the title fix: improve null comarasion fix: improve null comparison Oct 7, 2023
@HerrCai0907 HerrCai0907 marked this pull request as draft October 7, 2023 02:59
@HerrCai0907 HerrCai0907 removed the request for review from CountBleck October 7, 2023 02:59
Copy link
Member

@CountBleck CountBleck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The big question is in my last comment.

src/resolver.ts Outdated Show resolved Hide resolved
src/resolver.ts Outdated Show resolved Hide resolved
src/resolver.ts Outdated Show resolved Hide resolved
Comment on lines +17 to +19
export function compare_null_with_operator_overload(): void {
null == "aa";
null != "aa";
"aa" == null;
"aa" != null;

null === "aa";
null !== "aa";
"aa" === null;
"aa" !== null;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is something to be very careful with. Your current code is perfectly fine for String, but I have to wonder what happens with classes with operator overloads that have signatures with non-null parameters. In that case, you would need to add some null-checking code for == that's equivalent to a && b ? eqOverload(a, b) : a == b, where a and b are the pointers of the LHS and RHS respectively, and eqOverload represents the overload function. Similar null-checking code applies to !=. You could also opt to error if ==/!= is used on nullable types that have an overload without nullable parameters.

@CountBleck
Copy link
Member

Whoops, sorry, I didn't realize you removed the request to review.

@HerrCai0907
Copy link
Member Author

Whoops, sorry, I didn't realize you removed the request to review.

I find it should handle float and integer promotion more carefully. commonType will not promotion i32 to f32.

@HerrCai0907 HerrCai0907 marked this pull request as ready for review October 7, 2023 06:52
@HerrCai0907 HerrCai0907 changed the title fix: improve null comparison [BREAK CHANGE]fix: improve null comparison Oct 7, 2023
Copy link
Member

@CountBleck CountBleck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't quite right either.

Comment on lines +379 to +386
i32.const 0
i32.const 32
i32.eq
drop
i32.const 0
i32.const 32
i32.ne
drop
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't give me a good feeling, because this means null == overloaded isn't being compiled correctly, while overloaded == null is.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I think it should not block this fix.
The same thing will happen when comparing new A() == new B()
We should introduce more strictly check for operator overload.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose so. I don't like having different semantics for null == vs == null as well as for A == and == A.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #2761

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like having different semantics for null == vs == null as well as for A == and == A.

Agree. This patch should land after we implement better operator overload resolution.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

implement better operator overload resolution

We would still need to special-case null somehow. There needs to be some way to differentiate between a value of type Object | null and a null literal, which has the same type in this PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we need to differentiate between a value of type Object | null and a null literal? If we want to do it, it is necessary to introduce a new primitive type.

tests/compiler/compare-null.debug.wat Show resolved Hide resolved
@CountBleck CountBleck changed the title [BREAK CHANGE]fix: improve null comparison [BREAKING CHANGE] fix: improve null comparison Oct 8, 2023
@CountBleck CountBleck changed the title [BREAKING CHANGE] fix: improve null comparison BREAKING CHANGE: Improve null comparison Oct 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Could not compare null with nullable type
2 participants