Skip to content

Commit e3ded4f

Browse files
authored
Rollup merge of #92933 - bjorn3:no_bin_lib_mixing, r=estebank
Deny mixing bin crate type with lib crate types The produced library would get a main shim too which conflicts with the main shim of the executable linking the library. ``` $ cat > main1.rs <<EOF fn main() {} pub fn bar() {} EOF $ cat > main2.rs <<EOF extern crate main1; fn main() { main1::bar(); } EOF $ rustc --crate-type bin --crate-type lib main1.rs $ rustc -L. main2.rs error: linking with `cc` failed: exit status: 1 [...] = note: /usr/bin/ld: /tmp/crate_bin_lib/libmain1.rlib(main1.main1.707747aa-cgu.0.rcgu.o): in function `main': main1.707747aa-cgu.0:(.text.main+0x0): multiple definition of `main'; main2.main2.02a148fe-cgu.0.rcgu.o:main2.02a148fe-cgu.0:(.text.main+0x0): first defined here collect2: error: ld returned 1 exit status ```
2 parents dd11126 + 0b59630 commit e3ded4f

File tree

6 files changed

+12
-21
lines changed

6 files changed

+12
-21
lines changed

compiler/rustc_builtin_macros/src/proc_macro_harness.rs

-5
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ pub fn inject(
5656
is_proc_macro_crate: bool,
5757
has_proc_macro_decls: bool,
5858
is_test_crate: bool,
59-
num_crate_types: usize,
6059
handler: &rustc_errors::Handler,
6160
) -> ast::Crate {
6261
let ecfg = ExpansionConfig::default("proc_macro".to_string());
@@ -81,10 +80,6 @@ pub fn inject(
8180
return krate;
8281
}
8382

84-
if num_crate_types > 1 {
85-
handler.err("cannot mix `proc-macro` crate type with others");
86-
}
87-
8883
if is_test_crate {
8984
return krate;
9085
}

compiler/rustc_interface/src/passes.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,18 @@ pub fn configure_and_expand(
393393
});
394394

395395
let crate_types = sess.crate_types();
396+
let is_executable_crate = crate_types.contains(&CrateType::Executable);
396397
let is_proc_macro_crate = crate_types.contains(&CrateType::ProcMacro);
397398

399+
if crate_types.len() > 1 {
400+
if is_executable_crate {
401+
sess.err("cannot mix `bin` crate type with others");
402+
}
403+
if is_proc_macro_crate {
404+
sess.err("cannot mix `proc-macro` crate type with others");
405+
}
406+
}
407+
398408
// For backwards compatibility, we don't try to run proc macro injection
399409
// if rustdoc is run on a proc macro crate without '--crate-type proc-macro' being
400410
// specified. This should only affect users who manually invoke 'rustdoc', as
@@ -411,7 +421,6 @@ pub fn configure_and_expand(
411421
msg.emit()
412422
} else {
413423
krate = sess.time("maybe_create_a_macro_crate", || {
414-
let num_crate_types = crate_types.len();
415424
let is_test_crate = sess.opts.test;
416425
rustc_builtin_macros::proc_macro_harness::inject(
417426
sess,
@@ -420,7 +429,6 @@ pub fn configure_and_expand(
420429
is_proc_macro_crate,
421430
has_proc_macro_decls,
422431
is_test_crate,
423-
num_crate_types,
424432
sess.diagnostic(),
425433
)
426434
});

src/test/run-make-fulldeps/libs-and-bins/Makefile

-6
This file was deleted.

src/test/run-make-fulldeps/libs-and-bins/foo.rs

-4
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
-include ../tools.mk
22

33
all:
4-
$(RUSTC) foo-bar.rs
4+
$(RUSTC) foo-bar.rs --crate-type bin
55
[ -f $(TMPDIR)/$(call BIN,foo-bar) ]
6+
$(RUSTC) foo-bar.rs --crate-type lib
67
[ -f $(TMPDIR)/libfoo_bar.rlib ]
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
#![crate_type = "lib"]
2-
#![crate_type = "bin"]
3-
41
fn main() {}

0 commit comments

Comments
 (0)