Skip to content

Commit fbb624d

Browse files
Separate module parameters from regular parameters
1 parent 6e0238b commit fbb624d

File tree

3 files changed

+32
-56
lines changed

3 files changed

+32
-56
lines changed

hkmc2/shared/src/main/scala/hkmc2/codegen/Lowering.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ class Lowering(using TL, Raise, Elaborator.State):
9292
case Tup(fs) =>
9393
val as = fs.map:
9494
case sem.Fld(sem.FldFlags.empty, value, N) => value
95+
case sem.Fld(sem.FldFlags(false, false, false, true), value, N) => value
9596
case sem.Fld(flags, value, asc) =>
9697
TODO("Other argument forms")
9798
val l = new TempSymbol(summon[Elaborator.State].nextUid, S(t))

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,8 @@ extends Importer:
607607
case InfixApp(lhs: Ident, Keyword.`:`, rhs) =>
608608
Param(FldFlags.empty, fieldOrVarSym(ParamBind, lhs), S(term(rhs))) :: Nil
609609
case App(Ident(","), list) => params(list)._1
610+
case TypeDef(Mod, inner, _, _) => param(inner)
611+
.map(p => p.copy(flags = p.flags.copy(mod = true)))
610612
case TermDef(ImmutVal, inner, _) => param(inner)
611613

612614
def params(t: Tree): Ctxl[(Ls[Param], Ctx)] = t match
@@ -678,7 +680,7 @@ extends Importer:
678680
class VarianceTraverser(var changed: Bool = true) extends Traverser:
679681
override def traverseType(pol: Pol)(trm: Term): Unit = trm match
680682
case Term.TyApp(lhs, targs) =>
681-
lhs.symbol.flatMap(_.asTpe) match
683+
lhs.symbol.flatMap(sym => sym.asTpe orElse sym.asMod) match
682684
case S(sym: ClassSymbol) =>
683685
sym.defn match
684686
case S(td: ClassDef) =>
@@ -690,6 +692,17 @@ extends Importer:
690692
if !tp.isCovariant then traverseType(pol.!)(targ)
691693
case N =>
692694
TODO(sym->sym.uid)
695+
case S(sym: ModuleSymbol) =>
696+
sym.defn match
697+
case S(td: ModuleDef) =>
698+
if td.tparams.sizeCompare(targs) =/= 0 then
699+
raise(ErrorReport(msg"Wrong number of type arguments" -> trm.toLoc :: Nil)) // TODO BE
700+
td.tparams.zip(targs).foreach:
701+
case (tp, targ) =>
702+
if !tp.isContravariant then traverseType(pol)(targ)
703+
if !tp.isCovariant then traverseType(pol.!)(targ)
704+
case N =>
705+
TODO(sym->sym.uid)
693706
case S(sym: TypeAliasSymbol) =>
694707
// TODO dedup with above...
695708
sym.defn match

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

Lines changed: 17 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -24,43 +24,21 @@ module M with {
2424

2525
:e
2626
fun f1(module m)
27-
//│ FAILURE: Unexpected exception
28-
//│ /!!!\ Uncaught error: scala.MatchError: TypeDef(Mod,Ident(m),None,None) (of class hkmc2.syntax.Tree$TypeDef)
29-
//│ at: hkmc2.semantics.Elaborator.param(Elaborator.scala:610)
30-
//│ at: hkmc2.semantics.Elaborator.$anonfun$19(Elaborator.scala:614)
31-
//│ at: scala.collection.immutable.List.flatMap(List.scala:294)
32-
//│ at: hkmc2.semantics.Elaborator.params(Elaborator.scala:614)
33-
//│ at: hkmc2.semantics.Elaborator.$anonfun$10(Elaborator.scala:484)
34-
//│ at: scala.collection.LinearSeqOps.foldLeft(LinearSeq.scala:183)
35-
//│ at: scala.collection.LinearSeqOps.foldLeft$(LinearSeq.scala:179)
36-
//│ at: scala.collection.immutable.List.foldLeft(List.scala:79)
37-
//│ at: hkmc2.semantics.Elaborator.go$2(Elaborator.scala:483)
38-
//│ at: hkmc2.semantics.Elaborator.block$$anonfun$3(Elaborator.scala:597)
27+
//│ FAILURE: Unexpected lack of type error
3928

4029
:e
4130
fun f2[T](module m: T)
42-
//│ FAILURE: Unexpected exception
43-
//│ /!!!\ Uncaught error: scala.MatchError: TypeDef(Mod,InfixApp(Ident(m),keyword ':',Ident(T)),None,None) (of class hkmc2.syntax.Tree$TypeDef)
44-
//│ at: hkmc2.semantics.Elaborator.param(Elaborator.scala:610)
45-
//│ at: hkmc2.semantics.Elaborator.$anonfun$19(Elaborator.scala:614)
46-
//│ at: scala.collection.immutable.List.flatMap(List.scala:294)
47-
//│ at: hkmc2.semantics.Elaborator.params(Elaborator.scala:614)
48-
//│ at: hkmc2.semantics.Elaborator.$anonfun$10(Elaborator.scala:484)
49-
//│ at: scala.collection.LinearSeqOps.foldLeft(LinearSeq.scala:183)
50-
//│ at: scala.collection.LinearSeqOps.foldLeft$(LinearSeq.scala:179)
51-
//│ at: scala.collection.immutable.List.foldLeft(List.scala:79)
52-
//│ at: hkmc2.semantics.Elaborator.go$2(Elaborator.scala:483)
53-
//│ at: hkmc2.semantics.Elaborator.block$$anonfun$3(Elaborator.scala:597)
31+
//│ FAILURE: Unexpected lack of type error
5432

5533
:e
5634
module N with {
5735
fun f3(): M = M
5836
}
5937
//│ ╔══[ERROR] Name not found: M
60-
//│ ║ l.57: fun f3(): M = M
38+
//│ ║ l.35: fun f3(): M = M
6139
//│ ╙── ^
6240
//│ ╔══[ERROR] Name not found: M
63-
//│ ║ l.57: fun f3(): M = M
41+
//│ ║ l.35: fun f3(): M = M
6442
//│ ╙── ^
6543

6644
:e
@@ -85,30 +63,30 @@ module N with {
8563
fun f5(): M = M
8664
}
8765
//│ ╔══[ERROR] Name not found: M
88-
//│ ║ l.85: fun f5(): M = M
66+
//│ ║ l.63: fun f5(): M = M
8967
//│ ╙── ^
9068
//│ ╔══[ERROR] Name not found: M
91-
//│ ║ l.85: fun f5(): M = M
69+
//│ ║ l.63: fun f5(): M = M
9270
//│ ╙── ^
9371

9472

9573
fun f6(m: M)
9674
//│ FAILURE: Unexpected type error
9775
//│ ╔══[ERROR] Name not found: M
98-
//│ ║ l.95: fun f6(m: M)
76+
//│ ║ l.73: fun f6(m: M)
9977
//│ ╙── ^
10078

10179
:e
10280
f6(M)
10381
//│ ╔══[ERROR] Name not found: M
104-
//│ ║ l.102: f6(M)
105-
//│ ╙── ^
82+
//│ ║ l.80: f6(M)
83+
//│ ╙── ^
10684

10785
:e
10886
f6(M.self())
10987
//│ ╔══[ERROR] Name not found: M
110-
//│ ║ l.108: f6(M.self())
111-
//│ ╙── ^
88+
//│ ║ l.86: f6(M.self())
89+
//│ ╙── ^
11290

11391
:e
11492
fun f7(): module M
@@ -127,18 +105,10 @@ fun f7(): module M
127105

128106

129107
fun ok1(module m: M)
130-
//│ FAILURE: Unexpected exception
131-
//│ /!!!\ Uncaught error: scala.MatchError: TypeDef(Mod,InfixApp(Ident(m),keyword ':',Ident(M)),None,None) (of class hkmc2.syntax.Tree$TypeDef)
132-
//│ at: hkmc2.semantics.Elaborator.param(Elaborator.scala:610)
133-
//│ at: hkmc2.semantics.Elaborator.$anonfun$19(Elaborator.scala:614)
134-
//│ at: scala.collection.immutable.List.flatMap(List.scala:294)
135-
//│ at: hkmc2.semantics.Elaborator.params(Elaborator.scala:614)
136-
//│ at: hkmc2.semantics.Elaborator.$anonfun$10(Elaborator.scala:484)
137-
//│ at: scala.collection.LinearSeqOps.foldLeft(LinearSeq.scala:183)
138-
//│ at: scala.collection.LinearSeqOps.foldLeft$(LinearSeq.scala:179)
139-
//│ at: scala.collection.immutable.List.foldLeft(List.scala:79)
140-
//│ at: hkmc2.semantics.Elaborator.go$2(Elaborator.scala:483)
141-
//│ at: hkmc2.semantics.Elaborator.block$$anonfun$3(Elaborator.scala:597)
108+
//│ FAILURE: Unexpected type error
109+
//│ ╔══[ERROR] Name not found: M
110+
//│ ║ l.107: fun ok1(module m: M)
111+
//│ ╙── ^
142112

143113
module N with {
144114
fun ok2(): module M = M
@@ -158,20 +128,12 @@ module N with {
158128

159129
ok1(M)
160130
//│ FAILURE: Unexpected type error
161-
//│ ╔══[ERROR] Name not found: ok1
162-
//│ ║ l.159: ok1(M)
163-
//│ ╙── ^^^
164-
//│ FAILURE: Unexpected type error
165131
//│ ╔══[ERROR] Name not found: M
166-
//│ ║ l.159: ok1(M)
132+
//│ ║ l.129: ok1(M)
167133
//│ ╙── ^
168134

169135
ok1(M.self())
170136
//│ FAILURE: Unexpected type error
171-
//│ ╔══[ERROR] Name not found: ok1
172-
//│ ║ l.169: ok1(M.self())
173-
//│ ╙── ^^^
174-
//│ FAILURE: Unexpected type error
175137
//│ ╔══[ERROR] Name not found: M
176-
//│ ║ l.169: ok1(M.self())
138+
//│ ║ l.135: ok1(M.self())
177139
//│ ╙── ^

0 commit comments

Comments
 (0)