diff --git a/text/0000-used.md b/text/0000-used.md new file mode 100644 index 00000000000..aab4bf86030 --- /dev/null +++ b/text/0000-used.md @@ -0,0 +1,62 @@ +- Feature Name: preserve +- Start Date: 2016-01-11 +- RFC PR: (leave this empty) +- Rust Issue: (leave this empty) + +# Summary +[summary]: #summary + +Add a `preserve` attribute to prevent symbols from being discarded. This +attribute is similar to GCC's `used` attribute. + +# 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; + +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 `#[preserve]` attribute is added. This attribute can be applied to static +variables and functions without type parameters. + +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 + +None. + +# Alternatives +[alternatives]: #alternatives + +The name of the attribute could be changed. + +# Unresolved questions +[unresolved]: #unresolved-questions + +None.