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

Mismatched types shows 2 equal types #104285

Closed
marcoow opened this issue Nov 11, 2022 · 3 comments
Closed

Mismatched types shows 2 equal types #104285

marcoow opened this issue Nov 11, 2022 · 3 comments
Labels
C-bug Category: This is a bug.

Comments

@marcoow
Copy link

marcoow commented Nov 11, 2022

I'm trying to add an async function argument to a function and run into a situation where it get a mismatched types error although the types I'm presented in the error message are actually both identical:

   = note: expected trait `for<'r> <for<'r> fn(&'r str, std::string::String) -> impl for<'r> Future<Output = u16> {request_sendgrid} as FnOnce<(&'r str, std::string::String)>>`
              found trait `for<'r> <for<'r> fn(&'r str, std::string::String) -> impl for<'r> Future<Output = u16> {request_sendgrid} as FnOnce<(&'r str, std::string::String)>>`

I'm aware I'm almost 100% certainly doing sth. wrong, still the note there seems probably wrong as well?

I tried this code:

pub async fn send_message<Fut>(payload: Payload, api_key: &str, sendgrid: impl FnOnce(&str, String) -> Fut) -> Result<Response>
where
Fut: Future<Output = u16>, {
    let message = payload.message.trim();
    let message = if !message.is_empty() { message } else { "–" };

    let data = json!({
        "personalizations": [{
            "to": [
                { "email": "contact@domain.tld", "name": "My Domain" }
            ]}
        ],
        "from": { "email": "no-reply@domain.tld", "name": format!("{} via domain.tld", payload.name) },
        "reply_to": { "email": payload.email, "name": payload.name },
        "subject": "Inquiry",
        "content": [{
            "type": "text/plain",
            "value": message
        }]
    });

    let client = reqwest::Client::new();
    let result = client
        .post("https://api.sendgrid.com/v3/mail/send")
        .header("Authorization", format!("Bearer {}", api_key))
        .header("Content-Type", "application/json")
        .body(data.to_string())
        .send()
        .await;

    match result {
        Ok(response) => match response.status() {
            reqwest::StatusCode::ACCEPTED => Response::ok(""),
            _ => Response::error("Bad Gateway", 502),
        },
        Err(_) => Response::error("Internal Server Error", 500),
    }
}

async fn request_sendgrid(api_key: &str, data: String) -> u16 {
    let client = reqwest::Client::new();
    let result = client
        .post("https://api.sendgrid.com/v3/mail/send")
        .header("Authorization", format!("Bearer {}", api_key))
        .header("Content-Type", "application/json")
        .body(data)
        .send()
        .await;

    match result {
        Ok(response) => response.status().as_u16(),
        Err(_) => 500u16,
    }
}

I expected to see this happen: 2 different types in the error message

Instead, this happened: 2 equal types in the error message

Meta

rustc --version --verbose:

rustc 1.64.0 (a55dd71d5 2022-09-19)
binary: rustc
commit-hash: a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52
commit-date: 2022-09-19
host: aarch64-apple-darwin
release: 1.64.0
LLVM version: 14.0.6
Backtrace

   Compiling mainmatter-website-mailer v0.0.0 (/Users/marcoow/Code/mainmatter-website-mailer)
error[E0308]: mismatched types
  --> src/lib.rs:43:32
   |
43 |                 Ok(payload) => send_message(payload, &api_key, &request_sendgrid).await,
   |                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
   |
   = note: expected trait `for<'r> <for<'r> fn(&'r str, std::string::String) -> impl for<'r> Future<Output = u16> {request_sendgrid} as FnOnce<(&'r str, std::string::String)>>`
              found trait `for<'r> <for<'r> fn(&'r str, std::string::String) -> impl for<'r> Future<Output = u16> {request_sendgrid} as FnOnce<(&'r str, std::string::String)>>`
note: the lifetime requirement is introduced here
  --> src/lib.rs:53:104
   |
53 | pub async fn send_message<Fut>(payload: Payload, api_key: &str, sendgrid: impl FnOnce(&str, String) -> Fut) -> Result<Response>
   |                                                                                                        ^^^

For more information about this error, try `rustc --explain E0308`.
error: could not compile `mainmatter-website-mailer` due to previous error

@marcoow marcoow added the C-bug Category: This is a bug. label Nov 11, 2022
@Noratrieb
Copy link
Member

Thanks, that does look like a bug in the error message. Could you create a more minimal example for easier reproduction, or at least share the full code needed for reproducing the error (if your code is public, you could create a branch for this isse with the broken state)?

@lukas-code
Copy link
Member

This is the same error message as #102400

@marcoow
Copy link
Author

marcoow commented Nov 11, 2022

nvm, closing 👍

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

3 participants