diff --git a/compiler/src/dotty/tools/dotc/transform/sjs/PrepJSInterop.scala b/compiler/src/dotty/tools/dotc/transform/sjs/PrepJSInterop.scala index 75323e30bfb9..e75769147f80 100644 --- a/compiler/src/dotty/tools/dotc/transform/sjs/PrepJSInterop.scala +++ b/compiler/src/dotty/tools/dotc/transform/sjs/PrepJSInterop.scala @@ -979,9 +979,9 @@ class PrepJSInterop extends MacroTransform with IdentityDenotTransformer { thisP report.error(s"$longKindStr may only call js.native.", pos) } - // Check that the resul type was explicitly specified + // Check that the result type was explicitly specified // (This is stronger than Scala 2, which only warns, and only if it was inferred as Nothing.) - if (tree.tpt.span.isSynthetic) + if (tree.tpt.isInstanceOf[InferredTypeTree]) report.error(i"The type of ${tree.name} must be explicitly specified because it is JS native.", tree) } diff --git a/tests/sjs-junit/test/org/scalajs/testsuite/compiler/RegressionTestScala3.scala b/tests/sjs-junit/test/org/scalajs/testsuite/compiler/RegressionTestScala3.scala index e8e2ef0b0746..dcf64eb1953b 100644 --- a/tests/sjs-junit/test/org/scalajs/testsuite/compiler/RegressionTestScala3.scala +++ b/tests/sjs-junit/test/org/scalajs/testsuite/compiler/RegressionTestScala3.scala @@ -78,6 +78,18 @@ class RegressionTestScala3 { val f3 = { () => i += 1 } assertSame(f3, Thunk.asFunction0(f3())) } + + @Test def literalTypeJSNativeIssue16173(): Unit = { + js.eval(""" + var RegressionTestScala3_Issue16173_foo = "constant"; + var RegressionTestScala3_Issue16173_bar = function() { return 5; }; + """) + + assertEquals("constant", Issue16173.foo1) + assertEquals("constant", Issue16173.foo2) + + assertEquals(5, Issue16173.bar1()) + } } object RegressionTestScala3 { @@ -148,6 +160,20 @@ object RegressionTestScala3 { val entries = js.Object.entries(obj) val js.Tuple2(k, v) = entries(0): @unchecked } + + object Issue16173 { + @js.native + @JSGlobal("RegressionTestScala3_Issue16173_foo") + val foo1: "constant" = js.native + + @js.native + @JSGlobal("RegressionTestScala3_Issue16173_foo") + def foo2: "constant" = js.native + + @js.native + @JSGlobal("RegressionTestScala3_Issue16173_bar") + def bar1(): 5 = js.native + } } // This class needs to be at the top-level, not in an object, to reproduce the issue