Skip to content

Conversation

@ChrisDodd
Copy link
Contributor

No description provided.

@jafingerhut
Copy link
Collaborator

Should we in this PR also define literal syntax for arrays, e.g. perhaps something like (typeRef[4]) {expr0, expr1, expr2, expr3}?

@jafingerhut
Copy link
Collaborator

In this section:

[#sec-uninitialized-values-and-writing-invalid-headers]
=== Reading uninitialized values and writing fields of invalid headers

there is an explicit statement about accessing elements of header stacks that are outside of range:

The result of reading a value in any of the situations below is that
some unspecified value will be used for that field.
[ ... ]
* reading a field of a header that is an element of a header stack,
  where the index is out of range for the header stack.

Should that be generalized to reading an element of an array where the index is out of range for the array?

Similarly later in that section for attempted writes to a header stack element it says:

If any of these kinds of writes are performed:
[ ... ]
* a write to a field in an element of a header stack, where the index
  is out of range
[ ... ]
then that write must not change any state that is currently defined in
the system, neither in header fields nor anywhere else.

Should that also be generalized to attempting to write to elements of an array where the index is out of bounds?

@jafingerhut jafingerhut added the ldwg-discussion Plan to discuss at next LDWG label Feb 1, 2025
Copy link
Member

@vlstill vlstill left a comment

Choose a reason for hiding this comment

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

Looks good to me, I agree we should have exact semantics of out-of-bounds access as suggested in #1360 (comment).

@kfcripps
Copy link
Contributor

kfcripps commented Feb 3, 2025

This opens up the possibility of having nested arrays, which could potentially complicate array / header stack processing in some passes. We might consider explicitly prohibiting nested arrays until there is real motivation to support them. Related discussion: p4lang/p4c#5115 (comment)

@ChrisDodd ChrisDodd marked this pull request as ready for review August 3, 2025 05:00
@ChrisDodd
Copy link
Contributor Author

Most of the support for this is already in p4c -- last remaining piece is p4lang/p4c#5338

@jonathan-dilorenzo
Copy link
Collaborator

Ready for review and merging. @jonathan-dilorenzo and @jafingerhut to review.

@jonathan-dilorenzo
Copy link
Collaborator

Let's resolve the "error?" to error and make a note that this isn't done for any deep reason, but simply due to the lack of compelling use cases and the ability to change our minds in a non-breaking way.


A header stack represents an array of headers or header unions. A header stack type is
defined as an array declaration
where `typeRef` is the name of a header or header union type. For a
Copy link
Contributor

Choose a reason for hiding this comment

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

Considering typedefs which may alias header or header union types, e.g.,

header Hdr { bit<32> x }
typedef Hdr hhh;
... and somewhere else we use hhh[5], a header stack

"where typeRef is the name of a header or header union type" is a bit imprecise.

So maybe we can put:

A header stack type is defined as an array declaration where `typeRef` represents 

Copy link
Collaborator

Choose a reason for hiding this comment

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

+1. I think that phrasing is quite nice.

Copy link
Collaborator

@jonathan-dilorenzo jonathan-dilorenzo left a comment

Choose a reason for hiding this comment

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

Minor comments, but approving and will merge immediately upon fix.

| `list types` | error | error | error | allowed | error [7] | error

| `list types` | error | error | error | allowed | error
| `extern instances` | error | error | error | erroe | allowed [6] | error
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: error*


A header stack represents an array of headers or header unions. A header stack type is
defined as an array declaration
where `typeRef` is the name of a header or header union type. For a
Copy link
Collaborator

Choose a reason for hiding this comment

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

+1. I think that phrasing is quite nice.

Comment that these combos do not appear useful, but don't seem hazardous
either, so could be added in the future.

Signed-off-by: Chris Dodd <[email protected]>
@jonathan-dilorenzo jonathan-dilorenzo merged commit 6d02aae into p4lang:main Nov 3, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ldwg-discussion Plan to discuss at next LDWG

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants