Skip to content

Commit f35cbc9

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Add NeverElementImpl2, make NeverElementImpl its fragment.
Change-Id: I3ef636dec6a678e30c44f0551276a8b3557a7641 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/394240 Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Phil Quitslund <[email protected]>
1 parent 0b22187 commit f35cbc9

File tree

9 files changed

+117
-22
lines changed

9 files changed

+117
-22
lines changed

pkg/analyzer/lib/src/dart/element/display_string_builder.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ class ElementDisplayStringBuilder {
3939
_write(element.name ?? '<unnamed $runtimeType>');
4040
}
4141

42+
void writeAbstractElement2(ElementImpl2 element) {
43+
_write(element.name3 ?? '<unnamed $runtimeType>');
44+
}
45+
4246
void writeClassElement(ClassElementImpl element) {
4347
if (element.isAugmentation) {
4448
_write('augment ');

pkg/analyzer/lib/src/dart/element/element.dart

Lines changed: 103 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3201,11 +3201,23 @@ abstract class ElementImpl2 implements Element2 {
32013201
return enclosingElement2?.session;
32023202
}
32033203

3204+
/// Append a textual representation of this element to the given [builder].
3205+
void appendTo(ElementDisplayStringBuilder builder) {
3206+
builder.writeAbstractElement2(this);
3207+
}
3208+
32043209
@override
32053210
String displayString2({
32063211
bool multiline = false,
32073212
bool preferTypeAlias = false,
3208-
});
3213+
}) {
3214+
var builder = ElementDisplayStringBuilder(
3215+
multiline: multiline,
3216+
preferTypeAlias: preferTypeAlias,
3217+
);
3218+
appendTo(builder);
3219+
return builder.toString();
3220+
}
32093221

32103222
@override
32113223
bool isAccessibleIn2(LibraryElement2 library) {
@@ -3240,6 +3252,15 @@ abstract class ElementImpl2 implements Element2 {
32403252
String toString() {
32413253
return displayString2();
32423254
}
3255+
3256+
/// Use the given [visitor] to visit all of the children of this element.
3257+
/// There is no guarantee of the order in which the children will be visited.
3258+
@override
3259+
void visitChildren2<T>(ElementVisitor2<T> visitor) {
3260+
for (var child in children2) {
3261+
child.accept2(visitor);
3262+
}
3263+
}
32433264
}
32443265

32453266
/// A concrete implementation of an [ElementLocation].
@@ -8198,10 +8219,8 @@ class MultiplyDefinedElementImpl implements MultiplyDefinedElement {
81988219
}
81998220
}
82008221

8201-
class MultiplyDefinedElementImpl2 implements MultiplyDefinedElement2 {
8202-
@override
8203-
final int id = ElementImpl._NEXT_ID++;
8204-
8222+
class MultiplyDefinedElementImpl2 extends ElementImpl2
8223+
implements MultiplyDefinedElement2 {
82058224
final CompilationUnitElementImpl libraryFragment;
82068225

82078226
@override
@@ -8365,7 +8384,8 @@ class MultiplyDefinedFragmentImpl implements MultiplyDefinedFragment {
83658384
}
83668385

83678386
/// The synthetic element representing the declaration of the type `Never`.
8368-
class NeverElementImpl extends ElementImpl implements TypeDefiningElement {
8387+
class NeverElementImpl extends ElementImpl
8388+
implements TypeDefiningElement, TypeDefiningFragment {
83698389
/// The unique instance of this class.
83708390
static final instance = NeverElementImpl._();
83718391

@@ -8380,9 +8400,33 @@ class NeverElementImpl extends ElementImpl implements TypeDefiningElement {
83808400
@override
83818401
List<Element2> get children2 => const [];
83828402

8403+
@override
8404+
List<Fragment> get children3 => const [];
8405+
8406+
@override
8407+
NeverElementImpl2 get element => NeverElementImpl2.instance;
8408+
8409+
@override
8410+
Null get enclosingFragment => null;
8411+
83838412
@override
83848413
ElementKind get kind => ElementKind.NEVER;
83858414

8415+
@override
8416+
Null get libraryFragment => null;
8417+
8418+
@override
8419+
String get name2 => 'Never';
8420+
8421+
@override
8422+
Null get nameOffset2 => null;
8423+
8424+
@override
8425+
Null get nextFragment => null;
8426+
8427+
@override
8428+
Null get previousFragment => null;
8429+
83868430
@override
83878431
T? accept<T>(ElementVisitor<T> visitor) => null;
83888432

@@ -8401,6 +8445,59 @@ class NeverElementImpl extends ElementImpl implements TypeDefiningElement {
84018445
}
84028446
}
84038447

8448+
/// The synthetic element representing the declaration of the type `Never`.
8449+
class NeverElementImpl2 extends TypeDefiningElementImpl2 {
8450+
/// The unique instance of this class.
8451+
static final instance = NeverElementImpl2._();
8452+
8453+
NeverElementImpl2._();
8454+
8455+
@override
8456+
List<Element2> get children2 => const [];
8457+
8458+
@override
8459+
Null get documentationComment => null;
8460+
8461+
@override
8462+
NeverElementImpl get firstFragment => NeverElementImpl.instance;
8463+
8464+
@override
8465+
bool get isSynthetic => true;
8466+
8467+
@override
8468+
ElementKind get kind => ElementKind.NEVER;
8469+
8470+
@override
8471+
Null get library2 => null;
8472+
8473+
@override
8474+
Metadata get metadata2 {
8475+
return MetadataImpl(0, const [], () => null);
8476+
}
8477+
8478+
@override
8479+
String get name3 => 'Never';
8480+
8481+
@override
8482+
T? accept2<T>(ElementVisitor2<T> visitor) {
8483+
return null;
8484+
}
8485+
8486+
DartType instantiate({
8487+
required NullabilitySuffix nullabilitySuffix,
8488+
}) {
8489+
switch (nullabilitySuffix) {
8490+
case NullabilitySuffix.question:
8491+
return NeverTypeImpl.instanceNullable;
8492+
case NullabilitySuffix.star:
8493+
// TODO(scheglov): remove together with `star`
8494+
return NeverTypeImpl.instanceNullable;
8495+
case NullabilitySuffix.none:
8496+
return NeverTypeImpl.instance;
8497+
}
8498+
}
8499+
}
8500+
84048501
/// A [VariableElementImpl], which is not a parameter.
84058502
abstract class NonParameterVariableElementImpl extends VariableElementImpl
84068503
with _HasLibraryMixin {
@@ -10191,13 +10288,6 @@ class TypeAliasElementImpl2 extends TypeDefiningElementImpl2
1019110288
required NullabilitySuffix nullabilitySuffix}) =>
1019210289
firstFragment.instantiate(
1019310290
typeArguments: typeArguments, nullabilitySuffix: nullabilitySuffix);
10194-
10195-
@override
10196-
void visitChildren2<T>(ElementVisitor2<T> visitor) {
10197-
for (var child in children2) {
10198-
child.accept2(visitor);
10199-
}
10200-
}
1020110291
}
1020210292

1020310293
abstract class TypeDefiningElementImpl2 extends ElementImpl2

pkg/analyzer/lib/src/dart/element/type.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,16 +1073,15 @@ class NeverTypeImpl extends TypeImpl implements NeverType {
10731073
@override
10741074
final NeverElementImpl element = NeverElementImpl.instance;
10751075

1076+
@override
1077+
final NeverElementImpl2 element3 = NeverElementImpl2.instance;
1078+
10761079
@override
10771080
final NullabilitySuffix nullabilitySuffix;
10781081

10791082
/// Prevent the creation of instances of this class.
10801083
NeverTypeImpl._(this.nullabilitySuffix);
10811084

1082-
@override
1083-
// TODO(augmentations): Implement this.
1084-
Element2? get element3 => throw UnimplementedError();
1085-
10861085
@override
10871086
int get hashCode => 0;
10881087

pkg/analyzer/lib/src/utilities/extensions/element.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ extension ElementOrNullExtension on Element? {
153153
self.conflictingElements.map((e) => e.asElement2).nonNulls.toList(),
154154
);
155155
} else if (self is NeverElementImpl) {
156-
return self;
156+
return NeverElementImpl2.instance;
157157
} else if (self is ParameterMember) {
158158
// TODO(scheglov): we lose types here
159159
return self.declaration.asElement2;

pkg/analyzer/test/src/dart/resolution/named_type_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ f(Never a) {}
803803
NamedType
804804
name: Never
805805
element: Never@-1
806-
element2: Never@-1
806+
element2: Never
807807
type: Never
808808
''');
809809
}

pkg/analyzer/test/src/dart/resolution/simple_identifier_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ main() {
739739
SimpleIdentifier
740740
token: Never
741741
staticElement: Never@-1
742-
element: Never@-1
742+
element: Never
743743
staticType: Type
744744
''');
745745
}

pkg/analyzer/test/src/diagnostics/extends_non_class_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ ExtendsClause
157157
superclass: NamedType
158158
name: Never
159159
element: Never@-1
160-
element2: Never@-1
160+
element2: Never
161161
type: Never
162162
''');
163163
}

pkg/analyzer/test/src/diagnostics/mixin_super_class_constraint_non_interface_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ MixinOnClause
9898
NamedType
9999
name: Never
100100
element: Never@-1
101-
element2: Never@-1
101+
element2: Never
102102
type: Never
103103
''');
104104
}

pkg/analyzer/test/util/element_printer.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ class ElementPrinter {
125125
case LocalVariableElementImpl2():
126126
// TODO(scheglov): nameOffset2 can be `null`
127127
_sink.write('${element.name3}@${element.firstFragment.nameOffset2}');
128+
case NeverElementImpl2():
129+
_sink.write('Never');
128130
case ClassElementImpl2 element:
129131
writeReference(element.reference);
130132
case EnumElementImpl2 element:

0 commit comments

Comments
 (0)