fix: use apply() instead of get() for Maps

This commit is contained in:
Gleb Koval 2025-02-05 18:04:04 +00:00
parent 8c5b85b8c2
commit 6027bea95e
Signed by: cyclane
GPG Key ID: 15E168A8B332382C

View File

@ -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)
}
}
}
}