Skip to content

Commit

Permalink
[analyzer] Rework InterfaceTypeImpl constructor to call NullTypeImpl
Browse files Browse the repository at this point in the history
Previously, the `InterfaceTypeImpl._` constructor threw an exception
if an interface type was being constructed for the type `Null`
(because the `NullTypeImpl` constructor should be used instead). This
was silly; as long as we're going to check, we might as well put the
check in the `InterfaceTypeImpl` factory constructor, and have it
simply construct the appropriate subtype. This is simpler, and should
be much lower risk, since we no longer have a code path that has to
throw an exception.

Change-Id: Ie3ed216b285e371866a6a35ccccdf997b8a1f95e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/396620
Reviewed-by: Brian Wilkerson <[email protected]>
Reviewed-by: Konstantin Shcheglov <[email protected]>
Commit-Queue: Paul Berry <[email protected]>
  • Loading branch information
stereotype441 authored and Commit Queue committed Nov 20, 2024
1 parent f6e4b3e commit 723c856
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 27 deletions.
20 changes: 5 additions & 15 deletions pkg/analyzer/lib/src/dart/element/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5913,16 +5913,11 @@ abstract class InterfaceElementImpl extends InstanceElementImpl
}
}

InterfaceTypeImpl result;
if (name == 'Null' && library.isDartCore) {
result = NullTypeImpl(element: this);
} else {
result = InterfaceTypeImpl(
element: this,
typeArguments: typeArguments,
nullabilitySuffix: nullabilitySuffix,
);
}
var result = InterfaceTypeImpl(
element: this,
typeArguments: typeArguments,
nullabilitySuffix: nullabilitySuffix,
);

if (typeArguments.isEmpty) {
switch (nullabilitySuffix) {
Expand Down Expand Up @@ -10283,11 +10278,6 @@ class TypeAliasElementImpl extends _ExistingElementImpl
typeArguments: typeArguments,
),
);
} else if (type is NullTypeImpl) {
return NullTypeImpl(
element: type.element,
alias: InstantiatedTypeAliasElementImpl(
element: this, typeArguments: typeArguments));
} else if (type is InterfaceType) {
return InterfaceTypeImpl(
element: type.element,
Expand Down
5 changes: 2 additions & 3 deletions pkg/analyzer/lib/src/dart/element/type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,8 @@ class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
: InvalidTypeImpl.instance,
nullabilitySuffix: nullabilitySuffix,
alias: alias);
} else if (element.name == 'Null' && element.library.isDartCore) {
return NullTypeImpl(element: element, alias: alias);
} else {
return InterfaceTypeImpl._(
element: element,
Expand All @@ -549,9 +551,6 @@ class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
required this.nullabilitySuffix,
required super.alias,
}) {
if (element.name == 'Null' && element.library.isDartCore) {
throw ArgumentError('NullTypeImpl should be used for `Null`');
}
if (element.augmentationTarget != null) {
throw ArgumentError(
'InterfaceType(s) can only be created for declarations',
Expand Down
14 changes: 5 additions & 9 deletions pkg/analyzer/lib/src/dart/element/type_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -608,14 +608,10 @@ class TypeProviderImpl extends TypeProviderBase {
}).toList(growable: false);
}

if (element.name == 'Null' && element.library.isDartCore) {
return NullTypeImpl(element: element);
} else {
return InterfaceTypeImpl(
element: element,
typeArguments: typeArguments,
nullabilitySuffix: NullabilitySuffix.none,
);
}
return InterfaceTypeImpl(
element: element,
typeArguments: typeArguments,
nullabilitySuffix: NullabilitySuffix.none,
);
}
}

0 comments on commit 723c856

Please sign in to comment.