Skip to content

Implement readonly checker on HIR#3881

Merged
CohenArthur merged 3 commits into
Rust-GCC:masterfrom
sakupan102:readonly-check
Aug 4, 2025
Merged

Implement readonly checker on HIR#3881
CohenArthur merged 3 commits into
Rust-GCC:masterfrom
sakupan102:readonly-check

Conversation

@sakupan102

@sakupan102 sakupan102 commented Jul 7, 2025

Copy link
Copy Markdown
Contributor

After this PR is closed, I will delete the old read-only check and switch to the new one.

@sakupan102 sakupan102 marked this pull request as draft July 7, 2025 13:13
@philberty

Copy link
Copy Markdown
Member

your probably missing the using statement in the visitor

@@ -0,0 +1,150 @@
// Copyright (C) 2020-2025 Free Software Foundation, Inc.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Copyright (C) 2020-2025 Free Software Foundation, Inc.
// Copyright (C) 2025 Free Software Foundation, Inc.

we can just use 2025 here since it is a new file :)

@@ -0,0 +1,51 @@
// Copyright (C) 2020-2025 Free Software Foundation, Inc.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Copyright (C) 2020-2025 Free Software Foundation, Inc.
// Copyright (C) 2025 Free Software Foundation, Inc.

{
auto ident_pattern
= static_cast<HIR::IdentifierPattern *> (*maybe_pattern);
if (!ident_pattern->is_mut () && assignment_map[*hir_id] > 1)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want to check if it is greater than 0 here?

@sakupan102 sakupan102 Jul 9, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to check if the value is greater than 1.
In this context, assignment_map[hir_id] == 1 means the variable has been initialized, while assignment_map[hir_id] == 0 means it has only been declared.
If the assignment count is greater than 1, the variable has been assigned more than once, so I want to emit an error if the variable is not mutable.

== HIR::Item::ItemKind::Static)
{
auto static_item = static_cast<HIR::StaticItem *> (*maybe_static_item);
if (!static_item->is_mut () && assignment_map[*hir_id] > 1)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

namespace Rust {
namespace HIR {

static std::map<HirId, int> assignment_map = {};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we are only interested in whether we've seen the HirId or not, we can use a set here

Suggested change
static std::map<HirId, int> assignment_map = {};
static std::set<HirId> assignment_map = {};

I agree that if you need the count/number of times we've seen the HirId, then a map is better 👍

@sakupan102 sakupan102 marked this pull request as ready for review July 21, 2025 16:44
@powerboat9

Copy link
Copy Markdown
Collaborator

You can skip the #include "options.h" and assume flag_name_resolution_2_0 is true -- name resolution 1.0 is in the middle of being removed, so your code should only have to work with name resolution 2.0

Comment thread gcc/rust/hir/tree/rust-hir-visitor.cc Outdated
DefaultHIRVisitor::walk (IfExprConseqElse &expr)
{
reinterpret_cast<IfExpr &> (expr).accept_vis (*this);
reinterpret_cast<IfExpr &> (expr).IfExpr::accept_vis (*this);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need the reinterpret_cast here:

Suggested change
reinterpret_cast<IfExpr &> (expr).IfExpr::accept_vis (*this);
expr.IfExpr::accept_vis (*this);

Comment thread gcc/rust/hir/tree/rust-hir-visitor.cc Outdated
DefaultHIRVisitor::walk (EnumItemTuple &item_tuple)
{
reinterpret_cast<EnumItem &> (item_tuple).accept_vis (*this);
reinterpret_cast<EnumItem &> (item_tuple).EnumItem::accept_vis (*this);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

likewise and other uses in this file

Comment on lines +34 to +39
enum lvalue_use
{
lv_assign,
lv_increment,
lv_decrement,
};

@CohenArthur CohenArthur Jul 29, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
enum lvalue_use
{
lv_assign,
lv_increment,
lv_decrement,
};
enum class lvalue_use
{
assign,
increment,
decrement,
};

this way we get a bit stronger namespacing

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, meant enum class thank you @P-E-P

gcc/rust/ChangeLog:

	* hir/tree/rust-hir-visitor.cc (DefaultHIRVisitor::walk):
	Add check before calling  `get_trait_ref()`

Signed-off-by: Ryutaro Okada <1015ryu88@gmail.com>
gcc/rust/ChangeLog:

	* hir/tree/rust-hir-visitor.cc (DefaultHIRVisitor::walk):
	Call base class's  accept_vis method

Signed-off-by: Ryutaro Okada <1015ryu88@gmail.com>
gcc/rust/ChangeLog:

	* Make-lang.in (rust-readonly-check2.cc):
	Add read-only check on HIR
	* checks/errors/rust-readonly-check2.cc (ReadonlyChecker):
	Add read-only check on HIR
	* checks/errors/rust-readonly-check2.h (ReadonlyChecker):
	Add read-only check on HIR

Signed-off-by: Ryutaro Okada <1015ryu88@gmail.com>

@CohenArthur CohenArthur left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this looks good!

@CohenArthur CohenArthur added this pull request to the merge queue Aug 4, 2025
Merged via the queue into Rust-GCC:master with commit fa88250 Aug 4, 2025
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants