Skip to content

Commit 2cfcdf7

Browse files
Enforce functions with module parameters to have an explicit return type
1 parent 6175027 commit 2cfcdf7

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
@@ -513,12 +513,29 @@ extends Importer:
513513
case ((pss, ctx), ps) =>
514514
val (qs, newCtx) = params(ps)(using ctx)
515515
(pss :+ ParamList(ParamListFlags.empty, qs), newCtx)
516+
// * Elaborate signature
517+
val s = td.signature.orElse(newSignatureTrees.get(id.name)).map(term)
516518
val b = rhs.map(term(_)(using newCtx))
517519
val r = FlowSymbol(s"‹result of ${sym}", nextUid)
518-
val tdf = TermDefinition(owner, k, sym, pss,
519-
td.signature.orElse(newSignatureTrees.get(id.name)).map(term), b, r,
520-
TermDefFlags(mod))
520+
val tdf = TermDefinition(owner, k, sym, pss, s, b, r, TermDefFlags(mod))
521521
sym.defn = S(tdf)
522+
523+
// Restrictions regarding functions receiving module parameters
524+
525+
// not working because sometimes the signature is N
526+
// even when the function has a explicit return type
527+
// val explicitReturnType = s.isDefined
528+
529+
// FIXME: this is magic; we shall determine the return type based on the signature
530+
val explicitReturnType =
531+
td.head.isInstanceOf[InfixApp] && td.head.asInstanceOf[InfixApp]._2 == Keyword.`:`
532+
533+
if pss.flatMap(_.params).exists(_.flags.mod) && !explicitReturnType then
534+
raise:
535+
ErrorReport:
536+
msg"Functions with module parameters must have a explicit return type." ->
537+
td.head.toLoc :: Nil
538+
522539
tdf
523540
go(sts, tdf :: acc)
524541
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)