From 4de9c6d4913a02b5ce19a14e9e2ab0c46ceea771 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Thu, 16 Feb 2023 15:32:38 -0700 Subject: [PATCH 01/15] rustdoc: search by macro when query ends with `!` Related to #96399 --- src/librustdoc/html/static/js/search.js | 28 ++++++++++++--- tests/rustdoc-js-std/parser-errors.js | 20 +++++++++++ tests/rustdoc-js-std/parser-filter.js | 47 ++++++++++++++++++++++++- tests/rustdoc-js-std/parser-ident.js | 40 +++++++++++---------- tests/rustdoc-js/macro-search.js | 10 ++++++ tests/rustdoc-js/macro-search.rs | 10 ++++++ 6 files changed, 132 insertions(+), 23 deletions(-) create mode 100644 tests/rustdoc-js/macro-search.js create mode 100644 tests/rustdoc-js/macro-search.rs diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index 1e6c94d29ba47..6a8e93a243681 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -300,20 +300,21 @@ function initSearch(rawSearchIndex) { * @return {integer} */ function getIdentEndPosition(parserState) { + const start = parserState.pos; let end = parserState.pos; - let foundExclamation = false; + let foundExclamation = -1; while (parserState.pos < parserState.length) { const c = parserState.userQuery[parserState.pos]; if (!isIdentCharacter(c)) { if (c === "!") { - if (foundExclamation) { + if (foundExclamation !== -1) { throw new Error("Cannot have more than one `!` in an ident"); } else if (parserState.pos + 1 < parserState.length && isIdentCharacter(parserState.userQuery[parserState.pos + 1]) ) { throw new Error("`!` can only be at the end of an ident"); } - foundExclamation = true; + foundExclamation = parserState.pos; } else if (isErrorCharacter(c)) { throw new Error(`Unexpected \`${c}\``); } else if ( @@ -326,9 +327,18 @@ function initSearch(rawSearchIndex) { if (!isPathStart(parserState)) { break; } + if (foundExclamation !== -1) { + if (start <= (end - 2)) { + throw new Error("Cannot have associated items in macros"); + } else { + // if start == end - 1, we got the never type + // while the never type has no associated macros, we still + // can parse a path like that + foundExclamation = -1; + } + } // Skip current ":". parserState.pos += 1; - foundExclamation = false; } else { throw new Error(`Unexpected \`${c}\``); } @@ -336,6 +346,16 @@ function initSearch(rawSearchIndex) { parserState.pos += 1; end = parserState.pos; } + // if start == end - 1, we got the never type + if (foundExclamation !== -1 && start <= (end - 2)) { + if (parserState.typeFilter === null) { + parserState.typeFilter = "macro"; + } else if (parserState.typeFilter !== "macro") { + throw new Error(`Invalid search type: macro \`!\` and ` + + `\`${parserState.typeFilter}\` both specified`); + } + end = foundExclamation; + } return end; } diff --git a/tests/rustdoc-js-std/parser-errors.js b/tests/rustdoc-js-std/parser-errors.js index dc42031e05f2f..f82a2472063ce 100644 --- a/tests/rustdoc-js-std/parser-errors.js +++ b/tests/rustdoc-js-std/parser-errors.js @@ -37,6 +37,8 @@ const QUERY = [ "mod : :", "a!a", "a!!", + "mod:a!", + "a!::a", ]; const PARSED = [ @@ -382,4 +384,22 @@ const PARSED = [ userQuery: "a!!", error: 'Cannot have more than one `!` in an ident', }, + { + elems: [], + foundElems: 0, + original: "mod:a!", + returned: [], + typeFilter: -1, + userQuery: "mod:a!", + error: 'Invalid search type: macro `!` and `mod` both specified', + }, + { + elems: [], + foundElems: 0, + original: "a!::a", + returned: [], + typeFilter: -1, + userQuery: "a!::a", + error: 'Cannot have associated items in macros', + }, ]; diff --git a/tests/rustdoc-js-std/parser-filter.js b/tests/rustdoc-js-std/parser-filter.js index e5a87a415ac47..01f65b478f8e9 100644 --- a/tests/rustdoc-js-std/parser-filter.js +++ b/tests/rustdoc-js-std/parser-filter.js @@ -1,4 +1,4 @@ -const QUERY = ['fn:foo', 'enum : foo', 'macro:foo']; +const QUERY = ['fn:foo', 'enum : foo', 'macro:foo', 'macro!', 'macro:mac!', 'a::mac!']; const PARSED = [ { @@ -40,4 +40,49 @@ const PARSED = [ userQuery: "macro:foo", error: "Unexpected `:`", }, + { + elems: [{ + name: "macro", + fullPath: ["macro"], + pathWithoutLast: [], + pathLast: "macro", + generics: [], + }], + foundElems: 1, + original: "macro!", + returned: [], + typeFilter: 14, + userQuery: "macro!", + error: null, + }, + { + elems: [{ + name: "mac", + fullPath: ["mac"], + pathWithoutLast: [], + pathLast: "mac", + generics: [], + }], + foundElems: 1, + original: "macro:mac!", + returned: [], + typeFilter: 14, + userQuery: "macro:mac!", + error: null, + }, + { + elems: [{ + name: "a::mac", + fullPath: ["a", "mac"], + pathWithoutLast: ["a"], + pathLast: "mac", + generics: [], + }], + foundElems: 1, + original: "a::mac!", + returned: [], + typeFilter: 14, + userQuery: "a::mac!", + error: null, + }, ]; diff --git a/tests/rustdoc-js-std/parser-ident.js b/tests/rustdoc-js-std/parser-ident.js index 4b5ab01ac761b..6c17d00f16edc 100644 --- a/tests/rustdoc-js-std/parser-ident.js +++ b/tests/rustdoc-js-std/parser-ident.js @@ -3,6 +3,7 @@ const QUERY = [ "!", "a!", "a!::b", + "!::b", "a!::b!", ]; @@ -47,47 +48,50 @@ const PARSED = [ }, { elems: [{ - name: "a!", - fullPath: ["a!"], + name: "a", + fullPath: ["a"], pathWithoutLast: [], - pathLast: "a!", + pathLast: "a", generics: [], }], foundElems: 1, original: "a!", returned: [], - typeFilter: -1, + typeFilter: 14, userQuery: "a!", error: null, }, { - elems: [{ - name: "a!::b", - fullPath: ["a!", "b"], - pathWithoutLast: ["a!"], - pathLast: "b", - generics: [], - }], - foundElems: 1, + elems: [], + foundElems: 0, original: "a!::b", returned: [], typeFilter: -1, userQuery: "a!::b", - error: null, + error: "Cannot have associated items in macros", }, { elems: [{ - name: "a!::b!", - fullPath: ["a!", "b!"], - pathWithoutLast: ["a!"], - pathLast: "b!", + name: "!::b", + fullPath: ["!", "b"], + pathWithoutLast: ["!"], + pathLast: "b", generics: [], }], foundElems: 1, + original: "!::b", + returned: [], + typeFilter: -1, + userQuery: "!::b", + error: null, + }, + { + elems: [], + foundElems: 0, original: "a!::b!", returned: [], typeFilter: -1, userQuery: "a!::b!", - error: null, + error: "Cannot have associated items in macros", }, ]; diff --git a/tests/rustdoc-js/macro-search.js b/tests/rustdoc-js/macro-search.js new file mode 100644 index 0000000000000..2b179ce146bf0 --- /dev/null +++ b/tests/rustdoc-js/macro-search.js @@ -0,0 +1,10 @@ +// exact-check + +const QUERY = 'abracadabra!'; + +const EXPECTED = { + 'others': [ + { 'path': 'macro_search', 'name': 'abracadabra' }, + { 'path': 'macro_search', 'name': 'abracadabra_b' }, + ], +}; diff --git a/tests/rustdoc-js/macro-search.rs b/tests/rustdoc-js/macro-search.rs new file mode 100644 index 0000000000000..dc397490cf583 --- /dev/null +++ b/tests/rustdoc-js/macro-search.rs @@ -0,0 +1,10 @@ +#[macro_export] +macro_rules! abracadabra { + () => {} +} +#[macro_export] +macro_rules! abracadabra_b { + () => {} +} +pub fn abracadabra() {} +pub fn abracadabra_c() {} From d963318c1d04b2250f78f7fa43a8ee1173110d9f Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Thu, 16 Feb 2023 19:22:03 -0700 Subject: [PATCH 02/15] Correct eslint warning --- src/librustdoc/html/static/js/search.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index 6a8e93a243681..ae15155341d25 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -351,7 +351,7 @@ function initSearch(rawSearchIndex) { if (parserState.typeFilter === null) { parserState.typeFilter = "macro"; } else if (parserState.typeFilter !== "macro") { - throw new Error(`Invalid search type: macro \`!\` and ` + + throw new Error("Invalid search type: macro `!` and " + `\`${parserState.typeFilter}\` both specified`); } end = foundExclamation; From 07cf219722420deb87b0186a64d26eed25449e66 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Tue, 21 Feb 2023 21:52:44 -0700 Subject: [PATCH 03/15] Update how-to-read-rustdoc.md --- src/doc/rustdoc/src/how-to-read-rustdoc.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/doc/rustdoc/src/how-to-read-rustdoc.md b/src/doc/rustdoc/src/how-to-read-rustdoc.md index d666d54b31579..28a004a92531a 100644 --- a/src/doc/rustdoc/src/how-to-read-rustdoc.md +++ b/src/doc/rustdoc/src/how-to-read-rustdoc.md @@ -84,6 +84,9 @@ When typing in the search bar, you can prefix your search term with a type followed by a colon (such as `mod:`) to restrict the results to just that kind of item. (The available items are listed in the help popup.) +Searching for `println!` will search for a macro named `println`, just like +searching for `macro:println` does. + ### Changing displayed theme You can change the displayed theme by opening the settings menu (the gear From 317be6172af280ab0f8565f2fb5ffa2bcd3b2712 Mon Sep 17 00:00:00 2001 From: Pietro Albini Date: Thu, 23 Feb 2023 11:53:32 +0100 Subject: [PATCH 04/15] make --open work on all books --- src/bootstrap/doc.rs | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/bootstrap/doc.rs b/src/bootstrap/doc.rs index 7f8aa2573ddb3..cc80763ef4495 100644 --- a/src/bootstrap/doc.rs +++ b/src/bootstrap/doc.rs @@ -62,6 +62,7 @@ macro_rules! book { target: self.target, name: INTERNER.intern_str($book_name), src: INTERNER.intern_path(builder.src.join($path)), + parent: Some(self), }) } } @@ -119,18 +120,20 @@ impl Step for UnstableBook { target: self.target, name: INTERNER.intern_str("unstable-book"), src: INTERNER.intern_path(builder.md_doc_out(self.target).join("unstable-book")), + parent: Some(self), }) } } #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] -struct RustbookSrc { +struct RustbookSrc { target: TargetSelection, name: Interned, src: Interned, + parent: Option

, } -impl Step for RustbookSrc { +impl Step for RustbookSrc

{ type Output = (); fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { @@ -152,13 +155,18 @@ impl Step for RustbookSrc { let index = out.join("index.html"); let rustbook = builder.tool_exe(Tool::Rustbook); let mut rustbook_cmd = builder.tool_cmd(Tool::Rustbook); - if builder.config.dry_run() || up_to_date(&src, &index) && up_to_date(&rustbook, &index) { - return; + + if !builder.config.dry_run() && !(up_to_date(&src, &index) || up_to_date(&rustbook, &index)) + { + builder.info(&format!("Rustbook ({}) - {}", target, name)); + let _ = fs::remove_dir_all(&out); + + builder.run(rustbook_cmd.arg("build").arg(&src).arg("-d").arg(out)); } - builder.info(&format!("Rustbook ({}) - {}", target, name)); - let _ = fs::remove_dir_all(&out); - builder.run(rustbook_cmd.arg("build").arg(&src).arg("-d").arg(out)); + if self.parent.is_some() { + builder.maybe_open_in_browser::

(index) + } } } @@ -205,6 +213,7 @@ impl Step for TheBook { target, name: INTERNER.intern_str("book"), src: INTERNER.intern_path(builder.src.join(&relative_path)), + parent: Some(self), }); // building older edition redirects @@ -213,6 +222,9 @@ impl Step for TheBook { target, name: INTERNER.intern_string(format!("book/{}", edition)), src: INTERNER.intern_path(builder.src.join(&relative_path).join(edition)), + // There should only be one book that is marked as the parent for each target, so + // treat the other editions as not having a parent. + parent: Option::::None, }); } @@ -228,10 +240,6 @@ impl Step for TheBook { invoke_rustdoc(builder, compiler, &shared_assets, target, path); } - - let out = builder.doc_out(target); - let index = out.join("book").join("index.html"); - builder.maybe_open_in_browser::(index); } } @@ -1032,10 +1040,7 @@ impl Step for RustcBook { target: self.target, name: INTERNER.intern_str("rustc"), src: INTERNER.intern_path(out_base), + parent: Some(self), }); - - let out = builder.doc_out(self.target); - let index = out.join("rustc").join("index.html"); - builder.maybe_open_in_browser::(index); } } From 96e6fb6c57c951ab705bc773cb1f242005d6ea19 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Sat, 25 Feb 2023 08:20:53 -0700 Subject: [PATCH 05/15] Update search eBNF with `!` moved Co-Authored-By: GuillaumeGomez --- src/librustdoc/html/static/js/search.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index ae15155341d25..de423395cc138 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -611,8 +611,8 @@ function initSearch(rawSearchIndex) { * * The supported syntax by this parser is as follow: * - * ident = *(ALPHA / DIGIT / "_") [!] - * path = ident *(DOUBLE-COLON ident) + * ident = *(ALPHA / DIGIT / "_") + * path = ident *(DOUBLE-COLON ident) [!] * arg = path [generics] * arg-without-generic = path * type-sep = COMMA/WS *(COMMA/WS) From 379b18bb0ab07e8f7ba04ca29ba52a1817cec1ce Mon Sep 17 00:00:00 2001 From: pommicket Date: Sat, 25 Feb 2023 11:05:21 -0500 Subject: [PATCH 06/15] Use checked_add in VecDeque::append for ZSTs to avoid overflow --- library/alloc/src/collections/vec_deque/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index 1573b3d77dc16..d08c372cd7dea 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -1917,7 +1917,7 @@ impl VecDeque { #[stable(feature = "append", since = "1.4.0")] pub fn append(&mut self, other: &mut Self) { if T::IS_ZST { - self.len += other.len; + self.len = self.len.checked_add(other.len).expect("capacity overflow"); other.len = 0; other.head = 0; return; From 12f959ba39ad353ce07971aaf8cef564f86b068d Mon Sep 17 00:00:00 2001 From: pommicket Date: Sat, 25 Feb 2023 13:50:56 -0500 Subject: [PATCH 07/15] Add test for VecDeque::append ZST capacity overflow --- library/alloc/tests/vec_deque.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/library/alloc/tests/vec_deque.rs b/library/alloc/tests/vec_deque.rs index 0b8f5281b785c..5a0b852e8d5e2 100644 --- a/library/alloc/tests/vec_deque.rs +++ b/library/alloc/tests/vec_deque.rs @@ -1045,6 +1045,20 @@ fn test_append_double_drop() { assert_eq!(count_b, 1); } +#[test] +#[should_panic] +fn test_append_zst_capacity_overflow() { + let mut v = Vec::with_capacity(usize::MAX); + // note: using resize instead of set_len here would + // be *extremely* slow in unoptimized builds. + // SAFETY: `v` has capacity `usize::MAX`, and no initialization + // is needed for empty tuples. + unsafe { v.set_len(usize::MAX) }; + let mut v = VecDeque::from(v); + let mut w = vec![()].into(); + v.append(&mut w); +} + #[test] fn test_retain() { let mut buf = VecDeque::new(); From 0758c05c9792700dbc482eda8cd464c39d5ebed5 Mon Sep 17 00:00:00 2001 From: y21 <30553356+y21@users.noreply.github.com> Date: Thu, 23 Feb 2023 20:12:01 +0100 Subject: [PATCH 08/15] recover from for-else and while-else --- compiler/rustc_parse/locales/en-US.ftl | 4 ++++ compiler/rustc_parse/src/errors.rs | 11 ++++++++++ compiler/rustc_parse/src/parser/expr.rs | 22 +++++++++++++++++++ tests/ui/for/for-else-err.rs | 8 +++++++ tests/ui/for/for-else-err.stderr | 17 ++++++++++++++ tests/ui/for/for-else-let-else-err.rs | 8 +++++++ tests/ui/for/for-else-let-else-err.stderr | 17 ++++++++++++++ .../let-else/let-else-brace-before-else.fixed | 4 ---- .../ui/let-else/let-else-brace-before-else.rs | 4 ---- .../let-else-brace-before-else.stderr | 17 +++----------- tests/ui/loops/loop-else-break-with-value.rs | 10 +++++++++ .../loops/loop-else-break-with-value.stderr | 18 +++++++++++++++ tests/ui/loops/loop-else-err.rs | 8 +++++++ tests/ui/loops/loop-else-err.stderr | 17 ++++++++++++++ tests/ui/loops/loop-else-let-else-err.rs | 8 +++++++ tests/ui/loops/loop-else-let-else-err.stderr | 17 ++++++++++++++ tests/ui/while/while-else-err.rs | 8 +++++++ tests/ui/while/while-else-err.stderr | 17 ++++++++++++++ tests/ui/while/while-else-let-else-err.rs | 8 +++++++ tests/ui/while/while-else-let-else-err.stderr | 17 ++++++++++++++ 20 files changed, 218 insertions(+), 22 deletions(-) create mode 100644 tests/ui/for/for-else-err.rs create mode 100644 tests/ui/for/for-else-err.stderr create mode 100644 tests/ui/for/for-else-let-else-err.rs create mode 100644 tests/ui/for/for-else-let-else-err.stderr create mode 100644 tests/ui/loops/loop-else-break-with-value.rs create mode 100644 tests/ui/loops/loop-else-break-with-value.stderr create mode 100644 tests/ui/loops/loop-else-err.rs create mode 100644 tests/ui/loops/loop-else-err.stderr create mode 100644 tests/ui/loops/loop-else-let-else-err.rs create mode 100644 tests/ui/loops/loop-else-let-else-err.stderr create mode 100644 tests/ui/while/while-else-err.rs create mode 100644 tests/ui/while/while-else-err.stderr create mode 100644 tests/ui/while/while-else-let-else-err.rs create mode 100644 tests/ui/while/while-else-let-else-err.stderr diff --git a/compiler/rustc_parse/locales/en-US.ftl b/compiler/rustc_parse/locales/en-US.ftl index 4ddeeed5b7e0a..e76e91fc1b135 100644 --- a/compiler/rustc_parse/locales/en-US.ftl +++ b/compiler/rustc_parse/locales/en-US.ftl @@ -151,6 +151,10 @@ parse_missing_in_in_for_loop = missing `in` in `for` loop parse_missing_expression_in_for_loop = missing expression to iterate on in `for` loop .suggestion = try adding an expression to the `for` loop +parse_loop_else = `{$loop_kind}...else` loops are not supported + .note = consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run + .loop_keyword = `else` is attached to this loop + parse_missing_comma_after_match_arm = expected `,` following `match` arm .suggestion = missing a comma here to end this `match` arm diff --git a/compiler/rustc_parse/src/errors.rs b/compiler/rustc_parse/src/errors.rs index c746a87096476..1662db36d10f9 100644 --- a/compiler/rustc_parse/src/errors.rs +++ b/compiler/rustc_parse/src/errors.rs @@ -451,6 +451,17 @@ pub(crate) struct MissingExpressionInForLoop { pub span: Span, } +#[derive(Diagnostic)] +#[diag(parse_loop_else)] +#[note] +pub(crate) struct LoopElseNotSupported { + #[primary_span] + pub span: Span, + pub loop_kind: &'static str, + #[label(parse_loop_keyword)] + pub loop_kw: Span, +} + #[derive(Diagnostic)] #[diag(parse_missing_comma_after_match_arm)] pub(crate) struct MissingCommaAfterMatchArm { diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index 24d4c17f5d8ab..b2951e7a1847d 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -2503,9 +2503,27 @@ impl<'a> Parser<'a> { let (attrs, loop_block) = self.parse_inner_attrs_and_block()?; let kind = ExprKind::ForLoop(pat, expr, loop_block, opt_label); + + self.recover_loop_else("for", lo)?; + Ok(self.mk_expr_with_attrs(lo.to(self.prev_token.span), kind, attrs)) } + /// Recovers from an `else` clause after a loop (`for...else`, `while...else`) + fn recover_loop_else(&mut self, loop_kind: &'static str, loop_kw: Span) -> PResult<'a, ()> { + if self.token.is_keyword(kw::Else) && self.may_recover() { + let else_span = self.token.span; + self.bump(); + let else_clause = self.parse_expr_else()?; + self.sess.emit_err(errors::LoopElseNotSupported { + span: else_span.to(else_clause.span), + loop_kind, + loop_kw, + }); + } + Ok(()) + } + fn error_missing_in_for_loop(&mut self) { let (span, sub): (_, fn(_) -> _) = if self.token.is_ident_named(sym::of) { // Possibly using JS syntax (#75311). @@ -2530,6 +2548,9 @@ impl<'a> Parser<'a> { err.span_label(cond.span, "this `while` condition successfully parsed"); err })?; + + self.recover_loop_else("while", lo)?; + Ok(self.mk_expr_with_attrs( lo.to(self.prev_token.span), ExprKind::While(cond, body, opt_label), @@ -2541,6 +2562,7 @@ impl<'a> Parser<'a> { fn parse_expr_loop(&mut self, opt_label: Option