@@ -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 const_expr = match * val {
685+ VarType :: Bool ( val) => Some ( val. to_token_stream ( ) ) ,
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)
709701 }
710702 VarType :: String ( ref bytes) => {
711703 let prefix = ctx. trait_prefix ( ) ;
@@ -758,21 +750,24 @@ impl CodeGenerator for Var {
758750 pub const #canonical_ident: & #( #lifetime ) * #array_ty = #bytes ;
759751 } ) ;
760752 }
753+ None
761754 }
762- 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- }
769- }
770- VarType :: Char ( c) => {
771- result. push ( quote ! {
772- #( #attrs) *
773- pub const #canonical_ident : #ty = #c ;
774- } ) ;
755+ VarType :: Float ( f) => helpers:: ast_ty:: float_expr ( f) . ok ( ) ,
756+ VarType :: Char ( c) => Some ( c. to_token_stream ( ) ) ,
757+ } ;
758+
759+ if let Some ( mut val) = const_expr {
760+ let var_ty_item = ctx. resolve_item ( var_ty) ;
761+ if matches ! (
762+ var_ty_item. alias_style( ctx) ,
763+ AliasVariation :: NewType | AliasVariation :: NewTypeDeref
764+ ) {
765+ val = quote ! { #ty( #val) } ;
775766 }
767+ result. push ( quote ! {
768+ #( #attrs) *
769+ pub const #canonical_ident : #ty = #val ;
770+ } ) ;
776771 }
777772 } else {
778773 let symbol: & str = self . link_name ( ) . unwrap_or_else ( || {
@@ -1005,15 +1000,7 @@ impl CodeGenerator for Type {
10051000 quote ! { }
10061001 } ;
10071002
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- } ;
1003+ let alias_style = item. alias_style ( ctx) ;
10171004
10181005 // We prefer using `pub use` over `pub type` because of:
10191006 // https://github.com/rust-lang/rust/issues/26264
0 commit comments