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: NLL bug? #49616

Closed
ZhangHanDong opened this issue Apr 3, 2018 · 10 comments
Closed

ICE: NLL bug? #49616

ZhangHanDong opened this issue Apr 3, 2018 · 10 comments
Labels
A-destructors Area: Destructors (`Drop`, …) A-NLL Area: Non-lexical lifetimes (NLL) I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ NLL-complete Working towards the "valid code works" goal

Comments

@ZhangHanDong
Copy link

ZhangHanDong commented Apr 3, 2018

#![feature(nll)]

use std::cell::RefCell;

fn main() {
    let s = RefCell::new(Some(10));
    if let Some(n) = *(s.borrow_mut()) {
        println!("num: {}", n);
    }
}

I thought the code should be compiled with nll feature ,

but got the following errors:

Compiling playground v0.0.1 (file:///playground)
thread 'rustc' panicked at 'called `Option::unwrap()` on a `None` value', libcore/option.rs:335:21
note: Run with `RUST_BACKTRACE=1` for a backtrace.

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/blob/master/CONTRIBUTING.md#bug-reports

note: rustc 1.26.0-nightly (06fa27d7c 2018-04-01) 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

error: Could not compile `playground`.

To learn more, run the command again with --verbose.

play

the following code is good:

#![feature(nll)]

use std::cell::RefCell;

fn main() {
    let s = RefCell::new(Some(10));
    if let Some(n) = *(s.borrow_mut()) {
        println!("num: {}", n);
    }
    ;
}
@ZhangHanDong ZhangHanDong changed the title NLL bug? ICE: NLL bug? Apr 3, 2018
@Centril Centril added I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ A-NLL Area: Non-lexical lifetimes (NLL) WG-compiler-nll labels Apr 3, 2018
@nikomatsakis nikomatsakis added the NLL-complete Working towards the "valid code works" goal label Apr 10, 2018
@nikomatsakis
Copy link
Contributor

Maybe fixed by @spastorino in #49808

@spastorino
Copy link
Member

#49808 fixes this issue.

The output you get is ...

[santiago@archlinux tmp]$ rustc +stage1 test.rs 
error[E0597]: `s` does not live long enough
  --> test.rs:7:24
   |
7  |     if let Some(n) = *(s.borrow_mut()) {
   |                       -^--------------
   |                       ||
   |                       |borrowed value does not live long enough
   |                       borrow may end up in a temporary, created here
...
10 | }
   | -
   | |
   | borrowed value only lives until here
   | temporary later dropped here, potentially using the reference

error: aborting due to previous error

For more information about this error, try `rustc --explain E0597`.

@ZhangHanDong
Copy link
Author

@spastorino

Why is it still error with NLL ?

@spastorino
Copy link
Member

@ZhangHanDong I was just paying attention to the thing not ICEing :). Seems like there's something wrong /cc @nikomatsakis

@spastorino
Copy link
Member

@ZhangHanDong @nikomatsakis explained here the bug #46413 (comment) so besides from the ICE there's something else going on :)

@ZhangHanDong
Copy link
Author

@nikomatsakis

Why is it still error with NLL in #49808?
Can you help me explain the puzzle?

@KiChjang
Copy link
Member

@ZhangHanDong I'm inclined to believe that this is the drop check rule in action again, because if you add a semicolon right after your if-let statement, the code compiles:

#![feature(nll)]

use std::cell::RefCell;

fn main() {
    let s = RefCell::new(Some(10));
    if let Some(n) = *(s.borrow_mut()) {
        println!("num: {}", n);
    };
}

In other words, the BorrowMut that the borrow_mut call generates does not strictly outlive the n variable binding (they are dropped at the same time). See further details here.

@KiChjang KiChjang added the A-destructors Area: Destructors (`Drop`, …) label Apr 30, 2018
@KiChjang
Copy link
Member

Closing as a duplicate of #22321.

@pnkfelix
Copy link
Member

(it is more specifically a duplicate of #21114)

@pnkfelix
Copy link
Member

pnkfelix commented Sep 20, 2019

(or really, at this point I might argue its a duplicate of #46413 ...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-destructors Area: Destructors (`Drop`, …) A-NLL Area: Non-lexical lifetimes (NLL) I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ NLL-complete Working towards the "valid code works" goal
Projects
None yet
Development

No branches or pull requests

6 participants