Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #16173: SJS: Fix the detection of inferred types of = js.native. #16184

Merged
merged 1 commit into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

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