Skip to content

Commit

Permalink
Rollup merge of rust-lang#61231 - pnkfelix:issue-59548-linkage-diagno…
Browse files Browse the repository at this point in the history
…stic, 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
  • Loading branch information
Centril authored May 30, 2019
2 parents 74b3593 + c8887ab commit 6351267
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 18 deletions.
23 changes: 7 additions & 16 deletions src/librustc_codegen_llvm/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ fn check_and_apply_linkage(
attrs: &CodegenFnAttrs,
ty: Ty<'tcx>,
sym: LocalInternedString,
span: Option<Span>
span: Span
) -> &'ll Value {
let llty = cx.layout_of(ty).llvm_type(cx);
if let Some(linkage) = attrs.linkage {
Expand All @@ -116,11 +116,8 @@ fn check_and_apply_linkage(
let llty2 = if let ty::RawPtr(ref mt) = ty.sty {
cx.layout_of(mt.ty).llvm_type(cx)
} else {
if let Some(span) = span {
cx.sess().span_fatal(span, "must have type `*const T` or `*mut T`")
} else {
bug!("must have type `*const T` or `*mut T`")
}
cx.sess().span_fatal(
span, "must have type `*const T` or `*mut T` due to `#[linkage]` attribute")
};
unsafe {
// Declare a symbol `foo` with the desired linkage.
Expand All @@ -136,14 +133,7 @@ fn check_and_apply_linkage(
let mut real_name = "_rust_extern_with_linkage_".to_string();
real_name.push_str(&sym);
let g2 = cx.define_global(&real_name, llty).unwrap_or_else(||{
if let Some(span) = span {
cx.sess().span_fatal(
span,
&format!("symbol `{}` is already defined", &sym)
)
} else {
bug!("symbol `{}` is already defined", &sym)
}
cx.sess().span_fatal(span, &format!("symbol `{}` is already defined", &sym))
});
llvm::LLVMRustSetLinkage(g2, llvm::Linkage::InternalLinkage);
llvm::LLVMSetInitializer(g2, g1);
Expand Down Expand Up @@ -240,7 +230,7 @@ impl CodegenCx<'ll, 'tcx> {
ref attrs, span, node: hir::ForeignItemKind::Static(..), ..
}) => {
let fn_attrs = self.tcx.codegen_fn_attrs(def_id);
(check_and_apply_linkage(&self, &fn_attrs, ty, sym, Some(span)), attrs)
(check_and_apply_linkage(&self, &fn_attrs, ty, sym, span), attrs)
}

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

let attrs = self.tcx.codegen_fn_attrs(def_id);
let g = check_and_apply_linkage(&self, &attrs, ty, sym, None);
let span = self.tcx.def_span(def_id);
let g = check_and_apply_linkage(&self, &attrs, ty, sym, span);

// Thread-local statics in some other crate need to *always* be linked
// against in a thread-local fashion, so we need to be sure to apply the
Expand Down
7 changes: 7 additions & 0 deletions src/test/ui/linkage-attr/auxiliary/def_colliding_external.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#![feature(linkage)]
#![crate_type = "lib"]

extern {
#[linkage="external"]
pub static collision: *const i32;
}
5 changes: 5 additions & 0 deletions src/test/ui/linkage-attr/auxiliary/def_illtyped_external.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#![feature(linkage)]
#![crate_type = "lib"]

#[linkage="external"]
pub static EXTERN: u32 = 0;
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// rust-lang/rust#61232: We used to ICE when trying to detect a
// collision on the symbol generated for the external linkage item in
// an extern crate.

// aux-build:def_colliding_external.rs

extern crate def_colliding_external as dep1;

#[no_mangle]
pub static _rust_extern_with_linkage_collision: i32 = 0;

mod dep2 {
#[no_mangle]
pub static collision: usize = 0;
}

fn main() {
unsafe {
println!("{:p}", &dep1::collision);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: symbol `collision` is already defined
--> $DIR/auxiliary/def_colliding_external.rs:6:5
|
LL | pub static collision: *const i32;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#![feature(linkage)]

mod dep1 {
extern {
#[linkage="external"]
#[no_mangle]
pub static collision: *const i32; //~ ERROR symbol `collision` is already defined
}
}

#[no_mangle]
pub static _rust_extern_with_linkage_collision: i32 = 0;

mod dep2 {
#[no_mangle]
pub static collision: usize = 0;
}

fn main() {
unsafe {
println!("{:p}", &dep1::collision);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: symbol `collision` is already defined
--> $DIR/linkage-detect-local-generated-name-collision.rs:7:9
|
LL | pub static collision: *const i32;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

10 changes: 10 additions & 0 deletions src/test/ui/linkage-attr/linkage-requires-raw-ptr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// rust-lang/rust#59548: We used to ICE when trying to use a static
// with a type that violated its own `#[linkage]`.

// aux-build:def_illtyped_external.rs

extern crate def_illtyped_external as dep;

fn main() {
println!("{:p}", &dep::EXTERN);
}
8 changes: 8 additions & 0 deletions src/test/ui/linkage-attr/linkage-requires-raw-ptr.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: must have type `*const T` or `*mut T` due to `#[linkage]` attribute
--> $DIR/auxiliary/def_illtyped_external.rs:5:1
|
LL | pub static EXTERN: u32 = 0;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

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

fn main() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: must have type `*const T` or `*mut T`
error: must have type `*const T` or `*mut T` due to `#[linkage]` attribute
--> $DIR/linkage2.rs:9:32
|
LL | #[linkage = "extern_weak"] static foo: i32;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 6351267

Please sign in to comment.