]> git.lizzy.rs Git - rust.git/commitdiff
Auto merge of #84507 - crlf0710:codegen_nonlocal_main_wrapper, r=nagisa
authorbors <bors@rust-lang.org>
Mon, 10 May 2021 00:42:31 +0000 (00:42 +0000)
committerbors <bors@rust-lang.org>
Mon, 10 May 2021 00:42:31 +0000 (00:42 +0000)
Add primary marker on codegen unit and generate main wrapper on primary codegen.

This is the codegen part of changes extracted from #84062.

This add a marker called `primary` on each codegen units, where exactly one codegen unit will be `primary = true` at a time. This specific codegen unit will take charge of generating `main` wrapper when `main` is imported from a foreign crate after the implementation of RFC 1260.

cc #28937

I'm not sure who should i ask for review for codegen changes, so feel free to reassign.
r? `@nagisa`

compiler/rustc_codegen_ssa/src/base.rs
compiler/rustc_middle/src/mir/mono.rs
compiler/rustc_mir/src/monomorphize/partitioning/mod.rs
src/test/ui/entry-point/imported_main_from_extern_crate.rs
src/test/ui/entry-point/imported_main_from_extern_crate.stderr [deleted file]

index e045a23eb0ce3bcb62d52f7f3e0906be1d9dc6e7..7a19b0e4d5ac3565205aaa74147e05f6f75cb010 100644 (file)
@@ -357,20 +357,9 @@ pub fn maybe_create_entry_wrapper<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
         if !cx.codegen_unit().contains_item(&MonoItem::Fn(instance)) {
             return None;
         }
-    } else {
-        // FIXME: Add support for non-local main fn codegen
-        let span = cx.tcx().main_def.unwrap().span;
-        let n = 28937;
-        cx.sess()
-            .struct_span_err(span, "entry symbol `main` from foreign crate is not yet supported.")
-            .note(&format!(
-                "see issue #{} <https://github.com/rust-lang/rust/issues/{}> \
-                 for more information",
-                n, n,
-            ))
-            .emit();
-        cx.sess().abort_if_errors();
-        bug!();
+    } else if !cx.codegen_unit().is_primary() {
+        // We want to create the wrapper only when the codegen unit is the primary one
+        return None;
     }
 
     let main_llfn = cx.get_fn_addr(instance);
index 77f38e52ad2e415e22a6411ed55aa586c53f59a2..67440e6e0edf03140ae0c5f7a1a2932087a19c19 100644 (file)
@@ -229,6 +229,7 @@ pub struct CodegenUnit<'tcx> {
     name: Symbol,
     items: FxHashMap<MonoItem<'tcx>, (Linkage, Visibility)>,
     size_estimate: Option<usize>,
+    primary: bool,
 }
 
 /// Specifies the linkage type for a `MonoItem`.
@@ -258,7 +259,7 @@ pub enum Visibility {
 
 impl<'tcx> CodegenUnit<'tcx> {
     pub fn new(name: Symbol) -> CodegenUnit<'tcx> {
-        CodegenUnit { name, items: Default::default(), size_estimate: None }
+        CodegenUnit { name, items: Default::default(), size_estimate: None, primary: false }
     }
 
     pub fn name(&self) -> Symbol {
@@ -269,6 +270,14 @@ pub fn set_name(&mut self, name: Symbol) {
         self.name = name;
     }
 
+    pub fn is_primary(&self) -> bool {
+        self.primary
+    }
+
+    pub fn make_primary(&mut self) {
+        self.primary = true;
+    }
+
     pub fn items(&self) -> &FxHashMap<MonoItem<'tcx>, (Linkage, Visibility)> {
         &self.items
     }
@@ -378,6 +387,7 @@ fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHas
             name,
             // The size estimate is not relevant to the hash
             size_estimate: _,
+            primary: _,
         } = *self;
 
         name.hash_stable(hcx, hasher);
index dc2379fd92b834cba2dc31d3919230fbbede99f1..333cb30159093b4eaab17c675f6992c4b30d76fe 100644 (file)
@@ -350,12 +350,14 @@ fn collect_and_partition_mono_items<'tcx>(
     let (codegen_units, _) = tcx.sess.time("partition_and_assert_distinct_symbols", || {
         sync::join(
             || {
-                &*tcx.arena.alloc_from_iter(partition(
+                let mut codegen_units = partition(
                     tcx,
                     &mut items.iter().cloned(),
                     tcx.sess.codegen_units(),
                     &inlining_map,
-                ))
+                );
+                codegen_units[0].make_primary();
+                &*tcx.arena.alloc_from_iter(codegen_units)
             },
             || assert_symbols_are_distinct(tcx, items.iter()),
         )
index 6bbf67fa5408dcdd42f7df108770c68368a0e129..4fddfc44ac60a2afb753d09317d4888013303a2f 100644 (file)
@@ -1,9 +1,7 @@
-// build-fail
+// run-pass
 // aux-build:main_functions.rs
 
 #![feature(imported_main)]
 
 extern crate main_functions;
-pub use main_functions::boilerplate as main; //~ ERROR entry symbol `main` from foreign crate
-
-// FIXME: Should be run-pass
+pub use main_functions::boilerplate as main;
diff --git a/src/test/ui/entry-point/imported_main_from_extern_crate.stderr b/src/test/ui/entry-point/imported_main_from_extern_crate.stderr
deleted file mode 100644 (file)
index 8792e1e..0000000
+++ /dev/null
@@ -1,10 +0,0 @@
-error: entry symbol `main` from foreign crate is not yet supported.
-  --> $DIR/imported_main_from_extern_crate.rs:7:9
-   |
-LL | pub use main_functions::boilerplate as main;
-   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-   |
-   = note: see issue #28937 <https://github.com/rust-lang/rust/issues/28937> for more information
-
-error: aborting due to previous error
-