Skip to content

Commit d58b350

Browse files
Enforce rules on module arguments
1 parent 189e6eb commit d58b350

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,32 @@ 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+
.filter(_.isInstanceOf[BlockMemberSymbol])
244+
.flatMap(_.asInstanceOf[BlockMemberSymbol].trmTree)
245+
.filter(_.isInstanceOf[TermDef])
246+
.flatMap(_.asInstanceOf[TermDef].paramLists.headOption)
247+
for
248+
(args, params) <- (args zip params)
249+
(arg, param) <- (args zip params.fields)
250+
do
251+
val argMod = arg.flags.mod
252+
val paramMod = param match
253+
case Tree.TypeDef(Mod, _, _, _) => true
254+
case _ => false
255+
if !argMod && paramMod then raise:
256+
ErrorReport:
257+
msg"Only module arguments (values) may be passed to module parameters." ->
258+
arg.toLoc :: Nil
259+
260+
Term.App(lt, rt)(tree, sym)
236261
case Sel(pre, nme) =>
237262
val preTrm = term(pre)
238263
val sym = resolveField(nme, preTrm.symbol, nme)

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,14 @@ module N with {
4747
:e
4848
fun f6(module m: M)
4949
f6(new C)
50-
//│ FAILURE: Unexpected lack of type error
50+
//│ ╔══[ERROR] Only module arguments (values) may be passed to module parameters.
51+
//│ ║ l.49: f6(new C)
52+
//│ ╙── ^
5153

5254
:e
5355
fun f7(): module M
5456
//│ ╔══[ERROR] Only module methods may return module values.
55-
//│ ║ l.53: fun f7(): module M
57+
//│ ║ l.55: fun f7(): module M
5658
//│ ╙── ^^^^^^^^^^^^^^
5759

5860

0 commit comments

Comments
 (0)