From 6f8321757f1b866a93cf4782a29f8bb0dd22ff5b Mon Sep 17 00:00:00 2001 From: Konstantin Shcheglov Date: Thu, 17 Oct 2024 21:56:30 +0000 Subject: [PATCH] Elements. Store name2 in TypeParameterElementImpl. Change-Id: I93805cf3ac1b9e5aedb022e5d2f691137d188787 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/390802 Reviewed-by: Phil Quitslund Commit-Queue: Konstantin Shcheglov --- .../lib/src/dart/analysis/driver.dart | 2 +- .../lib/src/dart/element/element.dart | 18 +----- .../lib/src/summary2/bundle_reader.dart | 2 + .../lib/src/summary2/bundle_writer.dart | 15 ++--- .../lib/src/summary2/element_builder.dart | 2 + .../lib/src/summary2/informative_data.dart | 5 ++ .../test/src/summary/elements/class_test.dart | 55 +++++++++++++++++++ 7 files changed, 76 insertions(+), 23 deletions(-) diff --git a/pkg/analyzer/lib/src/dart/analysis/driver.dart b/pkg/analyzer/lib/src/dart/analysis/driver.dart index 08ad58afa823..737dd4be33fc 100644 --- a/pkg/analyzer/lib/src/dart/analysis/driver.dart +++ b/pkg/analyzer/lib/src/dart/analysis/driver.dart @@ -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. diff --git a/pkg/analyzer/lib/src/dart/element/element.dart b/pkg/analyzer/lib/src/dart/element/element.dart index 3440eb0a7188..1246ae2ac4bd 100644 --- a/pkg/analyzer/lib/src/dart/element/element.dart +++ b/pkg/analyzer/lib/src/dart/element/element.dart @@ -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. @@ -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; diff --git a/pkg/analyzer/lib/src/summary2/bundle_reader.dart b/pkg/analyzer/lib/src/summary2/bundle_reader.dart index d5e4ce9a7e14..fa34bf5d35cd 100644 --- a/pkg/analyzer/lib/src/summary2/bundle_reader.dart +++ b/pkg/analyzer/lib/src/summary2/bundle_reader.dart @@ -1801,9 +1801,11 @@ class LibraryReader { List _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; }); diff --git a/pkg/analyzer/lib/src/summary2/bundle_writer.dart b/pkg/analyzer/lib/src/summary2/bundle_writer.dart index fcbe4b9eeb06..5be4acf7ab3f 100644 --- a/pkg/analyzer/lib/src/summary2/bundle_writer.dart +++ b/pkg/analyzer/lib/src/summary2/bundle_writer.dart @@ -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 diff --git a/pkg/analyzer/lib/src/summary2/element_builder.dart b/pkg/analyzer/lib/src/summary2/element_builder.dart index bd7e33ac1687..fbcad55deaf2 100644 --- a/pkg/analyzer/lib/src/summary2/element_builder.dart +++ b/pkg/analyzer/lib/src/summary2/element_builder.dart @@ -1347,8 +1347,10 @@ class ElementBuilder extends ThrowingAstVisitor { 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); diff --git a/pkg/analyzer/lib/src/summary2/informative_data.dart b/pkg/analyzer/lib/src/summary2/informative_data.dart index 496439e96701..b823b748f922 100644 --- a/pkg/analyzer/lib/src/summary2/informative_data.dart +++ b/pkg/analyzer/lib/src/summary2/informative_data.dart @@ -816,6 +816,7 @@ class InformativeDataApplier { element as TypeParameterElementImpl; element.setCodeRange(info.codeOffset, info.codeLength); element.nameOffset = info.nameOffset; + _setFragmentNameOffset(element.name2, info.nameOffset2); }, ); } @@ -2009,6 +2010,7 @@ class _InformativeDataWriter { sink.writeUInt30(node.offset); sink.writeUInt30(node.length); sink.writeUInt30(node.name.offset); + sink.writeOptionalUInt30(node.name.offsetIfNotEmpty); }); } } @@ -2046,12 +2048,14 @@ 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(), ); } @@ -2059,6 +2063,7 @@ class _InfoTypeParameter { required this.codeOffset, required this.codeLength, required this.nameOffset, + required this.nameOffset2, }); } diff --git a/pkg/analyzer/test/src/summary/elements/class_test.dart b/pkg/analyzer/test/src/summary/elements/class_test.dart index 89c2b46e81e9..a2e57b0a5357 100644 --- a/pkg/analyzer/test/src/summary/elements/class_test.dart +++ b/pkg/analyzer/test/src/summary/elements/class_test.dart @@ -25037,6 +25037,61 @@ library '''); } + test_class_typeParameters_missingName() async { + var library = await buildLibrary(r''' +class A {} +'''); + checkElementText(library, r''' +library + reference: + definingUnit: + units + + enclosingElement3: + classes + class A @6 + reference: ::@class::A + enclosingElement3: + typeParameters + covariant T @8 + defaultType: dynamic + covariant @10 + defaultType: dynamic + constructors + synthetic @-1 + reference: ::@class::A::@constructor::new + enclosingElement3: ::@class::A +---------------------------------------- +library + reference: + fragments + + element: + classes + class A @6 + reference: ::@class::A + element: ::@class::A#element + typeParameters + T @8 + element: + + element: + constructors + synthetic + reference: ::@class::A::@constructor::new + element: ::@class::A::@constructor::new#element + classes + class A + firstFragment: ::@class::A + typeParameters + T + + constructors + synthetic new + firstFragment: ::@class::A::@constructor::new +'''); + } + test_class_typeParameters_variance_contravariant() async { var library = await buildLibrary('class C {}'); checkElementText(library, r'''