@@ -4,7 +4,9 @@ use syn::Expr;
44
55use crate :: {
66 diagnostics:: TranslationResult ,
7- translator:: { signed_int_expr, ConvertedDecl , EnumMode , ExprContext , Translation } ,
7+ translator:: {
8+ signed_int_expr, ConvertedDecl , EnumMode , ExprContext , Translation , TranslationError ,
9+ } ,
810 with_stmts:: WithStmts ,
911 CDeclKind , CEnumConstantId , CEnumId , CQualTypeId , CTypeId , CTypeKind , ConstIntExpr ,
1012} ;
@@ -105,7 +107,15 @@ impl<'c> Translation<'c> {
105107 ) -> TranslationResult < WithStmts < Box < Expr > > > {
106108 match self . tcfg . enum_mode {
107109 // First extract the enum's inner type...
108- EnumMode :: NewType => val = self . integer_from_enum ( val) ,
110+ EnumMode :: NewType => {
111+ if ctx. is_pattern {
112+ return Err ( TranslationError :: generic (
113+ "cast from enum is not supported in patterns" ,
114+ ) ) ;
115+ }
116+
117+ val = self . integer_from_enum ( val) ;
118+ }
109119 EnumMode :: Consts => { }
110120 }
111121
@@ -149,7 +159,15 @@ impl<'c> Translation<'c> {
149159
150160 match self . tcfg . enum_mode {
151161 // Enum-to-enum casts need to be translated via the inner value as an intermediate.
152- EnumMode :: NewType => val = self . integer_from_enum ( val) ,
162+ EnumMode :: NewType => {
163+ if ctx. is_pattern {
164+ return Err ( TranslationError :: generic (
165+ "cast from enum is not supported in patterns" ,
166+ ) ) ;
167+ }
168+
169+ val = self . integer_from_enum ( val) ;
170+ }
153171 EnumMode :: Consts => { }
154172 }
155173
@@ -167,6 +185,12 @@ impl<'c> Translation<'c> {
167185 }
168186
169187 EnumMode :: Consts => {
188+ if ctx. is_pattern {
189+ return Err ( TranslationError :: generic (
190+ "cast to enum is not supported in patterns" ,
191+ ) ) ;
192+ }
193+
170194 let source_type_kind = & self . ast_context . resolve_type ( source_cty. ctype ) . kind ;
171195 let enum_integral_type_kind =
172196 & self . ast_context . resolve_type ( enum_integral_type. ctype ) . kind ;
0 commit comments