From 5adef6c79598bc7dbfa3882739a6e508559cae42 Mon Sep 17 00:00:00 2001 From: Lucas Dumont Date: Tue, 7 Jun 2022 14:45:25 +0200 Subject: [PATCH 1/7] Add std::alloc::set_alloc_error_hook example --- library/std/src/alloc.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/library/std/src/alloc.rs b/library/std/src/alloc.rs index 63c527b64da48..d3879273f5b03 100644 --- a/library/std/src/alloc.rs +++ b/library/std/src/alloc.rs @@ -296,6 +296,20 @@ static HOOK: AtomicPtr<()> = AtomicPtr::new(ptr::null_mut()); /// about the allocation that failed. /// /// The allocation error hook is a global resource. +/// +/// # Examples +/// +/// ``` +/// #![feature(alloc_error_hook)] +/// +/// use std::alloc::{Layout, set_alloc_error_hook}; +/// +/// fn custom_alloc_error_hook(layout: Layout) { +/// panic!("memory allocation of {} bytes failed", layout.size()); +/// } +/// +/// set_alloc_error_hook(custom_alloc_error_hook); +/// ``` #[unstable(feature = "alloc_error_hook", issue = "51245")] pub fn set_alloc_error_hook(hook: fn(Layout)) { HOOK.store(hook as *mut (), Ordering::SeqCst); From c1b1ec7e071b0f08985787a9a897bf78bc286e7e Mon Sep 17 00:00:00 2001 From: Chayim Refael Friedman Date: Wed, 8 Jun 2022 04:40:55 +0000 Subject: [PATCH 2/7] Suggest escaping `box` as identifier --- compiler/rustc_parse/src/parser/pat.rs | 61 ++++++++++++++++-- .../ui/parser/keyword-box-as-identifier.rs | 9 ++- .../parser/keyword-box-as-identifier.stderr | 64 ++++++++++++++++++- 3 files changed, 126 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_parse/src/parser/pat.rs b/compiler/rustc_parse/src/parser/pat.rs index 2ad3f3ec19d57..ca7915ed17a50 100644 --- a/compiler/rustc_parse/src/parser/pat.rs +++ b/compiler/rustc_parse/src/parser/pat.rs @@ -360,10 +360,7 @@ impl<'a> Parser<'a> { let mutbl = self.parse_mutability(); self.parse_pat_ident(BindingMode::ByRef(mutbl))? } else if self.eat_keyword(kw::Box) { - // Parse `box pat` - let pat = self.parse_pat_with_range_pat(false, None)?; - self.sess.gated_spans.gate(sym::box_patterns, lo.to(self.prev_token.span)); - PatKind::Box(pat) + self.parse_pat_box()? } else if self.check_inline_const(0) { // Parse `const pat` let const_expr = self.parse_const_block(lo.to(self.token.span), true)?; @@ -915,6 +912,62 @@ impl<'a> Parser<'a> { Ok(PatKind::TupleStruct(qself, path, fields)) } + /// Are we sure this could not possibly be the start of a pattern? + /// + /// Currently, this only accounts for tokens that can follow identifiers + /// in patterns, but this can be extended as necessary. + fn isnt_pattern_start(&self) -> bool { + [ + token::Eq, + token::Colon, + token::Comma, + token::Semi, + token::At, + token::OpenDelim(Delimiter::Brace), + token::CloseDelim(Delimiter::Brace), + token::CloseDelim(Delimiter::Parenthesis), + ] + .contains(&self.token.kind) + } + + /// Parses `box pat` + fn parse_pat_box(&mut self) -> PResult<'a, PatKind> { + let box_span = self.prev_token.span; + + if self.isnt_pattern_start() { + self.struct_span_err( + self.token.span, + format!("expected pattern, found {}", super::token_descr(&self.token)), + ) + .span_note(box_span, "`box` is a reserved keyword") + .span_suggestion_verbose( + box_span.shrink_to_lo(), + "escape `box` to use it as an identifier", + "r#", + Applicability::MaybeIncorrect, + ) + .emit(); + + // We cannot use `parse_pat_ident()` since it will complain `box` + // is not an identifier. + let sub = if self.eat(&token::At) { + Some(self.parse_pat_no_top_alt(Some("binding pattern"))?) + } else { + None + }; + + Ok(PatKind::Ident( + BindingMode::ByValue(Mutability::Not), + Ident::new(kw::Box, box_span), + sub, + )) + } else { + let pat = self.parse_pat_with_range_pat(false, None)?; + self.sess.gated_spans.gate(sym::box_patterns, box_span.to(self.prev_token.span)); + Ok(PatKind::Box(pat)) + } + } + /// Parses the fields of a struct-like pattern. fn parse_pat_fields(&mut self) -> PResult<'a, (Vec, bool)> { let mut fields = Vec::new(); diff --git a/src/test/ui/parser/keyword-box-as-identifier.rs b/src/test/ui/parser/keyword-box-as-identifier.rs index 33961bb308467..2cf49b66be61c 100644 --- a/src/test/ui/parser/keyword-box-as-identifier.rs +++ b/src/test/ui/parser/keyword-box-as-identifier.rs @@ -1,3 +1,10 @@ fn main() { - let box = "foo"; //~ error: expected pattern, found `=` + let box = 0; + //~^ ERROR expected pattern, found `=` + let box: bool; + //~^ ERROR expected pattern, found `:` + let mut box = 0; + //~^ ERROR expected pattern, found `=` + let (box,) = (0,); + //~^ ERROR expected pattern, found `,` } diff --git a/src/test/ui/parser/keyword-box-as-identifier.stderr b/src/test/ui/parser/keyword-box-as-identifier.stderr index 8b185948498d8..eaa1f8003c53a 100644 --- a/src/test/ui/parser/keyword-box-as-identifier.stderr +++ b/src/test/ui/parser/keyword-box-as-identifier.stderr @@ -1,8 +1,66 @@ error: expected pattern, found `=` --> $DIR/keyword-box-as-identifier.rs:2:13 | -LL | let box = "foo"; - | ^ expected pattern +LL | let box = 0; + | ^ + | +note: `box` is a reserved keyword + --> $DIR/keyword-box-as-identifier.rs:2:9 + | +LL | let box = 0; + | ^^^ +help: escape `box` to use it as an identifier + | +LL | let r#box = 0; + | ++ + +error: expected pattern, found `:` + --> $DIR/keyword-box-as-identifier.rs:4:12 + | +LL | let box: bool; + | ^ + | +note: `box` is a reserved keyword + --> $DIR/keyword-box-as-identifier.rs:4:9 + | +LL | let box: bool; + | ^^^ +help: escape `box` to use it as an identifier + | +LL | let r#box: bool; + | ++ + +error: expected pattern, found `=` + --> $DIR/keyword-box-as-identifier.rs:6:17 + | +LL | let mut box = 0; + | ^ + | +note: `box` is a reserved keyword + --> $DIR/keyword-box-as-identifier.rs:6:13 + | +LL | let mut box = 0; + | ^^^ +help: escape `box` to use it as an identifier + | +LL | let mut r#box = 0; + | ++ + +error: expected pattern, found `,` + --> $DIR/keyword-box-as-identifier.rs:8:13 + | +LL | let (box,) = (0,); + | ^ + | +note: `box` is a reserved keyword + --> $DIR/keyword-box-as-identifier.rs:8:10 + | +LL | let (box,) = (0,); + | ^^^ +help: escape `box` to use it as an identifier + | +LL | let (r#box,) = (0,); + | ++ -error: aborting due to previous error +error: aborting due to 4 previous errors From 456f1ffe12062f99cf800dd20d81e12e4131cb69 Mon Sep 17 00:00:00 2001 From: Chayim Refael Friedman Date: Wed, 8 Jun 2022 11:09:08 +0000 Subject: [PATCH 3/7] Suggest using `iter()` or `into_iter()` for `Vec` We cannot do that for `&Vec` because `#[rustc_on_unimplemented]` is limited (it does not clean generic instantiation for references, only for ADTs). --- library/core/src/iter/traits/iterator.rs | 4 ++++ src/test/ui/iterators/vec-on-unimplemented.rs | 4 ++++ .../ui/iterators/vec-on-unimplemented.stderr | 20 +++++++++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 src/test/ui/iterators/vec-on-unimplemented.rs create mode 100644 src/test/ui/iterators/vec-on-unimplemented.stderr diff --git a/library/core/src/iter/traits/iterator.rs b/library/core/src/iter/traits/iterator.rs index 69f06fb06ef5d..1cc9133fc3dc4 100644 --- a/library/core/src/iter/traits/iterator.rs +++ b/library/core/src/iter/traits/iterator.rs @@ -40,6 +40,10 @@ fn _assert_is_object_safe(_: &dyn Iterator) {} label = "`{Self}` is not an iterator; try calling `.into_iter()` or `.iter()`" ), on(_Self = "&[]", label = "`{Self}` is not an iterator; try calling `.iter()`"), + on( + _Self = "std::vec::Vec", + label = "`{Self}` is not an iterator; try calling `.into_iter()` or `.iter()`" + ), on( _Self = "&str", label = "`{Self}` is not an iterator; try calling `.chars()` or `.bytes()`" diff --git a/src/test/ui/iterators/vec-on-unimplemented.rs b/src/test/ui/iterators/vec-on-unimplemented.rs new file mode 100644 index 0000000000000..42b5d36bfad4a --- /dev/null +++ b/src/test/ui/iterators/vec-on-unimplemented.rs @@ -0,0 +1,4 @@ +fn main() { + vec![true, false].map(|v| !v).collect::>(); + //~^ ERROR `Vec` is not an iterator +} diff --git a/src/test/ui/iterators/vec-on-unimplemented.stderr b/src/test/ui/iterators/vec-on-unimplemented.stderr new file mode 100644 index 0000000000000..afcce5c30ca02 --- /dev/null +++ b/src/test/ui/iterators/vec-on-unimplemented.stderr @@ -0,0 +1,20 @@ +error[E0599]: `Vec` is not an iterator + --> $DIR/vec-on-unimplemented.rs:2:23 + | +LL | vec![true, false].map(|v| !v).collect::>(); + | ^^^ `Vec` is not an iterator; try calling `.into_iter()` or `.iter()` + | + ::: $SRC_DIR/alloc/src/vec/mod.rs:LL:COL + | +LL | pub struct Vec { + | ------------------------------------------------------------------------------------------------ doesn't satisfy `Vec: Iterator` + | + = note: the following trait bounds were not satisfied: + `Vec: Iterator` + which is required by `&mut Vec: Iterator` + `[bool]: Iterator` + which is required by `&mut [bool]: Iterator` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0599`. From 0f2a2fbc0545799d83d01c2f35703edfbb8331dd Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Thu, 9 Jun 2022 00:09:28 +0900 Subject: [PATCH 4/7] Add regresion test for #67498 --- src/test/ui/lifetimes/issue-67498.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/test/ui/lifetimes/issue-67498.rs diff --git a/src/test/ui/lifetimes/issue-67498.rs b/src/test/ui/lifetimes/issue-67498.rs new file mode 100644 index 0000000000000..8d88264353a72 --- /dev/null +++ b/src/test/ui/lifetimes/issue-67498.rs @@ -0,0 +1,21 @@ +// check-pass + +// Regression test for #67498. + +pub fn f<'a, 'b, 'd, 'e> ( + x: for<'c> fn( + fn(&'c fn(&'c ())), + fn(&'c fn(&'c ())), + fn(&'c fn(&'c ())), + fn(&'c fn(&'c ())), + ) +) -> fn( + fn(&'a fn(&'d ())), + fn(&'b fn(&'d ())), + fn(&'a fn(&'e ())), + fn(&'b fn(&'e ())), +) { + x +} + +fn main() {} From 93c210abaa34d50a13a9b6d70cb79c95f90f8e70 Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Thu, 9 Jun 2022 00:13:57 +0900 Subject: [PATCH 5/7] Remove `ignore-compare-mode-nll` annotations from tests --- src/test/ui/json-multiple.rs | 1 - src/test/ui/json-options.rs | 1 - src/test/ui/rmeta/emit-artifact-notifications.rs | 1 - src/test/ui/save-analysis/emit-notifications.rs | 1 - 4 files changed, 4 deletions(-) diff --git a/src/test/ui/json-multiple.rs b/src/test/ui/json-multiple.rs index 4c37e20d94dda..fb126339dc216 100644 --- a/src/test/ui/json-multiple.rs +++ b/src/test/ui/json-multiple.rs @@ -1,6 +1,5 @@ // build-pass // ignore-pass (different metadata emitted in different modes) // compile-flags: --json=diagnostic-short --json artifacts --error-format=json -// ignore-compare-mode-nll #![crate_type = "lib"] diff --git a/src/test/ui/json-options.rs b/src/test/ui/json-options.rs index fea07cc9e3e54..8b6ba131eb002 100644 --- a/src/test/ui/json-options.rs +++ b/src/test/ui/json-options.rs @@ -1,6 +1,5 @@ // build-pass // ignore-pass (different metadata emitted in different modes) // compile-flags: --json=diagnostic-short,artifacts --error-format=json -// ignore-compare-mode-nll #![crate_type = "lib"] diff --git a/src/test/ui/rmeta/emit-artifact-notifications.rs b/src/test/ui/rmeta/emit-artifact-notifications.rs index be38fb4c3a69a..984a7fabb6633 100644 --- a/src/test/ui/rmeta/emit-artifact-notifications.rs +++ b/src/test/ui/rmeta/emit-artifact-notifications.rs @@ -2,7 +2,6 @@ // build-pass // ignore-pass // ^-- needed because `--pass check` does not emit the output needed. -// ignore-compare-mode-nll // A very basic test for the emission of artifact notifications in JSON output. diff --git a/src/test/ui/save-analysis/emit-notifications.rs b/src/test/ui/save-analysis/emit-notifications.rs index 8a696695ec0a2..9179944a6201d 100644 --- a/src/test/ui/save-analysis/emit-notifications.rs +++ b/src/test/ui/save-analysis/emit-notifications.rs @@ -3,6 +3,5 @@ // compile-flags: --crate-type rlib --error-format=json // ignore-pass // ^-- needed because otherwise, the .stderr file changes with --pass check -// ignore-compare-mode-nll pub fn foo() {} From 0f9cc06abda973678d5a237709713d6e5f0cca31 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Wed, 8 Jun 2022 11:19:53 -0700 Subject: [PATCH 6/7] Update books --- src/doc/book | 2 +- src/doc/embedded-book | 2 +- src/doc/nomicon | 2 +- src/doc/reference | 2 +- src/doc/rust-by-example | 2 +- src/doc/rustc-dev-guide | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/doc/book b/src/doc/book index b4dd5f00b8719..396fdb69de7fb 160000 --- a/src/doc/book +++ b/src/doc/book @@ -1 +1 @@ -Subproject commit b4dd5f00b87190ad5ef42cbc2a88a783c6ae57ef +Subproject commit 396fdb69de7fb18f24b15c7ad13491b1c1fa7231 diff --git a/src/doc/embedded-book b/src/doc/embedded-book index f7cefbb995eec..cbb494f96da32 160000 --- a/src/doc/embedded-book +++ b/src/doc/embedded-book @@ -1 +1 @@ -Subproject commit f7cefbb995eec8c6148f213235e9e2e03268e775 +Subproject commit cbb494f96da3268c2925bdadc65ca83d42f2d4ef diff --git a/src/doc/nomicon b/src/doc/nomicon index 10d40c59a581c..3a43983b76174 160000 --- a/src/doc/nomicon +++ b/src/doc/nomicon @@ -1 +1 @@ -Subproject commit 10d40c59a581c66d8ecd29ad18d410bf97ed524d +Subproject commit 3a43983b76174342b7dbd3e12ea2c49f762e52be diff --git a/src/doc/reference b/src/doc/reference index b74825d8f88b6..683bfe5cd64d5 160000 --- a/src/doc/reference +++ b/src/doc/reference @@ -1 +1 @@ -Subproject commit b74825d8f88b685e239ade00f00de68ba4cd63d4 +Subproject commit 683bfe5cd64d589c6a1645312ab5f93b6385ccbb diff --git a/src/doc/rust-by-example b/src/doc/rust-by-example index 2ed26865e8c29..dbb7e5e2345ee 160000 --- a/src/doc/rust-by-example +++ b/src/doc/rust-by-example @@ -1 +1 @@ -Subproject commit 2ed26865e8c29ef939dc913a97bd321cadd72a9a +Subproject commit dbb7e5e2345ee26199ffba218156b6009016a20c diff --git a/src/doc/rustc-dev-guide b/src/doc/rustc-dev-guide index 554c00e4805df..6e4d6435db89b 160000 --- a/src/doc/rustc-dev-guide +++ b/src/doc/rustc-dev-guide @@ -1 +1 @@ -Subproject commit 554c00e4805df7f7bffac7db408437d62d6dfb9a +Subproject commit 6e4d6435db89bcc027b1bba9742e4f59666f5412 From bcfced862d7afb20e24f6ee744af5b85f34ff4de Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Wed, 8 Jun 2022 11:53:16 -0700 Subject: [PATCH 7/7] Fix polonius compare mode. --- src/tools/compiletest/src/runtest.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tools/compiletest/src/runtest.rs b/src/tools/compiletest/src/runtest.rs index 49c8248b80d1e..b758bb9cf6790 100644 --- a/src/tools/compiletest/src/runtest.rs +++ b/src/tools/compiletest/src/runtest.rs @@ -1939,7 +1939,7 @@ impl<'test> TestCx<'test> { match self.config.compare_mode { Some(CompareMode::Polonius) => { - rustc.args(&["-Zpolonius", "-Zborrowck=mir"]); + rustc.args(&["-Zpolonius"]); } Some(CompareMode::Chalk) => { rustc.args(&["-Zchalk"]);