Skip to content

Type identification with (match sth.get_mut()) of collections with structs #115935

Open
@GeForceLegend

Description

@GeForceLegend

I tried this code:

struct ShaderGroup {
    vertex: Option<String>,
    geometry: Option<String>,
    fragment: Option<String>,
}

fn main() {
    let mut shader_groups = HashMap::new();
    match shader_groups.get_mut("Test") {
        Some(shader_group) => {
            // Remove this line will make everything fine
            shader_group.vertex = None;
        }
        None => {
            shader_groups.insert("Test".to_owned(), ShaderGroup { vertex: None, geometry: None, fragment: None });
        }
    }
}

I expected to see this happen: Rust automanticly knows the type of shader_groups is HashMap<String, ShaderGroup>

Instead, this happened: If I don't remove the line with comment, Rust says "type annotations needed for HashMap<K, V>" on the definition of shader_group, and says "no field vertex on type &mut _" on the commented line. But if I remove the commented line, Rust then knows the type of shader_groups and compile will be successful.

Meta

rustc --version --verbose:

rustc 1.74.0-nightly (203c57dbe 2023-09-17)
binary: rustc
commit-hash: 203c57dbe20aee67eaa8f7be45d1e4ef0b274109
commit-date: 2023-09-17
host: x86_64-pc-windows-msvc
release: 1.74.0-nightly
LLVM version: 17.0.0
Backtrace

No back trace since it cannot even pass the compile :(

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-inferenceArea: Type inferenceC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions