From 37ce212f1f0a009a181d3a0a27dbd52505d4ac07 Mon Sep 17 00:00:00 2001 From: Trevor Spiteri Date: Wed, 23 Sep 2020 12:01:25 +0200 Subject: [PATCH 01/10] make exp_m1 examples more representative of use With this commit, the examples for exp_m1 would fail if x.exp() - 1.0 is used instead of x.exp_m1(). --- library/std/src/f32.rs | 9 +++++---- library/std/src/f64.rs | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/library/std/src/f32.rs b/library/std/src/f32.rs index 59c2da5273bde..cd9065b3a2115 100644 --- a/library/std/src/f32.rs +++ b/library/std/src/f32.rs @@ -719,12 +719,13 @@ impl f32 { /// # Examples /// /// ``` - /// let x = 6.0f32; + /// let x = 1e-8_f32; /// - /// // e^(ln(6)) - 1 - /// let abs_difference = (x.ln().exp_m1() - 5.0).abs(); + /// // for very small x, e^x is approximately 1 + x + x^2 / 2 + /// let approx = x + x * x / 2.0; + /// let abs_difference = (x.exp_m1() - approx).abs(); /// - /// assert!(abs_difference <= f32::EPSILON); + /// assert!(abs_difference < 1e-10); /// ``` #[must_use = "method returns a new number and does not mutate the original value"] #[stable(feature = "rust1", since = "1.0.0")] diff --git a/library/std/src/f64.rs b/library/std/src/f64.rs index bd094bdb55dc3..e412f89432c76 100644 --- a/library/std/src/f64.rs +++ b/library/std/src/f64.rs @@ -721,12 +721,13 @@ impl f64 { /// # Examples /// /// ``` - /// let x = 7.0_f64; + /// let x = 1e-16_f64; /// - /// // e^(ln(7)) - 1 - /// let abs_difference = (x.ln().exp_m1() - 6.0).abs(); + /// // for very small x, e^x is approximately 1 + x + x^2 / 2 + /// let approx = x + x * x / 2.0; + /// let abs_difference = (x.exp_m1() - approx).abs(); /// - /// assert!(abs_difference < 1e-10); + /// assert!(abs_difference < 1e-20); /// ``` #[must_use = "method returns a new number and does not mutate the original value"] #[stable(feature = "rust1", since = "1.0.0")] From 50d3ddcb0cbc36f782fa5939d1ef24422f6902d4 Mon Sep 17 00:00:00 2001 From: Trevor Spiteri Date: Wed, 23 Sep 2020 12:02:49 +0200 Subject: [PATCH 02/10] make ln_1p examples more representative of use With this commit, the examples for ln_1p would fail if (x + 1.0).ln() is used instead of x.ln_1p(). --- library/std/src/f32.rs | 9 +++++---- library/std/src/f64.rs | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/library/std/src/f32.rs b/library/std/src/f32.rs index cd9065b3a2115..ed975c4287981 100644 --- a/library/std/src/f32.rs +++ b/library/std/src/f32.rs @@ -740,12 +740,13 @@ impl f32 { /// # Examples /// /// ``` - /// let x = std::f32::consts::E - 1.0; + /// let x = 1e-8_f32; /// - /// // ln(1 + (e - 1)) == ln(e) == 1 - /// let abs_difference = (x.ln_1p() - 1.0).abs(); + /// // for very small x, ln(1 + x) is approximately x - x^2 / 2 + /// let approx = x - x * x / 2.0; + /// let abs_difference = (x.ln_1p() - approx).abs(); /// - /// assert!(abs_difference <= f32::EPSILON); + /// assert!(abs_difference < 1e-10); /// ``` #[must_use = "method returns a new number and does not mutate the original value"] #[stable(feature = "rust1", since = "1.0.0")] diff --git a/library/std/src/f64.rs b/library/std/src/f64.rs index e412f89432c76..8d0a85e056f71 100644 --- a/library/std/src/f64.rs +++ b/library/std/src/f64.rs @@ -742,12 +742,13 @@ impl f64 { /// # Examples /// /// ``` - /// let x = std::f64::consts::E - 1.0; + /// let x = 1e-16_f64; /// - /// // ln(1 + (e - 1)) == ln(e) == 1 - /// let abs_difference = (x.ln_1p() - 1.0).abs(); + /// // for very small x, ln(1 + x) is approximately x - x^2 / 2 + /// let approx = x - x * x / 2.0; + /// let abs_difference = (x.ln_1p() - approx).abs(); /// - /// assert!(abs_difference < 1e-10); + /// assert!(abs_difference < 1e-20); /// ``` #[must_use = "method returns a new number and does not mutate the original value"] #[stable(feature = "rust1", since = "1.0.0")] From 01b0aff1df1dd5ee7c60e8fbeff15cc3edaa3208 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Wed, 22 Jul 2020 12:17:36 +0200 Subject: [PATCH 03/10] Add std::panic::panic_box. --- library/std/src/panic.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/library/std/src/panic.rs b/library/std/src/panic.rs index 4281867314cca..06ce66c10f7ea 100644 --- a/library/std/src/panic.rs +++ b/library/std/src/panic.rs @@ -23,6 +23,20 @@ pub use crate::panicking::{set_hook, take_hook}; #[stable(feature = "panic_hooks", since = "1.10.0")] pub use core::panic::{Location, PanicInfo}; +/// Panic the current thread, with the given payload as the panic message. +/// +/// This supports an arbitrary panic payload, instead of just (formatted) strings. +/// +/// The message is attached as a `Box<'static + Any + Send>`, which can be +/// accessed using [`PanicInfo::payload`]. +/// +/// See the [`panic!`] macro for more information about panicking. +#[unstable(feature = "panic_box", issue = "none")] +#[inline] +pub fn panic_box(msg: M) -> ! { + crate::panicking::begin_panic(msg); +} + /// A marker trait which represents "panic safe" types in Rust. /// /// This trait is implemented by default for many types and behaves similarly in From 16201da6a4ff613d00ca3680c43cbb1b52f60cf1 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Sun, 18 Oct 2020 12:29:13 +0200 Subject: [PATCH 04/10] Rename panic_box to panic_any. --- library/std/src/panic.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/library/std/src/panic.rs b/library/std/src/panic.rs index 06ce66c10f7ea..9a756e4bbb4d4 100644 --- a/library/std/src/panic.rs +++ b/library/std/src/panic.rs @@ -23,17 +23,17 @@ pub use crate::panicking::{set_hook, take_hook}; #[stable(feature = "panic_hooks", since = "1.10.0")] pub use core::panic::{Location, PanicInfo}; -/// Panic the current thread, with the given payload as the panic message. +/// Panic the current thread with the given message as the panic payload. /// -/// This supports an arbitrary panic payload, instead of just (formatted) strings. +/// The message can be of any (`Any + Send`) type, not just strings. /// -/// The message is attached as a `Box<'static + Any + Send>`, which can be -/// accessed using [`PanicInfo::payload`]. +/// The message is wrapped in a `Box<'static + Any + Send>`, which can be +/// accessed later using [`PanicInfo::payload`]. /// /// See the [`panic!`] macro for more information about panicking. #[unstable(feature = "panic_box", issue = "none")] #[inline] -pub fn panic_box(msg: M) -> ! { +pub fn panic_any(msg: M) -> ! { crate::panicking::begin_panic(msg); } From a9d334d386e5abf79d8ee60f94bf32147b755c4c Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Wed, 28 Oct 2020 21:21:41 +0100 Subject: [PATCH 05/10] Update panic_any feature name. Co-authored-by: Camelid --- library/std/src/panic.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/panic.rs b/library/std/src/panic.rs index 9a756e4bbb4d4..ad91933d65102 100644 --- a/library/std/src/panic.rs +++ b/library/std/src/panic.rs @@ -31,7 +31,7 @@ pub use core::panic::{Location, PanicInfo}; /// accessed later using [`PanicInfo::payload`]. /// /// See the [`panic!`] macro for more information about panicking. -#[unstable(feature = "panic_box", issue = "none")] +#[unstable(feature = "panic_any", issue = "none")] #[inline] pub fn panic_any(msg: M) -> ! { crate::panicking::begin_panic(msg); From b48fee010c92dde304154ba45c0e41d396e60568 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Wed, 28 Oct 2020 21:23:45 +0100 Subject: [PATCH 06/10] Add tracking issue number for panic_any. --- library/std/src/panic.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/panic.rs b/library/std/src/panic.rs index ad91933d65102..d18b94b6c1aef 100644 --- a/library/std/src/panic.rs +++ b/library/std/src/panic.rs @@ -31,7 +31,7 @@ pub use core::panic::{Location, PanicInfo}; /// accessed later using [`PanicInfo::payload`]. /// /// See the [`panic!`] macro for more information about panicking. -#[unstable(feature = "panic_any", issue = "none")] +#[unstable(feature = "panic_any", issue = "78500")] #[inline] pub fn panic_any(msg: M) -> ! { crate::panicking::begin_panic(msg); From 4ba57aa703397a34e92f055c9e07bc880b771226 Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Thu, 29 Oct 2020 11:37:55 -0400 Subject: [PATCH 07/10] Strip tokens from trait and impl items before printing AST JSON Fixes #78510 --- compiler/rustc_interface/src/passes.rs | 11 +++++++++++ src/test/ui/ast-json/issue-78510-assoc-ice.rs | 18 ++++++++++++++++++ .../ui/ast-json/issue-78510-assoc-ice.stderr | 15 +++++++++++++++ .../ui/ast-json/issue-78510-assoc-ice.stdout | 1 + 4 files changed, 45 insertions(+) create mode 100644 src/test/ui/ast-json/issue-78510-assoc-ice.rs create mode 100644 src/test/ui/ast-json/issue-78510-assoc-ice.stderr create mode 100644 src/test/ui/ast-json/issue-78510-assoc-ice.stdout diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index 9dbd59506b188..a1487aa0060d5 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -70,6 +70,17 @@ impl mut_visit::MutVisitor for TokenStripper { i.tokens = None; mut_visit::noop_flat_map_foreign_item(i, self) } + fn flat_map_trait_item( + &mut self, + mut i: P, + ) -> SmallVec<[P; 1]> { + i.tokens = None; + mut_visit::noop_flat_map_assoc_item(i, self) + } + fn flat_map_impl_item(&mut self, mut i: P) -> SmallVec<[P; 1]> { + i.tokens = None; + mut_visit::noop_flat_map_assoc_item(i, self) + } fn visit_block(&mut self, b: &mut P) { b.tokens = None; mut_visit::noop_visit_block(b, self); diff --git a/src/test/ui/ast-json/issue-78510-assoc-ice.rs b/src/test/ui/ast-json/issue-78510-assoc-ice.rs new file mode 100644 index 0000000000000..ef3117c49cad3 --- /dev/null +++ b/src/test/ui/ast-json/issue-78510-assoc-ice.rs @@ -0,0 +1,18 @@ +// compile-flags: -Zast-json +// +// Regression test for issue #78510 +// Tests that we don't ICE when we have tokens for an associated item + +struct S; + +impl S { + #[derive(Debug)] //~ ERROR `derive` may only be applied to structs, enums and unions + fn f() {} +} + +trait Bar { + #[derive(Debug)] //~ ERROR `derive` may only be applied to structs, enums and unions + fn foo() {} +} + +fn main() {} diff --git a/src/test/ui/ast-json/issue-78510-assoc-ice.stderr b/src/test/ui/ast-json/issue-78510-assoc-ice.stderr new file mode 100644 index 0000000000000..3573c203a7893 --- /dev/null +++ b/src/test/ui/ast-json/issue-78510-assoc-ice.stderr @@ -0,0 +1,15 @@ +error[E0774]: `derive` may only be applied to structs, enums and unions + --> $DIR/issue-78510-assoc-ice.rs:9:5 + | +LL | #[derive(Debug)] + | ^^^^^^^^^^^^^^^^ + +error[E0774]: `derive` may only be applied to structs, enums and unions + --> $DIR/issue-78510-assoc-ice.rs:14:5 + | +LL | #[derive(Debug)] + | ^^^^^^^^^^^^^^^^ + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0774`. diff --git a/src/test/ui/ast-json/issue-78510-assoc-ice.stdout b/src/test/ui/ast-json/issue-78510-assoc-ice.stdout new file mode 100644 index 0000000000000..fef9504285b53 --- /dev/null +++ b/src/test/ui/ast-json/issue-78510-assoc-ice.stdout @@ -0,0 +1 @@ +{"module":{"inner":{"lo":139,"hi":397},"unsafety":"No","items":[{"attrs":[{"kind":{"variant":"Normal","fields":[{"path":{"span":{"lo":0,"hi":0},"segments":[{"ident":{"name":"prelude_import","span":{"lo":0,"hi":0}},"id":3,"args":null}],"tokens":null},"args":"Empty","tokens":null}]},"id":null,"style":"Outer","span":{"lo":0,"hi":0},"tokens":null}],"id":4,"span":{"lo":0,"hi":0},"vis":{"kind":"Inherited","span":{"lo":0,"hi":0},"tokens":null},"ident":{"name":"","span":{"lo":0,"hi":0}},"kind":{"variant":"Use","fields":[{"prefix":{"span":{"lo":0,"hi":0},"segments":[{"ident":{"name":"{{root}}","span":{"lo":0,"hi":0}},"id":5,"args":null},{"ident":{"name":"std","span":{"lo":0,"hi":0}},"id":6,"args":null},{"ident":{"name":"prelude","span":{"lo":0,"hi":0}},"id":7,"args":null},{"ident":{"name":"v1","span":{"lo":0,"hi":0}},"id":8,"args":null}],"tokens":null},"kind":"Glob","span":{"lo":0,"hi":0}}]},"tokens":null},{"attrs":[{"kind":{"variant":"Normal","fields":[{"path":{"span":{"lo":0,"hi":0},"segments":[{"ident":{"name":"macro_use","span":{"lo":0,"hi":0}},"id":9,"args":null}],"tokens":null},"args":"Empty","tokens":null}]},"id":null,"style":"Outer","span":{"lo":0,"hi":0},"tokens":null}],"id":10,"span":{"lo":0,"hi":0},"vis":{"kind":"Inherited","span":{"lo":0,"hi":0},"tokens":null},"ident":{"name":"std","span":{"lo":0,"hi":0}},"kind":{"variant":"ExternCrate","fields":[null]},"tokens":null},{"attrs":[],"id":11,"span":{"lo":139,"hi":148},"vis":{"kind":"Inherited","span":{"lo":139,"hi":139},"tokens":null},"ident":{"name":"S","span":{"lo":146,"hi":147}},"kind":{"variant":"Struct","fields":[{"variant":"Unit","fields":[12]},{"params":[],"where_clause":{"has_where_token":false,"predicates":[],"span":{"lo":147,"hi":147}},"span":{"lo":147,"hi":147}}]},"tokens":null},{"attrs":[],"id":13,"span":{"lo":150,"hi":263},"vis":{"kind":"Inherited","span":{"lo":150,"hi":150},"tokens":null},"ident":{"name":"","span":{"lo":0,"hi":0}},"kind":{"variant":"Impl","fields":["No","Positive","Final","No",{"params":[],"where_clause":{"has_where_token":false,"predicates":[],"span":{"lo":156,"hi":156}},"span":{"lo":154,"hi":154}},null,{"id":14,"kind":{"variant":"Path","fields":[null,{"span":{"lo":155,"hi":156},"segments":[{"ident":{"name":"S","span":{"lo":155,"hi":156}},"id":15,"args":null}],"tokens":null}]},"span":{"lo":155,"hi":156},"tokens":null},[{"attrs":[],"id":19,"span":{"lo":252,"hi":261},"vis":{"kind":"Inherited","span":{"lo":252,"hi":252},"tokens":null},"ident":{"name":"f","span":{"lo":255,"hi":256}},"kind":{"variant":"Fn","fields":["Final",{"header":{"unsafety":"No","asyncness":"No","constness":"No","ext":"None"},"decl":{"inputs":[],"output":{"variant":"Default","fields":[{"lo":259,"hi":259}]}},"span":{"lo":252,"hi":258}},{"params":[],"where_clause":{"has_where_token":false,"predicates":[],"span":{"lo":258,"hi":258}},"span":{"lo":256,"hi":256}},{"stmts":[],"id":20,"rules":"Default","span":{"lo":259,"hi":261},"tokens":null}]},"tokens":null}]]},"tokens":null},{"attrs":[],"id":16,"span":{"lo":265,"hi":383},"vis":{"kind":"Inherited","span":{"lo":265,"hi":265},"tokens":null},"ident":{"name":"Bar","span":{"lo":271,"hi":274}},"kind":{"variant":"Trait","fields":["No","No",{"params":[],"where_clause":{"has_where_token":false,"predicates":[],"span":{"lo":274,"hi":274}},"span":{"lo":274,"hi":274}},[],[{"attrs":[],"id":21,"span":{"lo":370,"hi":381},"vis":{"kind":"Inherited","span":{"lo":370,"hi":370},"tokens":null},"ident":{"name":"foo","span":{"lo":373,"hi":376}},"kind":{"variant":"Fn","fields":["Final",{"header":{"unsafety":"No","asyncness":"No","constness":"No","ext":"None"},"decl":{"inputs":[],"output":{"variant":"Default","fields":[{"lo":379,"hi":379}]}},"span":{"lo":370,"hi":378}},{"params":[],"where_clause":{"has_where_token":false,"predicates":[],"span":{"lo":378,"hi":378}},"span":{"lo":376,"hi":376}},{"stmts":[],"id":22,"rules":"Default","span":{"lo":379,"hi":381},"tokens":null}]},"tokens":null}]]},"tokens":null},{"attrs":[],"id":17,"span":{"lo":385,"hi":397},"vis":{"kind":"Inherited","span":{"lo":385,"hi":385},"tokens":null},"ident":{"name":"main","span":{"lo":388,"hi":392}},"kind":{"variant":"Fn","fields":["Final",{"header":{"unsafety":"No","asyncness":"No","constness":"No","ext":"None"},"decl":{"inputs":[],"output":{"variant":"Default","fields":[{"lo":395,"hi":395}]}},"span":{"lo":385,"hi":394}},{"params":[],"where_clause":{"has_where_token":false,"predicates":[],"span":{"lo":394,"hi":394}},"span":{"lo":392,"hi":392}},{"stmts":[],"id":18,"rules":"Default","span":{"lo":395,"hi":397},"tokens":null}]},"tokens":null}],"inline":true},"attrs":[],"span":{"lo":139,"hi":397},"proc_macros":[]} From 8cf7d66d0a7e5405f55887ca008cca7fce7f7396 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Thu, 29 Oct 2020 21:23:55 -0400 Subject: [PATCH 08/10] Create config.toml in the current directory, not the top-level directory See https://github.com/rust-lang/rust/issues/78509 for discussion. --- src/bootstrap/setup.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bootstrap/setup.rs b/src/bootstrap/setup.rs index c6e1c99564c09..f5ce45a5bd11b 100644 --- a/src/bootstrap/setup.rs +++ b/src/bootstrap/setup.rs @@ -89,7 +89,7 @@ pub fn setup(src_path: &Path, profile: Profile) { std::process::exit(1); } - let path = cfg_file.unwrap_or_else(|| src_path.join("config.toml")); + let path = cfg_file.unwrap_or("config.toml".into()); let settings = format!( "# Includes one of the default files in src/bootstrap/defaults\n\ profile = \"{}\"\n\ From 59c6ae615e5547610c3348b466a45ff2a5b3d935 Mon Sep 17 00:00:00 2001 From: Maarten de Vries Date: Fri, 30 Oct 2020 14:05:53 +0100 Subject: [PATCH 09/10] Use SOCK_CLOEXEC and accept4() on more platforms. --- library/std/src/sys/unix/net.rs | 38 +++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/library/std/src/sys/unix/net.rs b/library/std/src/sys/unix/net.rs index 74c7db27226ef..71c6aa5a0e7ea 100644 --- a/library/std/src/sys/unix/net.rs +++ b/library/std/src/sys/unix/net.rs @@ -55,9 +55,18 @@ impl Socket { pub fn new_raw(fam: c_int, ty: c_int) -> io::Result { unsafe { cfg_if::cfg_if! { - if #[cfg(target_os = "linux")] { - // On Linux we pass the SOCK_CLOEXEC flag to atomically create - // the socket and set it as CLOEXEC, added in 2.6.27. + if #[cfg(any( + target_os = "android", + target_os = "dragonfly", + target_os = "freebsd", + target_os = "illumos", + target_os = "linux", + target_os = "netbsd", + target_os = "opensbd", + ))] { + // On platforms that support it we pass the SOCK_CLOEXEC + // flag to atomically create the socket and set it as + // CLOEXEC. On Linux this was added in 2.6.27. let fd = cvt(libc::socket(fam, ty | libc::SOCK_CLOEXEC, 0))?; Ok(Socket(FileDesc::new(fd))) } else { @@ -83,7 +92,15 @@ impl Socket { let mut fds = [0, 0]; cfg_if::cfg_if! { - if #[cfg(target_os = "linux")] { + if #[cfg(any( + target_os = "android", + target_os = "dragonfly", + target_os = "freebsd", + target_os = "illumos", + target_os = "linux", + target_os = "netbsd", + target_os = "opensbd", + ))] { // Like above, set cloexec atomically cvt(libc::socketpair(fam, ty | libc::SOCK_CLOEXEC, 0, fds.as_mut_ptr()))?; Ok((Socket(FileDesc::new(fds[0])), Socket(FileDesc::new(fds[1])))) @@ -174,9 +191,18 @@ impl Socket { pub fn accept(&self, storage: *mut sockaddr, len: *mut socklen_t) -> io::Result { // Unfortunately the only known way right now to accept a socket and // atomically set the CLOEXEC flag is to use the `accept4` syscall on - // Linux. This was added in 2.6.28, glibc 2.10 and musl 0.9.5. + // platforms that support it. On Linux, this was added in 2.6.28, + // glibc 2.10 and musl 0.9.5. cfg_if::cfg_if! { - if #[cfg(target_os = "linux")] { + if #[cfg(any( + target_os = "android", + target_os = "dragonfly", + target_os = "freebsd", + target_os = "illumos", + target_os = "linux", + target_os = "netbsd", + target_os = "opensbd", + ))] { let fd = cvt_r(|| unsafe { libc::accept4(self.0.raw(), storage, len, libc::SOCK_CLOEXEC) })?; From 307cc11bebe930fa13dec86408e8ae6dbc04a037 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20du=20Garreau?= Date: Fri, 30 Oct 2020 19:24:08 +0100 Subject: [PATCH 10/10] Constantify more BTreeMap and BTreeSet functions - BTreeMap::len - BTreeMap::is_empty - BTreeSet::len - BTreeSet::is_empty --- library/alloc/src/collections/btree/map.rs | 6 ++++-- library/alloc/src/collections/btree/map/tests.rs | 7 +++++++ library/alloc/src/collections/btree/set.rs | 6 ++++-- library/alloc/src/collections/btree/set/tests.rs | 7 +++++++ 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/library/alloc/src/collections/btree/map.rs b/library/alloc/src/collections/btree/map.rs index 2704aab322924..07c23d29e20a6 100644 --- a/library/alloc/src/collections/btree/map.rs +++ b/library/alloc/src/collections/btree/map.rs @@ -2188,7 +2188,8 @@ impl BTreeMap { /// assert_eq!(a.len(), 1); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - pub fn len(&self) -> usize { + #[rustc_const_unstable(feature = "const_btree_new", issue = "71835")] + pub const fn len(&self) -> usize { self.length } @@ -2207,7 +2208,8 @@ impl BTreeMap { /// assert!(!a.is_empty()); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - pub fn is_empty(&self) -> bool { + #[rustc_const_unstable(feature = "const_btree_new", issue = "71835")] + pub const fn is_empty(&self) -> bool { self.len() == 0 } diff --git a/library/alloc/src/collections/btree/map/tests.rs b/library/alloc/src/collections/btree/map/tests.rs index adb94972f5bb6..09aabdcd0fb27 100644 --- a/library/alloc/src/collections/btree/map/tests.rs +++ b/library/alloc/src/collections/btree/map/tests.rs @@ -1527,6 +1527,13 @@ fn test_send() { } } +#[allow(dead_code)] +fn test_const() { + const MAP: &'static BTreeMap<(), ()> = &BTreeMap::new(); + const LEN: usize = MAP.len(); + const IS_EMPTY: bool = MAP.is_empty(); +} + #[test] fn test_occupied_entry_key() { let mut a = BTreeMap::new(); diff --git a/library/alloc/src/collections/btree/set.rs b/library/alloc/src/collections/btree/set.rs index 3ad74969bec33..684019f8f5f5e 100644 --- a/library/alloc/src/collections/btree/set.rs +++ b/library/alloc/src/collections/btree/set.rs @@ -950,7 +950,8 @@ impl BTreeSet { /// assert_eq!(v.len(), 1); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - pub fn len(&self) -> usize { + #[rustc_const_unstable(feature = "const_btree_new", issue = "71835")] + pub const fn len(&self) -> usize { self.map.len() } @@ -967,7 +968,8 @@ impl BTreeSet { /// assert!(!v.is_empty()); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - pub fn is_empty(&self) -> bool { + #[rustc_const_unstable(feature = "const_btree_new", issue = "71835")] + pub const fn is_empty(&self) -> bool { self.len() == 0 } } diff --git a/library/alloc/src/collections/btree/set/tests.rs b/library/alloc/src/collections/btree/set/tests.rs index 9267435728216..2069cde4dba3b 100644 --- a/library/alloc/src/collections/btree/set/tests.rs +++ b/library/alloc/src/collections/btree/set/tests.rs @@ -15,6 +15,13 @@ fn test_clone_eq() { assert_eq!(m.clone(), m); } +#[allow(dead_code)] +fn test_const() { + const SET: &'static BTreeSet<()> = &BTreeSet::new(); + const LEN: usize = SET.len(); + const IS_EMPTY: bool = SET.is_empty(); +} + #[test] fn test_iter_min_max() { let mut a = BTreeSet::new();