μ΅μνμ μ¬μμ°:
// src/lib.rs
#![feature(decl_macro)]
pub macro use_fmt {
() => {
use std::fmt;
}
}
// examples/test.rs
#![feature(use_extern_macros)]
extern crate macro_test;
use macro_test::use_fmt;
use_fmt!();
fn main() {}
The error:
$ cargo build --example test
Compiling macro-test v0.1.0 (file:///.../macro-test)
error[E0432]: unresolved import `std::fmt`
--> examples/test.rs:7:1
|
7 | use_fmt!();
| ^^^^^^^^^^^ Could not find `std` in `{{root}}`
|
= note: this error originates in a macro outside of the current crate
error: aborting due to previous error(s)
error: Could not compile `macro-test`.
μ μλ κ²κ³Ό λμΌν μμμμ 맀ν¬λ‘λ₯Ό νΈμΆνλ©΄ μμλλ‘ μλν©λλ€.
cc @jseyfried
λ¦μ΄μ μ£μ‘ν©λλ€. # 46419μμ μμ λμμ΅λλ€.
λμΌν λ¬Έμ μΈμ§(κ·Έλ¦¬κ³ PRμ΄ λ¬Έμ λ₯Ό ν΄κ²°νλμ§) λͺ¨λ₯΄κ² μ§λ§ proc_macro
μ λμΌν μ€λ₯κ° λ°μν©λλ€.
λ€μμ μ΄ λ¬Έμ λ₯Ό μ¬ννκΈ° μν μ΅μνμ μμ
λλ€.
κ°μ λ¬Έμ μ
λκΉ?
cargo build
μ€ννλ©΄ λ€μ μ€λ₯κ° λ°μν©λλ€.
error[E0433]: failed to resolve. Could not find `postgres` in `{{root}}`
--> src/lib.rs:9:17
|
9 | #[derive(Debug, SqlTable)]
| ^^^^^^^^ Could not find `postgres` in `{{root}}`
error[E0433]: failed to resolve. Could not find `std` in `{{root}}`
--> src/lib.rs:9:17
|
9 | #[derive(Debug, SqlTable)]
| ^^^^^^^^ Could not find `std` in `{{root}}`
error[E0412]: cannot find type `Table` in this scope
--> src/lib.rs:9:17
|
9 | #[derive(Debug, SqlTable)]
| ^^^^^^^^ not found in this scope
help: possible candidate is found in another module, you can import it into scope
|
3 | use Table;
|
μ΄ λ¬Έμ μ λν ν΄κ²° λ°©λ²μ΄ μμ΅λκΉ?
κ°μ¬ν©λλ€.
@antoyo μ΄κ²μ μμμ κ²°κ³Όμ λλ€.
::postgres::types::Type
λ proc-macro μ μμ μμΌλ―λ‘ λ§€ν¬λ‘ μ μ λ²μμμ ν΄κ²°λ©λλ€. νΉν 맀ν¬λ‘κ° νΈμΆλλ μμΉμ κ΄κ³μμ΄ λμΌνκ² ν΄κ²°λ©λλ€.
proc-macro crate rootμλ extern crate postgres;
κ° μμΌλ―λ‘ ν΄κ²° μ€λ₯κ° λ°μν©λλ€. proc-macro crate rootμ extern crate postgres;
λ₯Ό μΆκ°νλ κ²λ§μΌλ‘λ μΆ©λΆνμ§ μμ΅λλ€. κ·Έλ¬λ proc-macro μ’
μμ±μ λμ νλ«νΌ(1λ¨κ³)μ΄ μλλΌ νΈμ€νΈ νλ«νΌ(0λ¨κ³)μ λν΄ μ»΄νμΌλλ―λ‘ νμ₯μ κ·Έλ€μ λ³Ό μ μμ΅λλ€.
μ¬κΈ°μ ν΄κ²°μ± μ https://github.com/rust-lang/rust/issues/45934#issuecomment -344497531μ μ€λͺ λ λλ‘ proc-macro ν¬λ μ΄νΈμ λμ μ’ μμ±μ μΆκ°νλ κ²μ λλ€.
μ§κΈμ 맀ν¬λ‘ νμ₯μ extern crate postgres;
λ₯Ό λ£μ΄μΌν©λλ€. μμ λλ¬Έμ μ΄κ²μ νΈμΆ μ¬μ΄νΈμμ postgres
λΌλ μ΄λ¦κ³Ό μΆ©λνμ§ μμΌλ©° 맀ν¬λ‘ μΈμ λ±μ ν΄κ²°μ μν₯μ λ―ΈμΉμ§ μμ΅λλ€. μλ₯Ό λ€λ©΄ λ€μκ³Ό κ°μ΅λλ€.
quote! {
extern crate std;
extern crate postgres;
use std::io::Write;
use postgres::types::{IsNull, ToSql, Type};
impl ToSql for #table_ident {
fn to_sql<W: Write + ?Sized>(&self, ty: &Type, out: &mut W) -> postgres::Result<IsNull> {
self.#primary_key_ident.to_sql(ty, out)
}
fn accepts(ty: &Type) -> bool {
match *ty {
Type::Int4 => true,
_ => false,
}
}
fn to_sql_checked(&self, ty: &Type, out: &mut Write) -> postgres::Result<IsNull> {
if !<Self as ToSql>::accepts(ty) {
return Err(postgres::error::Error::WrongType(ty.clone()));
}
self.to_sql(ty, out)
}
}
}
λλ Span::call_site()
( quote!
ν ν°μ κΈ°λ³Έμ μΌλ‘ Span::def_site()
λ₯Ό κ°μ§)λ₯Ό μ 곡νμ¬ ::postgres::types::Type
νΈμΆ μ¬μ΄νΈμμ (λΉμμμ μΌλ‘) ν΄κ²°νλλ‘ λ§λ€ μ μμ΅λλ€. 곧 spanned_quote!(span, tokens...)
μ(λ₯Ό) μ¬μ©νλ©΄ μ΄ μμ
μ΄ λ μ¬μμ§λλ€.
κ°μ₯ μ μ©ν λκΈ
λ¦μ΄μ μ£μ‘ν©λλ€. # 46419μμ μμ λμμ΅λλ€.