-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Mutability issue while indexing static immutable array #46095
Comments
Highly related question from Stack Overflow: trait Bar {
fn bar(&mut self);
}
fn foo(a1: &mut Bar, j: usize) {
let a = [a1];
a[0].bar(); //compilation ok
a[j % 2].bar();
}
fn main() {}
|
Moreover, |
Conversation on IRC points to this being a difference in how builtin indexing works vs overloaded indexing. My cleaned up version of the transcript:
Highly related to #33903, which will cause these types of situations to occur more frequently. |
As a workaround, you can write an explicit type of fn foo<'a>(a1: &'a mut Bar, a2: &'a mut Bar) {
let a = [a1, a2];
for i in 0..42 {
let index2: usize = i % 2;
a[i].bar();
a[index2].bar();
}
} fn foo(a1: &mut Bar, j: usize) {
let a = [a1];
a[0].bar();
let x: usize = j % 2;
a[x].bar();
} |
trait Bar {
fn bar(&mut self);
}
fn foo(a1: &mut Bar, j: usize) {
//let a = [a1];
// why doesn't vec work?
let a = vec![a1];
a[0].bar();
let x: usize = j % 2;
a[x].bar();
}
fn main() {} |
Fix built-in indexing not being used where index type wasn't "obviously" usize Fixes #33903 Fixes #46095 This PR was made possible thanks to the generous help of @eddyb Following the example of binary operators, builtin checking for indexing has been moved from the typecheck stage to a writeback stage, after type constraints have been resolved.
Hello, today we've found some weird behaviour of array indexing.
Here is the example
Code snippet:
The question is, why is the indexing operator behaves depending on whether we evaluate
index
in place or not.The text was updated successfully, but these errors were encountered: