Skip to content

Commit

Permalink
Elements. Use identical() to compare ElementImpl(s).
Browse files Browse the repository at this point in the history
Change-Id: Idbe935bc6ae333d9940d8ea53ad44011eff237ab
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/401384
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
  • Loading branch information
scheglov authored and Commit Queue committed Dec 18, 2024
1 parent da70ebe commit 6494efb
Show file tree
Hide file tree
Showing 16 changed files with 86 additions and 17 deletions.
4 changes: 4 additions & 0 deletions pkg/analysis_server/test/abstract_context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ class AbstractContextTest
return result as ResolvedUnitResult;
}

void makeFilePriority(File file) {
driverFor(file).priorityFiles2 = [file];
}

@override
File newFile(String path, String content) {
if (_analysisContextCollection != null && !path.endsWith('.dart')) {
Expand Down
8 changes: 8 additions & 0 deletions pkg/analysis_server/test/analysis_server_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ abstract class ContextResolutionTest with ResourceProviderMixin {
);
}

Future<void> setPriorityFiles2(List<File> files) async {
await handleSuccessfulRequest(
AnalysisSetPriorityFilesParams(
files.map((e) => e.path).toList(),
).toRequest('0', clientUriConverter: server.uriConverter),
);
}

Future<void> setRoots({
required List<String> included,
required List<String> excluded,
Expand Down
6 changes: 6 additions & 0 deletions pkg/analysis_server/test/edit/refactoring_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1232,6 +1232,7 @@ void f() {
}
''');
// Start refactoring.
await setPriorityFiles2([testFile]);
var result = await getRefactoringResult(() {
return _sendInlineRequest('res =');
});
Expand All @@ -1241,6 +1242,7 @@ void f() {
}

Future<void> test_feedback() {
setPriorityFiles([testFile]);
addTestFile('''
void f() {
int test = 42;
Expand Down Expand Up @@ -1272,6 +1274,7 @@ void f() {
}

Future<void> test_OK() {
setPriorityFiles([testFile]);
addTestFile('''
void f() {
int test = 42;
Expand Down Expand Up @@ -2547,6 +2550,7 @@ void f() {
}

Future<void> test_feedback() {
setPriorityFiles([testFile]);
addTestFile('''
class Test {}
void f() {
Expand Down Expand Up @@ -2770,6 +2774,7 @@ part of my.new_name;
}

Future<void> test_localVariable() {
setPriorityFiles([testFile]);
addTestFile('''
void f() {
int test = 0;
Expand Down Expand Up @@ -2881,6 +2886,7 @@ void f(Object? x) {
}

Future<void> test_patternVariable_patternAssignment() {
setPriorityFiles([testFile]);
addTestFile('''
void f(Object? x) {
int test;
Expand Down
2 changes: 2 additions & 0 deletions pkg/analysis_server/test/lsp/code_actions_abstract.dart
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ abstract class AbstractCodeActionsTest extends AbstractLspAnalysisServerTest {
String? command,
String? title,
ProgressToken? commandWorkDoneToken,
bool openTargetFile = false,
}) async {
filePath ??= mainFilePath;

Expand All @@ -200,6 +201,7 @@ $expected''';
kind: kind,
command: command,
title: title,
openTargetFile: openTargetFile,
);

// Verify the edits either by executing the command we expected, or
Expand Down
2 changes: 2 additions & 0 deletions pkg/analysis_server/test/lsp/code_actions_refactor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,7 @@ class NewWidget extends StatelessWidget {
expectedContent,
command: Commands.performRefactor,
title: extractWidgetTitle,
openTargetFile: true,
);
}

Expand Down Expand Up @@ -927,6 +928,7 @@ void f() {
expectedContent,
command: Commands.performRefactor,
title: inlineVariableTitle,
openTargetFile: true,
);
}
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/analysis_server/test/search/element_references_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,7 @@ void f(A a, B b, C c) {
}

Future<void> test_label() async {
await setPriorityFiles2([testFile]);
addTestFile('''
void f() {
myLabel:
Expand All @@ -901,6 +902,7 @@ myLabel:
}

Future<void> test_localVariable() async {
await setPriorityFiles2([testFile]);
addTestFile('''
void f() {
var vvv = 1;
Expand Down Expand Up @@ -1300,6 +1302,7 @@ void f(({int foo}) x, num ^a) {
}

Future<void> test_pattern_relational_variable() async {
await setPriorityFiles2([testFile]);
await assertReferences(
kind: ElementKind.LOCAL_VARIABLE,
resultKinds: {0: SearchResultKind.READ},
Expand Down Expand Up @@ -1562,6 +1565,7 @@ class A<T> {
}

Future<void> test_variable_forEachElement_block() async {
await setPriorityFiles2([testFile]);
addTestFile('''
void f(List<int> values) {
{
Expand All @@ -1575,6 +1579,7 @@ void f(List<int> values) {
}

Future<void> test_variable_forEachElement_expressionBody() async {
await setPriorityFiles2([testFile]);
addTestFile('''
Object f() => [for (final value in []) value * 2];
''');
Expand All @@ -1584,6 +1589,7 @@ Object f() => [for (final value in []) value * 2];
}

Future<void> test_variable_forEachElement_functionBody() async {
await setPriorityFiles2([testFile]);
addTestFile('''
void f(List<int> values) {
[for (final value in values) value * 2];
Expand All @@ -1595,6 +1601,7 @@ void f(List<int> values) {
}

Future<void> test_variable_forEachElement_topLevelVariable() async {
await setPriorityFiles2([testFile]);
addTestFile('''
final a = [for (final value in []) value * 2];
''');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ abstract class RefactoringTest extends AbstractSingleUnitTest {
}

Future<void> indexTestUnit(String code) async {
// Make it priority, so the resolved unit stays in memory.
// So, when we get a local element, and search for it, we use this unit.
// This is important when local elements equality is identity.
makeFilePriority(testFile);

await resolveTestCode(code);
}

Expand Down
7 changes: 1 addition & 6 deletions pkg/analyzer/lib/src/dart/element/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3074,12 +3074,7 @@ abstract class ElementImpl implements Element, Element2 {

@override
bool operator ==(Object other) {
if (identical(this, other)) {
return true;
}
return other is ElementImpl &&
other.kind == kind &&
other.location == location;
return identical(this, other);
}

@override
Expand Down
32 changes: 22 additions & 10 deletions pkg/analyzer/test/src/dart/analysis/search_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1487,6 +1487,7 @@ main() {
}

test_searchReferences_FunctionElement_local() async {
makeFilePriority(testFile);
await resolveTestCode('''
main() {
test() {}
Expand Down Expand Up @@ -1602,6 +1603,7 @@ main() {
}

test_searchReferences_LabelElement() async {
makeFilePriority(testFile);
await resolveTestCode('''
main() {
label:
Expand Down Expand Up @@ -1693,6 +1695,7 @@ part 'unitB.dart';
}

test_searchReferences_LocalVariableElement() async {
makeFilePriority(testFile);
await resolveTestCode(r'''
main() {
var v;
Expand All @@ -1713,6 +1716,7 @@ main() {
}

test_searchReferences_LocalVariableElement_inForEachElement_expressionBody() async {
makeFilePriority(testFile);
await resolveTestCode('''
Object f() => [
for (var v in []) v,
Expand All @@ -1726,6 +1730,7 @@ Object f() => [
}

test_searchReferences_LocalVariableElement_inForEachElement_inBlock() async {
makeFilePriority(testFile);
await resolveTestCode('''
Object f() {
{
Expand All @@ -1743,6 +1748,7 @@ Object f() {
}

test_searchReferences_LocalVariableElement_inForEachElement_inFunctionBody() async {
makeFilePriority(testFile);
await resolveTestCode('''
Object f() {
return [
Expand All @@ -1758,6 +1764,7 @@ Object f() {
}

test_searchReferences_LocalVariableElement_inForEachElement_topLevel() async {
makeFilePriority(testFile);
await resolveTestCode('''
var x = [
for (var v in []) v,
Expand All @@ -1771,6 +1778,7 @@ var x = [
}

test_searchReferences_LocalVariableElement_inForEachLoop() async {
makeFilePriority(testFile);
await resolveTestCode('''
main() {
for (var v in []) {
Expand All @@ -1793,16 +1801,7 @@ main() {

test_searchReferences_LocalVariableElement_inPackage() async {
var aaaPackageRootPath = '$packagesRootPath/aaa';
var testPath = convertPath('$aaaPackageRootPath/lib/a.dart');

writeTestPackageConfig(
PackageConfigFileBuilder()
..add(name: 'aaa', rootPath: aaaPackageRootPath),
);

fileForContextSelection = testFile;

await resolveFileCode(testPath, '''
var a_file = newFile('$aaaPackageRootPath/lib/a.dart', '''
main() {
var v;
v = 1;
Expand All @@ -1811,6 +1810,17 @@ main() {
v();
}
''');

writeTestPackageConfig(
PackageConfigFileBuilder()
..add(name: 'aaa', rootPath: aaaPackageRootPath),
);

fileForContextSelection = testFile;

driverFor(testFile).priorityFiles2 = [a_file];
await resolveFile2(a_file);

var element = findElement2.localVar('v');
await assertElementReferencesText(element, r'''
package:aaa/a.dart::<fragment>::@function::main
Expand Down Expand Up @@ -2191,6 +2201,7 @@ main() {
}

test_searchReferences_ParameterElement_requiredPositional_ofLocalFunction() async {
makeFilePriority(testFile);
await resolveTestCode('''
main() {
foo(p) {
Expand Down Expand Up @@ -2753,6 +2764,7 @@ void f(Object? x) {
}

test_searchReferences_VariablePatternElement_patternAssignment() async {
makeFilePriority(testFile);
await resolveTestCode('''
void f() {
int v;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,10 @@ abstract class ContextResolutionTest
return libraryResult.element2;
}

void makeFilePriority(File file) {
driverFor(file).priorityFiles2 = [file];
}

@override
File newFile(String path, String content) {
if (_analysisContextCollection != null && !path.endsWith('.dart')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ enum E {
}
''', [
error(CompileTimeErrorCode.DUPLICATE_CONSTRUCTOR_DEFAULT, 35, 5),
error(WarningCode.UNUSED_ELEMENT, 37, 3),
]);
}

Expand All @@ -154,6 +155,7 @@ enum E {
}
''', [
error(CompileTimeErrorCode.DUPLICATE_CONSTRUCTOR_DEFAULT, 39, 5),
error(WarningCode.UNUSED_ELEMENT, 41, 3),
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ enum E {
}
''', [
error(CompileTimeErrorCode.DUPLICATE_CONSTRUCTOR_NAME, 45, 5),
error(WarningCode.UNUSED_ELEMENT, 47, 3),
]);
}

Expand Down
10 changes: 10 additions & 0 deletions pkg/analyzer/test/src/diagnostics/duplicate_definition_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2733,6 +2733,11 @@ class B extends A {
}
''', [
error(WarningCode.UNUSED_FIELD, 17, 1),
error(
CompileTimeErrorCode
.SUPER_FORMAL_PARAMETER_WITHOUT_ASSOCIATED_POSITIONAL,
74,
1),
]);
}

Expand All @@ -2752,6 +2757,11 @@ class B extends A {
error(WarningCode.UNUSED_FIELD, 61, 1),
error(CompileTimeErrorCode.DUPLICATE_DEFINITION, 118, 1,
contextMessages: [message(testFile, 109, 1)]),
error(
CompileTimeErrorCode
.SUPER_FORMAL_PARAMETER_WITHOUT_ASSOCIATED_POSITIONAL,
118,
1),
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// BSD-style license that can be found in the LICENSE file.

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

import '../dart/resolution/context_collection_resolution.dart';
Expand Down Expand Up @@ -72,6 +73,7 @@ enum E {
}
''', [
error(ParserErrorCode.INVALID_CONSTRUCTOR_NAME, 40, 1),
error(WarningCode.UNUSED_ELEMENT, 59, 3),
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,8 @@ library
reference: <testLibrary>::@class::X::@def::0
firstFragment: <testLibraryFragment>::@class::X::@def::0
supertype: A
mixins
M
constructors
synthetic new
firstFragment: <testLibraryFragment>::@class::X::@def::0::@constructor::new
Expand Down
Loading

0 comments on commit 6494efb

Please sign in to comment.