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 trait parents handling #165

Merged
merged 1 commit into from
Jun 1, 2023
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
25 changes: 9 additions & 16 deletions shared/src/main/scala/mlscript/JSBackend.scala
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,6 @@ class JSBackend(allowUnresolvedSymbols: Boolean) {
protected def translateLocalNewType(typeDef: NuTypeDef)(implicit scope: Scope): JSConstDecl = {
// TODO: support traitSymbols
val (traitSymbols, classSymbols, mixinSymbols, moduleSymbols) = declareNewTypeDefs(typeDef :: Nil, false)
if (!traitSymbols.isEmpty) throw CodeGenError("traits are not supported yet.")

val sym = classSymbols match {
case s :: _ => S(s)
Expand Down Expand Up @@ -579,22 +578,17 @@ class JSBackend(allowUnresolvedSymbols: Boolean) {
}

private def translateParents(superFields: Ls[Term], constructorScope: Scope)(implicit scope: Scope): Opt[JSExpr] = {
val bases = superFields.map {
case App(lhs, _) => translateTerm(App(lhs, Tup(Ls())))(constructorScope)
case t => translateTerm(t)(constructorScope)
}

def translateParent(current: JSExpr, base: JSExpr, mixinOnly: Bool): JSExpr = {
val name = current match {
case JSIdent(nme) => nme
case JSInvoke(JSIdent(nme), _) => nme
case JSNew(JSIdent(nme)) => nme
case JSInvoke(JSNew(JSIdent(nme)), _) => nme
case f: JSField => f.property.name
case JSInvoke(f: JSField, _) => f.property.name
def translateParent(current: Term, base: JSExpr, mixinOnly: Bool): JSExpr = {
def resolveName(term: Term): Str = term match {
case App(lhs, _) => resolveName(lhs)
case Var(name) => name
case Sel(_, Var(fieldName)) => fieldName
case TyApp(lhs, _) => resolveName(lhs)
case _ => throw CodeGenError("unsupported parents.")
}

val name = resolveName(current)

scope.resolveValue(name) match {
case Some(CapturedSymbol(_, _: TraitSymbol)) => base // TODO:
case Some(CapturedSymbol(out, sym: MixinSymbol)) =>
Expand All @@ -619,7 +613,7 @@ class JSBackend(allowUnresolvedSymbols: Boolean) {

// for non-first parent classes, they must be mixins or we would get more than one parent classes,
// which is not allowed in JS
bases match {
superFields match {
case head :: tail => S(tail.foldLeft(
translateParent(head, JSIdent("Object"), false)
)((res, next) => translateParent(next, res, true)))
Expand Down Expand Up @@ -732,7 +726,6 @@ class JSBackend(allowUnresolvedSymbols: Boolean) {

// TODO: support traitSymbols
val (traitSymbols, classSymbols, mixinSymbols, moduleSymbols) = declareNewTypeDefs(sym.nested, true)(nuTypeScope)
if (!traitSymbols.isEmpty) throw CodeGenError("traits are not supported yet.")

if (keepTopLevelScope) // also declare in the top level for diff tests
declareNewTypeDefs(sym.nested, false)(topLevelScope)
Expand Down
2 changes: 1 addition & 1 deletion shared/src/test/diff/nu/ParamOverride.mls
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Derived0(n: int) extends Base
//│ ╙── ^^^^
//│ class Derived0(n: int)
//│ Code generation encountered an error:
//│ unresolved symbol Base
//│ unresolved parent Base.


mixin Base1(n: number) {
Expand Down