Skip to content

Adding type annotation equivalent to existing type changes code semantics #35919

@F001

Description

@F001

This piece of code can't compile:

fn main()
{
    let y = &mut 2i32;
    {
        let x = y;
        *x = 3;
    }
    println!("{}", y);
}

But, if we add the type annotation, it compiles:

fn main()
{
    let y = &mut 2i32;
    {
        let x: &mut i32 = y;
        *x = 3;
    }
    println!("{}", y);
}

I guess the later form triggers "auto-deref" mechanism, which equals to:

let x = &mut *y;

But this behaviour is still very confusing. Type annotation or not doesn't change the type of x at all. Type of x is always &mut i32. But they lead to different compile result. I have no idea whether should we fix it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-type-systemArea: Type systemC-enhancementCategory: An issue proposing an enhancement or a PR with one.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types 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