Skip to content

Refactor Value.Ref into Value.{SimpleRef,MemberRef,InnerRef}#491

Draft
Derppening wants to merge 31 commits into
hkust-taco:hkmc2from
Derppening:refactor/ref-symbol
Draft

Refactor Value.Ref into Value.{SimpleRef,MemberRef,InnerRef}#491
Derppening wants to merge 31 commits into
hkust-taco:hkmc2from
Derppening:refactor/ref-symbol

Conversation

@Derppening
Copy link
Copy Markdown
Contributor

@Derppening Derppening commented May 16, 2026

TODO:

  • Migrate case Value.Ref into apply and unapply functions

@Derppening
Copy link
Copy Markdown
Contributor Author

034

[error] -- Error: /home/runner/work/mlscript/mlscript/hkmc2DiffTests/src/test/scala/hkmc2/JSBackendDiffMaker.scala:293:38 
[error] 293 |                    case sym => Value.Ref(sym, N),
[error]     |                                ^^^^^^^^^
[error]     |object Ref in object Value is deprecated: Use Value.SimpleRef, Value.MemberRef, or Value.This instead.
[error] one error found
[error] (hkmc2DiffTests / Test / compileIncremental) Compilation failed

I have no idea what's up with the CI failure. It is only deprecated but it's treated as an error for some reason.

@Derppening
Copy link
Copy Markdown
Contributor Author

@LPTK You can take a look at the changes first. I will mark this as ready for review once the TODO is also addressed.

@Derppening Derppening marked this pull request as ready for review May 16, 2026 07:57
@Derppening Derppening requested a review from LPTK May 16, 2026 07:57
Copy link
Copy Markdown
Contributor

@LPTK LPTK left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this should have been marked as a draft and that you didn't properly review the diff. Given the amount of duplicated logic, a lot of it looks like thoughtless slop 😕

PS: thoughtless slop is perfectly fine as long as the PR is marked as Draft and you eventually clean it up 😉

override def applyValue(v: Value): Unit =
v match
case Value.Ref(l, disamb) if !inCtx(l) && l.asClsLike.isEmpty => freeVars.add(l)
case Value.SimpleRef(l) if !inCtx(l) && l.asClsLike.isEmpty => freeVars.add(l)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think asClsLike can be true for SimpleRef. Just look at its impl.

case Value.Ref(l, disamb) if !inCtx(l) && l.asClsLike.isEmpty => freeVars.add(l)
case Value.SimpleRef(l) if !inCtx(l) && l.asClsLike.isEmpty => freeVars.add(l)
case Value.MemberRef(bms, _) if !inCtx(bms) && bms.asClsLike.isEmpty => freeVars.add(bms)
case Value.InnerRef(l) if !inCtx(l) && l.asClsLike.isEmpty => freeVars.add(l)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will never meaningfully match. this references are almost always to a class-ike thing. @ychenfo what's the intent, here?

Comment on lines +358 to +362
case bms: BlockMemberSymbol => Value.MemberRef(bms, bms.defaultDisamb.get)
case sym: TempSymbol => Value.SimpleRef(sym)
case sym: VarSymbol => Value.SimpleRef(sym)
case sym: (LocalSymbol | BuiltinSymbol) => Value.SimpleRef(sym)
case sym: InnerSymbol => Value.InnerRef(sym)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This jungle of cases is almost certainly unnecessary. Check what type of symbol flows here and split/refine the type of this extension method, isntead of using pattern matching. Perhaps @ychenfo can help.

case _ => ()
generatedProdVars(sym).asProdStrat
case _: Value.Ref => lastWords("already handled in `RefLike` case")
case _: (Value.SimpleRef | Value.MemberRef) => lastWords("already handled in `RefLike` case")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that this Value.SimpleRef | Value.MemberRef pattern seems common enough to make them extend a base Value.Ref sealed trait that could be used here instead.

Comment on lines +105 to +106
classCtorSymbol(sym) orElse S(sym)
classCtorSymbol(sym) orElse S(sym)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicated line...

Comment on lines +287 to +292
sym match
case sym: TempSymbol => Value.SimpleRef(sym)
case sym: VarSymbol => Value.SimpleRef(sym)
case sym: BlockMemberSymbol => Value.MemberRef(sym, sym.defaultDisamb.get)
case sym: (LocalSymbol | BuiltinSymbol) => Value.SimpleRef(sym)
case sym: InnerSymbol => Value.InnerRef(sym)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again some duplicated (and suspicious) logic.

@@ -1,6 +1,5 @@
:js


Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👎

case Value.SimpleRef(l) =>
mapping.get(l) match
case None => super.applyValue(v)(k)
case Some(newBms: BlockMemberSymbol) =>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will the mapping ever map a SimpleRef symbol to a BlockMemberSymbol?! 🤔

warnStmt
k(loweringCtx(Value.Ref(sym, disamb).withLocOf(ref)))
(sym, disamb) match
case (sym: TopLevelSymbol, _) =>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useless case.

Comment on lines +591 to +594
case (sym: TempSymbol, _) =>
k(loweringCtx(Value.SimpleRef(sym).withLocOf(ref)))
case (sym: VarSymbol, _) =>
k(loweringCtx(Value.SimpleRef(sym).withLocOf(ref)))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Collapse using sym: (TempSymbol | VarSymbol)

@Derppening
Copy link
Copy Markdown
Contributor Author

Sorry for all the redundant logic - I will convert this as a draft to clean this up first, then when it's ready I will re-request for your review.

@Derppening Derppening marked this pull request as draft May 17, 2026 03:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants