Skip to content

Commit 236c7e8

Browse files
Enforce functions with module parameters to have an explicit return type
1 parent ac26092 commit 236c7e8

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,10 @@ idMod(IntM2).f(0) // OK
387387

388388
// Error: functions taking module parameters must have an explicit result type.
389389
fun idMod[T](t: M2[T]) = t
390+
//│ FAILURE: Unexpected type error
391+
//│ ╔══[ERROR] Functions with module parameters must have a explicit return type.
392+
//│ ║ l.389: fun idMod[T](t: M2[T]) = t
393+
//│ ╙── ^^^^^^^^^^^^^^^^^^
390394
//│ Elaborated tree:
391395
//│ Blk:
392396
//│ stats = Ls of

0 commit comments

Comments
 (0)