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

Same type, but compiler says it's different types. #124799

Open
amab8901 opened this issue May 6, 2024 · 1 comment
Open

Same type, but compiler says it's different types. #124799

amab8901 opened this issue May 6, 2024 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-papercut Diagnostics: An error or lint that needs small tweaks.

Comments

@amab8901
Copy link
Contributor

amab8901 commented May 6, 2024

I tried this code:

image

Cargo.toml

cargo-features = ["codegen-backend"]

[profile.dev]
codegen-backend = "cranelift"

[workspace]
resolver = "2"
members = ["nice"]

nice/Cargo.toml:

[package]
name = "nice"

[dependencies]

nice/src/kiwi/kiwi_file.rs

#![allow(clippy::let_and_return)]

#[derive(Debug, Clone, PartialEq)]
pub struct Hello {
    pub field: String,
}

pub fn hello_func() -> Hello {
    let hello = Hello {
        field: String::default(),
    };
    hello
}

nice/src/kiwi.rs

pub mod kiwi_file;

nice/src/lib.rs

pub mod kiwi;

nice/src/main.rs:

extern crate nice;

use nice::kiwi::kiwi_file::hello_func;

use crate::kiwi::kiwi_file::Hello;

pub mod kiwi;

fn main() {
    let func_hello = hello_func();

    let direct_hello = Hello {
        field: String::default(),
    };

    assert_eq!(func_hello, direct_hello);
}

I expected to see this happen:

cargo check works without throwing any errors

Instead, this happened:

cargo check produces this error:

error[E0308]: mismatched types
  --> nice/src/main.rs:16:28
   |
16 |     assert_eq!(func_hello, direct_hello);
   |                            ^^^^^^^^^^^^ expected `nice::kiwi::kiwi_file::Hello`, found `kiwi::kiwi_file::Hello`
   |
   = note: `kiwi::kiwi_file::Hello` and `nice::kiwi::kiwi_file::Hello` have similar names, but are actually distinct types
note: `kiwi::kiwi_file::Hello` is defined in the current crate
  --> nice/src/kiwi/kiwi_file.rs:4:1
   |
4  | pub struct Hello {
   | ^^^^^^^^^^^^^^^^
note: `nice::kiwi::kiwi_file::Hello` is defined in crate `nice`
  --> /home/amir-abdin/src/nice/nice/src/kiwi/kiwi_file.rs:4:1
   |
4  | pub struct Hello {
   | ^^^^^^^^^^^^^^^^
   = note: the crate `nice` is compiled multiple times, possibly with different configurations

Meta

rustc --version --verbose:

rustc 1.77.0-nightly (d5fd09972 2024-01-22)
binary: rustc
commit-hash: d5fd0997291ca0135401a39dff25c8a9c13b8961
commit-date: 2024-01-22
host: x86_64-unknown-linux-gnu
release: 1.77.0-nightly
LLVM version: 17.0.6

@amab8901 amab8901 added the C-bug Category: This is a bug. label May 6, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 6, 2024
@saethlin
Copy link
Member

saethlin commented May 6, 2024

The error is correct, but a bit unhelpful. There are two crates in this build called nice, one is a lib crate whose root is src/lib.rs and the other is a bin crate whose root is src/main.rs.

You can fix this problem by removing the mod declaration from src/main.rs and using everything through nice:: (which is the lib crate) not crate:: (which is the bin crate).

I'd prefer to see this diagnostic mention that the two builds are as a bin crate and a lib crate. It would be awesome if it could detect that the two crates are declaring the same module, because I don't see any good reason to do that.

@saethlin saethlin added A-diagnostics Area: Messages for errors, warnings, and lints D-papercut Diagnostics: An error or lint that needs small tweaks. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 6, 2024
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-bug Category: This is a bug. D-papercut Diagnostics: An error or lint that needs small tweaks.
Projects
None yet
Development

No branches or pull requests

3 participants