about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2022-02-18 16:23:28 +0100
committerGitHub <noreply@github.com>2022-02-18 16:23:28 +0100
commite3ded4fc4fa15f0b7acee429edf30e1e0cde26f8 (patch)
tree55d6dc9b06fc459da34e45980eea5f1bb69db04d
parentdd111262b2a18e532d700b67a8062faa286d1a36 (diff)
parent0b5963023d9d3bdbbf27e9f92171047cf2a7f008 (diff)
downloadrust-e3ded4fc4fa15f0b7acee429edf30e1e0cde26f8.tar.gz
rust-e3ded4fc4fa15f0b7acee429edf30e1e0cde26f8.zip
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
```
-rw-r--r--compiler/rustc_builtin_macros/src/proc_macro_harness.rs5
-rw-r--r--compiler/rustc_interface/src/passes.rs12
-rw-r--r--src/test/run-make-fulldeps/libs-and-bins/Makefile6
-rw-r--r--src/test/run-make-fulldeps/libs-and-bins/foo.rs4
-rw-r--r--src/test/run-make-fulldeps/output-with-hyphens/Makefile3
-rw-r--r--src/test/run-make-fulldeps/output-with-hyphens/foo-bar.rs3
6 files changed, 12 insertions, 21 deletions
diff --git a/compiler/rustc_builtin_macros/src/proc_macro_harness.rs b/compiler/rustc_builtin_macros/src/proc_macro_harness.rs
index 6f61e4cba07..c9dd114047b 100644
--- a/compiler/rustc_builtin_macros/src/proc_macro_harness.rs
+++ b/compiler/rustc_builtin_macros/src/proc_macro_harness.rs
@@ -56,7 +56,6 @@ pub fn inject(
     is_proc_macro_crate: bool,
     has_proc_macro_decls: bool,
     is_test_crate: bool,
-    num_crate_types: usize,
     handler: &rustc_errors::Handler,
 ) -> ast::Crate {
     let ecfg = ExpansionConfig::default("proc_macro".to_string());
@@ -81,10 +80,6 @@ pub fn inject(
         return krate;
     }
 
-    if num_crate_types > 1 {
-        handler.err("cannot mix `proc-macro` crate type with others");
-    }
-
     if is_test_crate {
         return krate;
     }
diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs
index 66e1e78b285..c0552fd200b 100644
--- a/compiler/rustc_interface/src/passes.rs
+++ b/compiler/rustc_interface/src/passes.rs
@@ -393,8 +393,18 @@ pub fn configure_and_expand(
     });
 
     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);
 
+    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
     // 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
@@ -411,7 +421,6 @@ pub fn configure_and_expand(
         msg.emit()
     } else {
         krate = sess.time("maybe_create_a_macro_crate", || {
-            let num_crate_types = crate_types.len();
             let is_test_crate = sess.opts.test;
             rustc_builtin_macros::proc_macro_harness::inject(
                 sess,
@@ -420,7 +429,6 @@ pub fn configure_and_expand(
                 is_proc_macro_crate,
                 has_proc_macro_decls,
                 is_test_crate,
-                num_crate_types,
                 sess.diagnostic(),
             )
         });
diff --git a/src/test/run-make-fulldeps/libs-and-bins/Makefile b/src/test/run-make-fulldeps/libs-and-bins/Makefile
deleted file mode 100644
index cc3b257a5c5..00000000000
--- a/src/test/run-make-fulldeps/libs-and-bins/Makefile
+++ /dev/null
@@ -1,6 +0,0 @@
--include ../tools.mk
-
-all:
-	$(RUSTC) foo.rs
-	$(call RUN,foo)
-	rm $(TMPDIR)/$(call DYLIB_GLOB,foo)
diff --git a/src/test/run-make-fulldeps/libs-and-bins/foo.rs b/src/test/run-make-fulldeps/libs-and-bins/foo.rs
deleted file mode 100644
index ae166b17840..00000000000
--- a/src/test/run-make-fulldeps/libs-and-bins/foo.rs
+++ /dev/null
@@ -1,4 +0,0 @@
-#![crate_type = "dylib"]
-#![crate_type = "bin"]
-
-fn main() {}
diff --git a/src/test/run-make-fulldeps/output-with-hyphens/Makefile b/src/test/run-make-fulldeps/output-with-hyphens/Makefile
index 783d826a53d..69a286f0b74 100644
--- a/src/test/run-make-fulldeps/output-with-hyphens/Makefile
+++ b/src/test/run-make-fulldeps/output-with-hyphens/Makefile
@@ -1,6 +1,7 @@
 -include ../tools.mk
 
 all:
-	$(RUSTC) foo-bar.rs
+	$(RUSTC) foo-bar.rs --crate-type bin
 	[ -f $(TMPDIR)/$(call BIN,foo-bar) ]
+	$(RUSTC) foo-bar.rs --crate-type lib
 	[ -f $(TMPDIR)/libfoo_bar.rlib ]
diff --git a/src/test/run-make-fulldeps/output-with-hyphens/foo-bar.rs b/src/test/run-make-fulldeps/output-with-hyphens/foo-bar.rs
index 3f1a70458e3..f328e4d9d04 100644
--- a/src/test/run-make-fulldeps/output-with-hyphens/foo-bar.rs
+++ b/src/test/run-make-fulldeps/output-with-hyphens/foo-bar.rs
@@ -1,4 +1 @@
-#![crate_type = "lib"]
-#![crate_type = "bin"]
-
 fn main() {}