diff options
author | 2018-10-26 20:39:39 +0200 | |
---|---|---|
committer | 2018-10-26 20:39:39 +0200 | |
commit | 58cdad2fba9763ac590bb0460ecd7193014a1e12 (patch) | |
tree | 7fbb869c1dc8898c8d985b1e00fb0506e9b99f1a /cortex-m-rt/macros/src/lib.rs | |
parent | ac9c05361cfbe46ace2162a3985e4660610747aa (diff) | |
download | cortex-m-58cdad2fba9763ac590bb0460ecd7193014a1e12.tar.gz cortex-m-58cdad2fba9763ac590bb0460ecd7193014a1e12.tar.zst cortex-m-58cdad2fba9763ac590bb0460ecd7193014a1e12.zip |
entry/exception/interrupt: reduce namespace pollution when using static mut
this changes code generation of code like this:
``` rust
#[entry]
fn main() -> ! {
static mut FOO: u32 = 0;
// ..
}
```
from this:
``` rust
fn main() -> ! {
static mut FOO_: u32 = 0;
let FOO: &'static mut u32 = unsafe { &mut FOO_ };
// ..
}
```
to this:
``` rust
fn main() -> ! {
let FOO: &'static mut u32 = unsafe {
static mut FOO: u32 = 0;
&mut FOO;
};
// ..
}
```
this completely hides the `static mut` variable. Before it was possible to
(unsafely) access it via `${ident}_` (e.g. `FOO_`).
Diffstat (limited to 'cortex-m-rt/macros/src/lib.rs')
-rw-r--r-- | cortex-m-rt/macros/src/lib.rs | 39 |
1 files changed, 15 insertions, 24 deletions
diff --git a/cortex-m-rt/macros/src/lib.rs b/cortex-m-rt/macros/src/lib.rs index 56ec88c..560a05f 100644 --- a/cortex-m-rt/macros/src/lib.rs +++ b/cortex-m-rt/macros/src/lib.rs @@ -114,19 +114,16 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream { .into_iter() .map(|var| { let ident = var.ident; - // `let` can't shadow a `static mut` so we must give the `static` a different - // name. We'll create a new name by appending an underscore to the original name - // of the `static`. - let mut ident_ = ident.to_string(); - ident_.push('_'); - let ident_ = Ident::new(&ident_, Span::call_site()); let ty = var.ty; let expr = var.expr; quote!( - static mut #ident_: #ty = #expr; #[allow(non_snake_case)] - let #ident: &'static mut #ty = unsafe { &mut #ident_ }; + let #ident: &'static mut #ty = unsafe { + static mut #ident: #ty = #expr; + + &mut #ident + }; ) }).collect::<Vec<_>>(); @@ -402,19 +399,16 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream { .into_iter() .map(|var| { let ident = var.ident; - // `let` can't shadow a `static mut` so we must give the `static` a different - // name. We'll create a new name by appending an underscore to the original name - // of the `static`. - let mut ident_ = ident.to_string(); - ident_.push('_'); - let ident_ = Ident::new(&ident_, Span::call_site()); let ty = var.ty; let expr = var.expr; quote!( - static mut #ident_: #ty = #expr; #[allow(non_snake_case)] - let #ident: &mut #ty = unsafe { &mut #ident_ }; + let #ident: &mut #ty = unsafe { + static mut #ident: #ty = #expr; + + &mut #ident + }; ) }).collect::<Vec<_>>(); @@ -546,19 +540,16 @@ pub fn interrupt(args: TokenStream, input: TokenStream) -> TokenStream { .into_iter() .map(|var| { let ident = var.ident; - // `let` can't shadow a `static mut` so we must give the `static` a different - // name. We'll create a new name by appending an underscore to the original name - // of the `static`. - let mut ident_ = ident.to_string(); - ident_.push('_'); - let ident_ = Ident::new(&ident_, Span::call_site()); let ty = var.ty; let expr = var.expr; quote!( - static mut #ident_: #ty = #expr; #[allow(non_snake_case)] - let #ident: &mut #ty = unsafe { &mut #ident_ }; + let #ident: &mut #ty = unsafe { + static mut #ident: #ty = #expr; + + &mut #ident + }; ) }).collect::<Vec<_>>(); |