Rust: #![feature(use_extern_macros)] 없이 proc 맀크둜 2.0을 μ‚¬μš©ν•  λ•Œ μœ μš©ν•˜μ§€ μ•Šμ€ 진단

에 λ§Œλ“  2017λ…„ 04μ›” 17일  Β·  3μ½”λ©˜νŠΈ  Β·  좜처: rust-lang/rust

λ‹€μŒ 두 파일이 μžˆλ‹€κ³  κ°€μ •ν•©λ‹ˆλ‹€.

// foo.rs
#![crate_type = "proc-macro"]

extern crate proc_macro;
use proc_macro::TokenStream;

#[proc_macro]
pub fn foo(x: TokenStream) -> TokenStream {
    x
}
// bar.rs
extern crate foo;
use foo::foo;

fn main() {
    foo!();
}

rustc foo.rs && rustc bar.rs -L. λ₯Ό μ‚¬μš©ν•˜μ—¬ μ΄λŸ¬ν•œ νŒŒμΌμ„ μ»΄νŒŒμΌν•˜λ €κ³  ν•˜λ©΄ λ‹€μŒ 였λ₯˜κ°€ λ°œμƒν•©λ‹ˆλ‹€.

warning: proc macro crates and `#[no_link]` crates have no effect without `#[macro_use]`
 --> bar.rs:1:1
  |
1 | extern crate foo;
  | ^^^^^^^^^^^^^^^^^

error[E0432]: unresolved import `foo::foo`
 --> bar.rs:2:5
  |
2 | use foo::foo;
  |     ^^^^^^^^ no `foo` in the root

error: cannot find macro `foo!` in this scope
 --> bar.rs:5:5
  |
5 |     foo!();
  |     ^^^

error: aborting due to 2 previous errors

이제 컴파일러의 쑰언을 λ”°λ₯΄κ³  λŒ€μ‹  #[macro_use] λ₯Ό μ‹œλ„ν•˜λ©΄ λ‹€μŒ 였λ₯˜κ°€ ν‘œμ‹œλ©λ‹ˆλ‹€.

error: procedural macros cannot be imported with `#[macro_use]`
 --> bar.rs:5:5
  |
5 |     foo!();
  |     ^^^
  |
  = help: instead, import the procedural macro like any other item: `use foo::foo;`

error: aborting due to previous error

κ·ΈλŸ¬λ‚˜ 그것은 λ‚΄κ°€ μ²˜μŒμ— 가지고 있던 κ²ƒμž…λ‹ˆλ‹€!

이 λ¬Έμ œμ— λŒ€ν•œ μ‹€μ œ 해결책은 #![feature(use_extern_macros)] λ₯Ό bar.rs 에 μΆ”κ°€ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€. μ»΄νŒŒμΌλŸ¬λŠ” ν•¨μˆ˜μ™€ μœ μ‚¬ν•œ proc 맀크둜λ₯Ό λ§Œλ‚  λ•Œ λŒ€μ‹  이것을 μ œμ•ˆν•΄μ•Ό ν•©λ‹ˆλ‹€.

A-diagnostics A-macros-2.0 C-enhancement

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

이제 야간에 warning: this feature has been stable since 1.29.0. Attribute no longer needed for #![feature(proc_macro)] 도 λ°›μŠ΅λ‹ˆλ‹€.

κ·ΈλŸ¬λ‚˜ μ—¬μ „νžˆ @lfairy와 λ™μΌν•œ λ¬Έμ œκ°€ μžˆμŠ΅λ‹ˆλ‹€. 이제 맀크둜λ₯Ό μ‚¬μš©ν•˜λ €λ©΄ error: cannot find macro 'my_macro!' in this scope λ₯Ό μ–»μŠ΅λ‹ˆλ‹€. ν•˜μ§€λ§Œ use my_crate::my_macro; λ₯Ό μΆ”κ°€ν•˜λ©΄ error[E0432]: unresolved import 'my_crate::my_macro' κ°€ λ©λ‹ˆλ‹€.

이제 proc_macro κΈ°λŠ₯을 use_extern_macros 둜 ꡐ체해야 ν•˜λŠ” 것 κ°™μŠ΅λ‹ˆλ‹€ . Attribute is no longer needed λ©”μ‹œμ§€μ— μœ μš©ν•œ νžŒνŠΈκ°€ 될 κ²ƒμž…λ‹ˆλ‹€.. ;)

λͺ¨λ“  3 λŒ“κΈ€

λ‹¨κΈ°μ μœΌλ‘œ #![feature(proc_macro)] λ₯Ό bar.rs 에 μΆ”κ°€ ν•˜λ„λ‘ μ œμ•ˆν•˜λ„λ‘ 적어도 진단 λ©”μ‹œμ§€λ₯Ό λ³€κ²½ν•˜λŠ” 것은 μ‰¬μš΄ μˆ˜μ •μΌ 수 μžˆμŠ΅λ‹ˆλ‹€. (λ˜λŠ” μ œμ•ˆμ„ ν•˜κΈ° 전에 ν•΄λ‹Ή κΈ°λŠ₯이 ν™œμ„±ν™”λ˜μ–΄ μžˆλŠ”μ§€ λ¨Όμ € 진단을 톡해 ν™•μΈν•˜μ‹­μ‹œμ˜€.) μ‹€μ œ λ¬Έμ œλ„ ν¬ν•¨ν•˜λŠ” ν•œ μ œμ•ˆμ˜ 쀑볡 뢀뢄이 μžˆμ–΄λ„ 상관 μ—†μŠ΅λ‹ˆλ‹€.

이제 야간에 warning: this feature has been stable since 1.29.0. Attribute no longer needed for #![feature(proc_macro)] 도 λ°›μŠ΅λ‹ˆλ‹€.

κ·ΈλŸ¬λ‚˜ μ—¬μ „νžˆ @lfairy와 λ™μΌν•œ λ¬Έμ œκ°€ μžˆμŠ΅λ‹ˆλ‹€. 이제 맀크둜λ₯Ό μ‚¬μš©ν•˜λ €λ©΄ error: cannot find macro 'my_macro!' in this scope λ₯Ό μ–»μŠ΅λ‹ˆλ‹€. ν•˜μ§€λ§Œ use my_crate::my_macro; λ₯Ό μΆ”κ°€ν•˜λ©΄ error[E0432]: unresolved import 'my_crate::my_macro' κ°€ λ©λ‹ˆλ‹€.

이제 proc_macro κΈ°λŠ₯을 use_extern_macros 둜 ꡐ체해야 ν•˜λŠ” 것 κ°™μŠ΅λ‹ˆλ‹€ . Attribute is no longer needed λ©”μ‹œμ§€μ— μœ μš©ν•œ νžŒνŠΈκ°€ 될 κ²ƒμž…λ‹ˆλ‹€.. ;)

여기에 μžˆλŠ” λ§Žμ€ κΈ°λŠ₯이 μ•ˆμ •ν™”λ˜μ—ˆμœΌλ―€λ‘œ 이것은 μ˜€λž˜λ˜μ—ˆμ„ κ°€λŠ₯성이 μžˆμœΌλ―€λ‘œ λ‹«μŠ΅λ‹ˆλ‹€.

이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰