Skip to content

Refactor/ast#30

Merged
broccolingual merged 3 commits into
mainfrom
refactor/ast
Jan 12, 2026
Merged

Refactor/ast#30
broccolingual merged 3 commits into
mainfrom
refactor/ast

Conversation

@broccolingual
Copy link
Copy Markdown
Owner

No description provided.

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello @broccolingual, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

このプルリクエストは、AST (抽象構文木) 構築ロジックをリファクタリングし、様々なパースコンテキスト管理(関数、スコープ、ループ、switch文)にRAII (Resource Acquisition Is Initialization) パターンを導入し、型検証ロジックを一元化します。これにより、コードが大幅に整理され、コンテキスト処理がより堅牢になり、定型的なコードが削減されます。

Highlights

  • RAIIヘルパーの導入: 関数コンテキスト、スコープ、ループ、switch文のコンテキスト管理にRAIIパターンを導入しました。
  • 型検証ロジックの一元化: 構造体/共用体およびポインタ/配列の型検証をTypeRefに集約しました。
  • ASTパースメソッドの簡素化: 上記のヘルパーを使用することで、AST構築コードがより簡潔になりました。

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

このプルリクエストは、ASTの構築ロジックをリファクタリングするもので、主にRAII(Resource Acquisition Is Initialization)パターンの導入により、コンテキスト管理(スコープ、関数、ループ、switch)の安全性を向上させています。with_scopeのようなヘルパー関数を導入することで、コードがクリーンになり、リソースリークの可能性が低減されています。また、型チェック関連のロジックをTypeRefに集約したのも、関心の分離の観点から良い改善です。全体として、コードの堅牢性と保守性が向上する素晴らしいリファクタリングです。いくつか改善提案がありますので、ご確認ください。

Comment thread src/ast.rs
Comment on lines +167 to 175
fn with_scope<F, T>(&mut self, f: F) -> Result<T, CompileError>
where
F: FnOnce(&mut Self) -> Result<T, CompileError>,
{
self.symbol_table.push_scope(); // スコープをpush
let result = f(self);
self.symbol_table.pop_scope(); // スコープをpop
result
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

このwith_scope関数や、同様のパターンで実装されたwith_current_funcwith_loop_scopewith_switch_scopeは、渡されたクロージャfがパニックした場合にクリーンアップ処理(pop_scopeなど)が呼ばれないため、パニックセーフではありません。コンパイラがパニックで即座に終了するなら大きな問題にはなりませんが、より堅牢にするために、Dropトレイトを実装したガード構造体を使うのがRustの慣用的なRAIIパターンです。これにより、パニック時でも確実にクリーンアップ処理が実行されるようになります。

// 例: ScopeGuard
struct ScopeGuard<'a, 'b: 'a> {
    ast: &'a mut Ast<'b>,
}

impl<'a, 'b> Drop for ScopeGuard<'a, 'b> {
    fn drop(&mut self) {
        self.ast.symbol_table.pop_scope();
    }
}

// with_scopeの実装
fn with_scope<F, T>(&mut self, f: F) -> Result<T, CompileError>
where
    F: FnOnce(&mut Self) -> Result<T, CompileError>,
{
    self.symbol_table.push_scope();
    let _guard = ScopeGuard { ast: self }; // guardがスコープを抜けるときにpop_scopeが呼ばれる
    f(self)
}

上記のようにガード構造体を用いることで、f(self)がパニックを起こしても_guarddropが呼ばれ、pop_scopeが実行されることが保証されます。このパターンを他のwith_*関数にも適用することで、全体的な堅牢性が向上します。

Comment thread src/ast/decl.rs Outdated
Comment on lines 110 to 113
this.calc_current_func_offset()?; // ローカル変数のオフセットを計算
Ok(())
})?;

let NodeKind::Block { body } = func_body.kind else {
return Err(CompileError::InvalidDecl {
msg: "関数本体がブロックではありません。'{' と '}' で囲まれた複合文が必要です"
.to_string(),
span: func_body.span,
});
};

self.get_current_func_mut()?.body = body;
self.pop_scope(); // 引数スコープを抜ける
self.calc_current_func_offset()?; // ローカル変数のオフセットを計算
self.current_func = None;

Ok(())
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

with_current_funcの呼び出し後の?と最後のOk(())は冗長に感じられます。with_current_funcはすでにResult<(), CompileError>を返すため、その結果を直接func_defから返すようにすると、より簡潔になります。

具体的には、Ok(())を返す代わりにthis.calc_current_func_offset()をクロージャの最後の式とし、with_current_funcの呼び出しがfunc_def関数の最後の式となるようにします。calc_current_func_offsetResult<(), CompileError>を返すため、クロージャ全体も同じ型を返すことになり、コードがすっきりします。

Suggested change
this.calc_current_func_offset()?; // ローカル変数のオフセットを計算
Ok(())
})?;
let NodeKind::Block { body } = func_body.kind else {
return Err(CompileError::InvalidDecl {
msg: "関数本体がブロックではありません。'{' と '}' で囲まれた複合文が必要です"
.to_string(),
span: func_body.span,
});
};
self.get_current_func_mut()?.body = body;
self.pop_scope(); // 引数スコープを抜ける
self.calc_current_func_offset()?; // ローカル変数のオフセットを計算
self.current_func = None;
Ok(())
this.calc_current_func_offset() // ローカル変数のオフセットを計算
})

@broccolingual
Copy link
Copy Markdown
Owner Author

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

このプルリクエストは、ASTの構築ロジックをリファクタリングし、RAIIパターンのヘルパー関数(with_scopeなど)を導入して、関数、スコープ、ループ、switch文のコンテキスト管理を改善するものです。これにより、コードの可読性と安全性が大幅に向上しており、素晴らしい改善です。

しかし、新しく導入されたwith_loop_scopewith_switch_scope関数において、エラーハンドリングに潜在的な問題が見つかりました。内部のクロージャとコンテキストのポップ処理の両方でエラーが発生した場合、クロージャからの本来のエラーが失われてしまう可能性があります。詳細は各コメントをご確認ください。

@broccolingual broccolingual merged commit 262246f into main Jan 12, 2026
1 check passed
@broccolingual broccolingual deleted the refactor/ast branch January 12, 2026 03:32
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.

1 participant