Skip to content

Commit a789e0e

Browse files
Enforce rules on module arguments
1 parent b1b6711 commit a789e0e

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

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

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,34 @@ extends Importer:
232232
term(rhs)
233233
case tree @ App(lhs, rhs) =>
234234
val sym = FlowSymbol("‹app-res›", nextUid)
235-
Term.App(term(lhs), term(rhs))(tree, sym)
235+
val lt = term(lhs)
236+
val rt = term(rhs)
237+
238+
// Check if module arguments match module parameters
239+
val args = rt match
240+
case Term.Tup(fields) => S(fields)
241+
case _ => N
242+
val params = lt.symbol
243+
.collect:
244+
case sym: BlockMemberSymbol => sym.trmTree
245+
.flatten
246+
.collect:
247+
case td: TermDef => td.paramLists.headOption
248+
.flatten
249+
for
250+
(args, params) <- (args zip params)
251+
(arg, param) <- (args zip params.fields)
252+
do
253+
val argMod = arg.flags.mod
254+
val paramMod = param match
255+
case Tree.TypeDef(Mod, _, N, N) => true
256+
case _ => false
257+
if argMod && !paramMod then raise:
258+
ErrorReport:
259+
msg"Only module parameters may receive module arguments (values)." ->
260+
arg.toLoc :: Nil
261+
262+
Term.App(lt, rt)(tree, sym)
236263
case Sel(pre, nme) =>
237264
val preTrm = term(pre)
238265
val sym = resolveField(nme, preTrm.symbol, nme)
@@ -329,9 +356,10 @@ extends Importer:
329356
Fld(FldFlags.empty, term(lhs), S(term(rhs)))
330357
case _ =>
331358
val t = term(tree)
332-
t.symbol.flatMap(_.asMod) match
333-
case S(_) => Fld(FldFlags.empty.copy(mod = true), t, N)
334-
case N => Fld(FldFlags.empty, t, N)
359+
val flags = FldFlags.empty
360+
if ModuleChecker.evalsToModule(t)
361+
then Fld(flags.copy(mod = true), t, N)
362+
else Fld(flags, t, N)
335363

336364
def unit: Term.Lit = Term.Lit(UnitLit(true))
337365

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,20 @@ fun f6(m: M)
5151

5252
:e
5353
f6(M)
54-
//│ FAILURE: Unexpected lack of type error
54+
//│ ╔══[ERROR] Only module parameters may receive module arguments (values).
55+
//│ ║ l.53: f6(M)
56+
//│ ╙── ^
5557

5658
:e
5759
f6(M.self())
58-
//│ FAILURE: Unexpected lack of type error
60+
//│ ╔══[ERROR] Only module parameters may receive module arguments (values).
61+
//│ ║ l.59: f6(M.self())
62+
//│ ╙── ^^^^^^^^
5963

6064
:e
6165
fun f7(): module M
6266
//│ ╔══[ERROR] Only module methods may return module values.
63-
//│ ║ l.61: fun f7(): module M
67+
//│ ║ l.65: fun f7(): module M
6468
//│ ╙── ^^^^^^^^^^^^^^
6569

6670

0 commit comments

Comments
 (0)