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 ```
This commit is contained in:
parent
38c22af015
commit
1b0c0eb717
2 changed files with 10 additions and 7 deletions
|
@ -56,7 +56,6 @@ pub fn inject(
|
||||||
is_proc_macro_crate: bool,
|
is_proc_macro_crate: bool,
|
||||||
has_proc_macro_decls: bool,
|
has_proc_macro_decls: bool,
|
||||||
is_test_crate: bool,
|
is_test_crate: bool,
|
||||||
num_crate_types: usize,
|
|
||||||
handler: &rustc_errors::Handler,
|
handler: &rustc_errors::Handler,
|
||||||
) -> ast::Crate {
|
) -> ast::Crate {
|
||||||
let ecfg = ExpansionConfig::default("proc_macro".to_string());
|
let ecfg = ExpansionConfig::default("proc_macro".to_string());
|
||||||
|
@ -81,10 +80,6 @@ pub fn inject(
|
||||||
return krate;
|
return krate;
|
||||||
}
|
}
|
||||||
|
|
||||||
if num_crate_types > 1 {
|
|
||||||
handler.err("cannot mix `proc-macro` crate type with others");
|
|
||||||
}
|
|
||||||
|
|
||||||
if is_test_crate {
|
if is_test_crate {
|
||||||
return krate;
|
return krate;
|
||||||
}
|
}
|
||||||
|
|
|
@ -382,8 +382,18 @@ pub fn configure_and_expand(
|
||||||
});
|
});
|
||||||
|
|
||||||
let crate_types = sess.crate_types();
|
let crate_types = sess.crate_types();
|
||||||
|
let is_executable_crate = crate_types.contains(&CrateType::Executable);
|
||||||
let is_proc_macro_crate = crate_types.contains(&CrateType::ProcMacro);
|
let is_proc_macro_crate = crate_types.contains(&CrateType::ProcMacro);
|
||||||
|
|
||||||
|
if crate_types.len() > 1 {
|
||||||
|
if is_executable_crate {
|
||||||
|
sess.err("cannot mix `bin` crate type with others");
|
||||||
|
}
|
||||||
|
if is_proc_macro_crate {
|
||||||
|
sess.err("cannot mix `proc-macro` crate type with others");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// For backwards compatibility, we don't try to run proc macro injection
|
// For backwards compatibility, we don't try to run proc macro injection
|
||||||
// if rustdoc is run on a proc macro crate without '--crate-type proc-macro' being
|
// if rustdoc is run on a proc macro crate without '--crate-type proc-macro' being
|
||||||
// specified. This should only affect users who manually invoke 'rustdoc', as
|
// specified. This should only affect users who manually invoke 'rustdoc', as
|
||||||
|
@ -400,7 +410,6 @@ pub fn configure_and_expand(
|
||||||
msg.emit()
|
msg.emit()
|
||||||
} else {
|
} else {
|
||||||
krate = sess.time("maybe_create_a_macro_crate", || {
|
krate = sess.time("maybe_create_a_macro_crate", || {
|
||||||
let num_crate_types = crate_types.len();
|
|
||||||
let is_test_crate = sess.opts.test;
|
let is_test_crate = sess.opts.test;
|
||||||
rustc_builtin_macros::proc_macro_harness::inject(
|
rustc_builtin_macros::proc_macro_harness::inject(
|
||||||
sess,
|
sess,
|
||||||
|
@ -409,7 +418,6 @@ pub fn configure_and_expand(
|
||||||
is_proc_macro_crate,
|
is_proc_macro_crate,
|
||||||
has_proc_macro_decls,
|
has_proc_macro_decls,
|
||||||
is_test_crate,
|
is_test_crate,
|
||||||
num_crate_types,
|
|
||||||
sess.diagnostic(),
|
sess.diagnostic(),
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue