rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
98.57k stars 12.74k forks source link

ICE: lazy type alias: IOOB `compiler/rustc_hir_analysis/src/variance/constraints.rs` #114468

Closed matthiaskrgr closed 1 year ago

matthiaskrgr commented 1 year ago

Code

original code from tests/ui/issues/issue-20797.rs

// build-pass

// Regression test for #20797.

use std::default::Default;
use std::io;
use std::fs;
use std::path::PathBuf;

pub trait PathExtensions {
    fn is_dir(&self) -> bool { false }
}

impl PathExtensions for PathBuf {}

/// A strategy for acquiring more subpaths to walk.
pub trait Strategy {
    type P: PathExtensions;
    /// Gets additional subpaths from a given path.
    fn get_more(&self, item: &Self::P) -> io::Result<Vec<Self::P>>;
    /// Determine whether a path should be walked further.
    /// This is run against each item from `get_more()`.
    fn prune(&self, p: &Self::P) -> bool;
}

/// The basic fully-recursive strategy. Nothing is pruned.
#[derive(Copy, Clone, Default)]
pub struct Recursive;

impl Strategy for Recursive {
    type P = PathBuf;
    fn get_more(&self, p: &PathBuf) -> io::Result<Vec<PathBuf>> {
        Ok(fs::read_dir(p).unwrap().map(|s| s.unwrap().path()).collect())
    }

    fn prune(&self, _: &PathBuf) -> bool { false }
}

/// A directory walker of `P` using strategy `S`.
pub struct Subpaths<S: Strategy> {
    stack: Vec<S::P>,
    strategy: S,
}

impl<S: Strategy> Subpaths<S> {
    /// Creates a directory walker with a root path and strategy.
    pub fn new(p: &S::P, strategy: S) -> io::Result<Subpaths<S>> {
        let stack = strategy.get_more(p)?;
        Ok(Subpaths { stack: stack, strategy: strategy })
    }
}

impl<S: Default + Strategy> Subpaths<S> {
    /// Creates a directory walker with a root path and a default strategy.
    pub fn walk(p: &S::P) -> io::Result<Subpaths<S>> {
        Subpaths::new(p, Default::default())
    }
}

impl<S: Default + Strategy> Default for Subpaths<S> {
    fn default() -> Subpaths<S> {
        Subpaths { stack: Vec::new(), strategy: Default::default() }
    }
}

impl<S: Strategy> Iterator for Subpaths<S> {
    type Item = S::P;
    fn next (&mut self) -> Option<S::P> {
        let mut opt_path = self.stack.pop();
        while opt_path.is_some() && self.strategy.prune(opt_path.as_ref().unwrap()) {
            opt_path = self.stack.pop();
        }
        match opt_path {
            Some(path) => {
                if path.is_dir() {
                    let result = self.strategy.get_more(&path);
                    match result {
                        Ok(dirs) => { self.stack.extend(dirs); },
                        Err(..) => { }
                    }
                }
                Some(path)
            }
            None => None,
        }
    }
}

fn _foo() {
    let _walker: Subpaths<Recursive> = Subpaths::walk(&PathBuf::from("/home")).unwrap();
}

fn main() {}

reduced

#![feature(lazy_type_alias)]

use std::io;

pub trait Strategy {}

pub struct Subpaths<S: Strategy> {}

impl<S: Strategy> Subpaths<S> {
    pub fn new() -> io::Result<Subpaths<S>> {}
}

Meta

rustc --version --verbose:

rustc 1.73.0-nightly (474709a9a 2023-08-03)
binary: rustc
commit-hash: 474709a9a2a74a8bcf0055fadb335d0ca0d2d939
commit-date: 2023-08-03
host: x86_64-unknown-linux-gnu
release: 1.73.0-nightly
LLVM version: 16.0.5

Error output

<output>
Backtrace

``` warning: the feature `lazy_type_alias` is incomplete and may not be safe to use and/or cause compiler crashes --> treereduce.out:1:12 | 1 | #![feature(lazy_type_alias)] | ^^^^^^^^^^^^^^^ | = note: see issue #112792 for more information = note: `#[warn(incomplete_features)]` on by default error[E0601]: `main` function not found in crate `treereduce` --> treereduce.out:11:2 | 11 | } | ^ consider adding a `main` function to `treereduce.out` thread 'rustc' panicked at compiler/rustc_hir_analysis/src/variance/constraints.rs:362:36: index out of bounds: the len is 0 but the index is 0 stack backtrace: 0: 0x7f2b33b6313c - std::backtrace_rs::backtrace::libunwind::trace::h607c5cae6680b0fb at /rustc/4f7bb9890c0402cd145556ac1929d13d7524959e/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5 1: 0x7f2b33b6313c - std::backtrace_rs::backtrace::trace_unsynchronized::hc1cbea2e412d60ef at /rustc/4f7bb9890c0402cd145556ac1929d13d7524959e/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5 2: 0x7f2b33b6313c - std::sys_common::backtrace::_print_fmt::h62a9831c7ff163f5 at /rustc/4f7bb9890c0402cd145556ac1929d13d7524959e/library/std/src/sys_common/backtrace.rs:67:5 3: 0x7f2b33b6313c - ::fmt::hf49ca0eb9dc1b6fd at /rustc/4f7bb9890c0402cd145556ac1929d13d7524959e/library/std/src/sys_common/backtrace.rs:44:22 4: 0x7f2b33bc983c - core::fmt::rt::Argument::fmt::h9cc0ab3f56c44ea1 at /rustc/4f7bb9890c0402cd145556ac1929d13d7524959e/library/core/src/fmt/rt.rs:138:9 5: 0x7f2b33bc983c - core::fmt::write::h87aa5f2149ab03a1 at /rustc/4f7bb9890c0402cd145556ac1929d13d7524959e/library/core/src/fmt/mod.rs:1094:21 6: 0x7f2b33b556de - std::io::Write::write_fmt::h04dc89e115afce4f at /rustc/4f7bb9890c0402cd145556ac1929d13d7524959e/library/std/src/io/mod.rs:1714:15 7: 0x7f2b33b62f25 - std::sys_common::backtrace::_print::h4e2feb31096e94de at /rustc/4f7bb9890c0402cd145556ac1929d13d7524959e/library/std/src/sys_common/backtrace.rs:47:5 8: 0x7f2b33b62f25 - std::sys_common::backtrace::print::h2c16c4ace2baa3ab at /rustc/4f7bb9890c0402cd145556ac1929d13d7524959e/library/std/src/sys_common/backtrace.rs:34:9 9: 0x7f2b33b6607a - std::panicking::panic_hook_with_disk_dump::{{closure}}::h18ad7c40aef108e0 at /rustc/4f7bb9890c0402cd145556ac1929d13d7524959e/library/std/src/panicking.rs:278:22 10: 0x7f2b33b65d13 - std::panicking::panic_hook_with_disk_dump::hce08e9b5439b012b at /rustc/4f7bb9890c0402cd145556ac1929d13d7524959e/library/std/src/panicking.rs:312:9 11: 0x7f2b36e59f99 - rustc_driver_impl[2f92a0ccc46c059]::install_ice_hook::{closure#0} 12: 0x7f2b33b66920 - as core::ops::function::Fn>::call::h02014ddf1cce8192 at /rustc/4f7bb9890c0402cd145556ac1929d13d7524959e/library/alloc/src/boxed.rs:2021:9 13: 0x7f2b33b66920 - std::panicking::rust_panic_with_hook::h07367ca72cc1b034 at /rustc/4f7bb9890c0402cd145556ac1929d13d7524959e/library/std/src/panicking.rs:733:13 14: 0x7f2b33b666a7 - std::panicking::begin_panic_handler::{{closure}}::h1091911fd2e9526e at /rustc/4f7bb9890c0402cd145556ac1929d13d7524959e/library/std/src/panicking.rs:621:13 15: 0x7f2b33b63676 - std::sys_common::backtrace::__rust_end_short_backtrace::h6770f526fbcecfe3 at /rustc/4f7bb9890c0402cd145556ac1929d13d7524959e/library/std/src/sys_common/backtrace.rs:170:18 16: 0x7f2b33b663f2 - rust_begin_unwind at /rustc/4f7bb9890c0402cd145556ac1929d13d7524959e/library/std/src/panicking.rs:617:5 17: 0x7f2b33bc5bf3 - core::panicking::panic_fmt::h2ea3095a9e99667b at /rustc/4f7bb9890c0402cd145556ac1929d13d7524959e/library/core/src/panicking.rs:67:14 18: 0x7f2b33bc5d52 - core::panicking::panic_bounds_check::hf798c1571563a745 at /rustc/4f7bb9890c0402cd145556ac1929d13d7524959e/library/core/src/panicking.rs:162:5 19: 0x7f2b36fbe3c3 - ::add_constraints_from_args 20: 0x7f2b35734c40 - ::build_constraints_for_item 21: 0x7f2b357337d9 - rustc_hir_analysis[1f87ebcc916a8bb0]::variance::crate_variances 22: 0x7f2b36509694 - rustc_query_impl[49f881e28274e957]::plumbing::__rust_begin_short_backtrace::> 23: 0x7f2b36509679 - >::call_once 24: 0x7f2b362ae62c - rustc_query_system[cfe4b6087db7ea89]::query::plumbing::try_execute_query::>, false, false, false>, rustc_query_impl[49f881e28274e957]::plumbing::QueryCtxt, false> 25: 0x7f2b367bbc45 - rustc_query_impl[49f881e28274e957]::query_impl::crate_variances::get_query_non_incr::__rust_end_short_backtrace 26: 0x7f2b35a7ef4c - rustc_hir_analysis[1f87ebcc916a8bb0]::variance::variances_of 27: 0x7f2b34e23893 - rustc_query_impl[49f881e28274e957]::plumbing::__rust_begin_short_backtrace::> 28: 0x7f2b34e23855 - >::call_once 29: 0x7f2b34ea5964 - rustc_query_system[cfe4b6087db7ea89]::query::plumbing::try_execute_query::>, false, false, false>, rustc_query_impl[49f881e28274e957]::plumbing::QueryCtxt, false> 30: 0x7f2b367bbfd3 - rustc_query_impl[49f881e28274e957]::query_impl::variances_of::get_query_non_incr::__rust_end_short_backtrace 31: 0x7f2b35771bdf - rustc_middle[1d4c141020d5ff1d]::query::plumbing::query_get_at::>> 32: 0x7f2b357925ec - rustc_hir_analysis[1f87ebcc916a8bb0]::check::wfcheck::check_variances_for_type_defn 33: 0x7f2b3576fd0f - rustc_hir_analysis[1f87ebcc916a8bb0]::check::wfcheck::check_well_formed 34: 0x7f2b34e11a7c - rustc_query_impl[49f881e28274e957]::plumbing::__rust_begin_short_backtrace::> 35: 0x7f2b35799986 - rustc_query_system[cfe4b6087db7ea89]::query::plumbing::try_execute_query::>, false, false, false>, rustc_query_impl[49f881e28274e957]::plumbing::QueryCtxt, false> 36: 0x7f2b357996a2 - rustc_query_impl[49f881e28274e957]::query_impl::check_well_formed::get_query_non_incr::__rust_end_short_backtrace 37: 0x7f2b3608a5bc - rustc_data_structures[d4f5b56d4da9ff99]::sync::par_for_each_in::<&[rustc_hir[4b4119f5a2aa97da]::hir::ImplItemId], ::par_impl_items::{closure#0}> 38: 0x7f2b3608a39a - rustc_hir_analysis[1f87ebcc916a8bb0]::check::wfcheck::check_mod_type_wf 39: 0x7f2b35ab967c - rustc_query_impl[49f881e28274e957]::plumbing::__rust_begin_short_backtrace::> 40: 0x7f2b35ab965e - >::call_once 41: 0x7f2b356d75df - rustc_query_system[cfe4b6087db7ea89]::query::plumbing::try_execute_query::>, false, false, false>, rustc_query_impl[49f881e28274e957]::plumbing::QueryCtxt, false> 42: 0x7f2b367c0eb2 - rustc_query_impl[49f881e28274e957]::query_impl::check_mod_type_wf::get_query_non_incr::__rust_end_short_backtrace 43: 0x7f2b362c2ed7 - rustc_data_structures[d4f5b56d4da9ff99]::sync::par_for_each_in::<&[rustc_hir[4b4119f5a2aa97da]::hir_id::OwnerId], ::par_for_each_module::{closure#0}> 44: 0x7f2b362c228e - rustc_hir_analysis[1f87ebcc916a8bb0]::check_crate 45: 0x7f2b362bcb7a - rustc_interface[6d12d522b9b133dc]::passes::analysis 46: 0x7f2b36331c9a - rustc_query_impl[49f881e28274e957]::plumbing::__rust_begin_short_backtrace::> 47: 0x7f2b36331c89 - >::call_once 48: 0x7f2b364cf5d8 - rustc_query_system[cfe4b6087db7ea89]::query::plumbing::try_execute_query::>, false, false, false>, rustc_query_impl[49f881e28274e957]::plumbing::QueryCtxt, false> 49: 0x7f2b364cf367 - rustc_query_impl[49f881e28274e957]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace 50: 0x7f2b360ca1c5 - ::enter::> 51: 0x7f2b360c97f2 - ::enter::, rustc_span[87ac8800f9b2bd45]::ErrorGuaranteed>> 52: 0x7f2b360c28c8 - std[b67388f384878aed]::sys_common::backtrace::__rust_begin_short_backtrace::, rustc_driver_impl[2f92a0ccc46c059]::run_compiler::{closure#1}>::{closure#0}, core[71fbaba24b011fa4]::result::Result<(), rustc_span[87ac8800f9b2bd45]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[71fbaba24b011fa4]::result::Result<(), rustc_span[87ac8800f9b2bd45]::ErrorGuaranteed>> 53: 0x7f2b360c204e - <::spawn_unchecked_, rustc_driver_impl[2f92a0ccc46c059]::run_compiler::{closure#1}>::{closure#0}, core[71fbaba24b011fa4]::result::Result<(), rustc_span[87ac8800f9b2bd45]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[71fbaba24b011fa4]::result::Result<(), rustc_span[87ac8800f9b2bd45]::ErrorGuaranteed>>::{closure#1} as core[71fbaba24b011fa4]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0} 54: 0x7f2b33b710c5 - as core::ops::function::FnOnce>::call_once::h50b018fa15c55b61 at /rustc/4f7bb9890c0402cd145556ac1929d13d7524959e/library/alloc/src/boxed.rs:2007:9 55: 0x7f2b33b710c5 - as core::ops::function::FnOnce>::call_once::h30bf8c9edaaa8962 at /rustc/4f7bb9890c0402cd145556ac1929d13d7524959e/library/alloc/src/boxed.rs:2007:9 56: 0x7f2b33b710c5 - std::sys::unix::thread::Thread::new::thread_start::hd4703b35fe8f9aee at /rustc/4f7bb9890c0402cd145556ac1929d13d7524959e/library/std/src/sys/unix/thread.rs:108:17 57: 0x7f2b3390244b - 58: 0x7f2b33985e40 - 59: 0x0 - error: 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: please attach the file at `/tmp/im/rustc-ice-2023-08-04T15:16:50.896216586Z-99271.txt` to your bug report query stack during panic: #0 [crate_variances] computing the variances for items in this crate #1 [variances_of] computing the variances of `Subpaths` #2 [check_well_formed] checking that `Subpaths` is well-formed #3 [check_mod_type_wf] checking that types are well-formed in top-level module #4 [analysis] running analysis passes on this crate end of query stack error: aborting due to previous error; 1 warning emitted For more information about this error, try `rustc --explain E0601`. ```

matthiaskrgr commented 1 year ago

https://github.com/rust-lang/rust/pull/114253 cc @fmease

fmease commented 1 year ago

@rustbot label -needs-triage @rustbot claim

fmease commented 1 year ago

Argh, I kinda knew that external crates were gonna haunt me (external crate: eager type aliases, local crate: lazy type aliases). This problem won't go away either once edition checks are involved.