Skip to content

Commit

Permalink
Fix #16173: SJS: Fix the detection of inferred types of = js.native.
Browse files Browse the repository at this point in the history
Use `tpt.isInstanceOf[InferredTypeTree]` instead of relying on the
span being synthetic.
  • Loading branch information
sjrd committed Oct 14, 2022
1 parent 62684d0 commit 488aa27
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,7 @@ class PrepJSInterop extends MacroTransform with IdentityDenotTransformer { thisP

// Check that the resul 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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 488aa27

Please sign in to comment.