Skip to content

ICE: cannot relate bound region: ReLateBound #82612

Closed
@cart

Description

@cart

Code

While writing a SparseArray impl, I forgot a return on value.get_or_insert_with(func). This should be an error, but I got an ice instead. This fails with the expected on stable.

use std::marker::PhantomData;

pub trait SparseSetIndex: Clone {
    fn sparse_set_index(&self) -> usize;
    fn get_sparse_set_index(value: usize) -> Self;
}


#[derive(Debug)]
pub struct SparseArray<I, V = I> {
    values: Vec<Option<V>>,
    marker: PhantomData<I>,
}

impl<I: SparseSetIndex, V> Default for SparseArray<I, V> {
    fn default() -> Self {
        Self::new()
    }
}

impl<I: SparseSetIndex, V> SparseArray<I, V> {
    #[inline]
    pub fn get_or_insert_with(&mut self, index: I, func: impl FnOnce() -> V) -> &mut V {
        let index = index.sparse_set_index();
        if index < self.values.len() {
            // SAFE: just checked bounds
            let value = unsafe { self.values.get_unchecked_mut(index) };
            value.get_or_insert_with(func)
        }
        self.values.resize_with(index + 1, || None);
        // SAFE: just inserted
        unsafe {
            let value = self.values.get_unchecked_mut(index);
            *value = Some(func());
            value.as_mut().unwrap()
        }
    }
}
fn main() {
}

playground: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=a3776450ab79249a20b477f8365c88df

(Note that switching to stable resolves the ICE)

Meta

rustc --version --verbose:

rustc 1.52.0-nightly (98f8cce6d 2021-02-25)
binary: rustc
commit-hash: 98f8cce6db6c6c6660eeffee2b3903104e547ecf
commit-date: 2021-02-25
host: x86_64-unknown-linux-gnu
release: 1.52.0-nightly
LLVM version: 11.0.1

Error output

error: internal compiler error: compiler/rustc_infer/src/infer/region_constraints/mod.rs:576:17: cannot relate bound region: ReLateBound(DebruijnIndex(0), BoundRegion { kind: BrAnon(0) }) <= '_#14r

thread 'rustc' panicked at 'Box<Any>', /rustc/9c09c1f7cfcf9de0522bcd1cfda32b552195c464/library/std/src/panic.rs:59:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: rustc 1.52.0-nightly (9c09c1f7c 2021-02-26) running on x86_64-unknown-linux-gnu

note: compiler flags: -C embed-bitcode=no -C codegen-units=1 -C debuginfo=2 --crate-type bin

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
#0 [typeck] type-checking `<impl at src/main.rs:21:1: 38:2>::get_or_insert_with`
#1 [typeck_item_bodies] type-checking all item bodies
end of query stack
error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0599`.
error: could not compile `playground`

To learn more, run the command again with --verbose.

Metadata

Metadata

Assignees

Labels

A-lifetimesArea: Lifetimes / regionsC-bugCategory: This is a bug.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-nightlyPerformance or correctness regression from stable to nightly.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions