Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gccrs: feat: Made changes to ensure no wrong assignments are done. #3300

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions gcc/rust/backend/rust-compile-base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1015,5 +1015,51 @@ HIRCompileBase::unit_expression (location_t locus)
return Backend::constructor_expression (unit_type, false, {}, -1, locus);
}

bool
HIRCompileBase::is_lvalue (const_tree ref)
{
const enum tree_code code = TREE_CODE (ref);

switch (code)
{
case REALPART_EXPR:
case IMAGPART_EXPR:
case COMPONENT_REF:
return is_lvalue (TREE_OPERAND (ref, 0));

case COMPOUND_LITERAL_EXPR:
case STRING_CST:
case CONST_DECL:
case INTEGER_CST:
return true;

case MEM_REF:
case TARGET_MEM_REF:
/* MEM_REFs can appear from -fgimple parsing or folding, so allow them
here as well. */
case INDIRECT_REF:
case ARRAY_REF:
case VAR_DECL:
case PARM_DECL:
case RESULT_DECL:
case ERROR_MARK:
return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
&& TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);

case BIND_EXPR:
return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
case PLUS_EXPR:
case MINUS_EXPR:
case MULT_EXPR:
case POINTER_PLUS_EXPR:
case POINTER_DIFF_EXPR:
case MULT_HIGHPART_EXPR:
case TRUNC_DIV_EXPR:
return false;
default:
return false;
}
}

} // namespace Compile
} // namespace Rust
1 change: 1 addition & 0 deletions gcc/rust/backend/rust-compile-base.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class HIRCompileBase
virtual ~HIRCompileBase () {}

static tree address_expression (tree expr, location_t locus);
bool is_lvalue (const_tree ref);

protected:
HIRCompileBase (Context *ctx) : ctx (ctx) {}
Expand Down
11 changes: 11 additions & 0 deletions gcc/rust/backend/rust-compile-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,17 @@ CompileExpr::visit (HIR::AssignmentExpr &expr)
auto lvalue = CompileExpr::Compile (expr.get_lhs (), ctx);
auto rvalue = CompileExpr::Compile (expr.get_rhs (), ctx);

bool validl_value = is_lvalue (lvalue);

if (!validl_value
|| expr.get_lhs ().get_expression_type ()
== HIR::Expr::ExprType::Operator)
{
rust_error_at (expr.get_lhs ().get_locus (), ErrorCode::E0770,
"invalid left-hand side of assignment");
return;
}

// assignments are coercion sites so lets convert the rvalue if necessary
TyTy::BaseType *expected = nullptr;
TyTy::BaseType *actual = nullptr;
Expand Down
4 changes: 4 additions & 0 deletions gcc/testsuite/rust/compile/issue-3297.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub fn main() {
let mut x = 42;
x + 1= 2; // { dg-error "invalid left-hand side of assignment" }
}
Loading