Skip to content

ICE related to if let Some(_) = ... #78426

Closed
@bmgxyz

Description

@bmgxyz

Code

I encountered this bug while running tests for this repo under clang-11. To do that, I ran

.../linux-kernel-module-rust/tests$ CLANG=clang-11 ./run_tests.py

On versions starting with nightly-2020-09-22, the compiler produces the three errors about if let Some(_) shown in the Error output section below. I applied the three compiler suggestions, resulting in this diff

diff --git a/src/file_operations.rs b/src/file_operations.rs
index 1f7d85c..c2ba4b8 100644
--- a/src/file_operations.rs
+++ b/src/file_operations.rs
@@ -144,17 +144,17 @@ impl<T: FileOperations> FileOperationsVtable<T> {
     pub(crate) const VTABLE: bindings::file_operations = bindings::file_operations {
         open: Some(open_callback::<T>),
         release: Some(release_callback::<T>),
-        read: if let Some(_) = T::READ {
+        read: if T::READ.is_some() = T::READ {
             Some(read_callback::<T>)
         } else {
             None
         },
-        write: if let Some(_) = T::WRITE {
+        write: if T::WRITE.is_some() = T::WRITE {
             Some(write_callback::<T>)
         } else {
             None
         },
-        llseek: if let Some(_) = T::SEEK {
+        llseek: if T::SEEK.is_some() = T::SEEK {
             Some(llseek_callback::<T>)
         } else {
             None

which upon re-running the tests produced the second error shown below.

The original code in part reads

...
impl<T: FileOperations> FileOperationsVtable<T> {
    pub(crate) const VTABLE: bindings::file_operations = bindings::file_operations {
        open: Some(open_callback::<T>),
        release: Some(release_callback::<T>),
        read: if T::READ.is_some() = T::READ {
            Some(read_callback::<T>)
        } else {
            None
        },
        write: if T::WRITE.is_some() = T::WRITE {
            Some(write_callback::<T>)
        } else {
            None
        },
        llseek: if T::SEEK.is_some() = T::SEEK {
            Some(llseek_callback::<T>)
        } else {
            None
        },
...

I realize that this isn't a minimal example, but I'm not sure what could be causing this bug, so I don't know how to minimize it.

Meta

I'm using nightly because the repo I'm working in requires it.

I tested several different versions to try to isolate when the bug appeared. The earliest example I could find was:

rustc --version --verbose:

rustc 1.48.0-nightly (0e2c1281e 2020-09-07)
binary: rustc
commit-hash: 0e2c1281e909ca38479b97962fc9248f75d66412
commit-date: 2020-09-07
host: x86_64-unknown-linux-gnu
release: 1.48.0-nightly
LLVM version: 11.0

However, the unmodified code above only generates normal ("external") compiler errors in versions starting with nightly-2020-09-22, as far as I can tell. A different message also appears in versions before nightly-2020-09-08.

Error output

The first error message suggests a fix:

error: redundant pattern matching, consider using `is_some()`
   --> /home/<user>/<project>/linux-kernel-module-rust/src/file_operations.rs:147:22
    |
147 |         read: if let Some(_) = T::READ {
    |               -------^^^^^^^---------- help: try this: `if T::READ.is_some()`
    |
    = note: `-D clippy::redundant-pattern-matching` implied by `-D warnings`
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pattern_matching

error: redundant pattern matching, consider using `is_some()`
   --> /home/<user>/<project>/linux-kernel-module-rust/src/file_operations.rs:152:23
    |
152 |         write: if let Some(_) = T::WRITE {
    |                -------^^^^^^^----------- help: try this: `if T::WRITE.is_some()`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pattern_matching

error: redundant pattern matching, consider using `is_some()`
   --> /home/<user>/<project>/linux-kernel-module-rust/src/file_operations.rs:157:24
    |
157 |         llseek: if let Some(_) = T::SEEK {
    |                 -------^^^^^^^---------- help: try this: `if T::SEEK.is_some()`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pattern_matching

error: aborting due to 3 previous errors

error: could not compile `linux-kernel-module`

but when I apply that fix, I get:

error: internal compiler error: compiler/rustc_typeck/src/check/mod.rs:3394:25: while adjusting Expr { hir_id: HirId { owner: DefId(0:141 ~ linux_kernel_module[2b6b]::file_operations[0]::{{impl}}[1]::VTAB
LE[0]), local_id: 42 }, kind: Path(TypeRelative(Ty { hir_id: HirId { owner: DefId(0:141 ~ linux_kernel_module[2b6b]::file_operations[0]::{{impl}}[1]::VTABLE[0]), local_id: 40 }, kind: Path(Resolved(None, 
Path { span: /home/<user>/<project>/linux-kernel-module-rust/src/file_operations.rs:147:18: 147:25 (#0), res: Def(TyParam, DefId(0:140 ~ linux_kernel_module[2b6b]::file_operations[0]::{{impl}}[1]::T[0])), s
egments: [PathSegment { ident: T#0, hir_id: Some(HirId { owner: DefId(0:141 ~ linux_kernel_module[2b6b]::file_operations[0]::{{impl}}[1]::VTABLE[0]), local_id: 39 }), res: Some(Def(TyParam, DefId(0:140 ~ 
linux_kernel_module[2b6b]::file_operations[0]::{{impl}}[1]::T[0]))), args: None, infer_args: true }] })), span: /home/<user>/<project>/linux-kernel-module-rust/src/file_operations.rs:147:18: 147:25 (#0) }, 
PathSegment { ident: READ#0, hir_id: Some(HirId { owner: DefId(0:141 ~ linux_kernel_module[2b6b]::file_operations[0]::{{impl}}[1]::VTABLE[0]), local_id: 41 }), res: Some(Err), args: None, infer_args: true
 })), attrs: ThinVec(None), span: /home/<user>/<project>/linux-kernel-module-rust/src/file_operations.rs:147:18: 147:25 (#0) }, can't compose [Borrow(Ref('_#0r, Not)) -> &Option<for<'r, 's, 't0> fn(&'r T, &
's File, &'t0 mut UserSlicePtrWriter, u64) -> core::result::Result<(), error::Error>>] and [Borrow(Ref('_#2r, Not)) -> &Option<for<'r, 's, 't0> fn(&'r T, &'s File, &'t0 mut UserSlicePtrWriter, u64) -> cor
e::result::Result<(), error::Error>>]

thread 'rustc' panicked at 'Box<Any>', compiler/rustc_errors/src/lib.rs:945:9

Note that I have redacted my local username and project name.

Backtrace

stack backtrace:                                                                                                                                                                                   [39/1895]
   0: std::panicking::begin_panic
   1: rustc_errors::HandlerInner::bug
   2: rustc_errors::Handler::bug
   3: rustc_middle::util::bug::opt_span_bug_fmt::{{closure}}
   4: rustc_middle::ty::context::tls::with_opt::{{closure}}
   5: rustc_middle::ty::context::tls::with_opt
   6: rustc_middle::util::bug::opt_span_bug_fmt
   7: rustc_middle::util::bug::bug_fmt
   8: rustc_typeck::check::FnCtxt::apply_adjustments
   9: rustc_typeck::check::method::confirm::ConfirmContext::confirm
  10: rustc_typeck::check::method::<impl rustc_typeck::check::FnCtxt>::lookup_method
  11: rustc_typeck::check::expr::<impl rustc_typeck::check::FnCtxt>::check_expr_kind
  12: rustc_typeck::check::expr::<impl rustc_typeck::check::FnCtxt>::check_expr_with_expectation
  13: rustc_typeck::check::expr::<impl rustc_typeck::check::FnCtxt>::check_expr_kind
  14: rustc_typeck::check::expr::<impl rustc_typeck::check::FnCtxt>::check_expr_with_expectation
  15: rustc_typeck::check::expr::<impl rustc_typeck::check::FnCtxt>::check_expr_kind
  16: rustc_typeck::check::expr::<impl rustc_typeck::check::FnCtxt>::check_expr_with_expectation
  17: rustc_typeck::check::_match::<impl rustc_typeck::check::FnCtxt>::check_match
  18: rustc_typeck::check::expr::<impl rustc_typeck::check::FnCtxt>::check_expr_kind
  19: rustc_typeck::check::expr::<impl rustc_typeck::check::FnCtxt>::check_expr_with_expectation
  20: rustc_typeck::check::expr::<impl rustc_typeck::check::FnCtxt>::check_expr_coercable_to_type
  21: rustc_typeck::check::expr::<impl rustc_typeck::check::FnCtxt>::check_expr_kind
  22: rustc_typeck::check::expr::<impl rustc_typeck::check::FnCtxt>::check_expr_with_expectation
  23: rustc_typeck::check::expr::<impl rustc_typeck::check::FnCtxt>::check_expr_coercable_to_type
  24: rustc_infer::infer::InferCtxtBuilder::enter
  25: rustc_typeck::check::typeck
  26: rustc_middle::ty::query::<impl rustc_query_system::query::config::QueryAccessors<rustc_middle::ty::context::TyCtxt> for rustc_middle::ty::query::queries::typeck>::compute
  27: rustc_middle::dep_graph::<impl rustc_query_system::dep_graph::DepKind for rustc_middle::dep_graph::dep_node::DepKind>::with_deps
  28: rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl
  29: rustc_middle::ty::query::plumbing::<impl rustc_query_system::query::QueryContext for rustc_middle::ty::context::TyCtxt>::start_query::{{closure}}::{{closure}}::{{closure}}
  30: rustc_query_system::query::plumbing::get_query_impl
  31: rustc_query_system::query::plumbing::ensure_query_impl
  32: rustc_typeck::check::typeck_item_bodies
  33: rustc_middle::ty::query::<impl rustc_query_system::query::config::QueryAccessors<rustc_middle::ty::context::TyCtxt> for rustc_middle::ty::query::queries::typeck_item_bodies>::compute
  34: rustc_middle::dep_graph::<impl rustc_query_system::dep_graph::DepKind for rustc_middle::dep_graph::dep_node::DepKind>::with_deps
  35: rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl
  36: rustc_data_structures::stack::ensure_sufficient_stack
  37: rustc_query_system::query::plumbing::get_query_impl
  38: rustc_typeck::check_crate
  39: rustc_interface::passes::analysis
  40: rustc_middle::ty::query::<impl rustc_query_system::query::config::QueryAccessors<rustc_middle::ty::context::TyCtxt> for rustc_middle::ty::query::queries::analysis>::compute
  41: rustc_middle::dep_graph::<impl rustc_query_system::dep_graph::DepKind for rustc_middle::dep_graph::dep_node::DepKind>::with_deps
  42: rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl
  43: rustc_data_structures::stack::ensure_sufficient_stack
  44: rustc_query_system::query::plumbing::get_query_impl
  45: rustc_interface::passes::QueryContext::enter
  46: rustc_interface::queries::<impl rustc_interface::interface::Compiler>::enter
  47: rustc_span::with_source_map
  48: rustc_interface::interface::create_compiler_and_run
  49: scoped_tls::ScopedKey<T>::set
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
...
query stack during panic:
#0 [typeck] type-checking `file_operations::FileOperationsVtable::<T>::VTABLE`
#1 [typeck_item_bodies] type-checking all item bodies
#2 [analysis] running analysis passes on this crate
end of query stack

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-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.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions