rust-lang / rust-lang/rust

Instantiating Same Type via Function vs Directly => Compiler Thinks They Are Different Types.

Open
#124,799 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics C-bug D-papercut T-compiler
Dominant language
Rust
Stars
119k
Forks
16.2k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the workspace and crate layout shown in Cargo.toml, then reproduce the type mismatch by running cargo check on the provided nice/src/main.rs example. Compare the direct and dependency-qualified Hello definitions and investigate why the nice crate is compiled multiple times. Done means the example checks successfully or the diagnostic clearly identifies the required correction.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.