Skip to content

Commit

Permalink
Elements. Tests for missing name for top-level and class method/gette…
Browse files Browse the repository at this point in the history
…r/setter.

Change-Id: Id94e73d5dbac817ee59e278fc980a792d612e6a2
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/390628
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Phil Quitslund <pquitslund@google.com>
  • Loading branch information
scheglov authored and Commit Queue committed Oct 18, 2024
1 parent 8f40f77 commit 1a6ae2e
Show file tree
Hide file tree
Showing 6 changed files with 295 additions and 9 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 = 399;
static const int DATA_VERSION = 400;

/// 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
3 changes: 2 additions & 1 deletion pkg/analyzer/lib/src/summary2/bundle_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1373,7 +1373,8 @@ class LibraryReader {
var resolutionOffset = _baseResolutionOffset + _reader.readUInt30();
var reference = _readReference();
var fragmentName = _readFragmentName();
var name = reference.elementName;
// TODO(scheglov): we do this only because MethodElement2 uses this name.
var name = _reader.readStringReference();
var element = MethodElementImpl(name, -1);
element.name2 = fragmentName;
var linkedData = MethodElementLinkedData(
Expand Down
1 change: 1 addition & 0 deletions pkg/analyzer/lib/src/summary2/bundle_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ class BundleWriter {
_sink.writeUInt30(_resolutionSink.offset);
_writeReference(element);
_writeFragmentName(element.name2);
_sink._writeStringReference(element.name);
MethodElementFlags.write(_sink, element);

_resolutionSink._writeAnnotationList(element.metadata);
Expand Down
26 changes: 19 additions & 7 deletions pkg/analyzer/lib/src/summary2/element_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,8 @@ class ElementBuilder extends ThrowingAstVisitor<void> {
reference = _enclosingContext.addGetter(name, element);
executableElement = element;

_buildSyntheticVariable(name: name, accessorElement: element);
var refName = fragmentName?.name ?? '${_nextUnnamedId++}';
_buildSyntheticVariable(name: refName, accessorElement: element);
} else if (node.isSetter) {
var element = PropertyAccessorElementImpl(name, nameOffset);
element.name2 = fragmentName;
Expand All @@ -1030,16 +1031,20 @@ class ElementBuilder extends ThrowingAstVisitor<void> {
element.isSetter = true;
element.isStatic = node.isStatic;

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

_buildSyntheticVariable(name: name, accessorElement: element);
} else {
if (name == '-') {
var isUnaryMinus = false;
if (fragmentName?.name == '-') {
var parameters = node.parameters;
if (parameters != null && parameters.parameters.isEmpty) {
name = 'unary-';
}
isUnaryMinus = parameters != null && parameters.parameters.isEmpty;
}

if (isUnaryMinus) {
name = 'unary-';
}

var element = MethodElementImpl(name, nameOffset);
Expand All @@ -1048,7 +1053,14 @@ class ElementBuilder extends ThrowingAstVisitor<void> {
element.isAugmentation = node.augmentKeyword != null;
element.isStatic = node.isStatic;

reference = _enclosingContext.addMethod(name, element);
String refName;
if (isUnaryMinus) {
refName = 'unary-';
} else {
refName = fragmentName?.name ?? '${_nextUnnamedId++}';
}

reference = _enclosingContext.addMethod(refName, element);
executableElement = element;
}
executableElement.hasImplicitReturnType = node.returnType == null;
Expand Down
177 changes: 177 additions & 0 deletions pkg/analyzer/test/src/summary/elements/class_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16829,6 +16829,62 @@ library
''');
}

test_class_getter_missingName() async {
var library = await buildLibrary('''
class A {
get () => 0;
}
''');
checkElementText(library, r'''
library
reference: <testLibrary>
definingUnit: <testLibraryFragment>
units
<testLibraryFragment>
enclosingElement3: <null>
classes
class A @6
reference: <testLibraryFragment>::@class::A
enclosingElement3: <testLibraryFragment>
constructors
synthetic @-1
reference: <testLibraryFragment>::@class::A::@constructor::new
enclosingElement3: <testLibraryFragment>::@class::A
methods
get @12
reference: <testLibraryFragment>::@class::A::@method::get
enclosingElement3: <testLibraryFragment>::@class::A
returnType: dynamic
----------------------------------------
library
reference: <testLibrary>
fragments
<testLibraryFragment>
element: <testLibrary>
classes
class A @6
reference: <testLibraryFragment>::@class::A
element: <testLibraryFragment>::@class::A#element
constructors
synthetic <null-name>
reference: <testLibraryFragment>::@class::A::@constructor::new
element: <testLibraryFragment>::@class::A::@constructor::new#element
methods
get @12
reference: <testLibraryFragment>::@class::A::@method::get
element: <testLibraryFragment>::@class::A::@method::get#element
classes
class A
firstFragment: <testLibraryFragment>::@class::A
constructors
synthetic new
firstFragment: <testLibraryFragment>::@class::A::@constructor::new
methods
get
firstFragment: <testLibraryFragment>::@class::A::@method::get
''');
}

test_class_getter_native() async {
var library = await buildLibrary('''
class C {
Expand Down Expand Up @@ -18209,6 +18265,62 @@ library
''');
}

test_class_method_missingName() async {
var library = await buildLibrary('''
class A {
() {}
}
''');
checkElementText(library, r'''
library
reference: <testLibrary>
definingUnit: <testLibraryFragment>
units
<testLibraryFragment>
enclosingElement3: <null>
classes
class A @6
reference: <testLibraryFragment>::@class::A
enclosingElement3: <testLibraryFragment>
constructors
synthetic @-1
reference: <testLibraryFragment>::@class::A::@constructor::new
enclosingElement3: <testLibraryFragment>::@class::A
methods
@12
reference: <testLibraryFragment>::@class::A::@method::0
enclosingElement3: <testLibraryFragment>::@class::A
returnType: dynamic
----------------------------------------
library
reference: <testLibrary>
fragments
<testLibraryFragment>
element: <testLibrary>
classes
class A @6
reference: <testLibraryFragment>::@class::A
element: <testLibraryFragment>::@class::A#element
constructors
synthetic <null-name>
reference: <testLibraryFragment>::@class::A::@constructor::new
element: <testLibraryFragment>::@class::A::@constructor::new#element
methods
<null-name>
reference: <testLibraryFragment>::@class::A::@method::0
element: <testLibraryFragment>::@class::A::@method::0#element
classes
class A
firstFragment: <testLibraryFragment>::@class::A
constructors
synthetic new
firstFragment: <testLibraryFragment>::@class::A::@constructor::new
methods

firstFragment: <testLibraryFragment>::@class::A::@method::0
''');
}

test_class_method_namedAsSupertype() async {
var library = await buildLibrary(r'''
class A {}
Expand Down Expand Up @@ -23122,6 +23234,71 @@ library
''');
}

test_class_setter_missingName() async {
var library = await buildLibrary('''
class A {
set (int _) {}
}
''');
checkElementText(library, r'''
library
reference: <testLibrary>
definingUnit: <testLibraryFragment>
units
<testLibraryFragment>
enclosingElement3: <null>
classes
class A @6
reference: <testLibraryFragment>::@class::A
enclosingElement3: <testLibraryFragment>
constructors
synthetic @-1
reference: <testLibraryFragment>::@class::A::@constructor::new
enclosingElement3: <testLibraryFragment>::@class::A
methods
set @12
reference: <testLibraryFragment>::@class::A::@method::set
enclosingElement3: <testLibraryFragment>::@class::A
parameters
requiredPositional _ @21
type: int
returnType: dynamic
----------------------------------------
library
reference: <testLibrary>
fragments
<testLibraryFragment>
element: <testLibrary>
classes
class A @6
reference: <testLibraryFragment>::@class::A
element: <testLibraryFragment>::@class::A#element
constructors
synthetic <null-name>
reference: <testLibraryFragment>::@class::A::@constructor::new
element: <testLibraryFragment>::@class::A::@constructor::new#element
methods
set @12
reference: <testLibraryFragment>::@class::A::@method::set
element: <testLibraryFragment>::@class::A::@method::set#element
formalParameters
_ @21
element: <testLibraryFragment>::@class::A::@method::set::@parameter::_#element
classes
class A
firstFragment: <testLibraryFragment>::@class::A
constructors
synthetic new
firstFragment: <testLibraryFragment>::@class::A::@constructor::new
methods
set
firstFragment: <testLibraryFragment>::@class::A::@method::set
formalParameters
requiredPositional _
type: int
''');
}

test_class_setter_native() async {
var library = await buildLibrary('''
class C {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,26 @@ f() => 0;
expect(f.hasImplicitReturnType, isTrue);
}

test_function_missingName() async {
var library = await buildLibrary('''
() {}
''');
checkElementText(library, r'''
library
reference: <testLibrary>
definingUnit: <testLibraryFragment>
units
<testLibraryFragment>
enclosingElement3: <null>
----------------------------------------
library
reference: <testLibrary>
fragments
<testLibraryFragment>
element: <testLibrary>
''');
}

test_function_parameter_const() async {
var library = await buildLibrary('''
void f(const x) {}
Expand Down Expand Up @@ -1338,6 +1358,39 @@ library
''');
}

test_getter_missingName() async {
var library = await buildLibrary('''
get () => 0;
''');
checkElementText(library, r'''
library
reference: <testLibrary>
definingUnit: <testLibraryFragment>
units
<testLibraryFragment>
enclosingElement3: <null>
functions
get @0
reference: <testLibraryFragment>::@function::get
enclosingElement3: <testLibraryFragment>
returnType: dynamic
----------------------------------------
library
reference: <testLibrary>
fragments
<testLibraryFragment>
element: <testLibrary>
functions
get @0
reference: <testLibraryFragment>::@function::get
element: <testLibraryFragment>::@function::get#element
functions
get
firstFragment: <testLibraryFragment>::@function::get
returnType: dynamic
''');
}

test_main_class() async {
var library = await buildLibrary('class main {}');
checkElementText(library, r'''
Expand Down Expand Up @@ -1747,6 +1800,48 @@ library
fragments
<testLibraryFragment>
element: <testLibrary>
''');
}

test_setter_missingName() async {
var library = await buildLibrary('''
set (int _) {}
''');
checkElementText(library, r'''
library
reference: <testLibrary>
definingUnit: <testLibraryFragment>
units
<testLibraryFragment>
enclosingElement3: <null>
functions
set @0
reference: <testLibraryFragment>::@function::set
enclosingElement3: <testLibraryFragment>
parameters
requiredPositional _ @9
type: int
returnType: dynamic
----------------------------------------
library
reference: <testLibrary>
fragments
<testLibraryFragment>
element: <testLibrary>
functions
set @0
reference: <testLibraryFragment>::@function::set
element: <testLibraryFragment>::@function::set#element
formalParameters
_ @9
element: <testLibraryFragment>::@function::set::@parameter::_#element
functions
set
firstFragment: <testLibraryFragment>::@function::set
formalParameters
requiredPositional _
type: int
returnType: dynamic
''');
}
}
Expand Down

0 comments on commit 1a6ae2e

Please sign in to comment.