From 6027bea95e6ad180e7109c27582b3215584bbd97 Mon Sep 17 00:00:00 2001 From: Gleb Koval Date: Wed, 5 Feb 2025 18:04:04 +0000 Subject: [PATCH] fix: use apply() instead of get() for Maps --- src/main/wacc/renamer.scala | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main/wacc/renamer.scala b/src/main/wacc/renamer.scala index cdfe19d..e46d9dd 100644 --- a/src/main/wacc/renamer.scala +++ b/src/main/wacc/renamer.scala @@ -120,12 +120,15 @@ object renamer { globalNumbering: mutable.Map[String, Int], errors: mutable.Builder[Error, List[Error]] ): Unit = { - scope.current.withDefault(scope.parent).get((ident.v, identType)) match { - case Some(Ident(_, uid)) => ident.uid = uid - case None => { + // Unfortunately map defaults only work with `.apply()`, which throws an error when the key is not found. + // Neither is there a way to check whether a default exists, so we have to use a try-catch. + try { + val Ident(_, uid) = scope.current.withDefault(scope.parent)((ident.v, identType)) + ident.uid = uid + } catch { + case _: NoSuchElementException => errors += Error.UndefinedIdentifier(ident, identType) scope.add(?, ident, identType) - } } } }