]> git.lizzy.rs Git - rust.git/commitdiff
Ensure main() always has external linkage
authorvarkor <github@varkor.com>
Sun, 25 Feb 2018 23:05:06 +0000 (23:05 +0000)
committervarkor <github@varkor.com>
Sun, 25 Feb 2018 23:05:06 +0000 (23:05 +0000)
This ensures that the entry function is never elided due to inlining, even with `inline(always)`. Fixes #47783.

There were a couple of possible ways of addressing this issue; I simply picked the one that seemed most direct. A warning could be appropriate, but considering using inlining hints in other places it doesn't apply also throws no warnings, and it seems like an edge case anyway, I haven't added one for now.

src/librustc_mir/monomorphize/item.rs
src/test/run-pass/inlined-main.rs [new file with mode: 0644]

index 549919a2c891925d620aae621f8a85ae2ed80a56..7c86f5a4b1a84fce0f87807b4745ba719715e4b6 100644 (file)
@@ -118,10 +118,13 @@ fn instantiation_mode(&self,
 
         match *self.as_mono_item() {
             MonoItem::Fn(ref instance) => {
+                let entry_def_id =
+                    tcx.sess.entry_fn.borrow().map(|(id, _)| tcx.hir.local_def_id(id));
                 // If this function isn't inlined or otherwise has explicit
                 // linkage, then we'll be creating a globally shared version.
                 if self.explicit_linkage(tcx).is_some() ||
-                    !instance.def.requires_local(tcx)
+                    !instance.def.requires_local(tcx) ||
+                    Some(instance.def_id()) == entry_def_id
                 {
                     return InstantiationMode::GloballyShared  { may_conflict: false }
                 }
diff --git a/src/test/run-pass/inlined-main.rs b/src/test/run-pass/inlined-main.rs
new file mode 100644 (file)
index 0000000..1288c37
--- /dev/null
@@ -0,0 +1,12 @@
+// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#[inline(always)]
+fn main() {}