@@ -681,13 +681,8 @@ impl CodeGenerator for Var {
681
681
let ty = var_ty. to_rust_ty_or_opaque ( ctx, & ( ) ) ;
682
682
683
683
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 ( ) ) ,
691
686
VarType :: Int ( val) => {
692
687
let int_kind = var_ty
693
688
. into_resolver ( )
@@ -702,10 +697,7 @@ impl CodeGenerator for Var {
702
697
} else {
703
698
helpers:: ast_ty:: uint_expr ( val as _ )
704
699
} ;
705
- result. push ( quote ! {
706
- #( #attrs) *
707
- pub const #canonical_ident : #ty = #val ;
708
- } ) ;
700
+ Some ( val)
709
701
}
710
702
VarType :: String ( ref bytes) => {
711
703
let prefix = ctx. trait_prefix ( ) ;
@@ -758,21 +750,24 @@ impl CodeGenerator for Var {
758
750
pub const #canonical_ident: & #( #lifetime ) * #array_ty = #bytes ;
759
751
} ) ;
760
752
}
753
+ None
761
754
}
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) } ;
775
766
}
767
+ result. push ( quote ! {
768
+ #( #attrs) *
769
+ pub const #canonical_ident : #ty = #val ;
770
+ } ) ;
776
771
}
777
772
} else {
778
773
let symbol: & str = self . link_name ( ) . unwrap_or_else ( || {
@@ -1005,15 +1000,7 @@ impl CodeGenerator for Type {
1005
1000
quote ! { }
1006
1001
} ;
1007
1002
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) ;
1017
1004
1018
1005
// We prefer using `pub use` over `pub type` because of:
1019
1006
// https://github.com/rust-lang/rust/issues/26264
0 commit comments