Skip to content

Commit 76170a9

Browse files
Enforce rules on module parameters
1 parent 9659ced commit 76170a9

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

hkmc2/shared/src/main/scala/hkmc2/semantics/Elaborator.scala

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -613,8 +613,15 @@ extends Importer:
613613
case InfixApp(lhs: Ident, Keyword.`:`, rhs) =>
614614
Param(FldFlags.empty, fieldOrVarSym(ParamBind, lhs), S(term(rhs))) :: Nil
615615
case App(Ident(","), list) => params(list)._1
616-
case TypeDef(Mod, inner, _, _) => param(inner)
617-
.map(p => p.copy(flags = p.flags.copy(mod = true)))
616+
case TypeDef(Mod, inner, N, N) =>
617+
val ps = param(inner).map(p => p.copy(flags = p.flags.copy(mod = true)))
618+
for p <- ps if p.flags.mod do p.sign match
619+
case N =>
620+
raise(ErrorReport(msg"Module parameters must have explicit types." -> t.toLoc :: Nil))
621+
case S(ret) if ModuleChecker.isTypeParam(ret) =>
622+
raise(ErrorReport(msg"Module parameters must have concrete types." -> t.toLoc :: Nil))
623+
case _ => ()
624+
ps
618625
case TermDef(ImmutVal, inner, _) => param(inner)
619626

620627
def params(t: Tree): Ctxl[(Ls[Param], Ctx)] = t match
@@ -682,7 +689,15 @@ extends Importer:
682689
while trav.changed do
683690
trav.changed = false
684691
go(s)
692+
693+
object ModuleChecker:
685694

695+
/** Checks if a term is a reference to a type parameter. */
696+
def isTypeParam(t: Term): Bool = t.symbol
697+
.filter(_.isInstanceOf[VarSymbol])
698+
.flatMap(_.asInstanceOf[VarSymbol].decl)
699+
.fold(false)(_.isInstanceOf[TyParam])
700+
686701
class VarianceTraverser(var changed: Bool = true) extends Traverser:
687702
override def traverseType(pol: Pol)(trm: Term): Unit = trm match
688703
case Term.TyApp(lhs, targs) =>

hkmc2/shared/src/test/mlscript/basics/ModuleMethods.mls

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@ module M with {
1212

1313
:e
1414
fun f1(module m)
15-
//│ FAILURE: Unexpected lack of type error
15+
//│ ╔══[ERROR] Module parameters must have explicit types.
16+
//│ ║ l.14: fun f1(module m)
17+
//│ ╙── ^
1618

1719
:e
1820
fun f2[T](module m: T)
19-
//│ FAILURE: Unexpected lack of type error
21+
//│ ╔══[ERROR] Module parameters must have concrete types.
22+
//│ ║ l.20: fun f2[T](module m: T)
23+
//│ ╙── ^^^^
2024

2125
:e
2226
module N with {

0 commit comments

Comments
 (0)