Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ICE with probably recursive opaque types and closures #99073

Closed
Aegrithas opened this issue Jul 8, 2022 · 3 comments · Fixed by #99079 or #99345
Closed

ICE with probably recursive opaque types and closures #99073

Aegrithas opened this issue Jul 8, 2022 · 3 comments · Fixed by #99079 or #99345
Assignees
Labels
C-bug Category: This is a bug. glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness P-critical Critical priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@Aegrithas
Copy link

Messing around with a Haskell-like implementation of fix , I encountered two presumably similar ICEs
(would think they're the same, but the error messages are obviously different). The other one is here.

I'm pretty sure this should be an error, based on the reasoning that fix returns an opaque type, so, while the output of fix might be G, it also might not (and in this case, isn't), and since f only accepts Gs, f(fix(anything)) should be an error.

Code

fn main() {
  let _ = fix(|_: &dyn Fn()| {});
}

fn fix<F: Fn(G), G: Fn()>(f: F) -> impl Fn() {
  move || f(fix(&f))
}

Note that this fix implementation throws the ICE even without the main method, but I'm leaving it there for contrast to the other ICE.

Meta

rustc --version --verbose:

rustc 1.62.0 (a8314ef7d 2022-06-27)
binary: rustc
commit-hash: a8314ef7d0ec7b75c336af2c9857bfaf43002bfc
commit-date: 2022-06-27
host: x86_64-pc-windows-msvc
release: 1.62.0
LLVM version: 14.0.5

Same ICE on nightly:

rustc 1.64.0-nightly (7425fb293 2022-06-30)
binary: rustc
commit-hash: 7425fb293f510a6f138e82a963a3bc599a5b9e1c
commit-date: 2022-06-30
host: x86_64-pc-windows-msvc
release: 1.64.0-nightly
LLVM version: 14.0.6

Error output

error: internal compiler error: no errors encountered even though `delay_span_bug` issued

error: internal compiler error: opaque type with non-universal region substs
 --> ICE.rs:6:11
  |
6 |   move || f(fix(&f))
  |           ^^^^^^^^^^
  |
  = note: delayed at compiler\rustc_borrowck\src\region_infer\opaque_types.rs:81:40

thread 'rustc' panicked at 'Box<dyn Any>', compiler\rustc_errors\src\lib.rs:1369:13
stack backtrace:
   0:     0x7ff8a09a994f - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hacdb30195c80e0b1
   1:     0x7ff8a09e484a - core::fmt::write::h933e103661a87ff3
   2:     0x7ff8a099bf79 - <std::io::IoSlice as core::fmt::Debug>::fmt::hea673ff643a18274
   3:     0x7ff8a09ad1fb - std::panicking::default_hook::h663c37f307fa2cce
   4:     0x7ff8a09acdee - std::panicking::default_hook::h663c37f307fa2cce
   5:     0x7ff861bbefb9 - <rustc_middle[fc52725189803489]::ty::SymbolName as core[eeb9ad4fed407cd7]::fmt::Debug>::fmt
   6:     0x7ff8a09adada - std::panicking::rust_panic_with_hook::ha00d018f7a00c749
   7:     0x7ff866310515 - <rustc_errors[7c23d5e4cf22f7a2]::diagnostic_builder::DiagnosticBuilderInner as core[eeb9ad4fed407cd7]::ops::drop::Drop>::drop
   8:     0x7ff8663104c9 - <rustc_errors[7c23d5e4cf22f7a2]::diagnostic_builder::DiagnosticBuilderInner as core[eeb9ad4fed407cd7]::ops::drop::Drop>::drop
   9:     0x7ff8666546d9 - rustc_query_system[58927a9c078e4f74]::query::plumbing::incremental_verify_ich_cold
  10:     0x7ff8663025f9 - <rustc_feature[3e4da95902b85434]::builtin_attrs::AttributeType as core[eeb9ad4fed407cd7]::fmt::Debug>::fmt
  11:     0x7ff866306828 - <rustc_errors[7c23d5e4cf22f7a2]::HandlerInner as core[eeb9ad4fed407cd7]::ops::drop::Drop>::drop
  12:     0x7ff861bde5a4 - rustc_driver[87b9ba788b0782fb]::pretty::print_after_hir_lowering
  13:     0x7ff861be781a - rustc_driver[87b9ba788b0782fb]::pretty::print_after_hir_lowering
  14:     0x7ff861bd48dd - rustc_driver[87b9ba788b0782fb]::pretty::print_after_hir_lowering
  15:     0x7ff861bd1ae9 - rustc_driver[87b9ba788b0782fb]::pretty::print_after_hir_lowering
  16:     0x7ff861b50a30 - <rustc_privacy[9daebf6d6dd1f9f8]::NamePrivacyVisitor as rustc_hir[ef6dd62a99d46082]::intravisit::Visitor>::visit_mod
  17:     0x7ff861b72d37 - <rustc_privacy[9daebf6d6dd1f9f8]::NamePrivacyVisitor as rustc_hir[ef6dd62a99d46082]::intravisit::Visitor>::visit_mod
  18:     0x7ff861b7cd56 - <rustc_driver[87b9ba788b0782fb]::args::Error as core[eeb9ad4fed407cd7]::fmt::Debug>::fmt
  19:     0x7ff861b7de78 - <rustc_driver[87b9ba788b0782fb]::args::Error as core[eeb9ad4fed407cd7]::fmt::Debug>::fmt
  20:     0x7ff8a09beccc - std::sys::windows::thread::Thread::new::hc027ed84984f61da
  21:     0x7ff8d65f7034 - BaseThreadInitThunk
  22:     0x7ff8d7242651 - RtlUserThreadStart

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.62.0 (a8314ef7d 2022-06-27) running on x86_64-pc-windows-msvc

query stack during panic:
end of query stack
Backtrace

   0:     0x7ff8a022994f - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hacdb30195c80e0b1
   1:     0x7ff8a026484a - core::fmt::write::h933e103661a87ff3
   2:     0x7ff8a021bf79 - <std::io::IoSlice as core::fmt::Debug>::fmt::hea673ff643a18274
   3:     0x7ff8a022d1fb - std::panicking::default_hook::h663c37f307fa2cce
   4:     0x7ff8a022cdee - std::panicking::default_hook::h663c37f307fa2cce
   5:     0x7ff861bbefb9 - <rustc_middle[fc52725189803489]::ty::SymbolName as core[eeb9ad4fed407cd7]::fmt::Debug>::fmt
   6:     0x7ff8a022dada - std::panicking::rust_panic_with_hook::ha00d018f7a00c749
   7:     0x7ff866310515 - <rustc_errors[7c23d5e4cf22f7a2]::diagnostic_builder::DiagnosticBuilderInner as core[eeb9ad4fed407cd7]::ops::drop::Drop>::drop
   8:     0x7ff8663104c9 - <rustc_errors[7c23d5e4cf22f7a2]::diagnostic_builder::DiagnosticBuilderInner as core[eeb9ad4fed407cd7]::ops::drop::Drop>::drop
   9:     0x7ff8666546d9 - rustc_query_system[58927a9c078e4f74]::query::plumbing::incremental_verify_ich_cold
  10:     0x7ff8663025f9 - <rustc_feature[3e4da95902b85434]::builtin_attrs::AttributeType as core[eeb9ad4fed407cd7]::fmt::Debug>::fmt
  11:     0x7ff866306828 - <rustc_errors[7c23d5e4cf22f7a2]::HandlerInner as core[eeb9ad4fed407cd7]::ops::drop::Drop>::drop
  12:     0x7ff861bde5a4 - rustc_driver[87b9ba788b0782fb]::pretty::print_after_hir_lowering
  13:     0x7ff861be781a - rustc_driver[87b9ba788b0782fb]::pretty::print_after_hir_lowering
  14:     0x7ff861bd48dd - rustc_driver[87b9ba788b0782fb]::pretty::print_after_hir_lowering
  15:     0x7ff861bd1ae9 - rustc_driver[87b9ba788b0782fb]::pretty::print_after_hir_lowering
  16:     0x7ff861b50a30 - <rustc_privacy[9daebf6d6dd1f9f8]::NamePrivacyVisitor as rustc_hir[ef6dd62a99d46082]::intravisit::Visitor>::visit_mod
  17:     0x7ff861b72d37 - <rustc_privacy[9daebf6d6dd1f9f8]::NamePrivacyVisitor as rustc_hir[ef6dd62a99d46082]::intravisit::Visitor>::visit_mod
  18:     0x7ff861b7cd56 - <rustc_driver[87b9ba788b0782fb]::args::Error as core[eeb9ad4fed407cd7]::fmt::Debug>::fmt
  19:     0x7ff861b7de78 - <rustc_driver[87b9ba788b0782fb]::args::Error as core[eeb9ad4fed407cd7]::fmt::Debug>::fmt
  20:     0x7ff8a023eccc - std::sys::windows::thread::Thread::new::hc027ed84984f61da
  21:     0x7ff8d65f7034 - BaseThreadInitThunk
  22:     0x7ff8d7242651 - RtlUserThreadStart

@Aegrithas Aegrithas added C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 8, 2022
@Aegrithas Aegrithas changed the title ICE with probably recursive opaque types and clojures ICE with probably recursive opaque types and closures Jul 9, 2022
@compiler-errors
Copy link
Member

This regressed in #94081

@compiler-errors
Copy link
Member

compiler-errors commented Jul 9, 2022

I was able to "weaponize" this into a transmute, lol:

use std::fmt::Display;

fn main() {
  test("hi", true);
}

fn test<T: Display>(t: T, recurse: bool) -> impl Display {
  let f = || { let i: u32 = test::<i32>(-1, false); println!("{i}"); };
  if recurse { f(); }
  t
}

So the issue here is that, at least in the case of RPITs, we are not checking for opaque type mismatches in closures correctly.

The closure, in this example, is able to incorrectly constrain test::{opaque}::<i32> == u32 even though in the enclosing function, we define test::{opaque}::<T> == T. During mir borrowck, we never check these two together, so we never report a type mismatch.

I think I have a fix, though.

@oli-obk oli-obk added the I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness label Jul 9, 2022
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Jul 9, 2022
@compiler-errors compiler-errors self-assigned this Jul 9, 2022
@apiraino
Copy link
Contributor

WG-prioritization assigning priority (Zulip discussion).

@rustbot label -I-prioritize +P-critical

@rustbot rustbot added P-critical Critical priority and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Jul 13, 2022
matthiaskrgr added a commit to matthiaskrgr/glacier that referenced this issue Jul 15, 2022
@rust-lang-glacier-bot rust-lang-glacier-bot added the glacier ICE tracked in rust-lang/glacier. label Jul 15, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Jul 18, 2022
…r=oli-obk

Do not allow typeck children items to constrain outer RPITs

Fixes rust-lang#99073 in a simpler and more conservative way than rust-lang#99079. Simply raise a mismatched types error if we try to constrain an RPIT in an item that isn't the RPIT's parent.

r? `@oli-obk`
@bors bors closed this as completed in 3c3c5da Jul 20, 2022
JohnTitor added a commit to JohnTitor/rust that referenced this issue Jul 27, 2022
Check that RPITs constrained by a recursive call in a closure are compatible

Fixes rust-lang#99073

Adapts a similar visitor pattern to `find_opaque_ty_constraints` (that we use to check TAITs), but with some changes:
0. Only walk the "OnlyBody" children, instead of all items in the RPIT's defining scope
1. Only walk through the body's children if we found a constraining usage
2. Don't actually do any inference, just do a comparison and error if they're mismatched

----

r? `@oli-obk` -- you know all this impl-trait stuff best... is this the right approach? I can explain the underlying issue better if you'd like, in case that might reveal a better solution. Not sure if it's possible to gather up the closure's defining usages of the RPIT while borrowck'ing the outer function, that might be a better place to put this check...
@compiler-errors compiler-errors added the T-types Relevant to the types team, which will review and decide on the PR/issue. label Oct 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness P-critical Critical priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
6 participants