-
Notifications
You must be signed in to change notification settings - Fork 182
Implement readonly checker on HIR #3881
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
base: master
Are you sure you want to change the base?
Conversation
your probably missing the using statement in the visitor |
@@ -0,0 +1,150 @@ | |||
// Copyright (C) 2020-2025 Free Software Foundation, Inc. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// 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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 = {}; |
There was a problem hiding this comment.
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
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 👍
gcc/rust/ChangeLog: * hir/tree/rust-hir-visitor.cc (DefaultHIRVisitor::walk): Add check before calling `get_trait_ref()` Signed-off-by: Ryutaro Okada <[email protected]>
gcc/rust/ChangeLog: * hir/tree/rust-hir-visitor.cc (DefaultHIRVisitor::walk): Cast to base class reuse the base class's accept_vis method Signed-off-by: Ryutaro Okada <[email protected]>
5681f7f
to
ec0556a
Compare
You can skip the |
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 <[email protected]>
ec0556a
to
bcf2748
Compare
No description provided.