Skip to content

Type alias do not used in functions #76606

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

Closed
juev opened this issue Sep 11, 2020 · 3 comments
Closed

Type alias do not used in functions #76606

juev opened this issue Sep 11, 2020 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked.

Comments

@juev
Copy link

juev commented Sep 11, 2020

First, create new lib with type alias and function:

pub type OperationType = String;

lazy_static! {
    pub static ref OPERATION_TYPE_BUY: OperationType = String::from("Buy");
}

    pub fn limit_order(
        &self,
        account_id: &str,
        figi: &str,
        lots: i64,
        operation: OperationType,
        price: f64,
    ) -> Result<PlacedOrder> {
    ...
    }

Then, we use this crate in our program:

    let limit_order =
         rest_client.limit_order("", "BBG000BB88K4", 2, OPERATION_TYPE_BUY, 11 as f64)?;

OPERATION_TYPE_BUY has type OperationType, this type we expect in limit_order function, but we have error:

error[E0308]: mismatched types
  --> src/main.rs:38:57
   |
38 |          rest_client.limit_order("", "BBG000BB88K4", 2, OPERATION_TYPE_BUY, 11 as f64)?;
   |                                                         ^^^^^^^^^^^^^^^^^^
   |                                                         |
   |                                                         expected struct `std::string::String`, found struct `invest_openapi_rs_sdk::OPERATION_TYPE_BUY`
   |                                                         help: try using a conversion method: `OPERATION_TYPE_BUY.to_string()`

error: aborting due to previous error

If our function expect OperationType type, why rust asked me convert to String?

Meta

rustc --version --verbose:

$ rustc --version --verbose
rustc 1.46.0 (04488afe3 2020-08-24)
binary: rustc
commit-hash: 04488afe34512aa4c33566eb16d8c912a3ae04f9
commit-date: 2020-08-24
host: x86_64-apple-darwin
release: 1.46.0
LLVM version: 10.0
@juev juev added the C-bug Category: This is a bug. label Sep 11, 2020
@Mark-Simulacrum
Copy link
Member

This is, to some extent, a diagnostics bug (#17164).

The problem in this case is that lazy_static generates a static with a unique type, invest_openapi_rs_sdk::OPERATION_TYPE_BUY, not String (aliases to OperationType in this snippet). You can call clone() on the lazy_static unique type to get a copy of the String in it.

@Mark-Simulacrum Mark-Simulacrum added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. and removed C-bug Category: This is a bug. labels Sep 11, 2020
@juev
Copy link
Author

juev commented Sep 11, 2020

@Mark-Simulacrum thank you!

If I use clone:

    let limit_order =
         rest_client.limit_order("", "BBG000BB88K4", 2, OPERATION_TYPE_BUY.clone(), 11 as f64)?;

it's work. But, its the same as

    let limit_order =
         rest_client.limit_order("", "BBG000BB88K4", 2, OPERATION_TYPE_BUY.to_string(), 11 as f64)?;

If I use type alias in function parameter, why it not the same unique type, what used in lazy_static?

@Mark-Simulacrum
Copy link
Member

I don't quite follow your question.

lazy_static produces a static with an opaque type (it's nameable, but generally you shouldn't need to). That type is then not String, and provides only &String access through deref. You can read some more about this in the docs here https://docs.rs/lazy_static/1.4.0/lazy_static/#semantics

I would recommend reaching out on users.rust-lang.org for further help with this question; I'm going to close this as I think it's essentially a duplicate of the general problem with type aliases, #17164.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked.
Projects
None yet
Development

No branches or pull requests

2 participants