8000 Call 'String?.length' in a constant expression · Issue #59904 · dart-lang/sdk · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Call 'String?.length' in a constant expression #59904

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

Closed
sgrekhov opened this issue Jan 14, 2025 · 5 comments
Closed

Call 'String?.length' in a constant expression #59904

sgrekhov opened this issue Jan 14, 2025 · 5 comments
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). type-question A question about expected behavior or functionality

Comments

@sgrekhov
Copy link
Contributor

The code below produces an error in the analyzer but works in VM. Which tool is right here?

const String? s = null;
const int? c = s?.length; // Analyzer: The property 'length' can't be accessed on the type 'Null' in a constant expression.

main() {
  print(c); // prints null
}

Dart SDK version: 3.7.0-309.0.dev (dev) (Tue Jan 7 20:06:23 2025 -0800) on "windows_x64"

@sgrekhov sgrekhov added the type-question A question about expected behavior or functionality label Jan 14, 2025
@dart-github-bot
Copy link
Collaborator
dart-github-bot commented Jan 14, 2025

Summary: Analyzer incorrectly flags String?.length within a constant expression as an error. The VM correctly handles null propagation, resulting in null.

@dart-github-bot dart-github-bot added legacy-area-analyzer Use area-devexp instead. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. labels Jan 14, 2025
@eernstg
Copy link
Member
eernstg commented Jan 14, 2025

(Errm, I looked at the summary, but it's wrong. So I deleted my first comment which is not relevant to the actual issue.)

Here's a better comment:

s?.length is never a constant expression, not even if s is a constant expression whose value is null or a String.

So both tools should report an error because s?.length isn't a constant expression (and the error should not be "'length' can't be accessed on the type 'Null'" because that isn't the semantics of ?. in any context).

@lrhn
Copy link
Member
lrhn commented Jan 14, 2025

Very, very likely e?.id was not made a constant expression in any case because I didn't think it could possibly be valid. There is one case where e.id is constant, constantString.length, and constantStringOrNull?.length could soundly be constant. But it's currently not specified as such.

Maybe we can add it next time we touch the constant sublanguage for some reason, along with is ConstantType. And throw <expr>.

@devoncarew devoncarew added area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). and removed legacy-area-analyzer Use area-devexp instead. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. labels Jan 15, 2025
@lrhn
Copy link
Member
lrhn commented Apr 9, 2025

(Question answered. Changing it would be a language feature. Until then s == null ? null : s.length works.)

@sgrekhov
Copy link
Contributor Author
sgrekhov commented Apr 9, 2025

Filed #60509 for the error non reported by CFE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). type-question A question about expected behavior or functionality
Projects
None yet
Development

No branches or pull requests

5 participants
0