Skip to content

Commit 6351267

Browse files
authored
Rollup merge of rust-lang#61231 - pnkfelix:issue-59548-linkage-diagnostic, r=petrochenkov
Fix linkage diagnostic so it doesn't ICE for external crates Fix linkage diagnostic so it doesn't ICE for external crates (As a drive-by improvement, improved the diagnostic to indicate *why* `*const T` or `*mut T` is required.) Fix rust-lang#59548 Fix rust-lang#61232
2 parents 74b3593 + c8887ab commit 6351267

15 files changed

+99
-18
lines changed

src/librustc_codegen_llvm/consts.rs

+7-16
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ fn check_and_apply_linkage(
102102
attrs: &CodegenFnAttrs,
103103
ty: Ty<'tcx>,
104104
sym: LocalInternedString,
105-
span: Option<Span>
105+
span: Span
106106
) -> &'ll Value {
107107
let llty = cx.layout_of(ty).llvm_type(cx);
108108
if let Some(linkage) = attrs.linkage {
@@ -116,11 +116,8 @@ fn check_and_apply_linkage(
116116
let llty2 = if let ty::RawPtr(ref mt) = ty.sty {
117117
cx.layout_of(mt.ty).llvm_type(cx)
118118
} else {
119-
if let Some(span) = span {
120-
cx.sess().span_fatal(span, "must have type `*const T` or `*mut T`")
121-
} else {
122-
bug!("must have type `*const T` or `*mut T`")
123-
}
119+
cx.sess().span_fatal(
120+
span, "must have type `*const T` or `*mut T` due to `#[linkage]` attribute")
124121
};
125122
unsafe {
126123
// Declare a symbol `foo` with the desired linkage.
@@ -136,14 +133,7 @@ fn check_and_apply_linkage(
136133
let mut real_name = "_rust_extern_with_linkage_".to_string();
137134
real_name.push_str(&sym);
138135
let g2 = cx.define_global(&real_name, llty).unwrap_or_else(||{
139-
if let Some(span) = span {
140-
cx.sess().span_fatal(
141-
span,
142-
&format!("symbol `{}` is already defined", &sym)
143-
)
144-
} else {
145-
bug!("symbol `{}` is already defined", &sym)
146-
}
136+
cx.sess().span_fatal(span, &format!("symbol `{}` is already defined", &sym))
147137
});
148138
llvm::LLVMRustSetLinkage(g2, llvm::Linkage::InternalLinkage);
149139
llvm::LLVMSetInitializer(g2, g1);
@@ -240,7 +230,7 @@ impl CodegenCx<'ll, 'tcx> {
240230
ref attrs, span, node: hir::ForeignItemKind::Static(..), ..
241231
}) => {
242232
let fn_attrs = self.tcx.codegen_fn_attrs(def_id);
243-
(check_and_apply_linkage(&self, &fn_attrs, ty, sym, Some(span)), attrs)
233+
(check_and_apply_linkage(&self, &fn_attrs, ty, sym, span), attrs)
244234
}
245235

246236
item => bug!("get_static: expected static, found {:?}", item)
@@ -260,7 +250,8 @@ impl CodegenCx<'ll, 'tcx> {
260250
debug!("get_static: sym={} item_attr={:?}", sym, self.tcx.item_attrs(def_id));
261251

262252
let attrs = self.tcx.codegen_fn_attrs(def_id);
263-
let g = check_and_apply_linkage(&self, &attrs, ty, sym, None);
253+
let span = self.tcx.def_span(def_id);
254+
let g = check_and_apply_linkage(&self, &attrs, ty, sym, span);
264255

265256
// Thread-local statics in some other crate need to *always* be linked
266257
// against in a thread-local fashion, so we need to be sure to apply the
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#![feature(linkage)]
2+
#![crate_type = "lib"]
3+
4+
extern {
5+
#[linkage="external"]
6+
pub static collision: *const i32;
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#![feature(linkage)]
2+
#![crate_type = "lib"]
3+
4+
#[linkage="external"]
5+
pub static EXTERN: u32 = 0;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// rust-lang/rust#61232: We used to ICE when trying to detect a
2+
// collision on the symbol generated for the external linkage item in
3+
// an extern crate.
4+
5+
// aux-build:def_colliding_external.rs
6+
7+
extern crate def_colliding_external as dep1;
8+
9+
#[no_mangle]
10+
pub static _rust_extern_with_linkage_collision: i32 = 0;
11+
12+
mod dep2 {
13+
#[no_mangle]
14+
pub static collision: usize = 0;
15+
}
16+
17+
fn main() {
18+
unsafe {
19+
println!("{:p}", &dep1::collision);
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: symbol `collision` is already defined
2+
--> $DIR/auxiliary/def_colliding_external.rs:6:5
3+
|
4+
LL | pub static collision: *const i32;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
7+
error: aborting due to previous error
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#![feature(linkage)]
2+
3+
mod dep1 {
4+
extern {
5+
#[linkage="external"]
6+
#[no_mangle]
7+
pub static collision: *const i32; //~ ERROR symbol `collision` is already defined
8+
}
9+
}
10+
11+
#[no_mangle]
12+
pub static _rust_extern_with_linkage_collision: i32 = 0;
13+
14+
mod dep2 {
15+
#[no_mangle]
16+
pub static collision: usize = 0;
17+
}
18+
19+
fn main() {
20+
unsafe {
21+
println!("{:p}", &dep1::collision);
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: symbol `collision` is already defined
2+
--> $DIR/linkage-detect-local-generated-name-collision.rs:7:9
3+
|
4+
LL | pub static collision: *const i32;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
7+
error: aborting due to previous error
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// rust-lang/rust#59548: We used to ICE when trying to use a static
2+
// with a type that violated its own `#[linkage]`.
3+
4+
// aux-build:def_illtyped_external.rs
5+
6+
extern crate def_illtyped_external as dep;
7+
8+
fn main() {
9+
println!("{:p}", &dep::EXTERN);
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: must have type `*const T` or `*mut T` due to `#[linkage]` attribute
2+
--> $DIR/auxiliary/def_illtyped_external.rs:5:1
3+
|
4+
LL | pub static EXTERN: u32 = 0;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
7+
error: aborting due to previous error
8+

src/test/ui/linkage2.rs src/test/ui/linkage-attr/linkage2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
extern {
99
#[linkage = "extern_weak"] static foo: i32;
10-
//~^ ERROR: must have type `*const T` or `*mut T`
10+
//~^ ERROR: must have type `*const T` or `*mut T` due to `#[linkage]` attribute
1111
}
1212

1313
fn main() {

src/test/ui/linkage2.stderr src/test/ui/linkage-attr/linkage2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: must have type `*const T` or `*mut T`
1+
error: must have type `*const T` or `*mut T` due to `#[linkage]` attribute
22
--> $DIR/linkage2.rs:9:32
33
|
44
LL | #[linkage = "extern_weak"] static foo: i32;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)