@@ -66,11 +66,27 @@ TypeResolution::go ()
66
66
}
67
67
68
68
bool
69
- TypeResolution::typesAreCompatible (std::string &lhs, std::string &rhs) const
69
+ TypeResolution::typesAreCompatible (AST::Type *lhs, AST::Type *rhs,
70
+ Location locus)
70
71
{
72
+ lhs->accept_vis (*this );
73
+ rhs->accept_vis (*this );
74
+
75
+ auto rhsTypeStr = typeComparisonBuffer.back ();
76
+ typeComparisonBuffer.pop_back ();
77
+ auto lhsTypeStr = typeComparisonBuffer.back ();
78
+ typeComparisonBuffer.pop_back ();
79
+
71
80
// FIXME this needs to handle the cases of an i8 going into an i32 which is
72
81
// compatible
73
- return lhs.compare (rhs) == 0 ;
82
+ if (lhsTypeStr.compare (rhsTypeStr))
83
+ {
84
+ rust_error_at (locus, " E0308: expected: %s, found %s" ,
85
+ lhsTypeStr.c_str (), rhsTypeStr.c_str ());
86
+ return false ;
87
+ }
88
+
89
+ return true ;
74
90
}
75
91
76
92
void
@@ -256,34 +272,7 @@ TypeResolution::visit (AST::ArithmeticOrLogicalExpr &expr)
256
272
// scope will require knowledge of the type
257
273
258
274
// do the lhsType and the rhsType match
259
- before = typeComparisonBuffer.size ();
260
- lhsType->accept_vis (*this );
261
- if (typeComparisonBuffer.size () <= before)
262
- {
263
- rust_error_at (expr.locus , " Failed to unwrap type for lhs" );
264
- return ;
265
- }
266
-
267
- before = typeComparisonBuffer.size ();
268
- rhsType->accept_vis (*this );
269
- if (typeComparisonBuffer.size () <= before)
270
- {
271
- rust_error_at (expr.locus , " Failed to unwrap type for rhs" );
272
- return ;
273
- }
274
-
275
- auto rhsTypeStr = typeComparisonBuffer.back ();
276
- typeComparisonBuffer.pop_back ();
277
- auto lhsTypeStr = typeComparisonBuffer.back ();
278
- typeComparisonBuffer.pop_back ();
279
-
280
- if (!typesAreCompatible (lhsTypeStr, rhsTypeStr))
281
- {
282
- rust_error_at (expr.right_expr ->get_locus_slow (),
283
- " E0308: expected: %s, found %s" , lhsTypeStr.c_str (),
284
- rhsTypeStr.c_str ());
285
- return ;
286
- }
275
+ typesAreCompatible (lhsType, rhsType, expr.right_expr ->get_locus_slow ());
287
276
}
288
277
289
278
void
@@ -326,34 +315,7 @@ TypeResolution::visit (AST::AssignmentExpr &expr)
326
315
// scope will require knowledge of the type
327
316
328
317
// do the lhsType and the rhsType match
329
- before = typeComparisonBuffer.size ();
330
- lhsType->accept_vis (*this );
331
- if (typeComparisonBuffer.size () <= before)
332
- {
333
- rust_error_at (expr.locus , " Failed to unwrap type for lhs" );
334
- return ;
335
- }
336
-
337
- before = typeComparisonBuffer.size ();
338
- rhsType->accept_vis (*this );
339
- if (typeComparisonBuffer.size () <= before)
340
- {
341
- rust_error_at (expr.locus , " Failed to unwrap type for rhs" );
342
- return ;
343
- }
344
-
345
- auto rhsTypeStr = typeComparisonBuffer.back ();
346
- typeComparisonBuffer.pop_back ();
347
- auto lhsTypeStr = typeComparisonBuffer.back ();
348
- typeComparisonBuffer.pop_back ();
349
-
350
- if (!typesAreCompatible (lhsTypeStr, rhsTypeStr))
351
- {
352
- rust_error_at (expr.right_expr ->get_locus_slow (),
353
- " E0308: expected: %s, found %s" , lhsTypeStr.c_str (),
354
- rhsTypeStr.c_str ());
355
- return ;
356
- }
318
+ typesAreCompatible (lhsType, rhsType, expr.right_expr ->get_locus_slow ());
357
319
}
358
320
359
321
void
@@ -810,8 +772,11 @@ TypeResolution::visit (AST::LetStmt &stmt)
810
772
811
773
if (stmt.has_type () && stmt.has_init_expr ())
812
774
{
813
- auto declaredTyped = stmt.type .get ();
814
- // TODO compare this type to the inferred type to ensure they match
775
+ if (!typesAreCompatible (stmt.type .get (), inferedType,
776
+ stmt.init_expr ->get_locus_slow ()))
777
+ {
778
+ return ;
779
+ }
815
780
}
816
781
else if (stmt.has_type () && !stmt.has_init_expr ())
817
782
{
0 commit comments