Skip to content

Commit 2466644

Browse files
committed
Ensure main() always has external linkage
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.
1 parent 026339e commit 2466644

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/librustc_mir/monomorphize/item.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,13 @@ pub trait MonoItemExt<'a, 'tcx>: fmt::Debug {
118118

119119
match *self.as_mono_item() {
120120
MonoItem::Fn(ref instance) => {
121+
let entry_def_id =
122+
tcx.sess.entry_fn.borrow().map(|(id, _)| tcx.hir.local_def_id(id));
121123
// If this function isn't inlined or otherwise has explicit
122124
// linkage, then we'll be creating a globally shared version.
123125
if self.explicit_linkage(tcx).is_some() ||
124-
!instance.def.requires_local(tcx)
126+
!instance.def.requires_local(tcx) ||
127+
Some(instance.def_id()) == entry_def_id
125128
{
126129
return InstantiationMode::GloballyShared { may_conflict: false }
127130
}

src/test/run-pass/inlined-main.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[inline(always)]
12+
fn main() {}

0 commit comments

Comments
 (0)