@@ -14,7 +14,7 @@ use crate::{
1414 stmt:: HirPattern ,
1515 } ,
1616 macros_api:: { NodeInterner , StructId } ,
17- node_interner:: { FuncId , TraitId } ,
17+ node_interner:: { FuncId , TraitId , TraitImplId } ,
1818 parser:: NoirParser ,
1919 token:: { Token , Tokens } ,
2020 QuotedType , Type ,
@@ -77,8 +77,7 @@ pub(crate) fn get_array(
7777 value => {
7878 let type_var = Box :: new ( interner. next_type_variable ( ) ) ;
7979 let expected = Type :: Array ( type_var. clone ( ) , type_var) ;
80- let actual = value. get_type ( ) . into_owned ( ) ;
81- Err ( InterpreterError :: TypeMismatch { expected, actual, location } )
80+ type_mismatch ( value, expected, location)
8281 }
8382 }
8483}
@@ -92,8 +91,7 @@ pub(crate) fn get_slice(
9291 value => {
9392 let type_var = Box :: new ( interner. next_type_variable ( ) ) ;
9493 let expected = Type :: Slice ( type_var) ;
95- let actual = value. get_type ( ) . into_owned ( ) ;
96- Err ( InterpreterError :: TypeMismatch { expected, actual, location } )
94+ type_mismatch ( value, expected, location)
9795 }
9896 }
9997}
@@ -107,19 +105,15 @@ pub(crate) fn get_tuple(
107105 value => {
108106 let type_var = interner. next_type_variable ( ) ;
109107 let expected = Type :: Tuple ( vec ! [ type_var] ) ;
110- let actual = value. get_type ( ) . into_owned ( ) ;
111- Err ( InterpreterError :: TypeMismatch { expected, actual, location } )
108+ type_mismatch ( value, expected, location)
112109 }
113110 }
114111}
115112
116113pub ( crate ) fn get_field ( ( value, location) : ( Value , Location ) ) -> IResult < FieldElement > {
117114 match value {
118115 Value :: Field ( value) => Ok ( value) ,
119- value => {
120- let actual = value. get_type ( ) . into_owned ( ) ;
121- Err ( InterpreterError :: TypeMismatch { expected : Type :: FieldElement , actual, location } )
122- }
116+ value => type_mismatch ( value, Type :: FieldElement , location) ,
123117 }
124118}
125119
@@ -128,8 +122,7 @@ pub(crate) fn get_u8((value, location): (Value, Location)) -> IResult<u8> {
128122 Value :: U8 ( value) => Ok ( value) ,
129123 value => {
130124 let expected = Type :: Integer ( Signedness :: Unsigned , IntegerBitSize :: Eight ) ;
131- let actual = value. get_type ( ) . into_owned ( ) ;
132- Err ( InterpreterError :: TypeMismatch { expected, actual, location } )
125+ type_mismatch ( value, expected, location)
133126 }
134127 }
135128}
@@ -139,53 +132,36 @@ pub(crate) fn get_u32((value, location): (Value, Location)) -> IResult<u32> {
139132 Value :: U32 ( value) => Ok ( value) ,
140133 value => {
141134 let expected = Type :: Integer ( Signedness :: Unsigned , IntegerBitSize :: ThirtyTwo ) ;
142- let actual = value. get_type ( ) . into_owned ( ) ;
143- Err ( InterpreterError :: TypeMismatch { expected, actual, location } )
135+ type_mismatch ( value, expected, location)
144136 }
145137 }
146138}
147139
148140pub ( crate ) fn get_expr ( ( value, location) : ( Value , Location ) ) -> IResult < ExpressionKind > {
149141 match value {
150142 Value :: Expr ( expr) => Ok ( expr) ,
151- value => {
152- let expected = Type :: Quoted ( QuotedType :: Expr ) ;
153- let actual = value. get_type ( ) . into_owned ( ) ;
154- Err ( InterpreterError :: TypeMismatch { expected, actual, location } )
155- }
143+ value => type_mismatch ( value, Type :: Quoted ( QuotedType :: Expr ) , location) ,
156144 }
157145}
158146
159147pub ( crate ) fn get_function_def ( ( value, location) : ( Value , Location ) ) -> IResult < FuncId > {
160148 match value {
161149 Value :: FunctionDefinition ( id) => Ok ( id) ,
162- value => {
163- let expected = Type :: Quoted ( QuotedType :: FunctionDefinition ) ;
164- let actual = value. get_type ( ) . into_owned ( ) ;
165- Err ( InterpreterError :: TypeMismatch { expected, actual, location } )
166- }
150+ value => type_mismatch ( value, Type :: Quoted ( QuotedType :: FunctionDefinition ) , location) ,
167151 }
168152}
169153
170154pub ( crate ) fn get_module ( ( value, location) : ( Value , Location ) ) -> IResult < ModuleId > {
171155 match value {
172156 Value :: ModuleDefinition ( module_id) => Ok ( module_id) ,
173- value => {
174- let expected = Type :: Quoted ( QuotedType :: Module ) ;
175- let actual = value. get_type ( ) . into_owned ( ) ;
176- Err ( InterpreterError :: TypeMismatch { expected, actual, location } )
177- }
157+ value => type_mismatch ( value, Type :: Quoted ( QuotedType :: Module ) , location) ,
178158 }
179159}
180160
181161pub ( crate ) fn get_struct ( ( value, location) : ( Value , Location ) ) -> IResult < StructId > {
182162 match value {
183163 Value :: StructDefinition ( id) => Ok ( id) ,
184- _ => {
185- let expected = Type :: Quoted ( QuotedType :: StructDefinition ) ;
186- let actual = value. get_type ( ) . into_owned ( ) ;
187- Err ( InterpreterError :: TypeMismatch { expected, location, actual } )
188- }
164+ _ => type_mismatch ( value, Type :: Quoted ( QuotedType :: StructDefinition ) , location) ,
189165 }
190166}
191167
@@ -194,47 +170,43 @@ pub(crate) fn get_trait_constraint(
194170) -> IResult < ( TraitId , Vec < Type > ) > {
195171 match value {
196172 Value :: TraitConstraint ( trait_id, generics) => Ok ( ( trait_id, generics) ) ,
197- value => {
198- let expected = Type :: Quoted ( QuotedType :: TraitConstraint ) ;
199- let actual = value. get_type ( ) . into_owned ( ) ;
200- Err ( InterpreterError :: TypeMismatch { expected, actual, location } )
201- }
173+ value => type_mismatch ( value, Type :: Quoted ( QuotedType :: TraitConstraint ) , location) ,
202174 }
203175}
204176
205177pub ( crate ) fn get_trait_def ( ( value, location) : ( Value , Location ) ) -> IResult < TraitId > {
206178 match value {
207179 Value :: TraitDefinition ( id) => Ok ( id) ,
208- value => {
209- let expected = Type :: Quoted ( QuotedType :: TraitDefinition ) ;
210- let actual = value. get_type ( ) . into_owned ( ) ;
211- Err ( InterpreterError :: TypeMismatch { expected, actual, location } )
212- }
180+ value => type_mismatch ( value, Type :: Quoted ( QuotedType :: TraitDefinition ) , location) ,
181+ }
182+ }
183+
184+ pub ( crate ) fn get_trait_impl ( ( value, location) : ( Value , Location ) ) -> IResult < TraitImplId > {
185+ match value {
186+ Value :: TraitImpl ( id) => Ok ( id) ,
187+ value => type_mismatch ( value, Type :: Quoted ( QuotedType :: TraitImpl ) , location) ,
213188 }
214189}
215190
216191pub ( crate ) fn get_type ( ( value, location) : ( Value , Location ) ) -> IResult < Type > {
217192 match value {
218193 Value :: Type ( typ) => Ok ( typ) ,
219- value => {
220- let expected = Type :: Quoted ( QuotedType :: Type ) ;
221- let actual = value. get_type ( ) . into_owned ( ) ;
222- Err ( InterpreterError :: TypeMismatch { expected, actual, location } )
223- }
194+ value => type_mismatch ( value, Type :: Quoted ( QuotedType :: Type ) , location) ,
224195 }
225196}
226197
227198pub ( crate ) fn get_quoted ( ( value, location) : ( Value , Location ) ) -> IResult < Rc < Vec < Token > > > {
228199 match value {
229200 Value :: Quoted ( tokens) => Ok ( tokens) ,
230- value => {
231- let expected = Type :: Quoted ( QuotedType :: Quoted ) ;
232- let actual = value. get_type ( ) . into_owned ( ) ;
233- Err ( InterpreterError :: TypeMismatch { expected, actual, location } )
234- }
201+ value => type_mismatch ( value, Type :: Quoted ( QuotedType :: Quoted ) , location) ,
235202 }
236203}
237204
205+ fn type_mismatch < T > ( value : Value , expected : Type , location : Location ) -> IResult < T > {
206+ let actual = value. get_type ( ) . into_owned ( ) ;
207+ Err ( InterpreterError :: TypeMismatch { expected, actual, location } )
208+ }
209+
238210pub ( crate ) fn hir_pattern_to_tokens (
239211 interner : & NodeInterner ,
240212 hir_pattern : & HirPattern ,
0 commit comments