From f601692dd74aa389b592f4e0da006df1d7aeb4d7 Mon Sep 17 00:00:00 2001 From: Paul Berry Date: Wed, 20 Nov 2024 14:55:00 +0000 Subject: [PATCH] [_fe_analyzer_shared] Add SharedNullTypeStructure class. This is the shared base class for all representations of the type `Null`. This allows the shared codebase to use `is` tests to tell when a type is `Null`. Change-Id: I98059b60c7eaab9c9f1e3f7addb7913dffc9cf9d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/396380 Reviewed-by: Chloe Stefantsova Commit-Queue: Paul Berry --- .../lib/src/type_inference/type_analyzer_operations.dart | 6 +----- pkg/_fe_analyzer_shared/lib/src/types/shared_type.dart | 6 ++++++ pkg/_fe_analyzer_shared/test/mini_ast.dart | 3 --- pkg/_fe_analyzer_shared/test/mini_types.dart | 3 ++- pkg/analyzer/lib/src/dart/element/type.dart | 3 ++- .../lib/src/dart/resolver/flow_analysis_visitor.dart | 3 --- .../lib/src/type_inference/type_inference_engine.dart | 3 --- pkg/kernel/lib/ast.dart | 3 ++- pkg/kernel/lib/src/ast/types.dart | 2 +- 9 files changed, 14 insertions(+), 18 deletions(-) diff --git a/pkg/_fe_analyzer_shared/lib/src/type_inference/type_analyzer_operations.dart b/pkg/_fe_analyzer_shared/lib/src/type_inference/type_analyzer_operations.dart index 855556a19f4..02e030717b3 100644 --- a/pkg/_fe_analyzer_shared/lib/src/type_inference/type_analyzer_operations.dart +++ b/pkg/_fe_analyzer_shared/lib/src/type_inference/type_analyzer_operations.dart @@ -208,9 +208,6 @@ abstract interface class TypeAnalyzerOperations< /// a subtype query, but the implementations can do that more efficiently. bool isNonNullableInternal(TypeStructure type); - /// Returns `true` if [type] is `Null`. - bool isNullInternal(TypeStructure type); - /// Returns `true` if [type] is `Object` from `dart:core`. The method returns /// `false` for `Object?` and `Object*`. bool isObject(SharedTypeView type); @@ -1665,8 +1662,7 @@ abstract class TypeConstraintGenerator< // If `P` is `Null`, then the match holds under no constraints: // Only if `Q` is nullable. - if (pNullability == NullabilitySuffix.none && - typeAnalyzerOperations.isNullInternal(p)) { + if (p is SharedNullTypeStructure) { return typeAnalyzerOperations.isNullableInternal(q); } diff --git a/pkg/_fe_analyzer_shared/lib/src/types/shared_type.dart b/pkg/_fe_analyzer_shared/lib/src/types/shared_type.dart index 2135c12b27a..82a0459a680 100644 --- a/pkg/_fe_analyzer_shared/lib/src/types/shared_type.dart +++ b/pkg/_fe_analyzer_shared/lib/src/types/shared_type.dart @@ -66,6 +66,12 @@ abstract interface class SharedNamedTypeStructure< TypeStructure get type; } +/// Common interface for data structures used by implementations to represent +/// the type `Null`. +abstract interface class SharedNullTypeStructure< + TypeStructure extends SharedTypeStructure> + implements SharedTypeStructure {} + /// Common interface for data structures used by the implementations to /// represent a record type. abstract interface class SharedRecordTypeStructure< diff --git a/pkg/_fe_analyzer_shared/test/mini_ast.dart b/pkg/_fe_analyzer_shared/test/mini_ast.dart index 1ac22f82b9e..759af8beea1 100644 --- a/pkg/_fe_analyzer_shared/test/mini_ast.dart +++ b/pkg/_fe_analyzer_shared/test/mini_ast.dart @@ -2990,9 +2990,6 @@ class MiniAstOperations unwrappedType.nullabilitySuffix == NullabilitySuffix.none; } - @override - bool isNullInternal(Type type) => type is NullType; - @override bool isObject(SharedTypeView type) { Type unwrappedType = type.unwrapTypeView(); diff --git a/pkg/_fe_analyzer_shared/test/mini_types.dart b/pkg/_fe_analyzer_shared/test/mini_types.dart index 2e261ffa702..5bc0a910e2a 100644 --- a/pkg/_fe_analyzer_shared/test/mini_types.dart +++ b/pkg/_fe_analyzer_shared/test/mini_types.dart @@ -480,7 +480,8 @@ class NeverType extends _SpecialSimpleType { /// Representation of the type `Null` suitable for unit testing of code in the /// `_fe_analyzer_shared` package. -class NullType extends _SpecialSimpleType { +class NullType extends _SpecialSimpleType + implements SharedNullTypeStructure { static final instance = NullType._(); NullType._() diff --git a/pkg/analyzer/lib/src/dart/element/type.dart b/pkg/analyzer/lib/src/dart/element/type.dart index f1d5cbc6acb..03d648c2d74 100644 --- a/pkg/analyzer/lib/src/dart/element/type.dart +++ b/pkg/analyzer/lib/src/dart/element/type.dart @@ -1142,7 +1142,8 @@ class NeverTypeImpl extends TypeImpl implements NeverType { /// A concrete implementation of [DartType] representing the type `Null`, with /// no type parameters and no nullability suffix. -class NullTypeImpl extends InterfaceTypeImpl { +class NullTypeImpl extends InterfaceTypeImpl + implements SharedNullTypeStructure { NullTypeImpl({required super.element, super.alias}) : super._null(); @override diff --git a/pkg/analyzer/lib/src/dart/resolver/flow_analysis_visitor.dart b/pkg/analyzer/lib/src/dart/resolver/flow_analysis_visitor.dart index cb68fbb8615..cc13a4c0990 100644 --- a/pkg/analyzer/lib/src/dart/resolver/flow_analysis_visitor.dart +++ b/pkg/analyzer/lib/src/dart/resolver/flow_analysis_visitor.dart @@ -585,9 +585,6 @@ class TypeSystemOperations return typeSystem.isNullable(type); } - @override - bool isNullInternal(DartType type) => type.isDartCoreNull; - @override bool isObject(SharedTypeView type) { return type.unwrapTypeView().isDartCoreObject && diff --git a/pkg/front_end/lib/src/type_inference/type_inference_engine.dart b/pkg/front_end/lib/src/type_inference/type_inference_engine.dart index ce689e5f150..e19618cbe23 100644 --- a/pkg/front_end/lib/src/type_inference/type_inference_engine.dart +++ b/pkg/front_end/lib/src/type_inference/type_inference_engine.dart @@ -569,9 +569,6 @@ class OperationsCfe return typeEnvironment.coreTypes.isBottom(type.unwrapTypeView()); } - @override - bool isNullInternal(DartType type) => type is NullType; - @override // Coverage-ignore(suite): Not run. bool isObject(SharedTypeView type) { diff --git a/pkg/kernel/lib/ast.dart b/pkg/kernel/lib/ast.dart index 506a2558602..56855875bc8 100644 --- a/pkg/kernel/lib/ast.dart +++ b/pkg/kernel/lib/ast.dart @@ -74,10 +74,11 @@ import 'package:_fe_analyzer_shared/src/type_inference/type_analyzer_operations. import 'package:_fe_analyzer_shared/src/types/shared_type.dart' show SharedDynamicTypeStructure, - SharedNamedFunctionParameterStructure, SharedFunctionTypeStructure, SharedInvalidTypeStructure, + SharedNamedFunctionParameterStructure, SharedNamedTypeStructure, + SharedNullTypeStructure, SharedRecordTypeStructure, SharedTypeParameterStructure, SharedTypeStructure, diff --git a/pkg/kernel/lib/src/ast/types.dart b/pkg/kernel/lib/src/ast/types.dart index 05e7b9fa8ae..b940363dc83 100644 --- a/pkg/kernel/lib/src/ast/types.dart +++ b/pkg/kernel/lib/src/ast/types.dart @@ -774,7 +774,7 @@ class NeverType extends DartType { } } -class NullType extends DartType { +class NullType extends DartType implements SharedNullTypeStructure { @override final int hashCode = 415324;