rust-lang / rust-lang/rustfmt

[GSoC24] Implement `rewrite_result` for AST nodes

Open
#6,206 3 comments 6 reactions 1 assignee View on GitHub

@ding-young is already working on this.

Since Jun 22, 2024.

GSoC
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

This issue tracks the necessary tasks to implement rewrite_result for all the nodes that haverewrite implementation.

Background

Current rewrite method returns Option, which means that it gives None without any context when formatting fails. To improve tracking the context of formatting failures, we plan to add a new method rewrite_result in Rewrite trait and gradually replace rewrite with rewrite_result.

Target AST nodes

Below is the list of structs that currently implement the rewrite method. Most of these structs are rustc AST node, but it may be rustfmt representation of AST nodes (ex. Chain). Each task requires both implementing rewrite_result for that node and then modifying rewrite body with calling rewrite_result. Still, rewrite signature will remain the same and the return value of rewrite_result will be squashed into Option until we are ready to propagate Result upwards.

rewrite_*** on the right are the functions that need to be modified to implement rewrite_result for corresponding ast node.
attr.rs

  • ast::NestedMetaItem - rewrite_literal #6250
  • ast::MetaItem - rewrite_path, rewrite_literal, rewrite_with_parens #6248
  • ast::Attribute - rewrite_doc_comment #6250
  • [ast::Attribute] - rewrite_(initial)doc_comment #6250

chains.rs

  • ChainItem - rewrite_paren (local change), rewrite_method_call, rewrite_comment #6250
  • Chain - methods of ChainFormatter trait #6270

expr.rs

  • ast::Expr - tons of rewrite calls #6311
  • ast::Block - rewrite_block #6235
  • ControlFlow - rewrite_block_with_visitor, format_expr #6291

imports.rs

  • UseSegment - rewrite_nested_use_tree #6261
  • UseTree - UseSegment #6261

items.rs

  • ast::Local #6212
  • TraitAliasBound #6309
  • ast::FieldDef #6212
  • OpaqueType
  • ast::FnRetTy #6212
  • ast::Param #6212
  • ast::ForeignItem #6309

macros.rs

  • ast::Item #6271
  • MacroArg #6271
    rewrite_macro

matches.rs

  • ArmWrapper - #6239

overflow.rs

  • OverflowableItem - rewrite_result for
    Expr, GenericParam, MacroArg, NestedMetaItem, SegmentParam
    FieldDef, TuplePatField, Ty, Pat #6291

patterns.rs

  • RangeOperand #6248
  • (ast) Pat - write_list, rewrite_path, rewrite_unary_prefix #6262
  • (ast) PatField #6250
  • TuplePatField #6262

stmt.rs

  • Stmt #6291

types.rs

  • SegmentParam #6220
  • PreciseCapturingArg #6236
  • ast::AssocItemConstraint #6236
  • ast::AssocConstraintKind #6220
  • ast::WherePredicate #6309
  • ast::GenericArg #6220
  • ast::AnonConst - format_expr #6311
  • ast::Lifetime #6291
  • ast::GenericBound #6220
  • ast::GenericBounds #6220
  • ast::GenericParam #6220
  • ast::PolyTraitRef #6236
  • ast::TraitRef #6236
  • ast:Ty #6220

Future Tasks

Future tasks have two directions: one is propagating errors upward, and the other is generating a Result from downward that can potentially cause failure. Corresponding gh issues will be created after the tasks for this issue are done.

Reference

This issue is part of the Google Summer of Code project. For detailed description, see GSoC project page.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.