From 348748d9dbce71ab4bf564d67b479d5eee2ee585 Mon Sep 17 00:00:00 2001 From: Rikito Taniguchi Date: Fri, 14 Aug 2026 14:16:55 +0900 Subject: [PATCH] Ignore @throws annotation on non-methods According to JVMS 4.7.9.1 https://docs.oracle.com/javase/specs/jvms/se24/html/jvms-4.html#jvms-4.7.9.1 `ThrowsSignature` is only valid in `MethodSignature`s. Previously, we add `^...` ThrowsSignature no matter the symbol is method or not. We just ignore @throws annotation on invalid locations. --- .../src/dotty/tools/backend/jvm/GenericSignatures.scala | 2 +- tests/run/throws-annot.scala | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/compiler/src/dotty/tools/backend/jvm/GenericSignatures.scala b/compiler/src/dotty/tools/backend/jvm/GenericSignatures.scala index adf5f72864a3..1a954519bada 100644 --- a/compiler/src/dotty/tools/backend/jvm/GenericSignatures.scala +++ b/compiler/src/dotty/tools/backend/jvm/GenericSignatures.scala @@ -439,7 +439,7 @@ object GenericSignatures { jsig(info, toplevel = true) for annot <- sym0.annotations do annot match - case ThrownException(e) => + case ThrownException(e) if sym0.is(Method) => // ThrowsSignature is only valid in MethodSignature builder.append('^') jsig(e, toplevel = true) case _ => () diff --git a/tests/run/throws-annot.scala b/tests/run/throws-annot.scala index 444251c7f83b..587c2947c9b3 100644 --- a/tests/run/throws-annot.scala +++ b/tests/run/throws-annot.scala @@ -68,6 +68,15 @@ object TL { def readNoEx(): Int = 0 } +// @throws on class/object/field must be ignored/compiler shouldn't crash. +@throws(classOf[IOException]) +class ThrowsOnClass + +@throws(classOf[IOException]) +object ThrowsOnObject: + @throws(classOf[IOException]) + val field: Int = 0 + object Test { def main(args: Array[String]): Unit = { TestThrows.run(classOf[TestThrows.Foo])