Skip to content

Commit facaffc

Browse files
committed
Elements. Stop using ElementLocation for Element.hashCode, use identityHashCode().
And because we don't use `location` often anymore, stop caching it. Change-Id: I3470803e09499fc49e57351bfe27a0993b223dae Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/401941 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Phil Quitslund <[email protected]>
1 parent b79f187 commit facaffc

File tree

4 files changed

+9
-45
lines changed

4 files changed

+9
-45
lines changed

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

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2528,12 +2528,6 @@ abstract class ElementImpl implements Element, Element2 {
25282528
/// Cached flags denoting presence of specific annotations in [_metadata].
25292529
int _metadataFlags = 0;
25302530

2531-
/// A cached copy of the calculated hashCode for this element.
2532-
int? _cachedHashCode;
2533-
2534-
/// A cached copy of the calculated location for this element.
2535-
ElementLocation? _cachedLocation;
2536-
25372531
/// The documentation comment for this element.
25382532
String? _docComment;
25392533

@@ -2666,13 +2660,6 @@ abstract class ElementImpl implements Element, Element2 {
26662660
return false;
26672661
}
26682662

2669-
@override
2670-
int get hashCode {
2671-
// TODO(scheglov): We might want to re-visit this optimization in the future.
2672-
// We cache the hash code value as this is a very frequently called method.
2673-
return _cachedHashCode ??= location.hashCode;
2674-
}
2675-
26762663
@override
26772664
bool get hasImmutable {
26782665
var metadata = this.metadata;
@@ -2992,7 +2979,7 @@ abstract class ElementImpl implements Element, Element2 {
29922979

29932980
@override
29942981
ElementLocation get location {
2995-
return _cachedLocation ??= ElementLocationImpl.con1(this);
2982+
return ElementLocationImpl.con1(this);
29962983
}
29972984

29982985
@override
@@ -3311,11 +3298,6 @@ abstract class ElementImpl implements Element, Element2 {
33113298
}
33123299

33133300
abstract class ElementImpl2 implements Element2 {
3314-
/// A cached copy of the calculated `hashCode` for this element.
3315-
int? _cachedHashCode;
3316-
3317-
ElementLocation? _cachedLocation;
3318-
33193301
@override
33203302
final int id = ElementImpl._NEXT_ID++;
33213303

@@ -3332,13 +3314,6 @@ abstract class ElementImpl2 implements Element2 {
33323314
// TODO(augmentations): implement enclosingElement2
33333315
Element2? get enclosingElement2 => throw UnimplementedError();
33343316

3335-
@override
3336-
int get hashCode {
3337-
// TODO(scheglov): We might want to re-visit this optimization in the future.
3338-
// We cache the hash code as this is a very frequently called method.
3339-
return _cachedHashCode ??= location.hashCode;
3340-
}
3341-
33423317
/// Return an identifier that uniquely identifies this element among the
33433318
/// children of this element's parent.
33443319
String get identifier {
@@ -3364,7 +3339,7 @@ abstract class ElementImpl2 implements Element2 {
33643339

33653340
@override
33663341
ElementLocation? get location {
3367-
return _cachedLocation ??= ElementLocationImpl.fromElement(this);
3342+
return ElementLocationImpl.fromElement(this);
33683343
}
33693344

33703345
@override
@@ -6546,9 +6521,6 @@ class JoinPatternVariableElementImpl extends PatternVariableElementImpl
65466521
JoinPatternVariableElementImpl2 get element =>
65476522
super.element as JoinPatternVariableElementImpl2;
65486523

6549-
@override
6550-
int get hashCode => identityHashCode(this);
6551-
65526524
@override
65536525
bool get isConsistent {
65546526
return inconsistency == shared.JoinedPatternVariableInconsistency.none;
@@ -7273,9 +7245,6 @@ class LibraryExportElementImpl extends _ExistingElementImpl
72737245
@override
72747246
LibraryElement2? get exportedLibrary2 => exportedLibrary;
72757247

7276-
@override
7277-
int get hashCode => identityHashCode(this);
7278-
72797248
@override
72807249
String get identifier => 'export@$nameOffset';
72817250

@@ -7330,9 +7299,6 @@ class LibraryImportElementImpl extends _ExistingElementImpl
73307299
return super.enclosingElement3 as CompilationUnitElementImpl;
73317300
}
73327301

7333-
@override
7334-
int get hashCode => identityHashCode(this);
7335-
73367302
@override
73377303
String get identifier => 'import@$nameOffset';
73387304

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -370,16 +370,15 @@ class FunctionTypeImpl extends TypeImpl implements FunctionType {
370370
for (var namedParameter in sortedNamedParameters) {
371371
namedParameterInfo.add(namedParameter.isRequired);
372372
namedParameterInfo.add(namedParameter.name);
373-
namedParameterInfo.add(namedParameter.type);
374373
}
375374
}
376375

377376
return Object.hash(
378-
nullabilitySuffix,
379-
returnType,
380-
requiredPositionalParameterCount,
381-
Object.hashAll(positionalParameterTypes),
382-
namedParameterInfo);
377+
nullabilitySuffix,
378+
returnType,
379+
requiredPositionalParameterCount,
380+
namedParameterInfo,
381+
);
383382
}
384383

385384
/// Given two functions [f1] and [f2] where f1 and f2 are known to be

pkg/analyzer/test/src/dart/element/element_test.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ class ClassElementImplTest extends AbstractTypeSystemTest {
104104
expect(classA.hasNonFinalField, isTrue);
105105
}
106106

107-
@FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/44522')
108107
void test_hasNonFinalField_true_inherited() {
109108
var classA = class_(name: 'A');
110109
ClassElementImpl classB =

pkg/analyzer/test/src/dart/element/function_type_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ class FunctionTypeTest extends AbstractTypeSystemTest {
303303
}
304304

305305
test_hash_optionalPositionalParameterType() {
306-
_testHashesSometimesDiffer((i) => FunctionTypeImpl(
306+
_testHashesAlwaysEqual((i) => FunctionTypeImpl(
307307
typeFormals: const [],
308308
parameters: [
309309
positionalParameter(name: 'x', type: class_(name: 'C$i').thisType)
@@ -364,7 +364,7 @@ class FunctionTypeTest extends AbstractTypeSystemTest {
364364
}
365365

366366
test_hash_requiredPositionalParameterType() {
367-
_testHashesSometimesDiffer((i) => FunctionTypeImpl(
367+
_testHashesAlwaysEqual((i) => FunctionTypeImpl(
368368
typeFormals: const [],
369369
parameters: [
370370
requiredParameter(name: 'x', type: class_(name: 'C$i').thisType)

0 commit comments

Comments
 (0)