Skip to content

Compiletest: Simplify {Html,Json}DocCk directive handling #143850

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ Similar to shell commands,
directives can extend across multiple lines if their last char is `\`.
In this case, the start of the next line should be `//`, with no `@`.

Similar to compiletest directives, besides a space you can also use a colon `:` to separate
the directive name and the arguments, however a space is preferred for HtmlDocCk directives.

Use the special string `{{channel}}` in XPaths, `PATTERN` arguments and [snapshot files](#snapshot)
if you'd like to refer to the URL `https://doc.rust-lang.org/CHANNEL` where `CHANNEL` refers to the
current release channel (e.g, `stable` or `nightly`).
Expand Down
55 changes: 8 additions & 47 deletions src/etc/htmldocck.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,48 +147,17 @@ def concat_multi_lines(f):
print_err(lineno, line, "Trailing backslash at the end of the file")


def get_known_directive_names():
def filter_line(line):
line = line.strip()
return line.startswith('"') and (line.endswith('",') or line.endswith('"'))

# Equivalent to `src/tools/compiletest/src/header.rs` constant of the same name.
with open(
os.path.join(
# We go back to `src`.
os.path.dirname(os.path.dirname(__file__)),
"tools/compiletest/src/directive-list.rs",
),
"r",
encoding="utf8",
) as fd:
content = fd.read()
return [
line.strip().replace('",', "").replace('"', "")
for line in content.split("\n")
if filter_line(line)
]


# To prevent duplicating the list of commmands between `compiletest` and `htmldocck`, we put
# it into a common file which is included in rust code and parsed here.
# FIXME: This setup is temporary until we figure out how to improve this situation.
# See <https://github.com/rust-lang/rust/issues/125813#issuecomment-2141953780>.
KNOWN_DIRECTIVE_NAMES = get_known_directive_names()

LINE_PATTERN = re.compile(
r"""
//@\s+
(?P<negated>!?)(?P<cmd>[A-Za-z0-9]+(?:-[A-Za-z0-9]+)*)
(?P<args>.*)$
(?P<negated>!?)(?P<cmd>.+?)
(?:[\s:](?P<args>.*))?$
""",
re.X | re.UNICODE,
)

DEPRECATED_LINE_PATTERN = re.compile(
r"""
//\s+@
""",
r"//\s+@",
re.X | re.UNICODE,
)

Expand All @@ -209,12 +178,7 @@ def get_commands(template):

cmd = m.group("cmd")
negated = m.group("negated") == "!"
if not negated and cmd in KNOWN_DIRECTIVE_NAMES:
continue
args = m.group("args")
if args and not args[:1].isspace():
print_err(lineno, line, "Invalid template syntax")
continue
Comment on lines -215 to -217
Copy link
Member Author

@fmease fmease Jul 12, 2025

Choose a reason for hiding this comment

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

In a roundabout way this used to check that the separator between the directive and its args was a space because the capture group args used to immediately follow the directive name.

args = m.group("args") or ""
try:
args = shlex.split(args)
except UnicodeEncodeError:
Expand Down Expand Up @@ -632,14 +596,11 @@ def check_command(c, cache):
else:
raise InvalidCheck("Invalid number of {} arguments".format(c.cmd))

elif c.cmd == "valid-html":
raise InvalidCheck("Unimplemented valid-html")

elif c.cmd == "valid-links":
raise InvalidCheck("Unimplemented valid-links")

else:
raise InvalidCheck("Unrecognized {}".format(c.cmd))
# Ignore unknown directives as they might be compiletest directives
# since they share the same `//@` prefix by convention. In any case,
# compiletest rejects unknown directives for us.
return

if ret == c.negated:
raise FailedCheck(cerr)
Expand Down
260 changes: 0 additions & 260 deletions src/tools/compiletest/src/directive-list.rs

This file was deleted.

Loading
Loading