Skip to content

Add exception about extension members in error about nullable receivers #4318

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

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

Conversation

eernstg
Copy link
Member

@eernstg eernstg commented Apr 3, 2025

Consider the following example:

extension on int? {
  bool get isEvenOrNull {
    var self = this; // To get promotion.
    return self == null || self.isEven;
  }
}

void main() {
  int? iq = 2 > 1 ? null : 42;
  bool b = iq.isEvenOrNull; // OK.
}

As implemented, the analyzer and CFE do not report any errors for iq.isEvenOrNull. However, the NNBD feature specification says that

It is an error to call a method, setter, getter or operator on an expression whose type is potentially nullable and not dynamic, except for the methods, setters, getters, and operators on Object.

Arguably, this makes extension member invocations an error when the receiver is potentially nullable, even in the case where the given extension is applicable to a receiver whose type is nullable (as in the example).

I believe the implemented behavior is the desired behavior, and the specification should be adjusted.

This change is non-breaking because the implementations already accept this kind of invocation.

@eernstg eernstg requested a review from leafpetersen April 3, 2025 16:56
@@ -531,7 +535,7 @@ location outside of the statement.

It is an error to call a method, setter, getter or operator on an expression
whose type is potentially nullable and not `dynamic`, except for the methods,
setters, getters, and operators on `Object`.
setters, getters, and operators on `Object`, and except for extension members.
Copy link
Member

Choose a reason for hiding this comment

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

(Does "extension members" include extension type members, because you are allowed to call those on an extension type which doesn't implement Object.)

Probably correct and good fix, but still indirect.

What would happen if we remove this paragraph entirely?

I feel like it should fall through to some rule that says that you can't call somethin which isn't there, and where there is also no extension member. That is, a member invocation would do a lookup on the static type to see if there is a member. If so, see if it's compatible. If not, check for applicable extension functions. If not one such, it's an error.

If not having this paragraph would err out missing something which says which members T? has, then that's what we should write:

A type of the form T? has only the same members as Object, with the same signatures.

Then it's just an error to call a member that a type doesn't have, as usual.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants