Rust-GCC / Rust-GCC/gccrs

Tracking issue for builtin derive macros

Open
#2,172 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
C++
Stars
2.9k
Forks
230
Avg merge
19h 55m
Merged PRs (30d)
67

Description

  • #2154
  • Validate #[derive] attribute and collect the attribute's inputs. collect_derives in rustc/compiler/rustc_expand/proc_macro.rs
  • Add new DeriveMacroAccumulator visitor to visit all required fields/variants of a struct/enum/union getting "derived"

This is different from custom derive macros, which will only receive a token stream - we have more information within the compiler and can make use of them

  • Handle all builtin derives (#927): Clone, Copy, Debug, Default, Hash, {Partial}Eq, {Partial}Ord)
  • Add good typechecking errors for when derive constraints are not met:
struct S;
#[derive(Clone)]
struct SuperS(S);

should fail because S does not implement Clone. The code produced by the macro should be similar to this:

struct S;

struct SuperS(S);

impl Clone for SuperS {
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}     

Without any derive-specific behavior, we'll get an error about how self.0 does not implement clone, which is true but not helpful for users as the code responsible will have been generated by the compiler.

During typechecking, we should check that the members of a type getting derived also implement the trait being derived, and produce errors more related to deriving ("consider annotating S with #[derive(Clone)]" etc)

  • Deriving Clone on a union requires that the union is Copy
  • Support deriving on statements inside a BlockExpr
  • Implement proper way to access a statement's outer attributes if that statement is an item
  • Ignore impls that were #[automatically_derived] in lints
  • #2238
  • Decide when to add #[inline] to generated impl functions

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.

Research direction

This is a tracking issue for builtin derive macros, with one entry point named: collect_derives in rustc/compiler/rustc_expand/proc_macro.rs. Review the linked #2154 and the remaining checklist items first; the work is done only when the listed derive handling, diagnostics, union and block support, lint behavior, and inline decisions are addressed.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.