Skip to content

Commit 087d2fe

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Store name2 in TypeAliasElementImpl.
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]>
1 parent 83aaca0 commit 087d2fe

File tree

8 files changed

+145
-19
lines changed

8 files changed

+145
-19
lines changed

pkg/analyzer/lib/src/dart/analysis/driver.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ import 'package:meta/meta.dart';
9696
// TODO(scheglov): Clean up the list of implicitly analyzed files.
9797
class AnalysisDriver {
9898
/// The version of data format, should be incremented on every format change.
99-
static const int DATA_VERSION = 397;
99+
static const int DATA_VERSION = 398;
100100

101101
/// The number of exception contexts allowed to write. Once this field is
102102
/// zero, we stop writing any new exception contexts in this process.

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

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10040,6 +10040,9 @@ class TypeAliasElementImpl extends _ExistingElementImpl
1004010040
AugmentableElement<TypeAliasElementImpl>,
1004110041
MacroTargetElement
1004210042
implements TypeAliasElement, TypeAliasFragment {
10043+
@override
10044+
FragmentNameImpl? name2;
10045+
1004310046
/// Is `true` if the element has direct or indirect reference to itself
1004410047
/// from anywhere except a class element or type parameter bounds.
1004510048
bool hasSelfReference = false;
@@ -10180,21 +10183,6 @@ class TypeAliasElementImpl extends _ExistingElementImpl
1018010183
return super.name!;
1018110184
}
1018210185

10183-
@override
10184-
FragmentName? get name2 {
10185-
var name = this.name;
10186-
10187-
// If synthetic name.
10188-
if (name.isEmpty) {
10189-
return null;
10190-
}
10191-
10192-
return FragmentNameImpl(
10193-
name: name,
10194-
nameOffset: nameOffset,
10195-
);
10196-
}
10197-
1019810186
@override
1019910187
// TODO(augmentations): Support the fragment chain.
1020010188
TypeAliasFragment? get nextFragment => null;

pkg/analyzer/lib/src/summary2/bundle_reader.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1759,7 +1759,8 @@ class LibraryReader {
17591759
) {
17601760
var resolutionOffset = _baseResolutionOffset + _reader.readUInt30();
17611761
var reference = _readReference();
1762-
var name = reference.elementName;
1762+
var fragmentName = _readFragmentName();
1763+
var name = _reader.readStringReference();
17631764

17641765
var isFunctionTypeAliasBased = _reader.readBool();
17651766

@@ -1770,6 +1771,7 @@ class LibraryReader {
17701771
} else {
17711772
element = TypeAliasElementImpl(name, -1);
17721773
}
1774+
element.name2 = fragmentName;
17731775

17741776
var linkedData = TypeAliasElementLinkedData(
17751777
reference: reference,

pkg/analyzer/lib/src/summary2/bundle_writer.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,8 @@ class BundleWriter {
706706
_sink.writeUInt30(_resolutionSink.offset);
707707

708708
_writeReference(element);
709+
_writeFragmentName(element.name2);
710+
_sink._writeStringReference(element.name);
709711
_sink.writeBool(element.isFunctionTypeAliasBased);
710712
TypeAliasElementFlags.write(_sink, element);
711713

pkg/analyzer/lib/src/summary2/element_builder.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -827,8 +827,10 @@ class ElementBuilder extends ThrowingAstVisitor<void> {
827827
void visitFunctionTypeAlias(covariant FunctionTypeAliasImpl node) {
828828
var nameToken = node.name;
829829
var name = nameToken.lexeme;
830+
var fragmentName = _buildFragmentName(nameToken);
830831

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

840-
var reference = _enclosingContext.addTypeAlias(name, element);
842+
var refName = fragmentName?.name ?? '${_nextUnnamedId++}';
843+
var reference = _enclosingContext.addTypeAlias(refName, element);
841844
_libraryBuilder.declare(name, reference);
842845

843846
var holder = _EnclosingContext(reference, element);
@@ -939,8 +942,10 @@ class ElementBuilder extends ThrowingAstVisitor<void> {
939942
void visitGenericTypeAlias(covariant GenericTypeAliasImpl node) {
940943
var nameToken = node.name;
941944
var name = nameToken.lexeme;
945+
var fragmentName = _buildFragmentName(nameToken);
942946

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

952-
var reference = _enclosingContext.addTypeAlias(name, element);
957+
var refName = fragmentName?.name ?? '${_nextUnnamedId++}';
958+
var reference = _enclosingContext.addTypeAlias(refName, element);
953959
if (!element.isAugmentation) {
954960
_libraryBuilder.declare(name, reference);
955961
}

pkg/analyzer/lib/src/summary2/informative_data.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,7 @@ class InformativeDataApplier {
618618
element as TypeAliasElementImpl;
619619
element.setCodeRange(info.codeOffset, info.codeLength);
620620
element.nameOffset = info.nameOffset;
621+
_setFragmentNameOffset(element.name2, info.nameOffset2);
621622
element.documentationComment = info.documentationComment;
622623
_applyToTypeParameters(
623624
element.typeParameters_unresolved,
@@ -638,6 +639,7 @@ class InformativeDataApplier {
638639
element as TypeAliasElementImpl;
639640
element.setCodeRange(info.codeOffset, info.codeLength);
640641
element.nameOffset = info.nameOffset;
642+
_setFragmentNameOffset(element.name2, info.nameOffset2);
641643
element.documentationComment = info.documentationComment;
642644
_applyToTypeParameters(
643645
element.typeParameters_unresolved,
@@ -1284,6 +1286,7 @@ class _InfoFunctionTypeAlias {
12841286
final int codeOffset;
12851287
final int codeLength;
12861288
final int nameOffset;
1289+
final int? nameOffset2;
12871290
final String? documentationComment;
12881291
final List<_InfoTypeParameter> typeParameters;
12891292
final List<_InfoFormalParameter> parameters;
@@ -1294,6 +1297,7 @@ class _InfoFunctionTypeAlias {
12941297
codeOffset: reader.readUInt30(),
12951298
codeLength: reader.readUInt30(),
12961299
nameOffset: reader.readUInt30(),
1300+
nameOffset2: reader.readOptionalUInt30(),
12971301
documentationComment: reader.readStringUtf8().nullIfEmpty,
12981302
typeParameters: reader.readTypedList(
12991303
() => _InfoTypeParameter(reader),
@@ -1309,6 +1313,7 @@ class _InfoFunctionTypeAlias {
13091313
required this.codeOffset,
13101314
required this.codeLength,
13111315
required this.nameOffset,
1316+
required this.nameOffset2,
13121317
required this.documentationComment,
13131318
required this.typeParameters,
13141319
required this.parameters,
@@ -1320,6 +1325,7 @@ class _InfoGenericTypeAlias {
13201325
final int codeOffset;
13211326
final int codeLength;
13221327
final int nameOffset;
1328+
final int? nameOffset2;
13231329
final String? documentationComment;
13241330
final List<_InfoTypeParameter> typeParameters;
13251331
final List<_InfoTypeParameter> aliasedTypeParameters;
@@ -1331,6 +1337,7 @@ class _InfoGenericTypeAlias {
13311337
codeOffset: reader.readUInt30(),
13321338
codeLength: reader.readUInt30(),
13331339
nameOffset: reader.readUInt30(),
1340+
nameOffset2: reader.readOptionalUInt30(),
13341341
documentationComment: reader.readStringUtf8().nullIfEmpty,
13351342
typeParameters: reader.readTypedList(
13361343
() => _InfoTypeParameter(reader),
@@ -1349,6 +1356,7 @@ class _InfoGenericTypeAlias {
13491356
required this.codeOffset,
13501357
required this.codeLength,
13511358
required this.nameOffset,
1359+
required this.nameOffset2,
13521360
required this.documentationComment,
13531361
required this.typeParameters,
13541362
required this.aliasedTypeParameters,
@@ -1607,6 +1615,7 @@ class _InformativeDataWriter {
16071615
sink.writeUInt30(node.offset);
16081616
sink.writeUInt30(node.length);
16091617
sink.writeUInt30(node.name.offset);
1618+
sink.writeOptionalUInt30(node.name.offsetIfNotEmpty);
16101619
_writeDocumentationComment(node);
16111620
_writeTypeParameters(node.typeParameters);
16121621
_writeFormalParameters(node.parameters);
@@ -1622,6 +1631,7 @@ class _InformativeDataWriter {
16221631
sink.writeUInt30(node.offset);
16231632
sink.writeUInt30(node.length);
16241633
sink.writeUInt30(node.name.offset);
1634+
sink.writeOptionalUInt30(node.name.offsetIfNotEmpty);
16251635
_writeDocumentationComment(node);
16261636
_writeTypeParameters(node.typeParameters);
16271637
if (aliasedType is GenericFunctionType) {

pkg/analyzer/test/src/dart/parser/type_alias_test.dart

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'package:analyzer/src/dart/error/syntactic_errors.dart';
56
import 'package:test_reflective_loader/test_reflective_loader.dart';
67

78
import '../../diagnostics/parser_diagnostics.dart';
@@ -50,6 +51,40 @@ GenericTypeAlias
5051
equals: =
5152
type: NamedType
5253
name: int
54+
''');
55+
}
56+
57+
test_modern_missingName() {
58+
// Does not look good.
59+
// https://github.com/dart-lang/sdk/issues/56912
60+
var parseResult = parseStringWithErrors(r'''
61+
typedef = int;
62+
''');
63+
parseResult.assertErrors([
64+
error(ParserErrorCode.EXPECTED_TOKEN, 0, 7),
65+
error(ParserErrorCode.MISSING_IDENTIFIER, 8, 1),
66+
error(ParserErrorCode.MISSING_TYPEDEF_PARAMETERS, 8, 1),
67+
error(ParserErrorCode.EXPECTED_EXECUTABLE, 8, 1),
68+
error(ParserErrorCode.MISSING_CONST_FINAL_VAR_OR_TYPE, 10, 3),
69+
]);
70+
71+
var node = parseResult.findNode.unit;
72+
assertParsedNodeText(node, r'''
73+
CompilationUnit
74+
declarations
75+
FunctionTypeAlias
76+
typedefKeyword: typedef
77+
name: <empty> <synthetic>
78+
parameters: FormalParameterList
79+
leftParenthesis: ( <synthetic>
80+
rightParenthesis: ) <synthetic>
81+
semicolon: ; <synthetic>
82+
TopLevelVariableDeclaration
83+
variables: VariableDeclarationList
84+
variables
85+
VariableDeclaration
86+
name: int
87+
semicolon: ;
5388
''');
5489
}
5590
}

pkg/analyzer/test/src/summary/elements/type_alias_test.dart

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6217,6 +6217,89 @@ library
62176217
''');
62186218
}
62196219

6220+
test_typedef_nonFunction_missingName() async {
6221+
var library = await buildLibrary(r'''
6222+
typedef = int;
6223+
''');
6224+
checkElementText(library, r'''
6225+
library
6226+
reference: <testLibrary>
6227+
definingUnit: <testLibraryFragment>
6228+
units
6229+
<testLibraryFragment>
6230+
enclosingElement3: <null>
6231+
typeAliases
6232+
functionTypeAliasBased @8
6233+
reference: <testLibraryFragment>::@typeAlias::0
6234+
aliasedType: dynamic Function()
6235+
aliasedElement: GenericFunctionTypeElement
6236+
returnType: dynamic
6237+
topLevelVariables
6238+
static int @10
6239+
reference: <testLibraryFragment>::@topLevelVariable::int
6240+
enclosingElement3: <testLibraryFragment>
6241+
type: dynamic
6242+
accessors
6243+
synthetic static get int @-1
6244+
reference: <testLibraryFragment>::@getter::int
6245+
enclosingElement3: <testLibraryFragment>
6246+
returnType: dynamic
6247+
synthetic static set int= @-1
6248+
reference: <testLibraryFragment>::@setter::int
6249+
enclosingElement3: <testLibraryFragment>
6250+
parameters
6251+
requiredPositional _int @-1
6252+
type: dynamic
6253+
returnType: void
6254+
----------------------------------------
6255+
library
6256+
reference: <testLibrary>
6257+
fragments
6258+
<testLibraryFragment>
6259+
element: <testLibrary>
6260+
typeAliases
6261+
<null-name>
6262+
reference: <testLibraryFragment>::@typeAlias::0
6263+
element: <testLibraryFragment>::@typeAlias::0#element
6264+
topLevelVariables
6265+
int @10
6266+
reference: <testLibraryFragment>::@topLevelVariable::int
6267+
element: <testLibraryFragment>::@topLevelVariable::int#element
6268+
getter2: <testLibraryFragment>::@getter::int
6269+
setter2: <testLibraryFragment>::@setter::int
6270+
getters
6271+
get <null-name>
6272+
reference: <testLibraryFragment>::@getter::int
6273+
element: <testLibraryFragment>::@getter::int#element
6274+
setters
6275+
set <null-name>
6276+
reference: <testLibraryFragment>::@setter::int
6277+
element: <testLibraryFragment>::@setter::int#element
6278+
formalParameters
6279+
<null-name>
6280+
element: <testLibraryFragment>::@setter::int::@parameter::_int#element
6281+
typeAliases
6282+
6283+
firstFragment: <testLibraryFragment>::@typeAlias::0
6284+
aliasedType: dynamic Function()
6285+
topLevelVariables
6286+
int
6287+
firstFragment: <testLibraryFragment>::@topLevelVariable::int
6288+
type: dynamic
6289+
getter: <testLibraryFragment>::@getter::int#element
6290+
setter: <testLibraryFragment>::@setter::int#element
6291+
getters
6292+
synthetic static get int
6293+
firstFragment: <testLibraryFragment>::@getter::int
6294+
setters
6295+
synthetic static set int=
6296+
firstFragment: <testLibraryFragment>::@setter::int
6297+
formalParameters
6298+
requiredPositional _int
6299+
type: dynamic
6300+
''');
6301+
}
6302+
62206303
test_typedef_nonFunction_using_dynamic() async {
62216304
var library = await buildLibrary(r'''
62226305
typedef A = dynamic;

0 commit comments

Comments
 (0)