Skip to content

Commit

Permalink
Elements. Store name2 in TypeAliasElementImpl.
Browse files Browse the repository at this point in the history
Change-Id: I5b096df892bd065d7d2f4013b53cc046646a77a9
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/390821
Reviewed-by: Brian Wilkerson <[email protected]>
Commit-Queue: Konstantin Shcheglov <[email protected]>
  • Loading branch information
scheglov authored and Commit Queue committed Oct 17, 2024
1 parent 83aaca0 commit 087d2fe
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 19 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 = 397;
static const int DATA_VERSION = 398;

/// 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 @@ -10040,6 +10040,9 @@ class TypeAliasElementImpl extends _ExistingElementImpl
AugmentableElement<TypeAliasElementImpl>,
MacroTargetElement
implements TypeAliasElement, TypeAliasFragment {
@override
FragmentNameImpl? name2;

/// Is `true` if the element has direct or indirect reference to itself
/// from anywhere except a class element or type parameter bounds.
bool hasSelfReference = false;
Expand Down Expand Up @@ -10180,21 +10183,6 @@ class TypeAliasElementImpl extends _ExistingElementImpl
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 the fragment chain.
TypeAliasFragment? get nextFragment => null;
Expand Down
4 changes: 3 additions & 1 deletion pkg/analyzer/lib/src/summary2/bundle_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1759,7 +1759,8 @@ class LibraryReader {
) {
var resolutionOffset = _baseResolutionOffset + _reader.readUInt30();
var reference = _readReference();
var name = reference.elementName;
var fragmentName = _readFragmentName();
var name = _reader.readStringReference();

var isFunctionTypeAliasBased = _reader.readBool();

Expand All @@ -1770,6 +1771,7 @@ class LibraryReader {
} else {
element = TypeAliasElementImpl(name, -1);
}
element.name2 = fragmentName;

var linkedData = TypeAliasElementLinkedData(
reference: reference,
Expand Down
2 changes: 2 additions & 0 deletions pkg/analyzer/lib/src/summary2/bundle_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,8 @@ class BundleWriter {
_sink.writeUInt30(_resolutionSink.offset);

_writeReference(element);
_writeFragmentName(element.name2);
_sink._writeStringReference(element.name);
_sink.writeBool(element.isFunctionTypeAliasBased);
TypeAliasElementFlags.write(_sink, element);

Expand Down
10 changes: 8 additions & 2 deletions pkg/analyzer/lib/src/summary2/element_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -827,8 +827,10 @@ class ElementBuilder extends ThrowingAstVisitor<void> {
void visitFunctionTypeAlias(covariant FunctionTypeAliasImpl node) {
var nameToken = node.name;
var name = nameToken.lexeme;
var fragmentName = _buildFragmentName(nameToken);

var element = TypeAliasElementImpl(name, nameToken.offset);
element.name2 = fragmentName;
element.isFunctionTypeAliasBased = true;
element.metadata = _buildAnnotations(node.metadata);
_setCodeRange(element, node);
Expand All @@ -837,7 +839,8 @@ class ElementBuilder extends ThrowingAstVisitor<void> {
node.declaredElement = element;
_linker.elementNodes[element] = node;

var reference = _enclosingContext.addTypeAlias(name, element);
var refName = fragmentName?.name ?? '${_nextUnnamedId++}';
var reference = _enclosingContext.addTypeAlias(refName, element);
_libraryBuilder.declare(name, reference);

var holder = _EnclosingContext(reference, element);
Expand Down Expand Up @@ -939,8 +942,10 @@ class ElementBuilder extends ThrowingAstVisitor<void> {
void visitGenericTypeAlias(covariant GenericTypeAliasImpl node) {
var nameToken = node.name;
var name = nameToken.lexeme;
var fragmentName = _buildFragmentName(nameToken);

var element = TypeAliasElementImpl(name, nameToken.offset);
element.name2 = fragmentName;
element.isAugmentation = node.augmentKeyword != null;
element.metadata = _buildAnnotations(node.metadata);
_setCodeRange(element, node);
Expand All @@ -949,7 +954,8 @@ class ElementBuilder extends ThrowingAstVisitor<void> {
node.declaredElement = element;
_linker.elementNodes[element] = node;

var reference = _enclosingContext.addTypeAlias(name, element);
var refName = fragmentName?.name ?? '${_nextUnnamedId++}';
var reference = _enclosingContext.addTypeAlias(refName, element);
if (!element.isAugmentation) {
_libraryBuilder.declare(name, reference);
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/analyzer/lib/src/summary2/informative_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,7 @@ class InformativeDataApplier {
element as TypeAliasElementImpl;
element.setCodeRange(info.codeOffset, info.codeLength);
element.nameOffset = info.nameOffset;
_setFragmentNameOffset(element.name2, info.nameOffset2);
element.documentationComment = info.documentationComment;
_applyToTypeParameters(
element.typeParameters_unresolved,
Expand All @@ -638,6 +639,7 @@ class InformativeDataApplier {
element as TypeAliasElementImpl;
element.setCodeRange(info.codeOffset, info.codeLength);
element.nameOffset = info.nameOffset;
_setFragmentNameOffset(element.name2, info.nameOffset2);
element.documentationComment = info.documentationComment;
_applyToTypeParameters(
element.typeParameters_unresolved,
Expand Down Expand Up @@ -1284,6 +1286,7 @@ class _InfoFunctionTypeAlias {
final int codeOffset;
final int codeLength;
final int nameOffset;
final int? nameOffset2;
final String? documentationComment;
final List<_InfoTypeParameter> typeParameters;
final List<_InfoFormalParameter> parameters;
Expand All @@ -1294,6 +1297,7 @@ class _InfoFunctionTypeAlias {
codeOffset: reader.readUInt30(),
codeLength: reader.readUInt30(),
nameOffset: reader.readUInt30(),
nameOffset2: reader.readOptionalUInt30(),
documentationComment: reader.readStringUtf8().nullIfEmpty,
typeParameters: reader.readTypedList(
() => _InfoTypeParameter(reader),
Expand All @@ -1309,6 +1313,7 @@ class _InfoFunctionTypeAlias {
required this.codeOffset,
required this.codeLength,
required this.nameOffset,
required this.nameOffset2,
required this.documentationComment,
required this.typeParameters,
required this.parameters,
Expand All @@ -1320,6 +1325,7 @@ class _InfoGenericTypeAlias {
final int codeOffset;
final int codeLength;
final int nameOffset;
final int? nameOffset2;
final String? documentationComment;
final List<_InfoTypeParameter> typeParameters;
final List<_InfoTypeParameter> aliasedTypeParameters;
Expand All @@ -1331,6 +1337,7 @@ class _InfoGenericTypeAlias {
codeOffset: reader.readUInt30(),
codeLength: reader.readUInt30(),
nameOffset: reader.readUInt30(),
nameOffset2: reader.readOptionalUInt30(),
documentationComment: reader.readStringUtf8().nullIfEmpty,
typeParameters: reader.readTypedList(
() => _InfoTypeParameter(reader),
Expand All @@ -1349,6 +1356,7 @@ class _InfoGenericTypeAlias {
required this.codeOffset,
required this.codeLength,
required this.nameOffset,
required this.nameOffset2,
required this.documentationComment,
required this.typeParameters,
required this.aliasedTypeParameters,
Expand Down Expand Up @@ -1607,6 +1615,7 @@ class _InformativeDataWriter {
sink.writeUInt30(node.offset);
sink.writeUInt30(node.length);
sink.writeUInt30(node.name.offset);
sink.writeOptionalUInt30(node.name.offsetIfNotEmpty);
_writeDocumentationComment(node);
_writeTypeParameters(node.typeParameters);
_writeFormalParameters(node.parameters);
Expand All @@ -1622,6 +1631,7 @@ class _InformativeDataWriter {
sink.writeUInt30(node.offset);
sink.writeUInt30(node.length);
sink.writeUInt30(node.name.offset);
sink.writeOptionalUInt30(node.name.offsetIfNotEmpty);
_writeDocumentationComment(node);
_writeTypeParameters(node.typeParameters);
if (aliasedType is GenericFunctionType) {
Expand Down
35 changes: 35 additions & 0 deletions pkg/analyzer/test/src/dart/parser/type_alias_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/src/dart/error/syntactic_errors.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';

import '../../diagnostics/parser_diagnostics.dart';
Expand Down Expand Up @@ -50,6 +51,40 @@ GenericTypeAlias
equals: =
type: NamedType
name: int
''');
}

test_modern_missingName() {
// Does not look good.
// https://github.com/dart-lang/sdk/issues/56912
var parseResult = parseStringWithErrors(r'''
typedef = int;
''');
parseResult.assertErrors([
error(ParserErrorCode.EXPECTED_TOKEN, 0, 7),
error(ParserErrorCode.MISSING_IDENTIFIER, 8, 1),
error(ParserErrorCode.MISSING_TYPEDEF_PARAMETERS, 8, 1),
error(ParserErrorCode.EXPECTED_EXECUTABLE, 8, 1),
error(ParserErrorCode.MISSING_CONST_FINAL_VAR_OR_TYPE, 10, 3),
]);

var node = parseResult.findNode.unit;
assertParsedNodeText(node, r'''
CompilationUnit
declarations
FunctionTypeAlias
typedefKeyword: typedef
name: <empty> <synthetic>
parameters: FormalParameterList
leftParenthesis: ( <synthetic>
rightParenthesis: ) <synthetic>
semicolon: ; <synthetic>
TopLevelVariableDeclaration
variables: VariableDeclarationList
variables
VariableDeclaration
name: int
semicolon: ;
''');
}
}
83 changes: 83 additions & 0 deletions pkg/analyzer/test/src/summary/elements/type_alias_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6217,6 +6217,89 @@ library
''');
}

test_typedef_nonFunction_missingName() async {
var library = await buildLibrary(r'''
typedef = int;
''');
checkElementText(library, r'''
library
reference: <testLibrary>
definingUnit: <testLibraryFragment>
units
<testLibraryFragment>
enclosingElement3: <null>
typeAliases
functionTypeAliasBased @8
reference: <testLibraryFragment>::@typeAlias::0
aliasedType: dynamic Function()
aliasedElement: GenericFunctionTypeElement
returnType: dynamic
topLevelVariables
static int @10
reference: <testLibraryFragment>::@topLevelVariable::int
enclosingElement3: <testLibraryFragment>
type: dynamic
accessors
synthetic static get int @-1
reference: <testLibraryFragment>::@getter::int
enclosingElement3: <testLibraryFragment>
returnType: dynamic
synthetic static set int= @-1
reference: <testLibraryFragment>::@setter::int
enclosingElement3: <testLibraryFragment>
parameters
requiredPositional _int @-1
type: dynamic
returnType: void
----------------------------------------
library
reference: <testLibrary>
fragments
<testLibraryFragment>
element: <testLibrary>
typeAliases
<null-name>
reference: <testLibraryFragment>::@typeAlias::0
element: <testLibraryFragment>::@typeAlias::0#element
topLevelVariables
int @10
reference: <testLibraryFragment>::@topLevelVariable::int
element: <testLibraryFragment>::@topLevelVariable::int#element
getter2: <testLibraryFragment>::@getter::int
setter2: <testLibraryFragment>::@setter::int
getters
get <null-name>
reference: <testLibraryFragment>::@getter::int
element: <testLibraryFragment>::@getter::int#element
setters
set <null-name>
reference: <testLibraryFragment>::@setter::int
element: <testLibraryFragment>::@setter::int#element
formalParameters
<null-name>
element: <testLibraryFragment>::@setter::int::@parameter::_int#element
typeAliases
firstFragment: <testLibraryFragment>::@typeAlias::0
aliasedType: dynamic Function()
topLevelVariables
int
firstFragment: <testLibraryFragment>::@topLevelVariable::int
type: dynamic
getter: <testLibraryFragment>::@getter::int#element
setter: <testLibraryFragment>::@setter::int#element
getters
synthetic static get int
firstFragment: <testLibraryFragment>::@getter::int
setters
synthetic static set int=
firstFragment: <testLibraryFragment>::@setter::int
formalParameters
requiredPositional _int
type: dynamic
''');
}

test_typedef_nonFunction_using_dynamic() async {
var library = await buildLibrary(r'''
typedef A = dynamic;
Expand Down

0 comments on commit 087d2fe

Please sign in to comment.