fix: support non-type params in RpcEncoding

This commit is contained in:
Gabriel Ebner 2022-07-17 20:01:37 +02:00 committed by Sebastian Ullrich
parent 62ede1fdfd
commit ed5e0f098c
3 changed files with 14 additions and 2 deletions

View file

@ -216,12 +216,15 @@ private def deriveInstance (typeName : Name) : CommandElabM Bool := do
for param in params do
let paramNm := (← getFVarLocalDecl param).userName
paramBinders := paramBinders.push (← `(bracketedBinder| ($(mkIdent paramNm))))
let ty ← PrettyPrinter.delab (← inferType param)
paramBinders := paramBinders.push (← `(bracketedBinder| ($(mkIdent paramNm) : $ty)))
let packet := mkIdent (← mkFreshUserName (paramNm.appendAfter "Packet"))
-- only look for encodings for `Type` parameters
if (← inferType param).isType then
let packet := mkIdent (← mkFreshUserName (paramNm.appendAfter "Packet"))
packetParamBinders := packetParamBinders.push (← `(bracketedBinder| ($packet : Type)))
encInstBinders := encInstBinders.push (← `(bracketedBinder| [RpcEncoding $(mkIdent paramNm) $packet]))
else
packetParamBinders := packetParamBinders.push paramBinders.back --(← `(bracketedBinder| ($packet : $ty)))
return (paramBinders, packetParamBinders, encInstBinders)

View file

@ -89,3 +89,10 @@ inductive FooNested (α : Type) where
#print FooNested.RpcEncodingPacket
#check @FooNested.RpcEncodingPacket.a
#eval test (FooNested BazInductive) (.a default #[default])
inductive FooParam (n : Nat) where
| a : Nat → FooParam n
deriving RpcEncoding, Inhabited
#check instRpcEncodingFooParamRpcEncodingPacket
#eval test (FooParam 10) (.a 42)

View file

@ -39,3 +39,5 @@ FooNested.RpcEncodingPacket.a : {αPacket : Type} →
@FooNested.RpcEncodingPacket.a : {αPacket : Type} →
αPacket → Array (FooNested.RpcEncodingPacket αPacket) → FooNested.RpcEncodingPacket αPacket
ok: {"a": [{"baz": []}, [{"a": [{"baz": []}, []]}]]}
instRpcEncodingFooParamRpcEncodingPacket : (n : Nat) → RpcEncoding (FooParam n) (FooParam.RpcEncodingPacket n)
ok: {"a": 42}