Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(85)

Unified Diff: pkg/analyzer/lib/src/dart/element/element.dart

Issue 1696193003: Fix cache corruption in incremental resolver (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/analyzer/lib/src/dart/element/element.dart
diff --git a/pkg/analyzer/lib/src/dart/element/element.dart b/pkg/analyzer/lib/src/dart/element/element.dart
index 81f557c958b60ae39e2494037254bf5997056e70..f4f7692188960f16443cd7e19304872dbeabb732 100644
--- a/pkg/analyzer/lib/src/dart/element/element.dart
+++ b/pkg/analyzer/lib/src/dart/element/element.dart
@@ -1792,8 +1792,7 @@ abstract class ElementImpl implements Element {
*/
void set enclosingElement(Element element) {
_enclosingElement = element as ElementImpl;
- _cachedLocation = null;
- _cachedHashCode = null;
+ _updateCaches();
}
@override
@@ -1867,8 +1866,7 @@ abstract class ElementImpl implements Element {
void set name(String name) {
this._name = name;
- _cachedLocation = null;
- _cachedHashCode = null;
+ _updateCaches();
}
@override
@@ -1883,8 +1881,7 @@ abstract class ElementImpl implements Element {
*/
void set nameOffset(int offset) {
_nameOffset = offset;
- _cachedHashCode = null;
- _cachedLocation = null;
+ _updateCaches();
}
@override
@@ -2045,6 +2042,33 @@ abstract class ElementImpl implements Element {
void visitChildren(ElementVisitor visitor) {
// There are no children to visit
}
+
+ /**
+ * Updates cached values after an input changed.
+ */
+ void _updateCaches() {
+ if (!hasModifier(Modifier.CACHE_KEY)) {
+ // Fast path.
+ _cachedLocation = null;
+ _cachedHashCode = null;
+ return;
+ }
+
+ // Save originals.
+ ElementLocation oldLocation = _cachedLocation;
+ int oldHashCode = _cachedHashCode;
+
+ _cachedLocation = null;
+ _cachedHashCode = null;
+
+ if (oldHashCode != hashCode) {
+ // Prevent cache corruption by restoring originals.
+ _cachedLocation = oldLocation;
+ _cachedHashCode = oldHashCode;
+ throw new StateError(
+ "can't update hashCode for a cache key: $this ($runtimeType)");
+ }
+ }
}
/**
@@ -3815,7 +3839,12 @@ class Modifier extends Enum<Modifier> {
*/
static const Modifier SYNTHETIC = const Modifier('SYNTHETIC', 18);
- static const List<Modifier> values = const [
+ /**
+ * Indicates that this element is being used as an analyzer cache key.
+ */
+ static const Modifier CACHE_KEY = const Modifier('CACHE_KEY', 19);
+
+ static const List<Modifier> persistedValues = const [
ABSTRACT,
ASYNCHRONOUS,
CONST,
@@ -3837,6 +3866,11 @@ class Modifier extends Enum<Modifier> {
SYNTHETIC
];
+ static const List<Modifier> transientValues = const [CACHE_KEY];
+
+ static final values = new List.unmodifiable(
+ []..addAll(persistedValues)..addAll(transientValues));
+
const Modifier(String name, int ordinal) : super(name, ordinal);
}

Powered by Google App Engine
This is Rietveld 408576698