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

Borrow checker's strange error. #57404

Closed
cheblin opened this issue Jan 7, 2019 · 5 comments · Fixed by #105945
Closed

Borrow checker's strange error. #57404

cheblin opened this issue Jan 7, 2019 · 5 comments · Fixed by #105945
Labels
A-borrow-checker Area: The borrow checker A-closures Area: Closures (`|…| { … }`) A-codegen Area: Code generation A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-MIR Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. F-unboxed_closures `#![feature(unboxed_closures)]` glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@cheblin
Copy link

cheblin commented Jan 7, 2019

#![feature(unboxed_closures)]
#![feature(const_vec_new)]
#![feature(fn_traits)]

pub struct PackBytes;

static mut HANDLERS: Vec<Box<dyn FnMut<&mut PackBytes, Output=()>>> = Vec::new();

fn main() {
    
    let mut pack = PackBytes;
    unsafe { HANDLERS[0].as_mut().call_mut(&mut pack); }
}
error[E0597]: `pack` does not live long enough
  --> src/main.rs:15:35
   |
15 |     HANDLERS[0].as_mut().call_mut(&mut pack); }
   |     ------------------------------^^^^^^^^^-
   |     |                             |
   |     |                             borrowed value does not live long enough
   |     argument requires that `pack` is borrowed for `'static`
16 | }
   | - `pack` dropped here while still borrowed

#![feature(unboxed_closures)]
#![feature(const_vec_new)]
#![feature(fn_traits)]
pub struct PackBytes;



static mut HANDLERS: Vec<Box<dyn for<'a> FnMut<&'a mut PackBytes, Output=()>>> = Vec::new();

fn main() {
    
    let mut pack = PackBytes;
    unsafe { HANDLERS[0].as_mut().call_mut(&mut pack); }
}
error: internal compiler error: src/librustc_codegen_llvm/abi.rs:433: argument to function with "rust-call" ABI is not a tuple

thread 'rustc' panicked at 'Box<Any>', src/librustc_errors/lib.rs:590:9
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
error: aborting due to previous error


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

note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports

note: rustc 1.33.0-nightly (b92552d55 2019-01-06) running on x86_64-unknown-linux-gnu

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

note: some of the compiler flags provided by cargo are hidden
@cheblin cheblin changed the title Borrow checker strange error. Borrow checker's strange error. Jan 7, 2019
@awaitlink awaitlink mentioned this issue Jan 7, 2019
@hellow554
Copy link
Contributor

Reduced example

#![feature(unboxed_closures)]
#![feature(fn_traits)]

fn main() {
    let handlers: Option<Box<dyn for<'a> FnMut<&'a mut (), Output=()>>> = None;
    handlers.unwrap().as_mut().call_mut(&mut ());
}

@cheblin
Copy link
Author

cheblin commented Jan 7, 2019

this, stripped works

#![feature(const_vec_new)]

pub struct PackBytes;

static mut HANDLERS: Vec<Box<dyn for<'a> FnMut(&'a mut PackBytes) -> ()>> = Vec::new();

fn main() {
    
    let mut pack = PackBytes;
    unsafe { (HANDLERS[0].as_mut())(&mut pack); }
}

@cheblin cheblin closed this as completed Jan 7, 2019
@cheblin cheblin reopened this Jan 7, 2019
@cheblin

This comment has been minimized.

@scalexm
Copy link
Member

scalexm commented Jan 7, 2019

The first compilation error -- the example with static mut HANDLERS: Vec<Box<dyn FnMut<&mut PackBytes, Output=()>>> = Vec::new(); looks fine to me. Types used for static variables must outlive the 'static lifetime. The compiler can prove that dyn FnMut<&'b mut PackBytes> + 'a outlives 'static only if 'a == 'b == 'static. Hence 'static lifetimes are inferred in lieu of the anonymous lifetimes. Since your pack variable does not live for the whole 'static lifetime, it cannot be used as you are trying to.

Maybe the actual bug is that dyn FnMut<&T, Output=()> should desugar to dyn for<'a> FnMut<&'a T, Output=()>, as it would be for dyn FnMut(&T)? It may be weird to special-case only the Fn/FnMut/FnOnce traits though -- i.e. if you define your own trait trait Foo<T> { }, then Foo<&i32> just means Foo<&'b i32> for some lifetime 'b to be inferred by the compiler, it does not mean for<'a> Foo<&'a i32>.

Your reduced example is actually different since it actually does not involve static data. It seems to be the same bug as in #22565, and hence is a bug with the unboxed_closure feature.

Going from FnMut<&'a mut (), Output=()> to FnMut<(&'a mut (),), Output=()> (note the tuple of size 1) makes the ICE go away.

@estebank estebank added A-codegen Area: Code generation I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ A-borrow-checker Area: The borrow checker A-MIR Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) labels Jan 19, 2019
@jonas-schievink jonas-schievink added C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 6, 2019
@rust-lang-glacier-bot rust-lang-glacier-bot added the glacier ICE tracked in rust-lang/glacier. label Oct 15, 2019
@Centril Centril added the requires-nightly This issue requires a nightly compiler in some way. label Oct 25, 2019
@Centril Centril added the F-unboxed_closures `#![feature(unboxed_closures)]` label Nov 5, 2019
@estebank estebank added the A-closures Area: Closures (`|…| { … }`) label May 4, 2021
@Alexendoo
Copy link
Member

Fixed by #99943

@Alexendoo Alexendoo added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Nov 9, 2022
JohnTitor added a commit to JohnTitor/rust that referenced this issue Dec 20, 2022
Signed-off-by: Yuki Okushi <jtitor@2k36.org>
bors added a commit to rust-lang-ci/rust that referenced this issue Dec 20, 2022
…iaskrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#105835 (Refactor post borrowck cleanup passes)
 - rust-lang#105930 (Disable `NormalizeArrayLen`)
 - rust-lang#105938 (Update coerce_unsized tracking issue from rust-lang#27732 to rust-lang#18598)
 - rust-lang#105939 (Improve description of struct-fields GUI test)
 - rust-lang#105943 (Add regression test for rust-lang#102206)
 - rust-lang#105944 (Add regression test for rust-lang#80816)
 - rust-lang#105945 (Add regression test for rust-lang#57404)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors closed this as completed in 8db5dd4 Dec 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-borrow-checker Area: The borrow checker A-closures Area: Closures (`|…| { … }`) A-codegen Area: Code generation A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-MIR Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. F-unboxed_closures `#![feature(unboxed_closures)]` glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants