Skip to content

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

Merged
eernstg merged 2 commits intomainfrom
spec_nullable_receiver_error_apr25
Jun 16, 2025
Merged

Add exception about extension members in error about nullable receivers#4318
eernstg merged 2 commits intomainfrom
spec_nullable_receiver_error_apr25

Conversation

@eernstg
Copy link
Copy Markdown
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
Comment thread accepted/2.12/nnbd/feature-specification.md Outdated
@eernstg eernstg force-pushed the spec_nullable_receiver_error_apr25 branch from c10de6e to e0afa02 Compare May 14, 2025 12:24
@eernstg eernstg merged commit ab11da9 into main Jun 16, 2025
3 checks passed
@eernstg eernstg deleted the spec_nullable_receiver_error_apr25 branch June 16, 2025 15:56
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