Skip to content
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

Stabilize const BTree{Map,Set}::new #102197

Merged
merged 2 commits into from
Sep 26, 2022
Merged
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
2 changes: 1 addition & 1 deletion compiler/rustc_hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#![feature(associated_type_defaults)]
#![feature(closure_track_caller)]
#![feature(const_btree_new)]
#![feature(const_btree_len)]
#![cfg_attr(bootstrap, feature(let_else))]
#![feature(once_cell)]
#![feature(min_specialization)]
Expand Down
6 changes: 3 additions & 3 deletions library/alloc/src/collections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ impl<K, V> BTreeMap<K, V> {
/// map.insert(1, "a");
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
#[rustc_const_stable(feature = "const_btree_new", since = "CURRENT_RUSTC_VERSION")]
#[must_use]
pub const fn new() -> BTreeMap<K, V> {
BTreeMap { root: None, length: 0, alloc: ManuallyDrop::new(Global), _marker: PhantomData }
Expand Down Expand Up @@ -2392,7 +2392,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
/// ```
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
#[rustc_const_unstable(feature = "const_btree_len", issue = "71835")]
pub const fn len(&self) -> usize {
self.length
}
Expand All @@ -2413,7 +2413,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
/// ```
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
#[rustc_const_unstable(feature = "const_btree_len", issue = "71835")]
pub const fn is_empty(&self) -> bool {
self.len() == 0
}
Expand Down
6 changes: 3 additions & 3 deletions library/alloc/src/collections/btree/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ impl<T> BTreeSet<T> {
/// let mut set: BTreeSet<i32> = BTreeSet::new();
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
#[rustc_const_stable(feature = "const_btree_new", since = "CURRENT_RUSTC_VERSION")]
#[must_use]
pub const fn new() -> BTreeSet<T> {
BTreeSet { map: BTreeMap::new() }
Expand Down Expand Up @@ -1174,7 +1174,7 @@ impl<T, A: Allocator + Clone> BTreeSet<T, A> {
/// ```
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
#[rustc_const_unstable(feature = "const_btree_len", issue = "71835")]
pub const fn len(&self) -> usize {
self.map.len()
}
Expand All @@ -1193,7 +1193,7 @@ impl<T, A: Allocator + Clone> BTreeSet<T, A> {
/// ```
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
#[rustc_const_unstable(feature = "const_btree_len", issue = "71835")]
pub const fn is_empty(&self) -> bool {
self.len() == 0
}
Expand Down
2 changes: 1 addition & 1 deletion library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
#![feature(coerce_unsized)]
#![cfg_attr(not(no_global_oom_handling), feature(const_alloc_error))]
#![feature(const_box)]
#![cfg_attr(not(no_global_oom_handling), feature(const_btree_new))]
#![cfg_attr(not(no_global_oom_handling), feature(const_btree_len))]
#![feature(const_cow_is_borrowed)]
#![feature(const_convert)]
#![feature(const_size_of_val)]
Expand Down
2 changes: 1 addition & 1 deletion library/alloc/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#![feature(slice_group_by)]
#![feature(slice_partition_dedup)]
#![feature(string_remove_matches)]
#![feature(const_btree_new)]
#![feature(const_btree_len)]
#![feature(const_default_impls)]
#![feature(const_trait_impl)]
#![feature(const_str_from_utf8)]
Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/consts/issue-88071.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
//
// regression test for #88071

#![feature(const_btree_new)]

use std::collections::BTreeMap;

pub struct CustomMap<K, V>(BTreeMap<K, V>);
Expand Down
13 changes: 12 additions & 1 deletion src/tools/clippy/clippy_utils/src/qualify_min_const_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,10 +367,21 @@ fn is_const_fn(tcx: TyCtxt<'_>, def_id: DefId, msrv: Option<RustcVersion>) -> bo
// Checking MSRV is manually necessary because `rustc` has no such concept. This entire
// function could be removed if `rustc` provided a MSRV-aware version of `is_const_fn`.
// as a part of an unimplemented MSRV check https://github.com/rust-lang/rust/issues/65262.

// HACK(nilstrieb): CURRENT_RUSTC_VERSION can return versions like 1.66.0-dev. `rustc-semver` doesn't accept
Copy link
Member

Choose a reason for hiding this comment

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

@est31 might be good to followup on this and figure out what's happening here -- we probably shouldn't be returning -dev in the version, since we don't include -nightly for example.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah the -dev is being returned because the CFG_VERSION env var contains it. That env var itself is set by the bootstrap tooling (and it also adds -nightly postfixes FTR). I originally went with the most simple code and didn't strip the postfix because that would have involved parsing the version number (like this code is doing). I'd thought it wouldn't be much of a problem, it seems that most of the tooling could handle the postfix.

I like the -nightly postfix because it gives it a notion of preliminarity: attention that compiler has not been released yet, the stabilization might be reverted (like never has been multiple times, or let chains very recently). Also, sometimes that number can lie: for example right now the cast_const function is shown to be stabilized on 1.66.0-nightly in the nightly docs:

Screenshot_20220926_083953

Even though it's actually been stabilized in 1.65.0, and beta already correctly shows this:

Screenshot_20220926_084224

Admittedly, this only occurs during a ~one week period during the release cycle, in this particular instance because #102051 hasn't been merged yet to master, which changes the placeholder to 1.65.0. But I think the -nightly postfix helps here in this instance, makes people more cautious that it might be wrong. Or idk.

So I'm not that unhappy with the current postfix, but I'm willing to remove it, if it's really not wanted. #101772 has introduced the rust_version_symbol function, so there is a centralized place to change it, to add code not dissimilar from this one.

// the `-dev` version number so we have to strip it off.
let short_version = since
.as_str()
.split('-')
.next()
.expect("rustc_attr::StabilityLevel::Stable::since` is empty");

let since = rustc_span::Symbol::intern(short_version);

crate::meets_msrv(
msrv,
RustcVersion::parse(since.as_str())
.expect("`rustc_attr::StabilityLevel::Stable::since` is ill-formatted"),
.unwrap_or_else(|err| panic!("`rustc_attr::StabilityLevel::Stable::since` is ill-formatted: `{since}`, {err:?}")),
)
} else {
// Unstable const fn with the feature enabled.
Expand Down
6 changes: 3 additions & 3 deletions src/tools/clippy/tests/ui/crashes/ice-7126.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// This test requires a feature gated const fn and will stop working in the future.

#![feature(const_btree_new)]
#![feature(const_btree_len)]

use std::collections::BTreeMap;

struct Foo(BTreeMap<i32, i32>);
struct Foo(usize);
impl Foo {
fn new() -> Self {
Self(BTreeMap::new())
Self(BTreeMap::len(&BTreeMap::<u8, u8>::new()))
}
}

Expand Down