Rust: fn ν˜ΈμΆœμ— "μ œμ•½ μ—†λŠ” μœ ν˜• λ§€κ°œλ³€μˆ˜"κ°€ λͺ…μ‹œμ μœΌλ‘œ 제곡될 수 μžˆλŠ” 경우 감지

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

https://github.com/rust-lang/rust/pull/39361 을 기반으둜 μ‚¬μš©μžκ°€ fn ν˜ΈμΆœμ— λͺ…μ‹œμ  μœ ν˜• λ§€κ°œλ³€μˆ˜λ₯Ό μΆ”κ°€ν•΄μ•Ό ν•˜λŠ” 경우λ₯Ό κ³ λ €ν•  수 μžˆμŠ΅λ‹ˆλ‹€. λ‚˜λŠ” λ‹€μŒκ³Ό 같은 예λ₯Ό μƒμƒν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

fn foo<T>() { }

fn main() {
    foo();
}

μ‚¬μš©μžκ°€ λͺ…μ‹œμ μΈ T $ 에 λŒ€ν•΄ foo::<T> λ₯Ό μž‘μ„±ν•˜λ„λ‘ μ œμ•ˆν•˜λŠ” 것이 μ’‹μŠ΅λ‹ˆλ‹€. 이것은 μ•½κ°„μ˜ 생각이 ν•„μš”ν•©λ‹ˆλ‹€:

지역 λ³€μˆ˜μ— 주석을 달기보닀 μ–Έμ œ 이것을 μ œμ•ˆν•˜κ³  μ‹ΆμŠ΅λ‹ˆκΉŒ? 지역 λ³€μˆ˜μ— 주석을 μ²¨λΆ€ν•˜λŠ” 것을 μ„ ν˜Έν•˜μ§€λ§Œ 지역 λ³€μˆ˜μ˜ μœ ν˜•μ΄ λ‚΄λΆ€ κΉŠμˆ™μ΄ μœ μΆ”λ˜μ§€ μ•Šμ€ μœ ν˜•μ„ μ–ΈκΈ‰ν•˜λŠ” λ³΅μž‘ν•œ 것인 κ²½μš°μ—λŠ” 그렇지 μ•Šμ„ μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€. λ°˜λ©΄μ— ν•¨μˆ˜μ— 주석을 μΆ”κ°€ν•˜λ©΄ μœ μΆ”λ˜μ§€ μ•Šμ€ μœ ν˜•μ„ 직접 지정할 수 μžˆμŠ΅λ‹ˆλ‹€. .

예:

fn foo<T>() -> Option<T> { }

fn main() {
    let x = foo();
}

x: Option<T> λ˜λŠ” foo::<T> λ ˆμ΄λΈ”μ„ μ œμ•ˆν•΄μ•Ό ν•©λ‹ˆκΉŒ? 이 κ²½μš°λŠ” κ²½κ³„μ„ μ΄μ§€λ§Œ Option λ₯Ό μ’€ 더 λ³΅μž‘ν•œ μœ ν˜•μœΌλ‘œ λ°”κΎΈλ©΄ κ· ν˜•μ΄ 더 κΈ°μšΈμ–΄μ§‘λ‹ˆλ‹€.

fn이 λ§Žμ€ μœ ν˜•μ„ μ·¨ν•˜κ³  λΆ€λΆ„μ μœΌλ‘œ μ•Œκ³  μžˆμ„ λ•Œ μš°λ¦¬λŠ” 무엇을 ν•©λ‹ˆκΉŒ? μš°λ¦¬λŠ” 일뢀 정보가 μžˆμ§€λ§Œ μ „λΆ€λŠ” 아닐 λ•Œ 무엇을 ν•΄μ•Ό ν•˜λŠ”μ§€μ— λŒ€ν•œ 문제λ₯Ό νšŒν”Όν–ˆμŠ΅λ‹ˆλ‹€. ν•˜μ§€λ§Œ μ—¬κΈ°κ°€ 더 μ€‘μš”ν•  것 κ°™μŠ΅λ‹ˆλ‹€. 예:

fn foo<T, U>() -> T { }

fn main() {
    let x: i32 = foo();
}

μ—¬κΈ°μ„œ μš°λ¦¬λŠ” T λ₯Ό μ•Œκ³  μžˆμ§€λ§Œ U λŠ” λͺ¨λ¦…λ‹ˆλ‹€. foo::<_, XXX> λ₯Ό μ œμ•ˆν•΄μ•Ό ν•©λ‹ˆκΉŒ? μ‚¬μš©μžκ°€ μ œκ³΅ν•΄μ•Ό ν•˜λŠ” ν•­λͺ©μž„을 λ‚˜νƒ€λ‚΄κΈ° μœ„ν•΄ XXX λ₯Ό μ–΄λ–»κ²Œ ν‘œν˜„ν•©λ‹ˆκΉŒ?

cc @cengizIO -- μΆ”κ΅¬ν•˜λŠ” 것이 μž¬λ―ΈμžˆμŠ΅λ‹ˆκΉŒ?

cc @estebank @jonathandturner -- ν‘œν˜„ 방법에 λŒ€ν•œ 생각은?

A-diagnostics C-enhancement E-needs-mentor T-compiler WG-compiler-errors

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

μ „λ₯˜ 좜λ ₯:

error[E0282]: type annotations needed
  --> $DIR/xxx.rs:xx:xx
   |
14 |     let x: i32 = foo();
   |                  ^^^ cannot infer type for `U`
   |
   = note: type annotations or generic parameter binding required

일뢀 μ˜΅μ…˜:

error[E0282]: type annotations needed
  --> $DIR/xxx.rs:xx:xx
   |
14 |     let x: i32 = foo();
   |                  ^^^ cannot infer type for `U`
   |
   = note: type annotations or generic parameter binding required
   = note: generic parameter `U` needs to be specified for foo::<T, U>()
error[E0282]: type annotations needed
  --> $DIR/xxx.rs:xx:xx
   |
14 |     let x: i32 = foo();
   |                  ^^^ cannot infer type for `U`
   |                  |
   |                  you can specify the generic parameter here using `foo::<_, U>()`
   |
   = note: generic parameter `U` needs to be specified for `fn foo::<T, U>() -> T`
error[E0282]: type annotations needed
  --> $DIR/xxx.rs:xx:xx
   |
14 |     let x: i32 = foo();
   |                  ^^^ cannot infer type for `U` in `fn foo<T, U>() -> T`
   |                  |
   |                  specify `U` using `foo::<_, U>()`
   |

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

μ•ˆλ…•ν•˜μ„Έμš” @nikomatsakis

이 μž‘μ—…μ„ μˆ˜ν–‰ν•˜κ² μŠ΅λ‹ˆλ‹€.

λ‹€μ‹œ ν•œλ²ˆ κ°μ‚¬ν•©λ‹ˆλ‹€!

μ „λ₯˜ 좜λ ₯:

error[E0282]: type annotations needed
  --> $DIR/xxx.rs:xx:xx
   |
14 |     let x: i32 = foo();
   |                  ^^^ cannot infer type for `U`
   |
   = note: type annotations or generic parameter binding required

일뢀 μ˜΅μ…˜:

error[E0282]: type annotations needed
  --> $DIR/xxx.rs:xx:xx
   |
14 |     let x: i32 = foo();
   |                  ^^^ cannot infer type for `U`
   |
   = note: type annotations or generic parameter binding required
   = note: generic parameter `U` needs to be specified for foo::<T, U>()
error[E0282]: type annotations needed
  --> $DIR/xxx.rs:xx:xx
   |
14 |     let x: i32 = foo();
   |                  ^^^ cannot infer type for `U`
   |                  |
   |                  you can specify the generic parameter here using `foo::<_, U>()`
   |
   = note: generic parameter `U` needs to be specified for `fn foo::<T, U>() -> T`
error[E0282]: type annotations needed
  --> $DIR/xxx.rs:xx:xx
   |
14 |     let x: i32 = foo();
   |                  ^^^ cannot infer type for `U` in `fn foo<T, U>() -> T`
   |                  |
   |                  specify `U` using `foo::<_, U>()`
   |

이 λ©”λͺ¨λŠ” 특히 λ‚΄ λˆˆμ„ 반짝거리게 λ§Œλ“­λ‹ˆλ‹€.

   = note: type annotations or generic parameter binding required

ν™•μ‹€νžˆ μ œκ±°ν•©μ‹œλ‹€. :)

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