Skip to content

Commit 9057c3f

Browse files
committed
Auto merge of #127968 - fmease:upd-jsondocck-directive-style, r=GuillaumeGomez
Update jsondocck directives to follow ui_test-style Context: Comment chain in #125813. Follow-up to #126788. Use the same temporary approach of "double parsing" until we figure out how we want to support compiletest/ui_test directive "add-ons" for child test runners like HtmlDocCk and JsonDocCk. I didn't touch much of jsondocck because I want to refactor it some other time (for robustness, maintainability and better diagnostics; basically by following a similar design of my WIP HtmlDocCk-next, cc #125780). r? `@GuillaumeGomez`
2 parents ff4b398 + 633f41d commit 9057c3f

File tree

131 files changed

+1095
-1090
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

131 files changed

+1095
-1090
lines changed

Diff for: src/etc/htmldocck.py

+1
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ def filter_line(line):
266266
# To prevent duplicating the list of commmands between `compiletest` and `htmldocck`, we put
267267
# it into a common file which is included in rust code and parsed here.
268268
# FIXME: This setup is temporary until we figure out how to improve this situation.
269+
# See <https://github.com/rust-lang/rust/issues/125813#issuecomment-2141953780>.
269270
KNOWN_DIRECTIVE_NAMES = get_known_directive_names()
270271

271272
LINE_PATTERN = re.compile(r'''

Diff for: src/tools/compiletest/src/header.rs

+22-9
Original file line numberDiff line numberDiff line change
@@ -723,12 +723,13 @@ pub fn line_directive<'line>(
723723
}
724724
}
725725

726-
// To prevent duplicating the list of commmands between `compiletest` and `htmldocck`, we put
727-
// it into a common file which is included in rust code and parsed here.
726+
// To prevent duplicating the list of commmands between `compiletest`,`htmldocck` and `jsondocck`,
727+
// we put it into a common file which is included in rust code and parsed here.
728728
// FIXME: This setup is temporary until we figure out how to improve this situation.
729+
// See <https://github.com/rust-lang/rust/issues/125813#issuecomment-2141953780>.
729730
include!("command-list.rs");
730731

731-
const KNOWN_RUSTDOC_DIRECTIVE_NAMES: &[&str] = &[
732+
const KNOWN_HTMLDOCCK_DIRECTIVE_NAMES: &[&str] = &[
732733
"count",
733734
"!count",
734735
"files",
@@ -747,6 +748,9 @@ const KNOWN_RUSTDOC_DIRECTIVE_NAMES: &[&str] = &[
747748
"!snapshot",
748749
];
749750

751+
const KNOWN_JSONDOCCK_DIRECTIVE_NAMES: &[&str] =
752+
&["count", "!count", "has", "!has", "is", "!is", "ismany", "!ismany", "set", "!set"];
753+
750754
/// The broken-down contents of a line containing a test header directive,
751755
/// which [`iter_header`] passes to its callback function.
752756
///
@@ -783,17 +787,26 @@ pub(crate) struct CheckDirectiveResult<'ln> {
783787

784788
pub(crate) fn check_directive<'a>(
785789
directive_ln: &'a str,
786-
is_rustdoc: bool,
790+
mode: Mode,
787791
original_line: &str,
788792
) -> CheckDirectiveResult<'a> {
789793
let (directive_name, post) = directive_ln.split_once([':', ' ']).unwrap_or((directive_ln, ""));
790794

791795
let trailing = post.trim().split_once(' ').map(|(pre, _)| pre).unwrap_or(post);
792796
let is_known = |s: &str| {
793797
KNOWN_DIRECTIVE_NAMES.contains(&s)
794-
|| (is_rustdoc
795-
&& original_line.starts_with("//@")
796-
&& KNOWN_RUSTDOC_DIRECTIVE_NAMES.contains(&s))
798+
|| match mode {
799+
Mode::Rustdoc | Mode::RustdocJson => {
800+
original_line.starts_with("//@")
801+
&& match mode {
802+
Mode::Rustdoc => KNOWN_HTMLDOCCK_DIRECTIVE_NAMES,
803+
Mode::RustdocJson => KNOWN_JSONDOCCK_DIRECTIVE_NAMES,
804+
_ => unreachable!(),
805+
}
806+
.contains(&s)
807+
}
808+
_ => false,
809+
}
797810
};
798811
let trailing_directive = {
799812
// 1. is the directive name followed by a space? (to exclude `:`)
@@ -875,7 +888,7 @@ fn iter_header(
875888
let directive_ln = non_revisioned_directive_line.trim();
876889

877890
let CheckDirectiveResult { is_known_directive, trailing_directive, .. } =
878-
check_directive(directive_ln, mode == Mode::Rustdoc, ln);
891+
check_directive(directive_ln, mode, ln);
879892

880893
if !is_known_directive {
881894
*poisoned = true;
@@ -928,7 +941,7 @@ fn iter_header(
928941
let rest = rest.trim_start();
929942

930943
let CheckDirectiveResult { is_known_directive, directive_name, .. } =
931-
check_directive(rest, mode == Mode::Rustdoc, ln);
944+
check_directive(rest, mode, ln);
932945

933946
if is_known_directive {
934947
*poisoned = true;

Diff for: src/tools/jsondocck/src/main.rs

+29-38
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,17 @@ impl CommandKind {
6666
};
6767

6868
if !count {
69-
print_err(&format!("Incorrect number of arguments to `@{}`", self), lineno);
69+
print_err(&format!("Incorrect number of arguments to `{}`", self), lineno);
7070
return false;
7171
}
7272

7373
if let CommandKind::Count = self {
7474
if args[1].parse::<usize>().is_err() {
7575
print_err(
76-
&format!("Second argument to @count must be a valid usize (got `{}`)", args[1]),
76+
&format!(
77+
"Second argument to `count` must be a valid usize (got `{}`)",
78+
args[1]
79+
),
7780
lineno,
7881
);
7982
return false;
@@ -101,7 +104,8 @@ static LINE_PATTERN: OnceLock<Regex> = OnceLock::new();
101104
fn line_pattern() -> Regex {
102105
RegexBuilder::new(
103106
r#"
104-
\s(?P<invalid>!?)@(?P<negated>!?)
107+
//@\s+
108+
(?P<negated>!?)
105109
(?P<cmd>[A-Za-z]+(?:-[A-Za-z]+)*)
106110
(?P<args>.*)$
107111
"#,
@@ -116,6 +120,10 @@ fn print_err(msg: &str, lineno: usize) {
116120
eprintln!("Invalid command: {} on line {}", msg, lineno)
117121
}
118122

123+
// FIXME: This setup is temporary until we figure out how to improve this situation.
124+
// See <https://github.com/rust-lang/rust/issues/125813#issuecomment-2141953780>.
125+
include!(concat!(env!("CARGO_MANIFEST_DIR"), "/../compiletest/src/command-list.rs"));
126+
119127
/// Get a list of commands from a file. Does the work of ensuring the commands
120128
/// are syntactically valid.
121129
fn get_commands(template: &str) -> Result<Vec<Command>, ()> {
@@ -132,36 +140,22 @@ fn get_commands(template: &str) -> Result<Vec<Command>, ()> {
132140
};
133141

134142
let negated = cap.name("negated").unwrap().as_str() == "!";
135-
let cmd = cap.name("cmd").unwrap().as_str();
136143

137-
let cmd = match cmd {
144+
let cmd = match cap.name("cmd").unwrap().as_str() {
138145
"has" => CommandKind::Has,
139146
"count" => CommandKind::Count,
140147
"is" => CommandKind::Is,
141148
"ismany" => CommandKind::IsMany,
142149
"set" => CommandKind::Set,
143-
_ => {
144-
print_err(&format!("Unrecognized command name `@{}`", cmd), lineno);
150+
// FIXME: See the comment above the `include!(...)`.
151+
cmd if KNOWN_DIRECTIVE_NAMES.contains(&cmd) => continue,
152+
cmd => {
153+
print_err(&format!("Unrecognized command name `{cmd}`"), lineno);
145154
errors = true;
146155
continue;
147156
}
148157
};
149158

150-
if let Some(m) = cap.name("invalid") {
151-
if m.as_str() == "!" {
152-
print_err(
153-
&format!(
154-
"`!@{0}{1}`, (help: try with `@!{1}`)",
155-
if negated { "!" } else { "" },
156-
cmd,
157-
),
158-
lineno,
159-
);
160-
errors = true;
161-
continue;
162-
}
163-
}
164-
165159
let args = cap.name("args").map_or(Some(vec![]), |m| shlex::split(m.as_str()));
166160

167161
let args = match args {
@@ -197,19 +191,19 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
197191
let result = match command.kind {
198192
CommandKind::Has => {
199193
match command.args.len() {
200-
// @has <jsonpath> = check path exists
194+
// `has <jsonpath>`: Check that `jsonpath` exists.
201195
1 => {
202196
let val = cache.value();
203197
let results = select(val, &command.args[0]).unwrap();
204198
!results.is_empty()
205199
}
206-
// @has <jsonpath> <value> = check *any* item matched by path equals value
200+
// `has <jsonpath> <value>`: Check *any* item matched by `jsonpath` equals `value`.
207201
2 => {
208202
let val = cache.value().clone();
209203
let results = select(&val, &command.args[0]).unwrap();
210204
let pat = string_to_value(&command.args[1], cache);
211205
let has = results.contains(&pat.as_ref());
212-
// Give better error for when @has check fails
206+
// Give better error for when `has` check fails.
213207
if !command.negated && !has {
214208
return Err(CkError::FailedCheck(
215209
format!(
@@ -227,16 +221,16 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
227221
_ => unreachable!(),
228222
}
229223
}
224+
// `ismany <path> <jsonpath> <value...>`
230225
CommandKind::IsMany => {
231-
// @ismany <path> <jsonpath> <value>...
226+
assert!(!command.negated, "`ismany` may not be negated");
232227
let (query, values) = if let [query, values @ ..] = &command.args[..] {
233228
(query, values)
234229
} else {
235230
unreachable!("Checked in CommandKind::validate")
236231
};
237232
let val = cache.value();
238233
let got_values = select(val, &query).unwrap();
239-
assert!(!command.negated, "`@!ismany` is not supported");
240234

241235
// Serde json doesn't implement Ord or Hash for Value, so we must
242236
// use a Vec here. While in theory that makes setwize equality
@@ -265,8 +259,8 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
265259
}
266260
true
267261
}
262+
// `count <jsonpath> <count>`: Check that `jsonpath` matches exactly `count` times.
268263
CommandKind::Count => {
269-
// @count <jsonpath> <count> = Check that the jsonpath matches exactly [count] times
270264
assert_eq!(command.args.len(), 2);
271265
let expected: usize = command.args[1].parse().unwrap();
272266
let val = cache.value();
@@ -287,8 +281,8 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
287281
eq
288282
}
289283
}
284+
// `has <jsonpath> <value>`: Check` *exactly one* item matched by `jsonpath`, and it equals `value`.
290285
CommandKind::Is => {
291-
// @has <jsonpath> <value> = check *exactly one* item matched by path, and it equals value
292286
assert_eq!(command.args.len(), 2);
293287
let val = cache.value().clone();
294288
let results = select(&val, &command.args[0]).unwrap();
@@ -308,16 +302,17 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
308302
is
309303
}
310304
}
305+
// `set <name> = <jsonpath>`
311306
CommandKind::Set => {
312-
// @set <name> = <jsonpath>
307+
assert!(!command.negated, "`set` may not be negated");
313308
assert_eq!(command.args.len(), 3);
314309
assert_eq!(command.args[1], "=", "Expected an `=`");
315310
let val = cache.value().clone();
316311
let results = select(&val, &command.args[2]).unwrap();
317312
assert_eq!(
318313
results.len(),
319314
1,
320-
"Expected 1 match for `{}` (because of @set): matched to {:?}",
315+
"Expected 1 match for `{}` (because of `set`): matched to {:?}",
321316
command.args[2],
322317
results
323318
);
@@ -330,7 +325,7 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
330325
}
331326
_ => {
332327
panic!(
333-
"Got multiple results in `@set` for `{}`: {:?}",
328+
"Got multiple results in `set` for `{}`: {:?}",
334329
&command.args[2], results,
335330
);
336331
}
@@ -341,18 +336,14 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
341336
if result == command.negated {
342337
if command.negated {
343338
Err(CkError::FailedCheck(
344-
format!(
345-
"`@!{} {}` matched when it shouldn't",
346-
command.kind,
347-
command.args.join(" ")
348-
),
339+
format!("`!{} {}` matched when it shouldn't", command.kind, command.args.join(" ")),
349340
command,
350341
))
351342
} else {
352343
// FIXME: In the future, try 'peeling back' each step, and see at what level the match failed
353344
Err(CkError::FailedCheck(
354345
format!(
355-
"`@{} {}` didn't match when it should",
346+
"`{} {}` didn't match when it should",
356347
command.kind,
357348
command.args.join(" ")
358349
),

Diff for: tests/rustdoc-json/assoc_items.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,32 @@
33
pub struct Simple;
44

55
impl Simple {
6-
// @has "$.index[*][?(@.name=='CONSTANT')].inner.assoc_const"
6+
//@ has "$.index[*][?(@.name=='CONSTANT')].inner.assoc_const"
77
pub const CONSTANT: usize = 0;
88
}
99

1010
pub trait EasyToImpl {
11-
// @has "$.index[*][?(@.docs=='ToDeclare trait')].inner.assoc_type"
12-
// @is "$.index[*][?(@.docs=='ToDeclare trait')].inner.assoc_type.default" null
13-
// @is "$.index[*][?(@.docs=='ToDeclare trait')].inner.assoc_type.bounds" []
11+
//@ has "$.index[*][?(@.docs=='ToDeclare trait')].inner.assoc_type"
12+
//@ is "$.index[*][?(@.docs=='ToDeclare trait')].inner.assoc_type.default" null
13+
//@ is "$.index[*][?(@.docs=='ToDeclare trait')].inner.assoc_type.bounds" []
1414
/// ToDeclare trait
1515
type ToDeclare;
16-
// @has "$.index[*][?(@.docs=='AN_ATTRIBUTE trait')].inner.assoc_const"
17-
// @is "$.index[*][?(@.docs=='AN_ATTRIBUTE trait')].inner.assoc_const.default" null
18-
// @is "$.index[*][?(@.docs=='AN_ATTRIBUTE trait')].inner.assoc_const.type.primitive" '"usize"'
16+
//@ has "$.index[*][?(@.docs=='AN_ATTRIBUTE trait')].inner.assoc_const"
17+
//@ is "$.index[*][?(@.docs=='AN_ATTRIBUTE trait')].inner.assoc_const.default" null
18+
//@ is "$.index[*][?(@.docs=='AN_ATTRIBUTE trait')].inner.assoc_const.type.primitive" '"usize"'
1919
/// AN_ATTRIBUTE trait
2020
const AN_ATTRIBUTE: usize;
2121
}
2222

2323
impl EasyToImpl for Simple {
24-
// @has "$.index[*][?(@.docs=='ToDeclare impl')].inner.assoc_type"
25-
// @is "$.index[*][?(@.docs=='ToDeclare impl')].inner.assoc_type.default.primitive" \"usize\"
24+
//@ has "$.index[*][?(@.docs=='ToDeclare impl')].inner.assoc_type"
25+
//@ is "$.index[*][?(@.docs=='ToDeclare impl')].inner.assoc_type.default.primitive" \"usize\"
2626
/// ToDeclare impl
2727
type ToDeclare = usize;
2828

29-
// @has "$.index[*][?(@.docs=='AN_ATTRIBUTE impl')].inner.assoc_const"
30-
// @is "$.index[*][?(@.docs=='AN_ATTRIBUTE impl')].inner.assoc_const.type.primitive" \"usize\"
31-
// @is "$.index[*][?(@.docs=='AN_ATTRIBUTE impl')].inner.assoc_const.default" \"12\"
29+
//@ has "$.index[*][?(@.docs=='AN_ATTRIBUTE impl')].inner.assoc_const"
30+
//@ is "$.index[*][?(@.docs=='AN_ATTRIBUTE impl')].inner.assoc_const.type.primitive" \"usize\"
31+
//@ is "$.index[*][?(@.docs=='AN_ATTRIBUTE impl')].inner.assoc_const.default" \"12\"
3232
/// AN_ATTRIBUTE impl
3333
const AN_ATTRIBUTE: usize = 12;
3434
}

Diff for: tests/rustdoc-json/assoc_type.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Regression test for <https://github.com/rust-lang/rust/issues/98547>.
22

3-
// @has "$.index[*][?(@.name=='Trait')]"
4-
// @has "$.index[*][?(@.name=='AssocType')]"
5-
// @has "$.index[*][?(@.name=='S')]"
6-
// @has "$.index[*][?(@.name=='S2')]"
3+
//@ has "$.index[*][?(@.name=='Trait')]"
4+
//@ has "$.index[*][?(@.name=='AssocType')]"
5+
//@ has "$.index[*][?(@.name=='S')]"
6+
//@ has "$.index[*][?(@.name=='S2')]"
77

88
pub trait Trait {
99
type AssocType;

Diff for: tests/rustdoc-json/blanket_impls.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#![no_std]
44

5-
// @has "$.index[*][?(@.name=='Error')].inner.assoc_type"
6-
// @has "$.index[*][?(@.name=='Error')].inner.assoc_type.default.resolved_path"
7-
// @has "$.index[*][?(@.name=='Error')].inner.assoc_type.default.resolved_path.name" \"Infallible\"
5+
//@ has "$.index[*][?(@.name=='Error')].inner.assoc_type"
6+
//@ has "$.index[*][?(@.name=='Error')].inner.assoc_type.default.resolved_path"
7+
//@ has "$.index[*][?(@.name=='Error')].inner.assoc_type.default.resolved_path.name" \"Infallible\"
88
pub struct ForBlanketTryFromImpl;

Diff for: tests/rustdoc-json/doc_hidden_failure.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ mod auto {
1111
}
1212
}
1313

14-
// @count "$.index[*][?(@.name=='builders')]" 1
15-
// @has "$.index[*][?(@.name == 'ActionRowBuilder')"]
14+
//@ count "$.index[*][?(@.name=='builders')]" 1
15+
//@ has "$.index[*][?(@.name == 'ActionRowBuilder')"]
1616
pub use auto::*;
1717

1818
pub mod builders {

Diff for: tests/rustdoc-json/enums/discriminant/basic.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#[repr(i8)]
22
pub enum Ordering {
3-
// @is "$.index[*][?(@.name=='Less')].inner.variant.discriminant.expr" '"-1"'
4-
// @is "$.index[*][?(@.name=='Less')].inner.variant.discriminant.value" '"-1"'
3+
//@ is "$.index[*][?(@.name=='Less')].inner.variant.discriminant.expr" '"-1"'
4+
//@ is "$.index[*][?(@.name=='Less')].inner.variant.discriminant.value" '"-1"'
55
Less = -1,
6-
// @is "$.index[*][?(@.name=='Equal')].inner.variant.discriminant.expr" '"0"'
7-
// @is "$.index[*][?(@.name=='Equal')].inner.variant.discriminant.value" '"0"'
6+
//@ is "$.index[*][?(@.name=='Equal')].inner.variant.discriminant.expr" '"0"'
7+
//@ is "$.index[*][?(@.name=='Equal')].inner.variant.discriminant.value" '"0"'
88
Equal = 0,
9-
// @is "$.index[*][?(@.name=='Greater')].inner.variant.discriminant.expr" '"1"'
10-
// @is "$.index[*][?(@.name=='Greater')].inner.variant.discriminant.value" '"1"'
9+
//@ is "$.index[*][?(@.name=='Greater')].inner.variant.discriminant.expr" '"1"'
10+
//@ is "$.index[*][?(@.name=='Greater')].inner.variant.discriminant.value" '"1"'
1111
Greater = 1,
1212
}

0 commit comments

Comments
 (0)