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

Ignore another dead_code hint for a weak-only null check. #63602

Merged
merged 2 commits into from Aug 13, 2020

Conversation

stereotype441
Copy link
Contributor

Description

When dart-lang/sdk#41985 is fixed, the
analyzer will begin detecting dead code due to "unnecessary" null
checks. Since we want to retain null checks in Flutter even when they
appear unnecessary (in order to preserve runtime behavior in weak
mode), we need to suppress these dead code hints.

(This is a follow-up to #63007; an additional case appeared before I had a chance to land the fix for dart-lang/sdk#41985).

Related Issues

dart-lang/sdk#41985 (PR must land before this issue can be fixed)

Tests

N/A since this change does not affect behavior, it merely avoids dead code hints.

Checklist

Before you create this PR, confirm that it meets all requirements listed below by checking the relevant checkboxes ([x]). This will ensure a smooth and quick review process.

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I signed the CLA.
  • I read and followed the Flutter Style Guide, including Features we expect every widget to implement.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I updated/added relevant documentation (doc comments with ///).
  • All existing and new tests are passing.
  • The analyzer (flutter analyze --flutter-repo) does not report any problems on my PR.
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

Did any tests fail when you ran them? Please read Handling breaking changes.

When dart-lang/sdk#41985 is fixed, the
analyzer will begin detecting dead code due to "unnecessary" null
checks.  Since we want to retain null checks in Flutter even when they
appear unnecessary (in order to preserve runtime behavior in weak
mode), we need to suppress these dead code hints.
@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact Hixie on the #hackers channel in Chat.

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

@flutter-dashboard flutter-dashboard bot added the framework flutter/packages/flutter repository. See also f: labels. label Aug 12, 2020
Copy link
Contributor

@a14n a14n left a comment

Choose a reason for hiding this comment

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

LGTM

// Note: `key.bundle.load` has a non-nullable return type, but might be null
// when running with weak checking, so we need to null check it anyway (and
// ignore the warning that the null-handling logic is dead code).
if (data == null) { // ignore: dead_code
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: I'm not 100% sure this is the right approach. Is it possible for the user to provide their own implementation of AssetBundle.load? If not, maybe it would be better to change the type of data to a ByteData (removing the ? on line 666) and just remove this check.

Actually, that might be a reasonable approach anyhow, even if it is user-overridable, since the user will still get an exception when data gets dereferenced on line 682; the only difference will be that the exception will be marginally less comprehensible, and the key won't be evicted from the image cache. But maybe that's ok.

Anyhow, I'm open to suggestions.

Copy link
Contributor

Choose a reason for hiding this comment

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

yes ? could be removed. But for now I would keep the if (data==null) for now.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can probably just drop this. If the method is not supposed to return null, we can assume it's handled by throwing an exception.

I'm not quite clear though on when weak checking would be used. Could you explain a bit more about how that would happen?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dnfield
Weak checking is when the user's app contains some files that are opted in to null safety (e.g. flutter) and others that aren't (e.g. their app and packages). It does all the static checks for null safety, but the type system is fundamentally unsound at the boundary between opted in and opted out code. For example, if you invoke an abstract method in an opted-in class that has a return type of int, that's supposed to guarantee that you'll never receive null as a result. But if the concrete implementation of that abstract method is in opted-out code, it could return null without causing a compile time errors. That's the sense in which it's unsound: your opted-in code can have a variable of type non-nullable int that contains a null value. Effectively, non-nullable types can't be completely trusted if there is some opted-out code somewhere in the program. Once the entire program has been migrated to null safety, the unsoundness goes away and ints are really ints.

Because of this, we have code in Flutter that looks to be dead code, but we want to keep it around until most users have upgraded their apps to null safety, so that we can continue reporting useful exceptions to them if they pass null to us where they shouldn't. The type system thinks that code is dead but the reality is it's not, so we have to add the // ignore: comment. The reason I'm having to make this CL now is because fixing a bug in the analyzer that was preventing some of the "supposedly dead" code from being recognized.

Does that help? If not, I'm happy to discuss over VC.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ahhh ok that makes sense. Yeah we should leave this then.

@fluttergithubbot fluttergithubbot merged commit 8f896c9 into flutter:master Aug 13, 2020
@stereotype441 stereotype441 deleted the b-41985-7 branch August 14, 2020 12:21
mingwandroid pushed a commit to mingwandroid/flutter that referenced this pull request Sep 6, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
framework flutter/packages/flutter repository. See also f: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants