Skip to content

! can't be cast to Fn(_) -> _ #120153

Open
Open
@Pr0methean

Description

@Pr0methean

I tried this code:

#[derive(Debug)]
pub struct SharedBufferRng<const WORDS_PER_SEED: usize, const SEEDS_CAPACITY: usize, SourceType: Rng,
FallbackFn: Send + Sync + Clone + Fn() -> SourceType> {
    receiver: Receiver<DefaultableAlignedArray<WORDS_PER_SEED, u64>>,
    calling_thread_fallback: Option<FallbackFn>
}
impl<
        const WORDS_PER_SEED: usize,
        const SEEDS_CAPACITY: usize,
        SourceType: Rng + Send + Debug + 'static,
        FallbackFn: Sync + Send + Clone + Fn() -> SourceType
    > SharedBufferRng<WORDS_PER_SEED, SEEDS_CAPACITY, SourceType, FallbackFn>
    where [(); WORDS_PER_SEED * size_of::<u64>()]:, [(); WORDS_PER_SEED * SEEDS_CAPACITY * size_of::<u64>()]:
{
    /// Creates an RNG that will have a new dedicated thread reading from [source] into a new buffer that's shared with
    /// all clones of this [SharedBufferRng].
    pub fn new(mut source: SourceType, fallback: Option<FallbackFn>) -> Self {/*...*/}
}
#[test]
fn test() {
     let seeder: SharedBufferRng<8, 4, _, _> =
         SharedBufferRng::new(BlockRng64::new(source.clone()), None::<!>);
}

I expected to see this happen: the code would compile. Option<!> would be a valid type with zero size, and would cast to Option<T> for all T, because the Some variant would be eliminated during monomorphization. In fact None would always be of type Option<!> unless and until the compiler found a way for it to become Some.

Instead, this happened: it didn't compile until I changed the turbofish to ::<fn(_) -> _>

Meta

rustc --version --verbose:

rustc 1.77.0-nightly (fb5ed726f 2023-12-28)
binary: rustc
commit-hash: fb5ed726f72c6d16c788517c60ec00d4564b9348
commit-date: 2023-12-28
host: x86_64-apple-darwin
release: 1.77.0-nightly
LLVM version: 17.0.6
Backtrace

<backtrace>

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-discussionCategory: Discussion or questions that doesn't represent real issues.F-never_type`#![feature(never_type)]`

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions