Skip to content

Commit

Permalink
Elements. Store name2 in TypeParameterElementImpl.
Browse files Browse the repository at this point in the history
Change-Id: I93805cf3ac1b9e5aedb022e5d2f691137d188787
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/390802
Reviewed-by: Phil Quitslund <[email protected]>
Commit-Queue: Konstantin Shcheglov <[email protected]>
  • Loading branch information
scheglov authored and Commit Queue committed Oct 17, 2024
1 parent e29aa0c commit 6f83217
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 23 deletions.
2 changes: 1 addition & 1 deletion pkg/analyzer/lib/src/dart/analysis/driver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ import 'package:meta/meta.dart';
// TODO(scheglov): Clean up the list of implicitly analyzed files.
class AnalysisDriver {
/// The version of data format, should be incremented on every format change.
static const int DATA_VERSION = 398;
static const int DATA_VERSION = 399;

/// The number of exception contexts allowed to write. Once this field is
/// zero, we stop writing any new exception contexts in this process.
Expand Down
18 changes: 3 additions & 15 deletions pkg/analyzer/lib/src/dart/element/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10372,6 +10372,9 @@ abstract class TypeDefiningElementImpl2 extends ElementImpl2
/// A concrete implementation of a [TypeParameterElement].
class TypeParameterElementImpl extends ElementImpl
implements TypeParameterElement, TypeParameterFragment {
@override
FragmentNameImpl? name2;

/// The default value of the type parameter. It is used to provide the
/// corresponding missing type argument in type annotations and as the
/// fall-back type value in type inference.
Expand Down Expand Up @@ -10469,21 +10472,6 @@ class TypeParameterElementImpl extends ElementImpl
return super.name!;
}

@override
FragmentName? get name2 {
var name = this.name;

// If synthetic name.
if (name.isEmpty) {
return null;
}

return FragmentNameImpl(
name: name,
nameOffset: nameOffset,
);
}

@override
// TODO(augmentations): Support chaining between the fragments.
TypeParameterFragment? get nextFragment => null;
Expand Down
2 changes: 2 additions & 0 deletions pkg/analyzer/lib/src/summary2/bundle_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1801,9 +1801,11 @@ class LibraryReader {
List<TypeParameterElementImpl> _readTypeParameters() {
return _reader.readTypedList(() {
var name = _reader.readStringReference();
var fragmentName = _readFragmentName();
var varianceEncoding = _reader.readByte();
var variance = _decodeVariance(varianceEncoding);
var element = TypeParameterElementImpl(name, -1);
element.name2 = fragmentName;
element.variance = variance;
return element;
});
Expand Down
15 changes: 8 additions & 7 deletions pkg/analyzer/lib/src/summary2/bundle_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -723,13 +723,14 @@ class BundleWriter {
});
}

void _writeTypeParameterElement(TypeParameterElement typeParameter) {
typeParameter as TypeParameterElementImpl;
_sink._writeStringReference(typeParameter.name);
_sink.writeByte(_encodeVariance(typeParameter).index);
_resolutionSink._writeAnnotationList(typeParameter.metadata);
_resolutionSink.writeType(typeParameter.bound);
_resolutionSink.writeType(typeParameter.defaultType);
void _writeTypeParameterElement(TypeParameterElement element) {
element as TypeParameterElementImpl;
_sink._writeStringReference(element.name);
_writeFragmentName(element.name2);
_sink.writeByte(_encodeVariance(element).index);
_resolutionSink._writeAnnotationList(element.metadata);
_resolutionSink.writeType(element.bound);
_resolutionSink.writeType(element.defaultType);
}

/// Add [typeParameters] to the indexing scope, so make them available
Expand Down
2 changes: 2 additions & 0 deletions pkg/analyzer/lib/src/summary2/element_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1347,8 +1347,10 @@ class ElementBuilder extends ThrowingAstVisitor<void> {
void visitTypeParameter(covariant TypeParameterImpl node) {
var nameToken = node.name;
var name = nameToken.lexeme;
var fragmentName = _buildFragmentName(nameToken);

var element = TypeParameterElementImpl(name, nameToken.offset);
element.name2 = fragmentName;
element.metadata = _buildAnnotations(node.metadata);
_setCodeRange(element, node);

Expand Down
5 changes: 5 additions & 0 deletions pkg/analyzer/lib/src/summary2/informative_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,7 @@ class InformativeDataApplier {
element as TypeParameterElementImpl;
element.setCodeRange(info.codeOffset, info.codeLength);
element.nameOffset = info.nameOffset;
_setFragmentNameOffset(element.name2, info.nameOffset2);
},
);
}
Expand Down Expand Up @@ -2009,6 +2010,7 @@ class _InformativeDataWriter {
sink.writeUInt30(node.offset);
sink.writeUInt30(node.length);
sink.writeUInt30(node.name.offset);
sink.writeOptionalUInt30(node.name.offsetIfNotEmpty);
});
}
}
Expand Down Expand Up @@ -2046,19 +2048,22 @@ class _InfoTypeParameter {
final int codeOffset;
final int codeLength;
final int nameOffset;
final int? nameOffset2;

factory _InfoTypeParameter(SummaryDataReader reader) {
return _InfoTypeParameter._(
codeOffset: reader.readUInt30(),
codeLength: reader.readUInt30(),
nameOffset: reader.readUInt30(),
nameOffset2: reader.readOptionalUInt30(),
);
}

_InfoTypeParameter._({
required this.codeOffset,
required this.codeLength,
required this.nameOffset,
required this.nameOffset2,
});
}

Expand Down
55 changes: 55 additions & 0 deletions pkg/analyzer/test/src/summary/elements/class_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25037,6 +25037,61 @@ library
''');
}

test_class_typeParameters_missingName() async {
var library = await buildLibrary(r'''
class A<T,> {}
''');
checkElementText(library, r'''
library
reference: <testLibrary>
definingUnit: <testLibraryFragment>
units
<testLibraryFragment>
enclosingElement3: <null>
classes
class A @6
reference: <testLibraryFragment>::@class::A
enclosingElement3: <testLibraryFragment>
typeParameters
covariant T @8
defaultType: dynamic
covariant @10
defaultType: dynamic
constructors
synthetic @-1
reference: <testLibraryFragment>::@class::A::@constructor::new
enclosingElement3: <testLibraryFragment>::@class::A
----------------------------------------
library
reference: <testLibrary>
fragments
<testLibraryFragment>
element: <testLibrary>
classes
class A @6
reference: <testLibraryFragment>::@class::A
element: <testLibraryFragment>::@class::A#element
typeParameters
T @8
element: <not-implemented>
<null-name>
element: <not-implemented>
constructors
synthetic <null-name>
reference: <testLibraryFragment>::@class::A::@constructor::new
element: <testLibraryFragment>::@class::A::@constructor::new#element
classes
class A
firstFragment: <testLibraryFragment>::@class::A
typeParameters
T

constructors
synthetic new
firstFragment: <testLibraryFragment>::@class::A::@constructor::new
''');
}

test_class_typeParameters_variance_contravariant() async {
var library = await buildLibrary('class C<in T> {}');
checkElementText(library, r'''
Expand Down

0 comments on commit 6f83217

Please sign in to comment.