rust-lang/rust · error · InvalidCheck

Invalid number of {} arguments

Error message

Invalid number of {} arguments

What it means

Raised by htmldocck (the rustdoc HTML test harness) when a 'has', 'matches', 'hasraw', or 'matchesraw' directive is written with an unsupported argument count. 'hasraw'/'matchesraw' require exactly 2 args (path + pattern); 'has'/'matches' require exactly 3 (path + xpath + pattern). Any other count falls through to this InvalidCheck, which is a hard error in the test definition, not a check failure.

Source

Thrown at src/etc/htmldocck.py:547

                    cache.get_file(c.args[0])
                    ret = True
                except FailedCheck as err:
                    cerr = str(err)
                    ret = False
            # hasraw/matchesraw <path> <pat> = string test
            elif len(c.args) == 2 and "raw" in c.cmd:
                cerr = "`PATTERN` did not match"
                if c.negated:
                    cerr = "`PATTERN` unexpectedly matched"
                ret = check_string(cache.get_file(c.args[0]), c.args[1], regexp)
            # has/matches <path> <pat> <match> = XML tree test
            elif len(c.args) == 3 and "raw" not in c.cmd:
                cerr = "`XPATH PATTERN` did not match"
                if c.negated:
                    cerr = "`XPATH PATTERN` unexpectedly matched"
                ret = get_nb_matching_elements(cache, c, regexp, True) != 0
            else:
                raise InvalidCheck("Invalid number of {} arguments".format(c.cmd))

        elif c.cmd == "files":  # check files in given folder
            if len(c.args) != 2:  # files <folder path> <file list>
                raise InvalidCheck("Invalid number of {} arguments".format(c.cmd))
            elif c.negated:
                raise InvalidCheck("{} doesn't support negative check".format(c.cmd))
            ret = check_files_in_folder(c, cache, c.args[0], c.args[1])

        elif c.cmd == "count":  # count test
            if len(c.args) == 3:  # count <path> <pat> <count> = count test
                expected = int(c.args[2])
                found = get_tree_count(cache.get_tree(c.args[0]), c.args[1])
                cerr = "Expected {} occurrences but found {}".format(expected, found)
                ret = expected == found
            elif len(c.args) == 4:  # count <path> <pat> <text> <count> = count test
                expected = int(c.args[3])
                found = get_nb_matching_elements(cache, c, False, False)
                cerr = "Expected {} occurrences but found {}".format(expected, found)

View on GitHub (pinned to 7088e4b63a)

Solutions

  1. Count the directive's arguments: hasraw/matchesraw = path + pattern (2); has/matches = path + xpath + pattern (3).
  2. If you meant a plain substring test, switch to hasraw/matchesraw and drop the xpath.
  3. If you meant an HTML tree test, add the xpath as the middle argument.

Example fix

// before
//@ has foo.html "some text"
// after
//@ hasraw foo.html "some text"
Defensive patterns

Strategy: validation

Validate before calling

# Validate a has/matches/hasraw/matchesraw directive before committing it to a test
raw_cmds = {"hasraw", "matchesraw"}
if cmd in raw_cmds and len(args) != 2:
    raise ValueError(f"{cmd} requires exactly 2 args (path, pattern), got {len(args)}")
if cmd in {"has", "matches"} and len(args) != 3:
    raise ValueError(f"{cmd} requires exactly 3 args (path, xpath, pattern), got {len(args)}")

Prevention

When it happens

Trigger: Writing `//@ has foo.html` (1 arg), `//@ matches a.html xpath pattern extra` (4 args), or `//@ hasraw a.html pat extra` (3 args) in a `.rs` rustdoc test. The directive is parsed by CustomCheck and this branch rejects every arity other than 2-for-raw and 3-for-tree.

Common situations: Test authors confuse has (needs an XPATH) with hasraw (plain string). Copy-pasting a compiletest directive into a rustdoc test, or forgetting the XPATH argument after renaming a tree check to a raw check.

Related errors


AI-assisted analysis of rust-lang/rust@7088e4b63a (2026-08-10). Data as JSON: /api/errors/0fe3357b28fa72fd. Report an issue: GitHub.