Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"if let WPARAM(VK_ESCAPE.0 as usize) = wparam"makes a compile error #104996

Closed
ghost opened this issue Nov 28, 2022 · 1 comment
Closed

"if let WPARAM(VK_ESCAPE.0 as usize) = wparam"makes a compile error #104996

ghost opened this issue Nov 28, 2022 · 1 comment
Labels
C-bug Category: This is a bug.

Comments

@ghost
Copy link

ghost commented Nov 28, 2022

I tried this code:

图片

unsafe extern "system" fn window_proc(hwnd:HWND,msg:u32,wparam:WPARAM,lparam:LPARAM) -> LRESULT{
    let mut result = LRESULT(0);
    match msg {
        WM_KEYDOWN =>{
            let _a:u16 = VK_ESCAPE.0; // VK_ESCAPE(u16)
            let _b:WPARAM = WPARAM(1usize); // WPARAM(usize)

            if let WPARAM(VK_ESCAPE.0 as usize) = wparam { //why
                DestroyWindow(hwnd);
            }
        }
        _ => {
            result = DefWindowProcW(hwnd, msg, wparam, lparam);
        }
    }
    return result;
}

I expected to see this happen: explanation

Instead, this happened: explanation

error: expected one of `)`, `,`, `@`, or `|`, found `.`
  --> src\main.rs:33:36
   |
33 |             if let WPARAM(VK_ESCAPE.0 as usize) = wparam {
   |                                    ^
   |                                    |
   |                                    expected one of `)`, `,`, `@`, or `|`
   |                                    help: missing `,`

error: expected identifier, found keyword `as`
  --> src\main.rs:33:39
   |
33 |             if let WPARAM(VK_ESCAPE.0 as usize) = wparam {
   |                                       ^^ expected identifier, found keyword

error: expected one of `)`, `,`, `...`, `..=`, `..`, or `|`, found keyword `as`
  --> src\main.rs:33:39
   |
33 |             if let WPARAM(VK_ESCAPE.0 as usize) = wparam {
   |                                      -^^ expected one of `)`, `,`, `...`, `..=`, `..`, or `|`
   |                                      |
   |                                      help: missing `,`

error: expected one of `)`, `,`, `@`, or `|`, found `usize`
  --> src\main.rs:33:42
   |
33 |             if let WPARAM(VK_ESCAPE.0 as usize) = wparam {
   |                                         -^^^^^ expected one of `)`, `,`, `@`, or `|`
   |                                         |
   |                                         help: missing `,`

warning: unused import: `h`
 --> src\main.rs:2:135
  |
2 | use windows::{Win32::{System::LibraryLoader::GetModuleHandleW, UI::WindowsAndMessaging::{WNDCLASSEXW, DefWindowProcW}}, core::PCWSTR, h};        
  |                                                                                                                                       ^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: unreachable expression
  --> src\main.rs:18:21
   |
17 |         cbClsExtra: todo!(),
   |                     ------- any code following this expression is unreachable
18 |         cbWndExtra: todo!(),
   |                     ^^^^^^^ unreachable expression
   |
   = note: `#[warn(unreachable_code)]` on by default
   = note: this warning originates in the macro `todo` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0023]: this pattern has 4 fields, but the corresponding tuple struct has 1 field
     --> src\main.rs:33:27
      |
33    |             if let WPARAM(VK_ESCAPE.0 as usize) = wparam {
      |                           ^^^^^^^^^ ^ ^^ ^^^^^ expected 1 field, found 4
      |
     ::: C:\Users\zzwxh\.cargo\registry\src\mirrors.ustc.edu.cn-61ef6e0cd06fb9b8\windows-0.43.0\src\Windows\Win32\Foundation\mod.rs:21158:19
      |
21158 | pub struct WPARAM(pub usize);
      |                   --------- tuple struct has 1 field

For more information about this error, try `rustc --explain E0023`.
warning: `learn_dx11` (bin "learn_dx11") generated 2 warnings
error: could not compile `learn_dx11` due to 5 previous errors; 2 warnings emitted

Meta

rustc --version --verbose:

rustc 1.65.0 (897e37553 2022-11-02)
binary: rustc
commit-hash: 897e37553bba8b42751c67658967889d11ecd120
commit-date: 2022-11-02
host: x86_64-pc-windows-msvc
release: 1.65.0
LLVM version: 15.0.0
Backtrace

<backtrace>

@ghost ghost added the C-bug Category: This is a bug. label Nov 28, 2022
@fmease
Copy link
Member

fmease commented Nov 28, 2022

Between the (if) let and the =, there needs to be a pattern (see The Book, The Reference). VK_ESCAPE.0 as usize is an expression, not a pattern.

To fix the error, assign the expression to a constant like this:

const ESCAPE: usize = VK_ESCAPE.0 as _;

if let WPARAM(ESCAPE) = wparam {
    DestroyWindow(hwnd);
}

@ghost ghost closed this as completed Nov 28, 2022
@fmease fmease closed this as not planned Won't fix, can't repro, duplicate, stale Jan 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

1 participant