Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Runtime error when passing record with missing default fields to encoder #7185

Open
jwoudenberg opened this issue Oct 25, 2024 · 0 comments
Open
Labels
bug Something isn't working P-medium Medium priority/frequency

Comments

@jwoudenberg
Copy link
Contributor

I can write a function that takes a record with some default fields and directly encodes it, without providing default values. This compiles, but then results in a runtime failure.

This is a minimal example using roc-json, but I think the error is the same regardless of what encoder you use. Run the file with roc test to get the error.

app [main] {
    cli: platform "https://github.com/roc-lang/basic-cli/releases/download/0.15.0/SlwdbJ-3GR7uBWQo6zlmYWNYOxnvo8r6YABXD-45UOw.tar.br",
    json: "https://github.com/lukewilliamboswell/roc-json/releases/download/0.10.2/FH4N0Sw-JSFXJfG3j54VEDPtXOoN-6I9v_IA8S18IGk.tar.br",
}

import json.Json

main = Task.ok {}

expect
    encode : { hi ? Str } -> List U8
    encode = \arg -> Encode.toBytes arg Json.utf8
    encode {} == ['{', '}']

Here's the error:

thread 'main' panicked at crates/compiler/mono/src/borrow.rs:396:34:
internal error: entered unreachable code: 
	No borrow signature for LambdaName { name: `12.IdentId(26)`, niche: Niche(Captures([])) } layout.

	Tip 1: This can happen when you call a function with less arguments than it expects.
	Like `Arg.list!` instead of `Arg.list! {}`.
	Tip 2: `roc check yourfile.roc` can sometimes give you a helpful error. 
                            
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

I don't know what the fix should be. Probably a compile time error rather than allowing the behavior at runtime? The reason I was trying this was to create a cute DSL for writing Html:

Html.ul { class: "list-of-things } [
    Html.li {} [ Html.text "an item!" ],
]

# --- Html.roc
ul : { class ? Str, id ? Str, ............ }, List Html -> Html
li : { class ? Str, id ? Str, ............ }, List Html -> Html

I realize that's not what these default field types are made for though 😅

@Anton-4 Anton-4 added bug Something isn't working P-medium Medium priority/frequency labels Oct 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working P-medium Medium priority/frequency
Projects
None yet
Development

No branches or pull requests

2 participants