Skip to content

Commit 3ba43fc

Browse files
committed
Reuse typeComparison checks in assignments and expressions
1 parent 6d03a8b commit 3ba43fc

File tree

2 files changed

+26
-61
lines changed

2 files changed

+26
-61
lines changed

gcc/rust/analysis/rust-resolution.cc

+25-60
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,27 @@ TypeResolution::go ()
6666
}
6767

6868
bool
69-
TypeResolution::typesAreCompatible (std::string &lhs, std::string &rhs) const
69+
TypeResolution::typesAreCompatible (AST::Type *lhs, AST::Type *rhs,
70+
Location locus)
7071
{
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+
7180
// FIXME this needs to handle the cases of an i8 going into an i32 which is
7281
// 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;
7490
}
7591

7692
void
@@ -256,34 +272,7 @@ TypeResolution::visit (AST::ArithmeticOrLogicalExpr &expr)
256272
// scope will require knowledge of the type
257273

258274
// 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 ());
287276
}
288277

289278
void
@@ -326,34 +315,7 @@ TypeResolution::visit (AST::AssignmentExpr &expr)
326315
// scope will require knowledge of the type
327316

328317
// 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 ());
357319
}
358320

359321
void
@@ -810,8 +772,11 @@ TypeResolution::visit (AST::LetStmt &stmt)
810772

811773
if (stmt.has_type () && stmt.has_init_expr ())
812774
{
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+
}
815780
}
816781
else if (stmt.has_type () && !stmt.has_init_expr ())
817782
{

gcc/rust/analysis/rust-resolution.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ class TypeResolution : public AST::ASTVisitor
225225

226226
bool go ();
227227

228-
bool typesAreCompatible (std::string &lhs, std::string &rhs) const;
228+
bool typesAreCompatible (AST::Type *lhs, AST::Type *rhs, Location locus);
229229

230230
Scope<AST::Type *> scope;
231231
Scope<AST::Type *> typeScope;

0 commit comments

Comments
 (0)