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

Confusing error message when a void value is used #56992

Open
sgrekhov opened this issue Oct 30, 2024 · 2 comments
Open

Confusing error message when a void value is used #56992

sgrekhov opened this issue Oct 30, 2024 · 2 comments
Labels
area-meta Cross-cutting, high-level issues (for tracking many other implementation issues, ...). improve-diagnostics Related to the quality of diagnostic messages

Comments

@sgrekhov
Copy link
Contributor

FutureOr<void> foo() => print(""); // print() returns void

The code above produces an error in both analyzer and VM. Analyzer error message is A value of type 'void' can't be returned from the function 'foo' because it has a return type of 'FutureOr<void>' and VM error is Error: A value of type 'void' can't be returned from a function with return type 'FutureOr<void>'.

This error message is confusing. void is assignable to FutureOr<void>, the problem here is that a void value returned by print() is used. Please update the error message accordingly.

cc @eernstg

Dart SDK version: 3.7.0-71.0.dev (dev) (Fri Oct 25 17:06:43 2024 -0700) on "windows_x64"

@sgrekhov sgrekhov added the improve-diagnostics Related to the quality of diagnostic messages label Oct 30, 2024
@dart-github-bot
Copy link
Collaborator

Summary: The error message "A value of type 'void' can't be returned from the function 'foo' because it has a return type of 'FutureOr'." is confusing because void is assignable to FutureOr<void>. The actual issue is that the void value returned by print() is being used, not the type itself. The error message should be updated to reflect this.

@dart-github-bot dart-github-bot added area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Oct 30, 2024
@eernstg eernstg added area-meta Cross-cutting, high-level issues (for tracking many other implementation issues, ...). and removed area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Oct 30, 2024
@eernstg
Copy link
Member

eernstg commented Oct 30, 2024

Right. I adjusted the labels to indicate that this is concerned with the diagnostics from both tools. I removed type-bug because i think improve-diagnostics is usually specified alone, and it is taken to imply some kind of a type-bug treatment.

The error message sounds like there was a subtype test void <: FutureOr<void>, and it failed (so we can't return that value because it isn't type correct). However, as @sgrekhov mentions, that subtype relation does hold.

The actual reason why there is an error is that the allowlist for using the result from evaluation of an expression of type void (https://github.com/dart-lang/language/blob/9dc3737010f3ccac5ef54bf63b402d8e86b9115c/specification/dartLangSpec.tex#L22666) doesn't contain any items that match return print("");.

So I'd expect an error message saying something like "This expression has a type of 'void' so its value can't be used" or "This expression has type 'void' and can't be used".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-meta Cross-cutting, high-level issues (for tracking many other implementation issues, ...). improve-diagnostics Related to the quality of diagnostic messages
Projects
None yet
Development

No branches or pull requests

3 participants