Index chosen over IndexMut when coercing index type #72002
Labels
A-inference
Area: Type inference
A-trait-system
Area: Trait system
C-bug
Category: This is a bug.
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
We've run into an issue where we have a type that implements both
Index<&str>
andIndexMut<&str>
. Passing an index value of&String
causes rustc to chooseIndex
instead ofIndexMut
, even if a mutable reference is required. If we first callString::as_str
and use that as the index,IndexMut
is 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 invokeIndexMut
onv
sinceboo()
expects a mutable reference.Instead, this happened:
v[&field]
invokedIndex
and complained thatIndexMut
isn'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
The text was updated successfully, but these errors were encountered: