Skip to content

Commit 7849230

Browse files
committed
Forbid implementing Freeze even if the trait is stabilized
1 parent f030d49 commit 7849230

File tree

10 files changed

+62
-5
lines changed

10 files changed

+62
-5
lines changed

compiler/rustc_codegen_cranelift/example/mini_core.rs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
rustc_attrs,
99
transparent_unions,
1010
auto_traits,
11+
freeze_impls,
1112
thread_local
1213
)]
1314
#![no_core]

compiler/rustc_codegen_gcc/example/mini_core.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![feature(
22
no_core, lang_items, intrinsics, unboxed_closures, type_ascription, extern_types,
3-
decl_macro, rustc_attrs, transparent_unions, auto_traits,
3+
decl_macro, rustc_attrs, transparent_unions, auto_traits, freeze_impls,
44
thread_local
55
)]
66
#![no_core]

compiler/rustc_feature/src/unstable.rs

+2
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,8 @@ declare_features! (
469469
(unstable, fn_align, "1.53.0", Some(82232)),
470470
/// Support delegating implementation of functions to other already implemented functions.
471471
(incomplete, fn_delegation, "1.76.0", Some(118212)),
472+
/// Allows impls for the Freeze trait.
473+
(internal, freeze_impls, "CURRENT_RUSTC_VERSION", Some(121675)),
472474
/// Allows defining gen blocks and `gen fn`.
473475
(unstable, gen_blocks, "1.75.0", Some(117078)),
474476
/// Infer generic args for both consts and types.

compiler/rustc_hir_analysis/src/coherence/mod.rs

+14
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use rustc_errors::{codes::*, struct_span_code_err};
1010
use rustc_hir::def_id::{DefId, LocalDefId};
1111
use rustc_middle::query::Providers;
1212
use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt};
13+
use rustc_session::parse::feature_err;
1314
use rustc_span::{sym, ErrorGuaranteed};
1415
use rustc_trait_selection::traits;
1516

@@ -49,6 +50,19 @@ fn enforce_trait_manually_implementable(
4950
) -> Result<(), ErrorGuaranteed> {
5051
let impl_header_span = tcx.def_span(impl_def_id);
5152

53+
if tcx.lang_items().freeze_trait() == Some(trait_def_id) {
54+
if !tcx.features().freeze_impls {
55+
feature_err(
56+
&tcx.sess,
57+
sym::freeze_impls,
58+
impl_header_span,
59+
"explicit impls for the `Freeze` trait are not permitted",
60+
)
61+
.with_span_label(impl_header_span, format!("impl of `Freeze` not allowed"))
62+
.emit();
63+
}
64+
}
65+
5266
// Disallow *all* explicit impls of traits marked `#[rustc_deny_explicit_impl]`
5367
if trait_def.deny_explicit_impl {
5468
let trait_name = tcx.item_name(trait_def_id);

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,7 @@ symbols! {
841841
format_placeholder,
842842
format_unsafe_arg,
843843
freeze,
844+
freeze_impls,
844845
freg,
845846
frem_algebraic,
846847
frem_fast,

library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@
203203
// Language features:
204204
// tidy-alphabetical-start
205205
#![cfg_attr(bootstrap, feature(platform_intrinsics))]
206+
#![cfg_attr(not(bootstrap), feature(freeze_impls))]
206207
#![feature(abi_unadjusted)]
207208
#![feature(adt_const_params)]
208209
#![feature(allow_internal_unsafe)]

library/core/src/marker.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -818,13 +818,13 @@ pub trait DiscriminantKind {
818818
/// will not contain interior mutability, and subsequently allow
819819
/// placing the constant behind references.
820820
#[lang = "freeze"]
821-
#[unstable(feature = "freeze", issue = "60715")]
821+
#[unstable(feature = "freeze", issue = "121675")]
822822
pub unsafe auto trait Freeze {}
823823

824-
#[unstable(feature = "freeze", issue = "60715")]
824+
#[unstable(feature = "freeze", issue = "121675")]
825825
impl<T: ?Sized> !Freeze for UnsafeCell<T> {}
826826
marker_impls! {
827-
#[unstable(feature = "freeze", issue = "60715")]
827+
#[unstable(feature = "freeze", issue = "121675")]
828828
unsafe Freeze for
829829
{T: ?Sized} PhantomData<T>,
830830
{T: ?Sized} *const T,

tests/run-make/min-global-align/min_global_align.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(no_core, lang_items)]
1+
#![feature(no_core, lang_items, freeze_impls)]
22
#![crate_type = "rlib"]
33
#![no_core]
44

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![feature(freeze, negative_impls)]
2+
3+
use std::marker::Freeze;
4+
5+
struct Foo;
6+
7+
unsafe impl Freeze for Foo {}
8+
//~^ explicit impls for the `Freeze` trait are not permitted
9+
10+
struct Bar;
11+
12+
impl !Freeze for Bar {}
13+
//~^ explicit impls for the `Freeze` trait are not permitted
14+
15+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error[E0658]: explicit impls for the `Freeze` trait are not permitted
2+
--> $DIR/feature-gate-freeze-impls.rs:7:1
3+
|
4+
LL | unsafe impl Freeze for Foo {}
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ impl of `Freeze` not allowed
6+
|
7+
= note: see issue #121675 <https://github.com/rust-lang/rust/issues/121675> for more information
8+
= help: add `#![feature(freeze_impls)]` to the crate attributes to enable
9+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
10+
11+
error[E0658]: explicit impls for the `Freeze` trait are not permitted
12+
--> $DIR/feature-gate-freeze-impls.rs:12:1
13+
|
14+
LL | impl !Freeze for Bar {}
15+
| ^^^^^^^^^^^^^^^^^^^^ impl of `Freeze` not allowed
16+
|
17+
= note: see issue #121675 <https://github.com/rust-lang/rust/issues/121675> for more information
18+
= help: add `#![feature(freeze_impls)]` to the crate attributes to enable
19+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
20+
21+
error: aborting due to 2 previous errors
22+
23+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)