Skip to content

Commit 1b0c0eb

Browse files
committed
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 ```
1 parent 38c22af commit 1b0c0eb

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
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
@@ -382,8 +382,18 @@ pub fn configure_and_expand(
382382
});
383383

384384
let crate_types = sess.crate_types();
385+
let is_executable_crate = crate_types.contains(&CrateType::Executable);
385386
let is_proc_macro_crate = crate_types.contains(&CrateType::ProcMacro);
386387

388+
if crate_types.len() > 1 {
389+
if is_executable_crate {
390+
sess.err("cannot mix `bin` crate type with others");
391+
}
392+
if is_proc_macro_crate {
393+
sess.err("cannot mix `proc-macro` crate type with others");
394+
}
395+
}
396+
387397
// For backwards compatibility, we don't try to run proc macro injection
388398
// if rustdoc is run on a proc macro crate without '--crate-type proc-macro' being
389399
// specified. This should only affect users who manually invoke 'rustdoc', as
@@ -400,7 +410,6 @@ pub fn configure_and_expand(
400410
msg.emit()
401411
} else {
402412
krate = sess.time("maybe_create_a_macro_crate", || {
403-
let num_crate_types = crate_types.len();
404413
let is_test_crate = sess.opts.test;
405414
rustc_builtin_macros::proc_macro_harness::inject(
406415
sess,
@@ -409,7 +418,6 @@ pub fn configure_and_expand(
409418
is_proc_macro_crate,
410419
has_proc_macro_decls,
411420
is_test_crate,
412-
num_crate_types,
413421
sess.diagnostic(),
414422
)
415423
});

0 commit comments

Comments
 (0)