Skip to content

Commit

Permalink
Fix the res running variable is not consistent with DiffTests
Browse files Browse the repository at this point in the history
  • Loading branch information
NeilKleistGao committed Jun 21, 2022
1 parent 4377e08 commit 3b87645
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
52 changes: 52 additions & 0 deletions shared/src/test/diff/mlscript/Issues65.mls
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
:js
42
//│ // Prelude
//│ let res;
//│ // Query 1
//│ res = 42;
//│ // End of generated code
//│ res: 42
//│ = 42

:js
def foo = "oops"
//│ // Query 1
//│ globalThis.foo = "oops";
//│ res = foo;
//│ // End of generated code
//│ foo: "oops"
//│ = 'oops'

:js
res
//│ // Query 1
//│ res = res;
//│ // End of generated code
//│ res: "oops"
//│ = 'oops'

:js
res = 5
//│ // Query 1
//│ globalThis.res1 = 5;
//│ res = res1;
//│ // End of generated code
//│ res: 5
//│ = 5

:js
foo
//│ // Query 1
//│ res = foo;
//│ // End of generated code
//│ res: "oops"
//│ = 'oops'

:js
res
//│ // Query 1
//│ res = res1;
//│ // End of generated code
//│ res: 5
//│ = 5

8 changes: 7 additions & 1 deletion shared/src/test/scala/mlscript/DiffTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class DiffTests extends org.scalatest.funsuite.AnyFunSuite with org.scalatest.Pa
override def emitDbg(str: String): Unit = output(str)
}
var ctx: typer.Ctx = typer.Ctx.init
var self_define_res: Bool = false
var declared: Map[Str, typer.PolymorphicType] = Map.empty
val failures = mutable.Buffer.empty[Int]
val unmergedChanges = mutable.Buffer.empty[Int]
Expand Down Expand Up @@ -510,6 +511,8 @@ class DiffTests extends org.scalatest.funsuite.AnyFunSuite with org.scalatest.Pa
typer.typeType(rhs)(ctx.nextLevel, raise,
vars = tps.map(tp => tp.name -> typer.freshVar(typer.noProv/*FIXME*/)(1)).toMap))
ctx += nme.name -> ty_sch
if (nme.name.contentEquals("res")) self_define_res = true
if (!self_define_res) ctx += "res" -> ty_sch
declared += nme.name -> ty_sch
val exp = getType(ty_sch)
output(s"$nme: ${exp.show}")
Expand All @@ -521,13 +524,15 @@ class DiffTests extends org.scalatest.funsuite.AnyFunSuite with org.scalatest.Pa
typer.dbg = mode.dbg
val ty_sch = typer.typeLetRhs(isrec, nme.name, rhs)(ctx, raise)
val exp = getType(ty_sch)
if (nme.name.contentEquals("res")) self_define_res = true
// statement does not have a declared type for the body
// the inferred type must be used and stored for lookup
declared.get(nme.name) match {
// statement has a body but it's type was not declared
// infer it's type and store it for lookup and type gen
case N =>
ctx += nme.name -> ty_sch
if (!self_define_res) ctx += "res" -> ty_sch
output(s"$nme: ${exp.show}")
if (mode.generateTsDeclarations) tsTypegenCodeBuilder.addTypeGenTermDefinition(exp, Some(nme.name))

Expand All @@ -536,6 +541,7 @@ class DiffTests extends org.scalatest.funsuite.AnyFunSuite with org.scalatest.Pa
// the inferred type is used to for ts type gen
case S(sign) =>
ctx += nme.name -> sign
if (!self_define_res) ctx += "res" -> sign
val sign_exp = getType(sign)
output(exp.show)
output(s" <: $nme:")
Expand Down Expand Up @@ -565,7 +571,7 @@ class DiffTests extends org.scalatest.funsuite.AnyFunSuite with org.scalatest.Pa
case L(pty) =>
val exp = getType(pty)
if (exp =/= TypeName("unit")) {
ctx += "res" -> pty
if (!self_define_res) ctx += "res" -> pty
output(s"res: ${exp.show}")
if (mode.generateTsDeclarations) tsTypegenCodeBuilder.addTypeGenTermDefinition(exp, None)
prefixLength = 3
Expand Down

0 comments on commit 3b87645

Please sign in to comment.