Skip to content

Match element diagnostics improvements - #12680

Open
nichturner wants to merge 7 commits into
slint-ui:masterfrom
nichturner:feature/initial-match-diagnostics-improvements
Open

Match element diagnostics improvements#12680
nichturner wants to merge 7 commits into
slint-ui:masterfrom
nichturner:feature/initial-match-diagnostics-improvements

Conversation

@nichturner

@nichturner nichturner commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Addresses the Diagnostics section of #1307 after an experimental implementation of match elements was merged in #12021.

Standardized Terminology

Updated all references to match expression or match statement to be match element.

export global Palette {
    in-out property <color> primary: blue;

    match primary {
//  >error{A global component cannot have match elements}
        Colors.blue: Rectangle { background: orange; }
        Colors.red: Rectangle { background: green; }
        *: Rectangle { background: black; }
    }
//  <error{A global component cannot have match elements}
}

Zero Cases

A match element with zero cases will now produce a compiler error.

export component EmptyMatchElement {
    in-out property <int> xx: 0;
    match x {

    }
//  ^error{Expected at least one case}
    in-out property <int> yy: 20;
}

Duplicate Cases

A match element with two cases that resolve to the same value will now produce a compiler error.

export component DuplicateCaseWithInt {
    in-out property <int> xx: 0;

    match xx {
        0: Rectangle { }
        0: Rectangle { }
//      ^error{Duplicate case value}
        1: Rectangle { }
    }
}
export component DuplicateCaseWithCast {
    in-out property <int> xx: 0;

    match xx {
        2: Rectangle { }
        2.0: Rectangle { }
//      > <error{Duplicate case value}
        *: Rectangle { }
    }
}

Exhaustiveness

A type that can be covered completely (bool, enum) must do so, otherwise a * case is required. All other types require the * case.

enum Nums {
    one,
    two,
    three,
}

export component ExhaustiveEnum {
    in-out property <Nums> num: Nums.one;

    match num {
        Nums.one: Rectangle { }
        Nums.two: Rectangle { }
        Nums.three: Rectangle { }
    }
}

export component NonExhaustiveEnum {
    in-out property <Nums> num: Nums.one;

    match num {
//        >  <error{Non-exhaustive match on enum Nums: missing two, three}
        Nums.one: Rectangle { }
    }
}
export component NonExhaustiveInt {
    in-out property <int> num: 0;

    match num {
//        >  <error{Non-exhaustive match on int: a '*' case is required}
        0: Rectangle { }
        1: Rectangle { }
    }
}

export component WildcardInt {
    in-out property <int> num: 0;

    match num {
        0: Rectangle { }
        1: Rectangle { }
        *: Rectangle { }
    }
}

Unconditional Wildcard Cases

Added support for an empty match element with only a wildcard case to unconditionally render the wildcard case element.

export component UnconditionalWildcard{
   in-out property <int> num: 0;

    match num {
        *: Rectangle { }
    }
}

Single Error Outputs

Fixed issue with the same error message being reported multiple times when a * case is present.

export component CastWithWildcard {
    in property <int> num: 42;
    in property <int> num_2: 42;
    in property <string> str_num: "42";

    match num {
        42: Rectangle { }
        "42": Rectangle { }
//      >  <error{Cannot convert string to int}
        str_num: Rectangle { }
//      >     <error{Cannot convert string to int}
//      >     <^error{Cases must be literal values}
        *: Rectangle { }
    }
}

Updated parser tree grammar to recognize an empty element for
a wildcard case as valid syntax, which previously it did not.
Updated compiler to leave an error message when a match element has no cases. Updated basic syntax test to verify proper behavior.
Comment thread internal/compiler/passes/resolving.rs
Comment thread internal/compiler/object_tree.rs Outdated
@0x6e
0x6e requested review from ogoffart and tronical July 29, 2026 13:49

@ogoffart ogoffart left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the match-elements.mdx to reflect the changes.

Comment thread internal/compiler/passes/resolving.rs
Comment thread editors/tree-sitter-slint/grammar.js Outdated
Comment thread internal/compiler/object_tree.rs Outdated
Comment thread internal/compiler/object_tree.rs Outdated
Found and updated all references of "match statement/expression" to "match element", using consistent terminology accross messages.
Updated syntax tests to output proper error messages with consistent messages.
Created MatchElementInfo struct to handle and track match element resolution, initially to target duplicated error messages with a wildcard case.
Created and ran new test verifying proper error messages while using non-literal cases with a wildcard present
Created new MatchCaseInfo struct to keep track of the syntax nodes of a match element to ensure that repeated cases produce a compiler error.
Created new match element syntax test to verify proper compiler errors when using a duplicate case in a match element.
Tests verify functionality on a variety of types and type coercion.
Created check_exhaustiveness helper function to detect whether
a case is exhaustively represented in a match element. Enums
and booleans may be covered entirely without a wildcard case,
and any other type must have a wildcard case. Created new test
to verify error outputs with non-exhaustive match element cases for
various data tyes and updated previous match element syntax tests to
be fully exhaustive.
…ildcard case

Updated parser to no longer produce a compiler error if there is only a wildcard
case within a match element. Updated object tree to anticipate this case and unconditionally
render the element. Reran syntax tests to remove previous error messages and
added a new driver test to verify functionality.
@nichturner
nichturner force-pushed the feature/initial-match-diagnostics-improvements branch from 8dc6914 to df6c134 Compare August 3, 2026 17:40
@nichturner
nichturner requested a review from ogoffart August 3, 2026 17:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants