-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rustc: Allow #[no_mangle]
anywhere in a crate
#54451
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// 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. | ||
|
||
#![crate_type = "lib"] | ||
|
||
mod private { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please add test cases for these patterns? fn internal() {
#[no_mangle]
static EXTERNAL: fn() = internal;
}
static FOO: u32 = {
#[no_mangle]
static BAR: &u32 = &FOO;
0
};
const PRIVATE: () = {
#[no_mangle]
fn exported() {}
}; I can see myself writing attributes / macros that expand into those patterns. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Certainly! |
||
// CHECK: @FOO = | ||
#[no_mangle] | ||
pub static FOO: u32 = 3; | ||
|
||
// CHECK: @BAR = | ||
#[export_name = "BAR"] | ||
static BAR: u32 = 3; | ||
|
||
// CHECK: void @foo() | ||
#[no_mangle] | ||
pub extern fn foo() {} | ||
|
||
// CHECK: void @bar() | ||
#[export_name = "bar"] | ||
extern fn bar() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// 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. | ||
|
||
// compile-flags: -O | ||
// `#[no_mangle]`d functions always have external linkage, i.e. no `internal` in their `define`s | ||
|
||
#![crate_type = "lib"] | ||
#![no_std] | ||
|
||
// CHECK: define void @a() | ||
#[no_mangle] | ||
fn a() {} | ||
|
||
// CHECK: define void @b() | ||
#[no_mangle] | ||
pub fn b() {} | ||
|
||
mod private { | ||
// CHECK: define void @c() | ||
#[no_mangle] | ||
fn c() {} | ||
|
||
// CHECK: define void @d() | ||
#[no_mangle] | ||
pub fn d() {} | ||
} | ||
|
||
const HIDDEN: () = { | ||
// CHECK: define void @e() | ||
#[no_mangle] | ||
fn e() {} | ||
|
||
// CHECK: define void @f() | ||
#[no_mangle] | ||
pub fn f() {} | ||
}; | ||
|
||
// The surrounding item should not accidentally become external | ||
// CHECK: define internal {{.*}} void @_ZN22external_no_mangle_fns1x | ||
#[inline(never)] | ||
fn x() { | ||
// CHECK: define void @g() | ||
#[no_mangle] | ||
fn g() { | ||
x(); | ||
} | ||
|
||
// CHECK: define void @h() | ||
#[no_mangle] | ||
pub fn h() {} | ||
|
||
// side effect to keep `x` around | ||
unsafe { | ||
core::ptr::read_volatile(&42); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// 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. | ||
|
||
// compile-flags: -O | ||
// `#[no_mangle]`d static variables always have external linkage, i.e. no `internal` in their | ||
// definitions | ||
|
||
#![crate_type = "lib"] | ||
#![no_std] | ||
|
||
// CHECK: @A = local_unnamed_addr constant | ||
#[no_mangle] | ||
static A: u8 = 0; | ||
|
||
// CHECK: @B = local_unnamed_addr global | ||
#[no_mangle] | ||
static mut B: u8 = 0; | ||
|
||
// CHECK: @C = local_unnamed_addr constant | ||
#[no_mangle] | ||
pub static C: u8 = 0; | ||
|
||
// CHECK: @D = local_unnamed_addr global | ||
#[no_mangle] | ||
pub static mut D: u8 = 0; | ||
|
||
mod private { | ||
// CHECK: @E = local_unnamed_addr constant | ||
#[no_mangle] | ||
static E: u8 = 0; | ||
|
||
// CHECK: @F = local_unnamed_addr global | ||
#[no_mangle] | ||
static mut F: u8 = 0; | ||
|
||
// CHECK: @G = local_unnamed_addr constant | ||
#[no_mangle] | ||
pub static G: u8 = 0; | ||
|
||
// CHECK: @H = local_unnamed_addr global | ||
#[no_mangle] | ||
pub static mut H: u8 = 0; | ||
} | ||
|
||
const HIDDEN: () = { | ||
// CHECK: @I = local_unnamed_addr constant | ||
#[no_mangle] | ||
static I: u8 = 0; | ||
|
||
// CHECK: @J = local_unnamed_addr global | ||
#[no_mangle] | ||
static mut J: u8 = 0; | ||
|
||
// CHECK: @K = local_unnamed_addr constant | ||
#[no_mangle] | ||
pub static K: u8 = 0; | ||
|
||
// CHECK: @L = local_unnamed_addr global | ||
#[no_mangle] | ||
pub static mut L: u8 = 0; | ||
}; | ||
|
||
// The surrounding item should not accidentally become external | ||
fn x() { | ||
// CHECK: @M = local_unnamed_addr constant | ||
#[no_mangle] | ||
static M: fn() = x; | ||
|
||
// CHECK: @N = local_unnamed_addr global | ||
#[no_mangle] | ||
static mut N: u8 = 0; | ||
|
||
// CHECK: @O = local_unnamed_addr constant | ||
#[no_mangle] | ||
pub static O: u8 = 0; | ||
|
||
// CHECK: @P = local_unnamed_addr global | ||
#[no_mangle] | ||
pub static mut P: u8 = 0; | ||
} | ||
// CHECK: define internal void @_ZN26external_no_mangle_statics1x{{.*$}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't look correct,
#[linkage]
can be used to decrease visibilities as well, in this case we don't need to mark the item and everything it uses as reachable.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that
#[linkage]
is an unstable attribute and has always been sort of wonky with how everything else in the compiler has evolved over time. This is just preserving the original behavior currently.TBH the
#[linkage]
attribute is basically buggy enough to not be worth using any more, it needs a larger overhaul than changing the behavior at this location to be usable.(for example there's no way to control visibility, and it's also not well defined whether the attribute makes it reachable).
Would you be ok leaving these sorts of questions to a later PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok.