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

Analyzer does not consistently consider invocations on Never to have type Never. #56985

Open
stereotype441 opened this issue Oct 29, 2024 · 2 comments
Assignees
Labels
analyzer-spec Issues with the analyzer's implementation of the language spec area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P3 A lower priority bug or feature request type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@stereotype441
Copy link
Member

The NNBD spec says:

Calling a method (including an operator) or getter on a receiver of static type Never is treated by static analysis as producing a result of type Never. Tearing off a method from a receiver of static type Never produces a value of type Never. Applying an expression of type Never in the function position of a function call produces a result of type Never.

Assuming "operator" means "user-definable operator" (i.e. not &&, ||, or ??), then as far as I can tell, the CFE implements this faithfully.

The analyzer is inconsistent, though:

  • expr1 operator expr2, where expr1 has static type Never, has static type Never, as specified.
  • expr(args), where expr has static type Never, has static type Never, as specified.
  • expr.identifier(args), where expr has static type Never, has static type Never, as specified.
  • identifier1.identifier2, where identifier1 has static type Never, has static type Never, as specified.
  • expr.identifier, where expr is an expression having static type Never, but is not a simple identifier, behaves as follows:
    • If identifier is hashCode, toString, runtimeType, or noSuchMethod, the type is the same as it would be if the receiver type were Object (e.g. (throw '').toString has type String Function()).
    • Otherwise, the type is InvalidType, but no error is issued.
  • In an extension whose on type is Never, identifier and identifier(args) are treated the same as they would be treated if the on type were Object.
@stereotype441 stereotype441 added area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. analyzer-spec Issues with the analyzer's implementation of the language spec labels Oct 29, 2024
@stereotype441 stereotype441 self-assigned this Oct 29, 2024
@stereotype441
Copy link
Member Author

I'm working on some refactoring that would benefit from fixing this, so I'll work on a fix. Note that the fix will be a non-breaking change, since it will bring the analyzer into alignment with the CFE.

@srawlins srawlins added P3 A lower priority bug or feature request type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Nov 4, 2024
@lrhn
Copy link
Member

lrhn commented Nov 4, 2024

Assuming "operator" means "user-definable operator"

It does here since it refers to "method (including an operator)" so it's about operators which are also instance methods.
Probably except == which always has type bool.

(Although it could safely apply to the rest too, if the first operand has type Never, the entire expression could soundly have type Never.)

Every place where a non-Never, non-dynamic invocation would use the member signature of the member on the receiver type to infer the result type, an invocation on Never has result type Never, no bounds for type arguments and no context type or required type for arguments.

"Tearing off a method from a receiver of static type Never produces a value of type Never. "

That's a little misleading. It's an unspecified member access. It's impossible to say whether a member access on Never targets a getter or a method. In fact, it's definitely neither.

Anyway, seems like the analyzer refused a little more of the behavior of dynamic.
The only real problem with that is inconsistency, it'll never cause a runtime problem (he says confidently).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-spec Issues with the analyzer's implementation of the language spec area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P3 A lower priority bug or feature request type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

3 participants