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

change tuple indexing #4186

Closed

Conversation

brymer-meneses
Copy link
Contributor

this changes the tuple index from tuple[0] to tuple.0 in accordance with the accepted propsal #3646

@github-actions github-actions bot requested a review from josh11b August 3, 2024 06:02
Copy link
Contributor

@josh11b josh11b left a comment

Choose a reason for hiding this comment

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

Some initial feedback. I'll look again once these are addressed.

toolchain/check/handle_name.cpp Outdated Show resolved Hide resolved
toolchain/check/handle_name.cpp Outdated Show resolved Hide resolved
toolchain/check/member_access.cpp Outdated Show resolved Hide resolved
toolchain/check/testdata/index/index_not_literal.carbon Outdated Show resolved Hide resolved
toolchain/parse/handle_period.cpp Outdated Show resolved Hide resolved
Comment on lines 495 to 498
CARBON_DIAGNOSTIC(
TupleWrongIndexSyntax, Error,
"Type `{0}` does not support array indexing. Did you mean tuple.0?",
SemIR::TypeId);
Copy link
Contributor

Choose a reason for hiding this comment

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

Should add a test that verifies that this diagnostic is being produced correctly. Also, isn't this the opposite case, where you are using tuple indexing on a non-tuple value?

Copy link
Contributor Author

@brymer-meneses brymer-meneses Aug 6, 2024

Choose a reason for hiding this comment

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

Also, isn't this the opposite case, where you are using tuple indexing on a non-tuple value?

My bad, for some reason I was thinking of tuple[0]. Should we add a note telling the user the right way to index a tuple?

Copy link
Contributor

Choose a reason for hiding this comment

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

That would be great, but I wouldn't say it is required.

toolchain/check/member_access.cpp Outdated Show resolved Hide resolved
toolchain/check/member_access.cpp Outdated Show resolved Hide resolved
toolchain/check/member_access.cpp Outdated Show resolved Hide resolved
@brymer-meneses
Copy link
Contributor Author

brymer-meneses commented Aug 6, 2024

I think everything should be resolved by now, except the parts wherein I am confused on what to do.

@brymer-meneses brymer-meneses requested a review from josh11b August 7, 2024 06:52
@brymer-meneses
Copy link
Contributor Author

Also, apologies if I'm unnecessarily pinging you by re-requesting a review even if you said that you'd review this once everything gets resolved.

Copy link
Contributor

@jonmeow jonmeow left a comment

Choose a reason for hiding this comment

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

Thanks, this looks really good! Josh had asked me to take a look, and I think the logic looks correct. I did see some spots though where I think it may be helpful to adjust API use, just subtle toolchain things.

// CHECK:STDERR: fail_non_tuple_index.carbon:[[@LINE+3]]:20: ERROR: Type `[i32; 1]` does not support tuple indexing. Only tuples can be indexed that way.
// CHECK:STDERR: var first: i32 = non_tuple.0;
// CHECK:STDERR: ^~~~~~~~~~~
var first: i32 = non_tuple.0;
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like the other tuple indexing tests are under testdata/index. Might that be a better place for this test?

Copy link
Contributor Author

@brymer-meneses brymer-meneses Aug 8, 2024

Choose a reason for hiding this comment

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

I didn't notice that there was a similar test named toolchain/check/testdata/fail_non_tuple_access. I think I'll just merge this with that.

Copy link
Contributor

Choose a reason for hiding this comment

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

Had you meant to remove this?

toolchain/lower/testdata/index/array_element_access.carbon Outdated Show resolved Hide resolved
toolchain/parse/handle_period.cpp Show resolved Hide resolved
toolchain/sem_ir/typed_insts.h Show resolved Hide resolved
toolchain/check/member_access.h Show resolved Hide resolved
toolchain/check/handle_name.cpp Outdated Show resolved Hide resolved
toolchain/check/member_access.cpp Outdated Show resolved Hide resolved
toolchain/check/member_access.cpp Outdated Show resolved Hide resolved
@jonmeow
Copy link
Contributor

jonmeow commented Aug 7, 2024

As a sidenote (not something I'm requesting for this PR), I'm thinking we may want to split up the index directory to reflect the logic split. Maybe leaving the IndexWith-y things in index, and moving the tuple indexing out to something like tuples/access. (which, is now making me notice that odd plural of "tuples": #4200)

This is partly because I feel odd seeing so many files with either array or tuple in the name, it seems like it'd be nice to have that implicit from the directory name. You can do this here if you want, but I'm happy to pick it up after you're merged since it feels like scope creep here.

@brymer-meneses
Copy link
Contributor Author

brymer-meneses commented Aug 8, 2024

You can do this here if you want, but I'm happy to pick it up after you're merged since it feels like scope creep here.

I'll happily do this in a separate PR, as I do not want to clutter this PR by syncing to trunk (to have #4200).

@brymer-meneses
Copy link
Contributor Author

brymer-meneses commented Aug 8, 2024

On a related note, do you think we should rename tuple index with tuple access across the repo? That would mean renaming a lot of things ( SemIR, typed_nodes.h) and stuff.

@jonmeow
Copy link
Contributor

jonmeow commented Aug 8, 2024

On a related note, do you think we should rename tuple index with tuple access across the repo? That would mean renaming a lot of things ( SemIR, typed_nodes.h) and stuff.

Asking for thoughts on discord. Maybe best to split out any rename.

@jonmeow
Copy link
Contributor

jonmeow commented Aug 8, 2024

Per discussion, we'll want to consolidate TupleIndex into the already-existing TupleAccess. They're essentially equivalent, and that'd align with how StructAccess works: using the same instruction for both implicit and explicit access. [ed: note this too might be better in a different PR, but I'm not sure how big the delta would be]

@brymer-meneses brymer-meneses requested a review from jonmeow August 14, 2024 16:53
Copy link
Contributor

@jonmeow jonmeow left a comment

Choose a reason for hiding this comment

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

Thanks for all the changes! Basically looks good, but I think you might've missed deleting the fail_non_tuple_index.carbon. Since there's fail_non_tuple_access (as you observed), can you delete that and I'll merge?

// CHECK:STDERR: fail_non_tuple_index.carbon:[[@LINE+3]]:20: ERROR: Type `[i32; 1]` does not support tuple indexing. Only tuples can be indexed that way.
// CHECK:STDERR: var first: i32 = non_tuple.0;
// CHECK:STDERR: ^~~~~~~~~~~
var first: i32 = non_tuple.0;
Copy link
Contributor

Choose a reason for hiding this comment

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

Had you meant to remove this?

// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/parse/testdata/tuple/index.carbon

let tuple: (i32, i32) = (5, 5);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
let tuple: (i32, i32) = (5, 5);

This is really small (I'm mainly mentioning it because of fail_non_tuple_index.carbon), but I think it's helpful to both emphasize the tuple.0 parse tree, and as a hint that this line isn't needed (name lookup doesn't occur during parse). So the parse tree below ends up a lot shorter, making it easier to see the syntax under test.

@jonmeow
Copy link
Contributor

jonmeow commented Aug 14, 2024

Thanks for all the changes! Basically looks good, but I think you might've missed deleting the fail_non_tuple_index.carbon. Since there's fail_non_tuple_access (as you observed), can you delete that and I'll merge?

Actually, it looks like things have gotten slightly out of date due to a LLVM IR change (#4212 looks like the source of conflicts). I'm happy to have you merge/autoupdate, or I can take care of that (let me know how you'd prefer).

zygoloid and others added 9 commits August 15, 2024 12:56
When forming a specific (previously called a generic instance), evaluate
the eval block of the generic to determine the values of any constants
used in that specific. The majority of the work here is updating
eval.cpp so that it can use the results of prior evaluations in the same
block when computing later values.

Include the computed results in the formatted SemIR output.

---------

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
When evaluating in a generic context, a constant with a symbolic type
might evaluate to a constant with a concrete type (or a more specific
symbolic type). This can't actually happen yet given the current state
of the toolchain, as far as I can determine, so this is more just a
refactoring for now, but will be relied upon by future generics work.

---------

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
Co-authored-by: Carbon Infra Bot <carbon-external-infra@google.com>
…anguage#4136)

The output is really basic, I'm just adding this to help track how
memory is allocated.

```
---
filename:        'check/testdata/expr_category/in_place_tuple_init.carbon'
source_:
  used_bytes:      8057
  reserved_bytes:  8057
tokens_.allocator_:
  used_bytes:      0
  reserved_bytes:  0
tokens_.token_infos_:
  used_bytes:      1040
  reserved_bytes:  2032

(eliding)

value_stores_.string_literals_.set_:
  used_bytes:      320
  reserved_bytes:  320
Total:
  used_bytes:      20609
  reserved_bytes:  29437
...
```

---------

Co-authored-by: josh11b <15258583+josh11b@users.noreply.github.com>
…arbon-language#4137)

Querying a local constant with an invalid instruction triggers a crash,
this will prevent self_param_id to be used unless it is defined.

Closes carbon-language#4071 and carbon-language#4080

This will only fix the crash. The scenario where a declaration-only
interface is imported then defined still gives an error message, but it
won't crash this time.
…re them to be fully defined. (carbon-language#4139)

When referring to a constant within a specific, such as a field of a
generic class, use that specific version of the constant's value.
…nguage#4142)

Most of this is enabled by default, but there is some that needs an
explicit flag.

Avoiding `-Wnon-virtual-dtor` itself for now because that warning can
require changes that carry overhead such as having an extra, unused
destructor entry in the vtable. Hopefully we don't have too many folks
who need our code to be `-Wnon-virtual-dtor` clean.
…-language#4133)

This improve the toolchain's performance by about 10%.

It will also allow us to leverage TCMalloc's extensions to do heap
profiling and get other information about how efficiently we're using
the heap.

Note that currently this causes all of our builds to produce a warning
due to an issue with `rules_python` and multiple modules registering
python toolchains:
bazelbuild/rules_python#1818

This is also only enabled on Linux as there is no support for other OSes
at the moment.
brymer-meneses and others added 23 commits August 15, 2024 12:57
Co-authored-by: josh11b <15258583+josh11b@users.noreply.github.com>
Co-authored-by: josh11b <15258583+josh11b@users.noreply.github.com>
Co-authored-by: josh11b <15258583+josh11b@users.noreply.github.com>
Co-authored-by: josh11b <15258583+josh11b@users.noreply.github.com>
Co-authored-by: josh11b <15258583+josh11b@users.noreply.github.com>
Co-authored-by: josh11b <15258583+josh11b@users.noreply.github.com>
also fix the bug the wherein the diagnostic `TupleIndexOnANonTupleType`
was not showing
Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
@brymer-meneses brymer-meneses force-pushed the tuple-indexing branch 2 times, most recently from 593a2b0 to 91d3545 Compare August 15, 2024 06:37
@brymer-meneses
Copy link
Contributor Author

I think, I might have messed things up when trying to sync.

github-merge-queue bot pushed a commit that referenced this pull request Aug 15, 2024
This changes the tuple index from tuple[0] to tuple.0 in accordance with
the accepted propsal
#3646

I messed up syncing to trunk on my original PR
#4186, that's why I'm
starting on a blank state. Please let me know if I missed incorporating
a change from my prior PR.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants