From 5ae69add7f8cfc73d1ec883b2bd8da246c4d986d Mon Sep 17 00:00:00 2001 From: Julian Orth Date: Mon, 11 Jan 2016 16:10:27 +0100 Subject: [PATCH 1/5] used --- text/0000-used.md | 63 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 text/0000-used.md diff --git a/text/0000-used.md b/text/0000-used.md new file mode 100644 index 00000000000..b078b606299 --- /dev/null +++ b/text/0000-used.md @@ -0,0 +1,63 @@ +- Feature Name: used +- Start Date: 2016-01-11 +- RFC PR: (leave this empty) +- Rust Issue: (leave this empty) + +# Summary +[summary]: #summary + +Add a `used` attribute to prevent symbols from being discarded. + +# Motivation +[motivation]: #motivation + +The compiler cannot always see all references to a symbol. For example, if a +symbol is only referenced from inline assembly, the compiler might think that +the symbol is unused. + +If the compiler thinks that a symbol is unused, it might optimize the symbol +away which might change the behavior of the program or cause the linking step to +fail. For example: + +```rust +#![allow(dead_code)] + +#[link_section = ".init_array"] +static F: extern fn() = f; + +static mut X: usize = 0; + +extern fn f() { + println!("test"); +} + +fn main() { } +``` + +When this program is compiled without optimization, it prints "test". But if it +is compiled with optimization, it prints nothing. + +# Detailed design +[design]: #detailed-design + +A `#[used]` attribute is added. This attribute can be applied to static +variables and functions without type parameters. + +If an items is marked with this attribute, the compiler generates the +corresponding symbol and the symbol is not discarded by the compilation or +linking process. + +# Drawbacks +[drawbacks]: #drawbacks + +None. + +# Alternatives +[alternatives]: #alternatives + +None. + +# Unresolved questions +[unresolved]: #unresolved-questions + +None. From 528aaca8a70c0930b1ce70ece107c0449e5b49b9 Mon Sep 17 00:00:00 2001 From: Julian Orth Date: Mon, 11 Jan 2016 16:12:16 +0100 Subject: [PATCH 2/5] fix typo --- text/0000-used.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/text/0000-used.md b/text/0000-used.md index b078b606299..d79c089a6b6 100644 --- a/text/0000-used.md +++ b/text/0000-used.md @@ -25,8 +25,6 @@ fail. For example: #[link_section = ".init_array"] static F: extern fn() = f; -static mut X: usize = 0; - extern fn f() { println!("test"); } From 6c2887bac7ba02039eb36790a60a66f89c3c0f0f Mon Sep 17 00:00:00 2001 From: Julian Orth Date: Sun, 17 Jan 2016 00:36:50 +0100 Subject: [PATCH 3/5] preserve --- text/0000-used.md | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/text/0000-used.md b/text/0000-used.md index d79c089a6b6..b8203b899e8 100644 --- a/text/0000-used.md +++ b/text/0000-used.md @@ -1,4 +1,4 @@ -- Feature Name: used +- Feature Name: preserve - Start Date: 2016-01-11 - RFC PR: (leave this empty) - Rust Issue: (leave this empty) @@ -6,7 +6,7 @@ # Summary [summary]: #summary -Add a `used` attribute to prevent symbols from being discarded. +Add a `preserve` attribute to prevent symbols from being discarded. # Motivation [motivation]: #motivation @@ -38,12 +38,16 @@ is compiled with optimization, it prints nothing. # Detailed design [design]: #detailed-design -A `#[used]` attribute is added. This attribute can be applied to static -variables and functions without type parameters. +Two attributes are added: -If an items is marked with this attribute, the compiler generates the -corresponding symbol and the symbol is not discarded by the compilation or -linking process. +* `#[preserve(compiler)]` +* `#[preserve]` + +These attributes can be applied to static variables and functions without type +parameters. + +If an item is marked with any of these attributes, the compiler generates the +corresponding symbol and the symbol is not discarded by the compilation process. # Drawbacks [drawbacks]: #drawbacks From a7a4a7be544cda709544f8443906c2c26a8ef3f8 Mon Sep 17 00:00:00 2001 From: Julian Orth Date: Sun, 17 Jan 2016 18:00:36 +0100 Subject: [PATCH 4/5] update --- text/0000-used.md | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/text/0000-used.md b/text/0000-used.md index b8203b899e8..a4e48523276 100644 --- a/text/0000-used.md +++ b/text/0000-used.md @@ -6,7 +6,8 @@ # Summary [summary]: #summary -Add a `preserve` attribute to prevent symbols from being discarded. +Add a `preserve` attribute to prevent symbols from being discarded. This +attribute is similar to GCC's `used` attribute. # Motivation [motivation]: #motivation @@ -38,16 +39,12 @@ is compiled with optimization, it prints nothing. # Detailed design [design]: #detailed-design -Two attributes are added: +A `#[preserve]` attribute is added. This attribute can be applied to static +variables and functions without type parameters. -* `#[preserve(compiler)]` -* `#[preserve]` - -These attributes can be applied to static variables and functions without type -parameters. - -If an item is marked with any of these attributes, the compiler generates the +If an item is marked with this attribute, the compiler generates the corresponding symbol and the symbol is not discarded by the compilation process. +It may be discarded by the linking process. # Drawbacks [drawbacks]: #drawbacks From 803db1dbc268f124aa8f37ebe01ce32080fc3511 Mon Sep 17 00:00:00 2001 From: Julian Orth Date: Tue, 19 Jan 2016 22:50:12 +0100 Subject: [PATCH 5/5] alt --- text/0000-used.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/text/0000-used.md b/text/0000-used.md index a4e48523276..aab4bf86030 100644 --- a/text/0000-used.md +++ b/text/0000-used.md @@ -54,7 +54,7 @@ None. # Alternatives [alternatives]: #alternatives -None. +The name of the attribute could be changed. # Unresolved questions [unresolved]: #unresolved-questions