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

Suggest removal of borrow in index when appropriate #117913

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

estebank
Copy link
Contributor

When encountering

    let a = std::collections::HashMap::<String,String>::new();
    let s = "hello";
    let _b = &a[&s];

suggest let _b = &a[s];.

Fix #66023.

r? @compiler-errors

@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 Nov 14, 2023
self.lookup_indexing(expr, base, base_t, idx, idx_t)
{
let (_ty, err) =
self.demand_coerce_diag(idx, idx_t, index_ty, None, AllowTwoPhase::No);
Copy link
Member

Choose a reason for hiding this comment

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

I think you should manually recreate the logic in lookup_indexing, and if you need to process obligations, you should throw them into your own ObligationCtxt.

As for the demand_coerce call, I think it's probably best for us to approximate it with a ObligationCtxt::eq call instead, to again avoid poisoning our own fulfillment context.

Essentially what I want to avoid is that fulfillment_ctxt.select_where_possible. It's not great, and unless you can convince me that there's a good reason that we can't approximate it with less invasive machinery, then I think we should try our best to avoid it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What do you think of creating a new fulfillment context for this check instead?

Copy link
Member

Choose a reason for hiding this comment

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

either works, though I think ObligationCtxt has an easier to use API

@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 Nov 16, 2023
When encountering

```rust
    let a = std::collections::HashMap::<String,String>::new();
    let s = "hello";
    let _b = &a[&s];
```
suggest `let _b = &a[s];`.

Fix rust-lang#66023.
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.

Sorry for the churn, but this is still not totally side-effect-less

let mut autoderef = self.autoderef(base.span, base_t);
let mut result = None;
while result.is_none() && autoderef.next().is_some() {
result = self.try_index_step(expr, base, &autoderef, idx_t, idx);
Copy link
Member

Choose a reason for hiding this comment

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

try_index_step still registers things into the fnctxt's fulfullment context :(

I think it's simpler to be creating your own Index trait obligation here -- the only thing that you'd be losing is the [u8; N] -> [u8] deref step, which I guess you could replicate here.

@bors
Copy link
Contributor

bors commented May 10, 2024

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

@Dylan-DPC
Copy link
Member

@estebank 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.

Supplying an &&str when an &str is expected missing a suggestion
5 participants