rust-lang / rust

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

ICE: Normalizing Binder without wrapping in a Binder #95421

Open QuineDot opened 2 years ago

QuineDot commented 2 years ago

Code

Library version (choose "Build" from the pulldown):

// Compile as library
fn h5<T>(_: T)
where
    for<'x> fn(&'x u32): Fn(&'x u32),
{
}

Binary version:

// Compile as binary
fn h5<T>(_: T)
where
    for<'x> fn(&'x u32): Fn(&'x u32),
{
}

fn main(){
    h5(|_|{})
}

The two have (slightly) differing output. Note: If you compile as a binary but don't call the problematic function, compilation succeeds. Compiling as a library ICEs even when the function is private.

Meta

Error output

Library version

warning: function is never used: `h5`
 [--> src/lib.rs:1:4
](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021#)  |
1 | fn h5<T>(_: T)
  |    ^^
  |
  = note: `#[warn(dead_code)]` on by defaulterror: internal compiler error: unexpected panic

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.59.0 (9d1b2106e 2022-02-23) running on x86_64-unknown-linux-gnu

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

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

query stack during panic:
#0 [evaluate_obligation] evaluating trait selection obligation `for<'x> for<'x> fn(&'x u32): core::ops::function::Fn<(&'x u32,)>`
#1 [optimized_mir] optimizing MIR for `h5`
end of query stack

Binary version

error: internal compiler error: unexpected panic

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.59.0 (9d1b2106e 2022-02-23) 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 [evaluate_obligation] evaluating trait selection obligation `for<'x> for<'x> fn(&'x u32): core::ops::function::Fn<(&'x u32,)>`
#1 [typeck] type-checking `main`
#2 [typeck] type-checking `main::{closure#0}`
#3 [typeck_item_bodies] type-checking all item bodies
#4 [analysis] running analysis passes on this crate
end of query stack
Backtrace (Library version)

``` thread 'rustc' panicked at 'Normalizing Binder( fn(&'x u32) as std::ops::Fn<(&'x u32,)>>, []) without wrapping in a `Binder`', compiler/rustc_trait_selection/src/traits/project.rs:333:9 stack backtrace: 0: rust_begin_unwind at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/std/src/panicking.rs:498:5 1: core::panicking::panic_fmt at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/core/src/panicking.rs:116:14 2: rustc_data_structures::stack::ensure_sufficient_stack::, rustc_trait_selection::traits::project::normalize_with_depth_to>::{closure#0}> 3: rustc_trait_selection::traits::project::normalize_with_depth::> 4: rustc_data_structures::stack::ensure_sufficient_stack::>, ::confirm_fn_pointer_candidate::{closure#1}> 5: ::confirm_candidate 6: ::evaluate_stack 7: >::with_anon_task::::in_task<::evaluate_trait_predicate_recursively::{closure#2}, core::result::Result>::{closure#0}, core::result::Result> 8: ::evaluate_trait_predicate_recursively 9: rustc_data_structures::stack::ensure_sufficient_stack::, ::evaluate_predicate_recursively::{closure#0}> 10: ::evaluate_root_obligation 11: ::enter_with_canonical::, core::result::Result, rustc_traits::evaluate_obligation::evaluate_obligation::{closure#0}> 12: rustc_traits::evaluate_obligation::evaluate_obligation 13: rustc_query_system::query::plumbing::get_query:: 14: ::evaluate_obligation 15: ::predicate_must_hold_considering_regions 16: ::progress_changed_obligations 17: >::process_obligations::> 18: ::select_all_or_error 19: ::enter:: 20: rustc_trait_selection::traits::impossible_predicates 21: ::run_pass 22: rustc_mir_transform::pass_manager::run_passes 23: rustc_mir_transform::optimized_mir 24: rustc_query_system::query::plumbing::try_execute_query::> 25: ::optimized_mir 26: ::encode_crate_root 27: rustc_metadata::rmeta::encoder::encode_metadata_impl 28: rustc_data_structures::sync::join:: 29: rustc_metadata::rmeta::encoder::encode_metadata 30: ::ongoing_codegen 31: ::enter::, rustc_errors::ErrorReported>> 32: rustc_span::with_source_map::, rustc_interface::interface::create_compiler_and_run, rustc_driver::run_compiler::{closure#1}>::{closure#1}> 33: rustc_interface::interface::create_compiler_and_run::, rustc_driver::run_compiler::{closure#1}> 34: >::set::, rustc_driver::run_compiler::{closure#1}>::{closure#0}, core::result::Result<(), rustc_errors::ErrorReported>>::{closure#0}::{closure#0}, core::result::Result<(), rustc_errors::ErrorReported>> note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace. ```

Backtrace (Binary version)

``` thread 'rustc' panicked at 'Normalizing Binder( fn(&'x u32) as std::ops::Fn<(&'x u32,)>>, []) without wrapping in a `Binder`', compiler/rustc_trait_selection/src/traits/project.rs:333:9 stack backtrace: 0: rust_begin_unwind at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/std/src/panicking.rs:498:5 1: core::panicking::panic_fmt at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/core/src/panicking.rs:116:14 2: rustc_data_structures::stack::ensure_sufficient_stack::, rustc_trait_selection::traits::project::normalize_with_depth_to>::{closure#0}> 3: rustc_trait_selection::traits::project::normalize_with_depth::> 4: rustc_data_structures::stack::ensure_sufficient_stack::>, ::confirm_fn_pointer_candidate::{closure#1}> 5: ::confirm_candidate 6: ::evaluate_stack 7: >::with_anon_task::::in_task<::evaluate_trait_predicate_recursively::{closure#2}, core::result::Result>::{closure#0}, core::result::Result> 8: ::evaluate_trait_predicate_recursively 9: rustc_data_structures::stack::ensure_sufficient_stack::, ::evaluate_predicate_recursively::{closure#0}> 10: ::evaluate_root_obligation 11: ::enter_with_canonical::, core::result::Result, rustc_traits::evaluate_obligation::evaluate_obligation::{closure#0}> 12: rustc_traits::evaluate_obligation::evaluate_obligation 13: rustc_query_system::query::plumbing::get_query:: 14: ::evaluate_obligation 15: ::predicate_must_hold_considering_regions 16: ::progress_changed_obligations 17: >::process_obligations::> 18: ::select_where_possible 19: ::check_call 20: ::check_expr_kind 21: ::check_expr_with_expectation_and_args 22: ::check_block_with_expected 23: ::check_expr_with_expectation_and_args 24: ::check_return_expr 25: rustc_typeck::check::check::check_fn 26: ::enter::<&rustc_middle::ty::context::TypeckResults, ::enter::{closure#1}, &rustc_middle::ty::context::TypeckResults>::{closure#0}> 27: rustc_typeck::check::typeck 28: rustc_query_system::query::plumbing::try_execute_query::> 29: ::typeck 30: rustc_typeck::check::typeck 31: rustc_query_system::query::plumbing::try_execute_query::> 32: ::typeck 33: ::par_body_owners:: 34: rustc_typeck::check::typeck_item_bodies 35: rustc_query_system::query::plumbing::try_execute_query::> 36: rustc_query_system::query::plumbing::get_query:: 37: ::time::<(), rustc_typeck::check_crate::{closure#7}> 38: rustc_typeck::check_crate 39: rustc_interface::passes::analysis 40: rustc_query_system::query::plumbing::try_execute_query::>> 41: rustc_query_system::query::plumbing::get_query:: 42: ::enter::, rustc_errors::ErrorReported>> 43: rustc_span::with_source_map::, rustc_interface::interface::create_compiler_and_run, rustc_driver::run_compiler::{closure#1}>::{closure#1}> 44: rustc_interface::interface::create_compiler_and_run::, rustc_driver::run_compiler::{closure#1}> 45: >::set::, rustc_driver::run_compiler::{closure#1}>::{closure#0}, core::result::Result<(), rustc_errors::ErrorReported>>::{closure#0}::{closure#0}, core::result::Result<(), rustc_errors::ErrorReported>> note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace. ```

Version it worked on

Works on 1.41.0. Works on versions prior to this down to 1.7.0 (spot checked). (Version 1.6.0 gives obsoleted error E0193.)

Versions with regression

Versions 1.42.0 and up (spot checked). The error changes between 1.54.0 and 1.55.0.

@rustbot modify labels: +regression-from-stable-to-stable -regression-untriaged

Dylan-DPC commented 2 years ago

output from bisect:

looking for regression commit between 2021-09-01 and 2021-09-02
cloning rust repository
fetching (via local git) commits from 29ef6cf1637aa8317f8911f93f14e18d404c1b0e to 50171c310cd15e1b2d3723766ce64e2e4d6696fc
refreshing repository at "rust.git"
From https://github.com/rust-lang/rust
 * branch                    HEAD       -> FETCH_HEAD
opening existing repository at "rust.git"
looking up first commit
looking up second commit
checking that commits are by bors and thus have ci artifacts...
finding bors merge commits
found 9 bors merge commits in the specified range
  commit[0] 2021-08-31UTC: Auto merge of #88506 - Mark-Simulacrum:fix-rlibs, r=ehuss
  commit[1] 2021-08-31UTC: Auto merge of #88533 - oli-obk:tait_🧊, r=spastorino
  commit[2] 2021-09-01UTC: Auto merge of #87688 - camsteffen:let-else, r=cjgillot
  commit[3] 2021-09-01UTC: Auto merge of #88121 - camelid:better-recursive-alias-error, r=estebank
  commit[4] 2021-09-01UTC: Auto merge of #88272 - willcrichton:mutable-sparse-matrix, r=ecstatic-morse
  commit[5] 2021-09-01UTC: Auto merge of #88556 - m-ou-se:rollup-q636wyd, r=m-ou-se
  commit[6] 2021-09-01UTC: Auto merge of #88395 - ricky26:llvm-submodule, r=nikic
  commit[7] 2021-09-01UTC: Auto merge of #88269 - prconrad:doctest-persist-binaries, r=jyn514
  commit[8] 2021-09-01UTC: Auto merge of #88563 - ehuss:update-cargo-books, r=ehuss
compiler-errors commented 2 years ago

@Dylan-DPC, I got the same bisection as you, but then realized it was because #88563 added support for edition = "2021" in cargo, and before 2021-09-01, it wasn't ICEing but failing to cargo-build (which makes bisect-rustc think it regressed there).

I don't think this code ever worked. Before 1.56 (when @jackh726 added the wonderful #85499), rustc ICEs with failure to relate late-bound and placeholder regions, and after 1.56, we fail with this binder variable issue. The library example provided above only newly begins to fail between 1.41 and 1.42 because someone added a check related to const prop that caused the predicate to be evaluated eagerly, but the binary version has always ICEd in some form.

The bug

This is probably a bug because we overwrite the late-bound regions we collect here in the where-clause (which we attach to the HIR id of the type in the LHS of Ty: Trait + Trait) with the ones we collect here when visiting the fn type.

Not exactly sure what the best way to fix this bug is. I'll play around with solutions, but don't want to claim the issue because I think it's a bit above my pay-grade, so to speak, since I know very little about rustc_resolve::late::lifetimes.

jackh726 commented 2 years ago

Heh this is weird. I was actually confused for a bit, because it looked like 'x was bound everywhere. But it's it a bit ambiguous where the for<'x> should go: on the fn or on the TraitRef.

apiraino commented 2 years ago

Assigning priority as discussed in the Zulip thread of the Prioritization Working Group.

@rustbot label -I-prioritize +P-medium

QuineDot commented 2 years ago

I don't think this code ever worked. Before 1.56 (when @jackh726 added the wonderful #85499), rustc ICEs with failure to relate late-bound and placeholder regions, and after 1.56, we fail with this binder variable issue. The library example provided above only newly begins to fail between 1.41 and 1.42 because someone added a check related to const prop that caused the predicate to be evaluated eagerly, but the binary version has always ICEd in some form.

Ah, sorry for not catching that -- I probably wouldn't have filed it as a regression if I had (though I guess it is still technically one).

compiler-errors commented 1 year ago

I think this is "fixed" (ICE -> error) by #108834. It's probably actually fixed by #108918 (error -> compiles), though.