-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add NullSafetyUnderstandingFlag, update APIs to use it.
DartType.== implementations ignore nullability if the flag is not true. TypeSystem APIs eliminate nullability from types before doing anything. Internally analyzer continues using actual nullability for types. Package nnbd_migration opted into NullSafetyUnderstandingFlag. Bug: #40500 Change-Id: Ifeea28c01adf1dc59ed2da675b4a62c6334d529a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/134865 Reviewed-by: Paul Berry <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
- Loading branch information
1 parent
8d9cbaf
commit 6f6797e
Showing
38 changed files
with
398 additions
and
232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
pkg/analyzer/lib/dart/element/null_safety_understanding_flag.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'dart:async'; | ||
|
||
import 'package:analyzer/dart/element/element.dart'; | ||
import 'package:analyzer/dart/element/type.dart'; | ||
|
||
/// Every [DartType] has the nullability suffix, elements declared in legacy | ||
/// libraries have types with star suffixes, and types in migrated libraries | ||
/// have none, question mark, or star suffixes. Analyzer itself can handle | ||
/// mixtures of nullabilities with legacy and migrated libraries. | ||
/// | ||
/// However analyzer clients saw only star suffixes so far. Exposing other | ||
/// nullabilities is a breaking change, because types types with different | ||
/// nullabilities are not equal, `null` cannot be used where a non-nullable | ||
/// type is expected, etc. When accessing elements and types that come from | ||
/// migrated libraries, while analyzing a legacy library, nullabilities must | ||
/// be erased, using [LibraryElement.toLegacyElementIfOptOut] and | ||
/// [LibraryElement.toLegacyTypeIfOptOut]. The client must explicitly do | ||
/// this, and explicitly specify that it knows how to handle nullabilities | ||
/// by setting this flag to `true`. | ||
/// | ||
/// When this flag is `false` (by default), all types will return their | ||
/// nullability as star. So, type equality and subtype checks will work | ||
/// as they worked before some libraries migrated. Note, that during | ||
/// analysis (building element models, and resolving ASTs), analyzer will use | ||
/// actual nullabilities, according to the language specification, so report | ||
/// all corresponding errors, and perform necessary type operations. It is | ||
/// only when the client later views on the types, they will look as legacy. | ||
class NullSafetyUnderstandingFlag { | ||
static final _zoneKey = Object(); | ||
|
||
static bool get isEnabled { | ||
return Zone.current[_zoneKey] ?? false; | ||
} | ||
|
||
/// Code that understands nullability should be run using this method, | ||
/// otherwise all type operations will treat all nullabilities as star. | ||
static R enableNullSafetyTypes<R>(R body()) { | ||
return runZoned<R>(body, zoneValues: {_zoneKey: true}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.