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

More precisely point out what is immutable for E0596 #113924

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

chenyukang
Copy link
Member

Fixes #113842

@rustbot
Copy link
Collaborator

rustbot commented Jul 21, 2023

r? @compiler-errors

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 21, 2023
@rust-log-analyzer

This comment has been minimized.

@chenyukang chenyukang force-pushed the yukang-fix-113842-ref branch 2 times, most recently from ed4fe97 to bb468d2 Compare July 23, 2023 14:50
Comment on lines 24 to 28
help: this expression is of type `&str`, which is an immutable reference
--> $DIR/issue-76510.rs:5:34
|
LL | const S: &'static mut str = &mut " hello ";
| ^^^^^^^^^
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not exactly sure what this is trying to demonstrate.

The issue here is a subtle detail -- &mut X will try to autoderef X to match it to the type it needs. From the surface, &mut "" looks like it should give you a type of &mut &'static str, which is valid, but it's not valid because we autoderef it to make it into a &mut str:

fn foo() {
    // works
    let x           = &mut "";
    // doesnt work
    let y: &mut str = &mut "";
}

If you're not pointing this behavior out, then I don't really know if it's more helpful or more misleading to say "this is an expr of type &str".

Copy link
Member Author

@chenyukang chenyukang Aug 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how we don't give this hint when X is a ExprKind::Lit(_) ?
since seems it does not provide more details.

@compiler-errors compiler-errors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 26, 2023
@chenyukang chenyukang force-pushed the yukang-fix-113842-ref branch 2 times, most recently from dde0b99 to a922780 Compare August 5, 2023 04:38
@Dylan-DPC Dylan-DPC added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 22, 2023
Copy link
Member

@compiler-errors compiler-errors left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still don't know if I like the presentation of this diagnostic. It still doesn't explain that the problem here is the autoderef behavior of &/&mut -- you're allowed to take a reference to &mut &Foo -- the problem is that there's a deref being inserted there that's not present in the code, and I think a diagnostic improvement here should explain this. Without that, you're really just adding more labels.

@compiler-errors
Copy link
Member

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 2, 2023
@chenyukang
Copy link
Member Author

I think one of the confusion from the origin issue is that the span contains the &mut, this PR improved it.

Considering autoderef, most newcomers may overlook the subtle details, I'm not sure whether it's helpful to refer to it.

@Dylan-DPC Dylan-DPC added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 12, 2024
@compiler-errors
Copy link
Member

Hi @chenyukang: I still don't believe this PR makes the underlying issue more easy to understand. It just points out that the pointee of the &mut ref is immutable. That alone does not explain what the root cause of the issue, which is that autoderef will attempt to coerce &mut &T to &mut T.

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 12, 2024
@bors
Copy link
Contributor

bors commented Feb 20, 2024

☔ The latest upstream changes (presumably #120576) made this pull request unmergeable. Please resolve the merge conflicts.

@Dylan-DPC
Copy link
Member

@chenyukang any updates on this? thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

More precisely point out what is immutable, in E0596 "cannot borrow data in a & reference as mutable"
7 participants