Skip to content

[WIP] const-stabilize Hash{Map/Set}::new #118416

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

Closed
Closed
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: 2 additions & 0 deletions compiler/rustc_const_eval/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,9 @@ const_eval_unreachable_unwind =
const_eval_unsigned_offset_from_overflow =
`ptr_offset_from_unsigned` called when first pointer has smaller offset than second: {$a_offset} < {$b_offset}
const_eval_unsized_local = unsized locals are not supported

const_eval_unstable_const_fn = `{$def_path}` is not yet stable as a const fn
const_eval_unstable_declared_here = `{$def_path}` declared unstable here

const_eval_unstable_in_stable =
const-stable function cannot use `#[feature({$gate})]`
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_const_eval/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ pub(crate) struct UnstableConstFn {
#[primary_span]
pub span: Span,
pub def_path: String,

#[label(const_eval_unstable_declared_here)]
pub declared: Span,
}

#[derive(Diagnostic)]
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_const_eval/src/transform/check_consts/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -992,8 +992,10 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
// `extern` functions, and these have no way to get marked `const`. So instead we
// use `rustc_const_(un)stable` attributes to mean that the intrinsic is `const`
if self.ccx.is_const_stable_const_fn() || tcx.is_intrinsic(callee) {
self.check_op(ops::FnCallUnstable(callee, None));
return;
if !super::rustc_allow_const_fn_unstable(tcx, caller, sym::any) {
self.check_op(ops::FnCallUnstable(callee, None));
return;
}
}
}
trace!("permitting call");
Expand Down
9 changes: 5 additions & 4 deletions compiler/rustc_const_eval/src/transform/check_consts/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,10 +339,11 @@ impl<'tcx> NonConstOp<'tcx> for FnCallUnstable {
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
let FnCallUnstable(def_id, feature) = *self;

let mut err = ccx
.tcx
.sess
.create_err(errors::UnstableConstFn { span, def_path: ccx.tcx.def_path_str(def_id) });
let mut err = ccx.tcx.sess.create_err(errors::UnstableConstFn {
span,
def_path: ccx.tcx.def_path_str(def_id),
declared: ccx.tcx.def_span(def_id),
});

if ccx.is_const_stable_const_fn() {
err.help("const-stable functions can only call other const-stable functions");
Expand Down
13 changes: 12 additions & 1 deletion library/std/src/collections/hash/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,18 @@ impl<K, V, S> HashMap<K, V, S> {
/// ```
#[inline]
#[stable(feature = "hashmap_build_hasher", since = "1.7.0")]
#[rustc_const_unstable(feature = "const_collections_with_hasher", issue = "102575")]
#[cfg_attr(
bootstrap,
rustc_const_unstable(feature = "const_collections_with_hasher", issue = "102575")
)]
#[cfg_attr(
not(bootstrap),
rustc_const_stable(
feature = "const_collections_with_hasher",
since = "CURRENT_RUSTC_VERSION"
)
)]
#[rustc_allow_const_fn_unstable(any)]
pub const fn with_hasher(hash_builder: S) -> HashMap<K, V, S> {
HashMap { base: base::HashMap::with_hasher(hash_builder) }
}
Expand Down
13 changes: 12 additions & 1 deletion library/std/src/collections/hash/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,18 @@ impl<T, S> HashSet<T, S> {
/// ```
#[inline]
#[stable(feature = "hashmap_build_hasher", since = "1.7.0")]
#[rustc_const_unstable(feature = "const_collections_with_hasher", issue = "102575")]
#[cfg_attr(
bootstrap,
rustc_const_unstable(feature = "const_collections_with_hasher", issue = "102575")
)]
#[cfg_attr(
not(bootstrap),
rustc_const_stable(
feature = "const_collections_with_hasher",
since = "CURRENT_RUSTC_VERSION"
)
)]
#[rustc_allow_const_fn_unstable(any)]
pub const fn with_hasher(hasher: S) -> HashSet<T, S> {
HashSet { base: base::HashSet::with_hasher(hasher) }
}
Expand Down
3 changes: 2 additions & 1 deletion library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@
#![feature(no_sanitize)]
#![feature(platform_intrinsics)]
#![feature(prelude_import)]
#![feature(rustc_allow_const_fn_unstable)]
#![feature(rustc_attrs)]
#![feature(rustdoc_internals)]
#![feature(staged_api)]
Expand All @@ -308,6 +309,7 @@
//
// Library features (core):
// tidy-alphabetical-start
#![cfg_attr(bootstrap, feature(const_collections_with_hasher))]
#![feature(char_internals)]
#![feature(core_intrinsics)]
#![feature(duration_constants)]
Expand Down Expand Up @@ -387,7 +389,6 @@
//
// Only for const-ness:
// tidy-alphabetical-start
#![feature(const_collections_with_hasher)]
#![feature(const_hash)]
#![feature(const_io_structs)]
#![feature(const_ip)]
Expand Down