From d73a7ee3aaac9106cfc8d5ff3ae1a7c9847a7804 Mon Sep 17 00:00:00 2001 From: Scott Guest Date: Mon, 4 Dec 2023 05:27:12 -0500 Subject: [PATCH] Remove `EAsSet[E]` and `WAsSet[W]` traits in `Transformers.scala` to address IntelliJ error (#3840) This PR removes the `EAsSet[E]` and `WAsSet[W]` traits in `Transformers.scala`, and instead just duplicates a few lines of code between `SetsGeneralTransformer` and `SetsTransformerWithErrors`. As a result, we fix an annoying issue where IntelliJ incorrectly reports an error on every inheritor of these classes, falsely claiming that `errorUnit()` has an incorrect return type. Given that the cost here is low, and similar issues have bitten us in the past by blocking use of the debugger (#3816), I think this is a worthwhile change. If no one is opposed, I plan to address any similar incorrectly-reported IntelliJ errors as I encounter them. --- .../org/kframework/parser/Transformer.scala | 60 ++++++++++--------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/kore/src/main/scala/org/kframework/parser/Transformer.scala b/kore/src/main/scala/org/kframework/parser/Transformer.scala index ab770b24345..bf1305b6a75 100644 --- a/kore/src/main/scala/org/kframework/parser/Transformer.scala +++ b/kore/src/main/scala/org/kframework/parser/Transformer.scala @@ -2,11 +2,11 @@ package org.kframework.parser -import java.util - -import collection.JavaConverters._ import org.kframework.Collections._ +import java.util +import scala.collection.JavaConverters._ + class Ignore @@ -89,8 +89,6 @@ abstract class ChildrenMapping[E, W] { * @tparam W container for warnings. */ abstract class GeneralTransformer[E, W] extends ChildrenMapping[E, W] { - - import collection.mutable // we expect this data structures to represent a DAG, so we // use a cache to remember nodes that we already visited. val cache = new util.IdentityHashMap[Term, (Either[E, Term], W)]() @@ -99,7 +97,7 @@ abstract class GeneralTransformer[E, W] extends ChildrenMapping[E, W] { def apply(t: Term): (Either[E, Term], W) = { if (cache.containsKey(t)) { - return cache.get(t); + return cache.get(t) } val res = t match { @@ -120,34 +118,31 @@ abstract class GeneralTransformer[E, W] extends ChildrenMapping[E, W] { def apply(c: Constant): (Either[E, Term], W) = simpleResult(c) } -trait EAsSet[E] { +abstract class SetsGeneralTransformer[E, W] + extends GeneralTransformer[java.util.Set[E], java.util.Set[W]] { /** * Merges the set of problematic (i.e., Left) results. */ def mergeErrors(a: java.util.Set[E], b: java.util.Set[E]): java.util.Set[E] = { - val c = new java.util.HashSet[E](a); - c.addAll(b); + val c = new java.util.HashSet[E](a) + c.addAll(b) c } val errorUnit: java.util.Set[E] = new java.util.HashSet[E]() -} -trait WAsSet[W] { val warningUnit: java.util.Set[W] = new java.util.HashSet[W]() + /** * Merges the set of problematic (i.e., Left) results. */ def mergeWarnings(a: java.util.Set[W], b: java.util.Set[W]): java.util.Set[W] = { - val c = new java.util.HashSet[W](a); - c.addAll(b); + val c = new java.util.HashSet[W](a) + c.addAll(b) c } } -abstract class SetsGeneralTransformer[E, W] - extends GeneralTransformer[java.util.Set[E], java.util.Set[W]] with EAsSet[E] with WAsSet[W] - /** * Visitor pattern for the front end classes. * Applies the visitor transformation on each node, and returns either a term, or a set of errors. (no warnings) @@ -156,15 +151,13 @@ abstract class SetsGeneralTransformer[E, W] abstract class TransformerWithErrors[E] extends ChildrenMapping[E, Ignore] { def applyTerm(t: Term): (Either[E, Term], Ignore) = (apply(t), Ignore) - - import collection.mutable // we expect this data structures to represent a DAG, so we // use a cache to remember nodes that we already visited. val cache = new util.IdentityHashMap[Term, Either[E, Term]] def apply(t: Term): Either[E, Term] = { if (cache.containsKey(t)) { - return cache.get(t); + return cache.get(t) } val res = t match { @@ -184,12 +177,23 @@ abstract class TransformerWithErrors[E] extends ChildrenMapping[E, Ignore] { def apply(tc: TermCons): Either[E, Term] = mapChildrenStrict(tc)._1 def apply(c: Constant): Either[E, Term] = Right(c) - override def mergeWarnings(a: Ignore, b: Ignore) = Ignore - override val warningUnit = Ignore + override def mergeWarnings(a: Ignore, b: Ignore): Ignore = Ignore + override val warningUnit: Ignore = Ignore } abstract class SetsTransformerWithErrors[E] - extends TransformerWithErrors[java.util.Set[E]] with EAsSet[E] + extends TransformerWithErrors[java.util.Set[E]] { + /** + * Merges the set of problematic (i.e., Left) results. + */ + def mergeErrors(a: java.util.Set[E], b: java.util.Set[E]): java.util.Set[E] = { + val c = new java.util.HashSet[E](a) + c.addAll(b) + c + } + + val errorUnit: java.util.Set[E] = new java.util.HashSet[E]() +} /** * Visitor pattern for the front end classes. @@ -198,15 +202,13 @@ abstract class SetsTransformerWithErrors[E] abstract class SafeTransformer extends ChildrenMapping[Ignore, Ignore] { def applyTerm(t: Term): (Either[Ignore, Term], Ignore) = (Right(apply(t)), Ignore) - - import collection.mutable // we expect this data structures to represent a DAG, so we // use a cache to remember nodes that we already visited. val cache = new util.IdentityHashMap[Term, Term] def apply(t: Term): Term = { if (cache.containsKey(t)) { - return cache.get(t); + return cache.get(t) } val res = t match { @@ -226,8 +228,8 @@ abstract class SafeTransformer extends ChildrenMapping[Ignore, Ignore] { def apply(tc: TermCons): Term = mapChildrenStrict(tc)._1.right.get def apply(c: Constant): Term = c - def mergeWarnings(a: Ignore, b: Ignore) = Ignore - val warningUnit = Ignore - def mergeErrors(a: Ignore, b: Ignore) = Ignore - val errorUnit = Ignore + def mergeWarnings(a: Ignore, b: Ignore): Ignore = Ignore + val warningUnit: Ignore = Ignore + def mergeErrors(a: Ignore, b: Ignore): Ignore = Ignore + val errorUnit: Ignore = Ignore }