Skip to content

Commit 62f7568

Browse files
Enforce functions with module parameters to have an explicit return type
1 parent c5d7e50 commit 62f7568

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: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,12 +516,29 @@ extends Importer:
516516
case ((pss, ctx), ps) =>
517517
val (qs, newCtx) = params(ps)(using ctx)
518518
(pss :+ ParamList(ParamListFlags.empty, qs), newCtx)
519+
// * Elaborate signature
520+
val s = td.signature.orElse(newSignatureTrees.get(id.name)).map(term)
519521
val b = rhs.map(term(_)(using newCtx))
520522
val r = FlowSymbol(s"‹result of ${sym}", nextUid)
521-
val tdf = TermDefinition(owner, k, sym, pss,
522-
td.signature.orElse(newSignatureTrees.get(id.name)).map(term), b, r,
523-
TermDefFlags(mod))
523+
val tdf = TermDefinition(owner, k, sym, pss, s, b, r, TermDefFlags(mod))
524524
sym.defn = S(tdf)
525+
526+
// Restrictions regarding functions receiving module parameters
527+
528+
// not working because sometimes the signature is N
529+
// even when the function has a explicit return type
530+
// val explicitReturnType = s.isDefined
531+
532+
// FIXME: this is magic; we shall determine the return type based on the signature
533+
val explicitReturnType =
534+
td.head.isInstanceOf[InfixApp] && td.head.asInstanceOf[InfixApp]._2 == Keyword.`:`
535+
536+
if pss.flatMap(_.params).exists(_.flags.mod) && !explicitReturnType then
537+
raise:
538+
ErrorReport:
539+
msg"Functions with module parameters must have a explicit return type." ->
540+
td.head.toLoc :: Nil
541+
525542
tdf
526543
go(sts, tdf :: acc)
527544
case L(d) =>

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,9 @@ idMod(IntMT)
344344
// Error: functions taking module parameters must have an explicit result type.
345345
:e
346346
fun f3(x: M) = x
347+
//│ ╔══[ERROR] Functions with module parameters must have a explicit return type.
348+
//│ ║ l.346: fun f3(x: M) = x
349+
//│ ╙── ^^^^^^^^
347350
//│ Elaborated tree:
348351
//│ Blk:
349352
//│ stats = Ls of
@@ -366,7 +369,6 @@ fun f3(x: M) = x
366369
//│ resSym = ‹result of member:f3›@66
367370
//│ flags = ()
368371
//│ res = Lit of UnitLit of true
369-
//│ FAILURE: Unexpected lack of type error
370372

371373
// OK: t is not a module parameter and doesn't need to have a explicit result type.
372374
fun f4(x: Int) = x

0 commit comments

Comments
 (0)