rust-analyzer: [feat] Optional Inlay Hint for Too Many Implicit Returns in One Function

I have previewed the history of issues, and noticed that the optional inlay hint for the implicit returns would be useful when there are too many returns in one function. For example, in crates\hir-def\src\body\lower\asm.rs, the lower_inline_asm function has too many returns for readers to distinguish the difference. I also see the similar issues in the history, e.g. #9969, #4691. I think adding inlay hint for the implicit return would be a useful option.

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Comments: 15 (9 by maintainers)

Most upvoted comments

I think this is an interesting idea that would be useful when the statements are very large. However, its implementation needs further discussion. We could also use document-highlight to indicate where the current value is returned.

What exactly will the hint show?

@Veykril @lnicola @ChayimFriedman2

I have designed a scheme to give a inlay hint to the reader, but it’s my first time to issue a new feature request. Thus, it needs more discussion. I use two code snippets in crates\hir-def\src\body\lower\asm.rs as examples. Please review them and give me some suggestions.

Example 1

for piece in asm.asm_pieces() {
    let slot = operands.len();
    let mut ▶lower_reg = |reg: Option<ast::AsmRegSpec>| {
        let reg = reg?;
        if let Some(string) = reg.string_token() {
            reg_args.insert(slot);
            ⏎Some(InlineAsmRegOrRegClass::Reg(Symbol::intern(string.text())))
        } else {
            ⏎reg.name_ref().map(|name_ref| {
                InlineAsmRegOrRegClass::RegClass(Symbol::intern(&name_ref.text()))
            }) // The cursor is in this line.
        }
    };

Example 2

ast::AsmPiece::AsmOperandNamed(op) => {
      let name = op.name().map(|name| Symbol::intern(&name.text()));
      if let Some(name) = &name {
          named_args.insert(name.clone(), slot);
          named_pos.insert(slot, name.clone());
      }
      let Some(op) = op.asm_operand() else { continue };
      (
          name.map(Name::new_symbol_root),
          ▶match op { // This is a rvalue, not a lvalue receiving the implicit return.
              ast::AsmOperand::AsmLabel(l) => {
                  ⏎AsmOperand::Label(self.collect_block_opt(l.block_expr()))
              }
              ast::AsmOperand::AsmConst(c) => {
                  ⏎AsmOperand::Const(self.collect_expr_opt(c.expr()))  // The cursor is in this line.
              }
              ast::AsmOperand::AsmSym(s) => {
                  let Some(path) =
                      s.path().and_then(|p| self.expander.parse_path(self.db, p))
                  else {
                      continue;
                  };
                  ⏎AsmOperand::Sym(path)
              }
          },
      )
  }
};

When a user hovers and clicks the line of the implicit return in the nesting match block, the lsp would give inlay hints of ⏎ before the clicked line and other implicit return lines in the same match block as shown in the code snippet. The user can find the variable assigned to the return value near the first ⏎ tag and the match keyword.

When user changes the cursor to the other line, the ⏎ tag is no longer hinted in the editor.

It is also helpful to add an comtemporary inlay hint of ▶ to the variable receiving the implicit return variable, also disappearing when the user moves the cursor to the other line.