Skip to content

Commit 58cdad2

Browse files
committed
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_`).
1 parent ac9c053 commit 58cdad2

File tree

1 file changed

+15
-24
lines changed

1 file changed

+15
-24
lines changed

cortex-m-rt/macros/src/lib.rs

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -114,19 +114,16 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
114114
.into_iter()
115115
.map(|var| {
116116
let ident = var.ident;
117-
// `let` can't shadow a `static mut` so we must give the `static` a different
118-
// name. We'll create a new name by appending an underscore to the original name
119-
// of the `static`.
120-
let mut ident_ = ident.to_string();
121-
ident_.push('_');
122-
let ident_ = Ident::new(&ident_, Span::call_site());
123117
let ty = var.ty;
124118
let expr = var.expr;
125119

126120
quote!(
127-
static mut #ident_: #ty = #expr;
128121
#[allow(non_snake_case)]
129-
let #ident: &'static mut #ty = unsafe { &mut #ident_ };
122+
let #ident: &'static mut #ty = unsafe {
123+
static mut #ident: #ty = #expr;
124+
125+
&mut #ident
126+
};
130127
)
131128
}).collect::<Vec<_>>();
132129

@@ -402,19 +399,16 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream {
402399
.into_iter()
403400
.map(|var| {
404401
let ident = var.ident;
405-
// `let` can't shadow a `static mut` so we must give the `static` a different
406-
// name. We'll create a new name by appending an underscore to the original name
407-
// of the `static`.
408-
let mut ident_ = ident.to_string();
409-
ident_.push('_');
410-
let ident_ = Ident::new(&ident_, Span::call_site());
411402
let ty = var.ty;
412403
let expr = var.expr;
413404

414405
quote!(
415-
static mut #ident_: #ty = #expr;
416406
#[allow(non_snake_case)]
417-
let #ident: &mut #ty = unsafe { &mut #ident_ };
407+
let #ident: &mut #ty = unsafe {
408+
static mut #ident: #ty = #expr;
409+
410+
&mut #ident
411+
};
418412
)
419413
}).collect::<Vec<_>>();
420414

@@ -546,19 +540,16 @@ pub fn interrupt(args: TokenStream, input: TokenStream) -> TokenStream {
546540
.into_iter()
547541
.map(|var| {
548542
let ident = var.ident;
549-
// `let` can't shadow a `static mut` so we must give the `static` a different
550-
// name. We'll create a new name by appending an underscore to the original name
551-
// of the `static`.
552-
let mut ident_ = ident.to_string();
553-
ident_.push('_');
554-
let ident_ = Ident::new(&ident_, Span::call_site());
555543
let ty = var.ty;
556544
let expr = var.expr;
557545

558546
quote!(
559-
static mut #ident_: #ty = #expr;
560547
#[allow(non_snake_case)]
561-
let #ident: &mut #ty = unsafe { &mut #ident_ };
548+
let #ident: &mut #ty = unsafe {
549+
static mut #ident: #ty = #expr;
550+
551+
&mut #ident
552+
};
562553
)
563554
}).collect::<Vec<_>>();
564555

0 commit comments

Comments
 (0)