@@ -681,13 +681,8 @@ impl CodeGenerator for Var {
681681 let ty = var_ty. to_rust_ty_or_opaque ( ctx, & ( ) ) ;
682682
683683 if let Some ( val) = self . val ( ) {
684- match * val {
685- VarType :: Bool ( val) => {
686- result. push ( quote ! {
687- #( #attrs) *
688- pub const #canonical_ident : #ty = #val ;
689- } ) ;
690- }
684+ let init = match * val {
685+ VarType :: Bool ( val) => Some ( ( val. to_token_stream ( ) , ty) ) ,
691686 VarType :: Int ( val) => {
692687 let int_kind = var_ty
693688 . into_resolver ( )
@@ -702,10 +697,7 @@ impl CodeGenerator for Var {
702697 } else {
703698 helpers:: ast_ty:: uint_expr ( val as _ )
704699 } ;
705- result. push ( quote ! {
706- #( #attrs) *
707- pub const #canonical_ident : #ty = #val ;
708- } ) ;
700+ Some ( ( val, ty) )
709701 }
710702 VarType :: String ( ref bytes) => {
711703 let prefix = ctx. trait_prefix ( ) ;
@@ -725,54 +717,50 @@ impl CodeGenerator for Var {
725717 None
726718 } ;
727719 if let Some ( cstr) = cstr {
728- let cstr_ty = quote ! { :: #prefix:: ffi:: CStr } ;
720+ let cstr_ty =
721+ syn:: parse_quote! { & :: #prefix:: ffi:: CStr } ;
729722 if rust_features. literal_cstr {
730- let cstr = proc_macro2:: Literal :: c_string ( cstr) ;
731- result. push ( quote ! {
732- #( #attrs) *
733- pub const #canonical_ident: & #cstr_ty = #cstr;
734- } ) ;
723+ let cstr = proc_macro2:: Literal :: c_string ( cstr)
724+ . into_token_stream ( ) ;
725+ Some ( ( cstr, cstr_ty) )
735726 } else {
736- let bytes =
727+ let bstr =
737728 proc_macro2:: Literal :: byte_string ( & cstr_bytes) ;
738- result. push ( quote ! {
739- #( #attrs) *
740- #[ allow( unsafe_code) ]
741- pub const #canonical_ident: & #cstr_ty = unsafe {
742- #cstr_ty:: from_bytes_with_nul_unchecked( #bytes)
743- } ;
744- } ) ;
729+ let val = quote ! {
730+ unsafe { :: #prefix:: ffi:: CStr :: from_bytes_with_nul_unchecked( #bstr) }
731+ } ;
732+ attrs. push ( quote ! { #[ allow( unsafe_code) ] } ) ;
733+ Some ( ( val, cstr_ty) )
745734 }
746735 } else {
747736 // TODO: Here we ignore the type we just made up, probably
748737 // we should refactor how the variable type and ty ID work.
749- let array_ty = quote ! { [ u8 ; #len] } ;
738+ let array_ty = syn :: parse_quote ! { & [ u8 ; #len] } ;
750739 let bytes =
751- proc_macro2:: Literal :: byte_string ( & cstr_bytes) ;
752- let lifetime =
753- if true { None } else { Some ( quote ! { ' static } ) }
754- . into_iter ( ) ;
755-
756- result. push ( quote ! {
757- #( #attrs) *
758- pub const #canonical_ident: & #( #lifetime ) * #array_ty = #bytes ;
759- } ) ;
740+ proc_macro2:: Literal :: byte_string ( & cstr_bytes)
741+ . into_token_stream ( ) ;
742+ Some ( ( bytes, array_ty) )
760743 }
761744 }
762745 VarType :: Float ( f) => {
763- if let Ok ( expr) = helpers:: ast_ty:: float_expr ( f) {
764- result. push ( quote ! {
765- #( #attrs) *
766- pub const #canonical_ident : #ty = #expr ;
767- } ) ;
768- }
746+ helpers:: ast_ty:: float_expr ( f) . ok ( ) . map ( |e| ( e, ty) )
769747 }
770- VarType :: Char ( c) => {
771- result. push ( quote ! {
772- #( #attrs) *
773- pub const #canonical_ident : #ty = #c ;
774- } ) ;
748+ VarType :: Char ( c) => Some ( ( c. to_token_stream ( ) , ty) ) ,
749+ } ;
750+
751+ if let Some ( ( mut val, ty) ) = init {
752+ let var_ty_item = ctx. resolve_item ( var_ty) ;
753+ let alias_style = var_ty_item. alias_style ( ctx) ;
754+ if matches ! (
755+ alias_style,
756+ AliasVariation :: NewType | AliasVariation :: NewTypeDeref
757+ ) {
758+ val = quote ! { #ty( #val) } ;
775759 }
760+ result. push ( quote ! {
761+ #( #attrs) *
762+ pub const #canonical_ident : #ty = #val ;
763+ } ) ;
776764 }
777765 } else {
778766 let symbol: & str = self . link_name ( ) . unwrap_or_else ( || {
@@ -1005,15 +993,7 @@ impl CodeGenerator for Type {
1005993 quote ! { }
1006994 } ;
1007995
1008- let alias_style = if ctx. options ( ) . type_alias . matches ( & name) {
1009- AliasVariation :: TypeAlias
1010- } else if ctx. options ( ) . new_type_alias . matches ( & name) {
1011- AliasVariation :: NewType
1012- } else if ctx. options ( ) . new_type_alias_deref . matches ( & name) {
1013- AliasVariation :: NewTypeDeref
1014- } else {
1015- ctx. options ( ) . default_alias_style
1016- } ;
996+ let alias_style = item. alias_style ( ctx) ;
1017997
1018998 // We prefer using `pub use` over `pub type` because of:
1019999 // https://github.com/rust-lang/rust/issues/26264
0 commit comments