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

Add IndexRef and IndexMut #11977

Closed
wants to merge 1 commit into from
Closed

Conversation

wycats
Copy link
Contributor

@wycats wycats commented Feb 1, 2014

This patch adds two new traits that implement aspects of index
overloading.

It retains the existing Index trait, which takes an &Element and returns
a Result. This is useful for objects whose indexing behavior is to
return simple values like booleans or numbers.

It adds a new IndexRef trait, which takes an &Element and returns a
&Result. This is useful for objects whose indexing behavior is to return
larger values where cloning would be awkward, inappropriate or
impossible.

It also adds a new IndexMut trait, which takes self as &mut self, takes
an &Element and returns a &mut Result. This is useful for objects that
want to allow mutations to their indexed results.

The Index and IndexRef trait both use the index method name, so that
only one of the two can be implemented by a given struct. The IndexMut
trait uses the index_mut method name, so it can be implemented
alongside one of Index or IndexRef.

This patch adds two new traits that implement aspects of index
overloading.

It retains the existing Index trait, which takes an &Element and returns
a Result. This is useful for objects whose indexing behavior is to
return simple values like booleans or numbers.

It adds a new IndexRef trait, which takes an &Element and returns a
&Result. This is useful for objects whose indexing behavior is to return
larger values where cloning would be awkward, inappropriate or
impossible.

It also adds a new IndexMut trait, which takes self as &mut self, takes
an &Element and returns a &mut Result. This is useful for objects that
want to allow mutations to their indexed results.

The Index and IndexRef trait both use the `index` method name, so that
only one of the two can be implemented by a given struct. The IndexMut
trait uses the `index_mut` method name, so it can be implemented
alongside one of Index or IndexRef.
@wycats
Copy link
Contributor Author

wycats commented Feb 1, 2014

I'm not indicating that this PR closes #6515, because I am waiting on feedback to this PR before I continue on to IndexAssign.

@brson
Copy link
Contributor

brson commented Feb 1, 2014

Woo! Glad to see this making progress. 🚂

@pcwalton
Copy link
Contributor

pcwalton commented Feb 2, 2014

r? @nikomatsakis

@nikomatsakis
Copy link
Contributor

@pcwalton yes, I plan to review today / tonight. It seems the work we land here will also pave the way for overloadable deref, since many of the complications are the same.

@nikomatsakis
Copy link
Contributor

Huh. This is ... surprisingly short! I have to digest it again. I think it's clear this is not quite how I see things working longer term but it might be a useful short step, since having a functional Index trait (even if it is evolving) would greatly help us towards getting a functional Vec type and so on.

@brson
Copy link
Contributor

brson commented Feb 11, 2014

@nikomatsakis Still hoping to hear your opinion.

@nikomatsakis
Copy link
Contributor

@nikomatsakis Still hoping to hear your opinion.

I did talk to @wycats on IRC at one point. I am trying to write up a
list of what I think needs to be done and so on in the bug itself.

@alexcrichton
Copy link
Member

Closing due to inactivity, but I'd love to see this get done!

flip1995 pushed a commit to flip1995/rust that referenced this pull request Dec 28, 2023
…hearth

don't visit nested bodies in `is_const_evaluatable`

Fixes rust-lang#11939

This ICE happened in `if_let_some_else_none`, but the root problem is in one of the utils that it uses.
It is (was) possible for `is_const_evalutable` to visit nested bodies which would lead to it trying to get the type of one of the expressions with the wrong typeck table, which won't have the type stored.

Notably, for the expression `Bytes::from_static(&[0; 256 * 1024]);` in the linked issue, the array length is an anonymous const in which type checking happens on its own, so we can't use the typeck table of the enclosing function in there.

Visiting nested bodies is also not needed for checking whether an expression can be const, so I think it's safe to ignore just ignore them altogether.

changelog: Fix ICE when checking for constness in nested bodies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants