id, title, category, status, canonical_id, aliases, duplicate_of, source_trust_level, confidence_score, verification_status, tags, raw_sources, last_reinforced, github_commit, tech_stack
| id |
title |
category |
status |
canonical_id |
aliases |
duplicate_of |
source_trust_level |
confidence_score |
verification_status |
tags |
raw_sources |
last_reinforced |
github_commit |
tech_stack |
| wiki-2026-0508-macros-매크로 |
Macros (매크로) |
10_Wiki/Topics |
verified |
self |
| Macros |
| Metaprogramming |
| Code Generation |
| Procedural Macros |
|
none |
A |
0.9 |
applied |
| programming |
| metaprogramming |
| rust |
| lisp |
| c |
| language-design |
|
|
2026-05-10 |
pending |
| language |
framework |
| rust-lisp-c |
language-features |
|
Macros (매크로)
매 한 줄
"매 컴파일 타임에 코드를 쓴다". 매크로는 텍스트/AST 단위로 코드를 생성하는 메타프로그래밍이며, hygiene/표현력에 따라 C 텍스트 치환 → Rust AST → Lisp의 동형성으로 진화한다.
매 핵심
매 분류
- Text-level: C/C++ 전처리기 — 토큰 치환, hygiene 없음.
- AST-level (declarative): Rust
macro_rules!, Scheme syntax-rules — 패턴 매칭.
- AST-level (procedural): Rust
proc-macro, OCaml ppx — 임의 컴파일타임 코드.
- Homoiconic: Lisp/Clojure — 코드 = 데이터, defmacro로 자유 변환.
- Reflection / templates: C++ template, Zig comptime — 매크로 인접.
매 핵심 속성
- Hygiene: 매크로 안의 식별자가 호출처 스코프와 충돌하지 않음.
- Phase: 컴파일타임 vs 런타임.
- Expressiveness: 단순 치환부터 임의 컴파일러 코드까지.
- Tooling: rust-analyzer/clangd가 macro expansion 지원.
- Cost: 컴파일 시간 / 디버깅 난이도 trade-off.
💻 패턴
Pattern 1 — Rust macro_rules! (declarative)
Pattern 2 — Rust derive (proc-macro)
Pattern 3 — Rust attribute proc-macro
Pattern 4 — Lisp defmacro
Pattern 5 — C 매크로 (주의)
Pattern 6 — Zig comptime (현대 대안)
Pattern 7 — Rust quote! (proc-macro 작성)
매 결정 기준
| 상황 |
도구 |
| 반복 boilerplate |
macro_rules! 또는 derive |
| 복잡 코드 생성 (DSL, ORM) |
proc-macro |
| 단순 상수 / 헤더 가드 |
const / module (매크로 X) |
| C에서 inline 강제 |
static inline (매크로보다 안전) |
| 코드 = 데이터 변형 |
Lisp/Clojure |
| 컴파일타임 일반화 |
Zig comptime, C++ constexpr/templates |
기본값: Rust → 가능하면 함수/제네릭, 그 다음 declarative macro, 마지막 proc-macro.
🔗 Graph
- 부모: Metaprogramming, Compilers
- 변형: Rust-Macros, Lisp-Macros, C-Preprocessor, Procedural-Macros
- 응용: Serde, Tokio-Async, DSL-Design, Code-Generation
- Adjacent: Reflection, Templates-CPP, Comptime-Zig, Hygiene
🤖 LLM 활용
언제:
macro_rules! 패턴 작성.
- proc-macro skeleton (syn/quote 보일러).
- C 매크로 함정 감사.
언제 X:
- 매크로 디버깅 (실제 expansion 확인 필요).
- 매우 복잡한 DSL 설계 (테스트 필수).
❌ 안티패턴
- 함수로 충분한데 매크로 사용 (디버깅 지옥).
- C 매크로에서 인자 괄호 누락.
- 부작용 있는 인자를 매크로에 넘김 (
MAX(i++, j)).
- proc-macro 컴파일 시간 폭증 무시.
- Hygiene 가정 (C는 없음, Rust/Scheme은 있음).
- 매크로로 타입 검사 우회.
🧪 검증 / 중복
- Verified. Rust 1.83/2024 edition 기준. 신뢰도 A.
🕓 Changelog
| 날짜 |
변경 |
| 2026-05-08 |
Phase 1 |
| 2026-05-10 |
Manual cleanup |