rust-lang / rust-lang/rust

`std::marker::Freeze` disallows blanket impl since it "could be implemented on `Cell` and `UnsafeCell` in the future"

Open
#133,714 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug F-freeze T-compiler
Dominant language
Rust
Stars
119k
Forks
16.2k
PR merge metrics
PR metrics pending

Description

I tried this code:

#![feature(freeze)]
use std::{cell::Cell, marker::Freeze};

unsafe trait IntoFreeze {
  type Frozen: Freeze;
}

unsafe impl<T: Freeze> IntoFreeze for T {
  type Frozen = T;
}

unsafe impl<T: IntoFreeze> IntoFreeze for Cell<T> {
  type Frozen = T::Frozen;
}

I expected to see this happen: Code should compile since Freeze will never be implemented for Cell and UnsafeCell.

Instead, this happened:

error[E0119]: conflicting implementations of trait `IntoFreeze` for type `Cell<_>`
  --> src/lib.rs:12:1
   |
8  | unsafe impl<T: Freeze> IntoFreeze for T {
   | --------------------------------------- first implementation here
...
12 | unsafe impl<T: IntoFreeze> IntoFreeze for Cell<T> {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `Cell<_>`
   |
   = note: upstream crates may add a new impl of trait `std::marker::Freeze` for type `std::cell::Cell<_>` in future versions

For more information about this error, try `rustc --explain E0119`.
Meta

rustc --version --verbose:

1.85.0-nightly (2024-11-30 7442931d49b199ad0a1c)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducer in src/lib.rs and the E0119 diagnostic involving std::marker::Freeze, std::cell::Cell, and UnsafeCell. Trace the compiler's trait-coherence handling for the blanket and Cell implementations, then verify the reported example against the expected compilation behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.