Skip to content

Bug in List pattern matching #8785

@lukewilliamboswell

Description

@lukewilliamboswell
9:20:33 ~/Documents/GitHub/roc main $ roc version
Roc compiler version debug-9731b3d4
9:21:23 ~/Documents/GitHub/roc main $ roc /Users/luke/Documents/GitHub/roc/test/fx/list_elem_pattern_match_bug.roc
Direct Text: Text: hello
Direct Element: Element: div
Text from List: Text: from-list
Element from List: Text: span
app [main!] { pf: platform "./platform/main.roc" }

import pf.Stdout

## BUG: Pattern matching on recursive opaque type elements retrieved from a List
## matches the WRONG variant.
##
## Expected output:
##   Direct Text: Text: hello
##   Direct Element: Element: div
##   Text from List: Text: from-list
##   Element from List: Element: span
##
## Actual output:
##   Direct Text: Text: hello
##   Direct Element: Element: div
##   Text from List: Text: from-list
##   Element from List: Text: span    <-- BUG: should be "Element: span"

Node := [
    Element(Str, List(Node)),
    Text(Str),
]

## Pattern match and return which branch was taken
identify : Node -> Str
identify = |node|
    match node {
        Text(content) => "Text: ${content}"
        Element(tag, _children) => "Element: ${tag}"
    }

main! = || {
    # Test 1: Direct Text node - works correctly
    text_node : Node
    text_node = Text("hello")
    Stdout.line!("Direct Text: ${identify(text_node)}")

    # Test 2: Direct Element node - works correctly
    elem_node : Node
    elem_node = Element("div", [])
    Stdout.line!("Direct Element: ${identify(elem_node)}")

    # Test 3: Text node from List - works correctly
    text_list : List(Node)
    text_list = [Text("from-list")]

    match List.first(text_list) {
        Ok(node) => Stdout.line!("Text from List: ${identify(node)}")
        Err(_) => Stdout.line!("Text from List: empty")
    }

    # Test 4: Element node from List - BUG: matches as Text
    elem_list : List(Node)
    elem_list = [Element("span", [])]

    match List.first(elem_list) {
        Ok(node) => Stdout.line!("Element from List: ${identify(node)}")
        Err(_) => Stdout.line!("Element from List: empty")
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions