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

Compiler crash with #[thread_local] #47215

Closed
Amanieu opened this issue Jan 5, 2018 · 3 comments
Closed

Compiler crash with #[thread_local] #47215

Amanieu opened this issue Jan 5, 2018 · 3 comments
Assignees
Labels
A-borrow-checker Area: The borrow checker A-MIR Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ P-medium Medium priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Amanieu
Copy link
Member

Amanieu commented Jan 5, 2018

#![feature(thread_local)]

#[thread_local]
static mut X: ::std::sync::atomic::AtomicUsize = ::std::sync::atomic::ATOMIC_USIZE_INIT;

fn main() {
    unsafe {
        let mut x = X;
        let _y = x.get_mut();
    }
}

produces the following error:

thread 'rustc' panicked at 'called `Result::unwrap()` on an `Err` value: (MoveData { move_paths: [MovePath { place: _0 }, MovePath { place: _1 }, MovePath { place: _2 }, MovePath { place: _3 }, MovePath { place: _4 }], moves: [mp2@bb0[3], mp2@bb0[4], mp4@bb0[8], mp4@bb1[0], mp3@bb1[2], mp1@bb1[3], mp0@bb1[4]], loc_map: LocationMap { map: [[[], [], [], [mo0], [mo1], [], [], [], [mo2]], [[mo3], [], [mo4], [mo5], [mo6]]] }, path_map: [[mo6], [mo5], [mo0, mo1], [mo4], [mo2, mo3]], rev_lookup: MovePathLookup { locals: [mp0, mp1, mp2, mp3, mp4], projections: {} }, inits: [mp2@src/main.rs:8:21: 8:22 (Deep), mp1@src/main.rs:8:21: 8:22 (Deep), mp4@src/main.rs:9:18: 9:19 (Deep), mp3@src/main.rs:9:18: 9:29 (NonPanicPathOnly), mp0@src/main.rs:7:5: 10:6 (Deep)], init_loc_map: LocationMap { map: [[[], [], [in0], [in1], [], [], [], [in2], [in3]], [[], [in4], [], [], []]] }, init_path_map: [[in4], [in1], [in0], [in3], [in2]] }, [IllegalMove { cannot_move_out_of: IllegalMoveOrigin { span: src/main.rs:8:21: 8:22, kind: Static } }])', /checkout/src/libcore/result.rs:916:5

Also, the following code fails to compile due to a lifetime error, but doesn't crash the compiler:

#![feature(thread_local)]

#[thread_local]
static mut X: ::std::sync::atomic::AtomicUsize = ::std::sync::atomic::ATOMIC_USIZE_INIT;

fn main() {
    unsafe {
        let mut _x = X.get_mut();
    }
}

Output:

error[E0597]: borrowed value does not live long enough
 --> src/main.rs:8:22
  |
8 |         let mut _x = X.get_mut();
  |                      ^          - temporary value dropped here while still borrowed
  |                      |
  |                      temporary value does not live long enough
9 |     }
  |     - temporary value needs to live until here
  |
  = note: consider using a `let` binding to increase its lifetime
@nagisa nagisa added A-borrow-checker Area: The borrow checker A-MIR Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ labels Jan 5, 2018
@matthewjasper
Copy link
Contributor

Both are solved by mir borrowck/nll.
It looks like ast borrowck incorrectly thinks that thread locals are temporaries, so can be moved from, and drop elaboration doesn't.

Def::Static(def_id, mutbl) => {
// `#[thread_local]` statics may not outlive the current function.
for attr in &self.tcx.get_attrs(def_id)[..] {
if attr.check_name("thread_local") {
return Ok(self.cat_rvalue_node(id, span, expr_ty));
}
}

@jkordish jkordish added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-bug Category: This is a bug. labels Mar 29, 2018
@pnkfelix pnkfelix added A-NLL Area: Non-lexical lifetimes (NLL) NLL-fixed-by-NLL Bugs fixed, but only when NLL is enabled. labels Oct 16, 2018
@pnkfelix pnkfelix self-assigned this Oct 17, 2018
pietroalbini added a commit to pietroalbini/rust that referenced this issue Oct 23, 2018
…-from-moving-out-of-thread-local-under-ast-borrowck, r=nikomatsakis

Do not allow moving out of thread local under ast borrowck

AST borrowck failed to prevent moving out of a thread-local static.

This was broken. And it also (sometimes?) caused an ICE during drop elaboration.

Fix rust-lang#47215
Fix rust-lang#54797
@pnkfelix
Copy link
Member

pnkfelix commented Oct 24, 2018

In addition to being fixed by NLL, this is also fixed by #55150. (The NLL-fixed-by-NLL is intended for issues that are only fixed when NLL is enabled...)

@pnkfelix pnkfelix added P-medium Medium priority and removed NLL-fixed-by-NLL Bugs fixed, but only when NLL is enabled. A-NLL Area: Non-lexical lifetimes (NLL) labels Oct 24, 2018
@pnkfelix
Copy link
Member

(assigning medium priority; the PR is approved that fixes this, #55150, is approved, and this ICE is only exposed by a feature that is currently nightly-only.)

pietroalbini added a commit to pietroalbini/rust that referenced this issue Oct 25, 2018
…-from-moving-out-of-thread-local-under-ast-borrowck, r=nikomatsakis

Do not allow moving out of thread local under ast borrowck

AST borrowck failed to prevent moving out of a thread-local static.

This was broken. And it also (sometimes?) caused an ICE during drop elaboration.

Fix rust-lang#47215
Fix rust-lang#54797
bors added a commit that referenced this issue Oct 27, 2018
…g-out-of-thread-local-under-ast-borrowck, r=nikomatsakis

Do not allow moving out of thread local under ast borrowck

AST borrowck failed to prevent moving out of a thread-local static.

This was broken. And it also (sometimes?) caused an ICE during drop elaboration.

Fix #47215
Fix #54797
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-MIR Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ P-medium Medium priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants