Skip to content

Commit fc569e4

Browse files
committed
gccrs: feat: Made changes to ensure no wrong assignments are done.
gcc/rust/ChangeLog: * backend/rust-compile-base.cc (HIRCompileBase::lvalue_p): Created a function that checks the lvalue. * backend/rust-compile-base.h: Created the Signature for above function * backend/rust-compile-expr.cc (lvalue_p): Moved the function from here to rust-compile-base. (CompileExpr::visit): Made changes to ensure proper readability and checking for wrong assignments.
1 parent 167b909 commit fc569e4

File tree

3 files changed

+51
-52
lines changed

3 files changed

+51
-52
lines changed

Diff for: gcc/rust/backend/rust-compile-base.cc

+46
Original file line numberDiff line numberDiff line change
@@ -1015,5 +1015,51 @@ HIRCompileBase::unit_expression (location_t locus)
10151015
return Backend::constructor_expression (unit_type, false, {}, -1, locus);
10161016
}
10171017

1018+
bool
1019+
HIRCompileBase::lvalue_p (const_tree ref)
1020+
{
1021+
const enum tree_code code = TREE_CODE (ref);
1022+
1023+
switch (code)
1024+
{
1025+
case REALPART_EXPR:
1026+
case IMAGPART_EXPR:
1027+
case COMPONENT_REF:
1028+
return lvalue_p (TREE_OPERAND (ref, 0));
1029+
1030+
case COMPOUND_LITERAL_EXPR:
1031+
case STRING_CST:
1032+
case CONST_DECL:
1033+
case INTEGER_CST:
1034+
return true;
1035+
1036+
case MEM_REF:
1037+
case TARGET_MEM_REF:
1038+
/* MEM_REFs can appear from -fgimple parsing or folding, so allow them
1039+
here as well. */
1040+
case INDIRECT_REF:
1041+
case ARRAY_REF:
1042+
case VAR_DECL:
1043+
case PARM_DECL:
1044+
case RESULT_DECL:
1045+
case ERROR_MARK:
1046+
return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
1047+
&& TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
1048+
1049+
case BIND_EXPR:
1050+
return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
1051+
case PLUS_EXPR:
1052+
case MINUS_EXPR:
1053+
case MULT_EXPR:
1054+
case POINTER_PLUS_EXPR:
1055+
case POINTER_DIFF_EXPR:
1056+
case MULT_HIGHPART_EXPR:
1057+
case TRUNC_DIV_EXPR:
1058+
return false;
1059+
default:
1060+
return false;
1061+
}
1062+
}
1063+
10181064
} // namespace Compile
10191065
} // namespace Rust

Diff for: gcc/rust/backend/rust-compile-base.h

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class HIRCompileBase
3030
virtual ~HIRCompileBase () {}
3131

3232
static tree address_expression (tree expr, location_t locus);
33+
bool lvalue_p (const_tree ref);
3334

3435
protected:
3536
HIRCompileBase (Context *ctx) : ctx (ctx) {}

Diff for: gcc/rust/backend/rust-compile-expr.cc

+4-52
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,7 @@ CompileExpr::visit (HIR::ContinueExpr &expr)
851851

852852
if (!ctx->lookup_label_decl (ref, &label))
853853
{
854-
rust_error_at (expr.get_label ().get_locus (),
854+
rust_error_at (expr.get_label ().get_locus (), ErrorCode::E0770,
855855
"failed to lookup compiled label");
856856
return;
857857
}
@@ -958,63 +958,15 @@ CompileExpr::visit (HIR::LiteralExpr &expr)
958958
}
959959
}
960960

961-
bool
962-
lvalue_p (const_tree ref)
963-
{
964-
const enum tree_code code = TREE_CODE (ref);
965-
966-
switch (code)
967-
{
968-
case REALPART_EXPR:
969-
case IMAGPART_EXPR:
970-
case COMPONENT_REF:
971-
return lvalue_p (TREE_OPERAND (ref, 0));
972-
973-
case C_MAYBE_CONST_EXPR:
974-
return lvalue_p (TREE_OPERAND (ref, 1));
975-
976-
case COMPOUND_LITERAL_EXPR:
977-
case STRING_CST:
978-
case CONST_DECL:
979-
case INTEGER_CST:
980-
return true;
981-
982-
case MEM_REF:
983-
case TARGET_MEM_REF:
984-
/* MEM_REFs can appear from -fgimple parsing or folding, so allow them
985-
here as well. */
986-
case INDIRECT_REF:
987-
case ARRAY_REF:
988-
case VAR_DECL:
989-
case PARM_DECL:
990-
case RESULT_DECL:
991-
case ERROR_MARK:
992-
return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
993-
&& TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
994-
995-
case BIND_EXPR:
996-
return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
997-
case PLUS_EXPR:
998-
case MINUS_EXPR:
999-
case MULT_EXPR:
1000-
case POINTER_PLUS_EXPR:
1001-
case POINTER_DIFF_EXPR:
1002-
case MULT_HIGHPART_EXPR:
1003-
case TRUNC_DIV_EXPR:
1004-
return false;
1005-
default:
1006-
rust_debug ("unknown");
1007-
return false;
1008-
}
1009-
}
1010-
1011961
void
1012962
CompileExpr::visit (HIR::AssignmentExpr &expr)
1013963
{
1014964
auto lvalue = CompileExpr::Compile (expr.get_lhs (), ctx);
1015965
auto rvalue = CompileExpr::Compile (expr.get_rhs (), ctx);
1016966

1017-
if (!lvalue_p (lvalue)
967+
bool validl_value = lvalue_p (lvalue);
968+
969+
if (!validl_value
1018970
|| expr.get_lhs ().get_expression_type ()
1019971
== HIR::Expr::ExprType::Operator)
1020972
{

0 commit comments

Comments
 (0)