-
-
Notifications
You must be signed in to change notification settings - Fork 14.7k
Index chosen over IndexMut when coercing index type #72002
Copy link
Copy link
Closed
Labels
A-inferenceArea: Type inferenceArea: Type inferenceA-trait-systemArea: Trait systemArea: Trait systemC-bugCategory: This is a bug.Category: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Metadata
Metadata
Assignees
Labels
A-inferenceArea: Type inferenceArea: Type inferenceA-trait-systemArea: Trait systemArea: Trait systemC-bugCategory: This is a bug.Category: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Type
Fields
Give feedbackNo fields configured for issues without a type.
We've run into an issue where we have a type that implements both
Index<&str>andIndexMut<&str>. Passing an index value of&Stringcauses rustc to chooseIndexinstead ofIndexMut, even if a mutable reference is required. If we first callString::as_strand use that as the index,IndexMutis chosen, as expected.Example repro by @idubrov (see https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=9f0e5d703465d4b87218cac8b1eea608):
I expected to see this happen:
v[&field]should invokeIndexMutonvsinceboo()expects a mutable reference.Instead, this happened:
v[&field]invokedIndexand complained thatIndexMutisn't implemented even though it is:Meta
This is reproducible on stable Rust 1.41.0 and 1.43.0 and
1.45.0-nightly (2020-05-06 1836e3b42a5b2f37fd79).cc: @estebank