Skip to content

Commit 798ce4a

Browse files
committed
Auto merge of #30856 - mneumann:thread_local_extern, r=alexcrichton
This will correctly add the thread_local attribute to the external static variable ```errno```: ```rust extern { #[thread_local] static errno: c_int; } ``` Before this commit, the thread_local attribute is ignored. Fixes #30795. Thanks @alexcrichton for pointing out the solution.
2 parents c9852e2 + 78d9544 commit 798ce4a

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

src/librustc_trans/trans/foreign.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ use syntax::attr;
3939
use syntax::codemap::Span;
4040
use syntax::parse::token::{InternedString, special_idents};
4141
use syntax::ast;
42+
use syntax::attr::AttrMetaMethods;
4243

4344
use rustc_front::print::pprust;
4445
use rustc_front::hir;
@@ -120,8 +121,8 @@ pub fn register_static(ccx: &CrateContext,
120121
let llty = type_of::type_of(ccx, ty);
121122

122123
let ident = link_name(foreign_item);
123-
match attr::first_attr_value_str_by_name(&foreign_item.attrs,
124-
"linkage") {
124+
let c = match attr::first_attr_value_str_by_name(&foreign_item.attrs,
125+
"linkage") {
125126
// If this is a static with a linkage specified, then we need to handle
126127
// it a little specially. The typesystem prevents things like &T and
127128
// extern "C" fn() from being non-null, so we can't just declare a
@@ -166,7 +167,16 @@ pub fn register_static(ccx: &CrateContext,
166167
}
167168
None => // Generate an external declaration.
168169
declare::declare_global(ccx, &ident[..], llty),
170+
};
171+
172+
// Handle thread-local external statics.
173+
for attr in foreign_item.attrs.iter() {
174+
if attr.check_name("thread_local") {
175+
llvm::set_thread_local(c, true);
176+
}
169177
}
178+
179+
return c;
170180
}
171181

172182
// only use this for foreign function ABIs and glue, use `get_extern_rust_fn` for Rust functions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(thread_local)]
12+
#![feature(cfg_target_thread_local)]
13+
#![crate_type = "lib"]
14+
15+
#[no_mangle]
16+
#[cfg_attr(target_thread_local, thread_local)]
17+
pub static FOO: u32 = 3;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// ignore-windows
12+
// aux-build:thread-local-extern-static.rs
13+
14+
#![feature(thread_local)]
15+
#![feature(cfg_target_thread_local)]
16+
17+
extern crate thread_local_extern_static;
18+
19+
extern {
20+
#[cfg_attr(target_thread_local, thread_local)]
21+
static FOO: u32;
22+
}
23+
24+
fn main() {
25+
assert_eq!(FOO, 3);
26+
}

0 commit comments

Comments
 (0)