bazel syntax: use identity equivalence for ASTNodes
Previously, all nodes except Identifer had no hashCode/equals,
and Identifiers were considered equivalent if they had the same name,
which is clearly wrong.
Now, two syntax nodes are equal iff they are the same node.
Also: remove stale test of old equivalence relation.
PiperOrigin-RevId: 257471668
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/ASTNode.java b/src/main/java/com/google/devtools/build/lib/syntax/ASTNode.java
index 790d280..af74177 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/ASTNode.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/ASTNode.java
@@ -20,15 +20,7 @@
import java.io.Serializable;
import java.util.List;
-/**
- * Root class for nodes in the Abstract Syntax Tree of the Build language.
- *
- * The standard {@link Object#equals} and {@link Object#hashCode} methods are not supported. This is
- * because their implementation would require traversing the entire tree in the worst case, and we
- * don't want this kind of cost to occur implicitly. An incomplete way to compare for equality is to
- * test whether two ASTs have the same string representation under {@link #prettyPrint()}. This
- * might miss some metadata, but it's useful in test assertions.
- */
+/** An ASTNode is a node in a Starlark syntax tree. */
public abstract class ASTNode implements Serializable {
private Location location;
@@ -154,16 +146,6 @@
return prettyPrint();
}
- @Override
- public int hashCode() {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public boolean equals(Object that) {
- throw new UnsupportedOperationException();
- }
-
/**
* Implements the double dispatch by calling into the node specific
* <code>visit</code> method of the {@link SyntaxTreeVisitor}
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/Identifier.java b/src/main/java/com/google/devtools/build/lib/syntax/Identifier.java
index 6cf89a7..d30bd16 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/Identifier.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/Identifier.java
@@ -57,22 +57,6 @@
buffer.append(name);
}
- @Override
- public boolean equals(@Nullable Object object) {
- // TODO(laurentlb): Remove this. AST nodes should probably not be comparable.
- if (object instanceof Identifier) {
- Identifier that = (Identifier) object;
- return this.name.equals(that.name);
- }
- return false;
- }
-
- @Override
- public int hashCode() {
- // TODO(laurentlb): Remove this.
- return name.hashCode();
- }
-
void setScope(ValidationEnvironment.Scope scope) {
Preconditions.checkState(this.scope == null);
this.scope = scope;
diff --git a/src/test/java/com/google/devtools/build/lib/syntax/ASTNodeTest.java b/src/test/java/com/google/devtools/build/lib/syntax/ASTNodeTest.java
deleted file mode 100644
index c380309..0000000
--- a/src/test/java/com/google/devtools/build/lib/syntax/ASTNodeTest.java
+++ /dev/null
@@ -1,57 +0,0 @@
-// Copyright 2015 The Bazel Authors. All rights reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-package com.google.devtools.build.lib.syntax;
-
-import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
-
-import java.io.IOException;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.JUnit4;
-
-/**
- * Tests {@link ASTNode}.
- */
-@RunWith(JUnit4.class)
-public class ASTNodeTest {
-
- private ASTNode node;
-
- @Before
- public final void createNode() throws Exception {
- node = new ASTNode() {
- @Override
- public void prettyPrint(Appendable buffer, int indentLevel) throws IOException {}
- @Override
- public String toString() {
- return null;
- }
- @Override
- public void accept(SyntaxTreeVisitor visitor) {
- }
- };
- }
-
- @Test
- public void testHashCodeNotSupported() {
- assertThrows(UnsupportedOperationException.class, () -> node.hashCode());
- }
-
- @Test
- public void testEqualsNotSupported() {
- assertThrows(UnsupportedOperationException.class, () -> node.equals(null));
- }
-
-}