Skip to content

19.2 Enum base type #399

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

Closed
gafter opened this issue Nov 17, 2021 · 4 comments
Closed

19.2 Enum base type #399

gafter opened this issue Nov 17, 2021 · 4 comments
Assignees
Labels
meeting: proposal There is an informal proposal in the issue, worth discussing in a meeting
Milestone

Comments

@gafter
Copy link
Member

gafter commented Nov 17, 2021

An enum base type may be an integral type's keyword or any type name that resolves to one of the integral types.

The current text of the spec requires that it be the fully qualified name, but that's not actually what is implemented in C# 6. It could use an alias or an imported type name.

@gafter gafter added this to the C# 6 milestone Nov 17, 2021
@gafter gafter changed the title Enum base type 19.2 Enum base type Nov 17, 2021
@RexJaeschke RexJaeschke self-assigned this Nov 25, 2021
@RexJaeschke
Copy link
Contributor

During the Nov 17 TG2 call, @jskeet noticed that admin/v6-feature-tracker.md did not seem to reflect the current status of that topic, and he asked me to investigate. Here are my findings:

15.5.4 Volatile fields

This currently contains the following:

These restrictions ensure that all threads will observe volatile writes performed by any other thread in the order in which they were performed. A conforming implementation is not required to provide a single total ordering of volatile writes as seen from all threads of execution. The type of a volatile field shall be one of the following:

  • An enum_type having an enum_base type of byte, sbyte, short, ushort, int, or uint.

Unless I'm reading it too literally, this constraint does not allow the System.* alias names for these keywords. Nor does it disallow the System.* alias names for long and ulong. I propose changing this constraint to the following:

  • An enum_type having an enum_base type other than long, ulong, and their corresponding System.* aliases.

19.2 Enum declarations

As a result of PR #351, we have the following:

enum_base
    : ':' integral_type
    | ':' integral_type_name
    ;
integral_type_name
    : type_name // restricted to one of System.{SByte,Byte,Int16,UInt16,Int32,UInt32,Int64,UInt64}
    ;

Each enum type has a corresponding integral type called the underlying type of the enum type. This underlying type shall be able to represent all the enumerator values defined in the enumeration. If the enum_base is present, it explicitly declares the underlying type. The underlying type shall be one of the integral types (§9.3.6) other than char; specified either by keyword (integral_type), or by one of the full type names that the integral types alias (§9.3.5) (integral_type_name).

Note: Neither char nor System.Char can be used as an underlying type. end note

This looks right to me. However, if we want to express this entirely in the grammar, without textual constraints (which I am not necessarily advocating we do), we could use the following instead:

enum_base
    : ':' enum_base_type
    ;
enum_base_type
    : 'sbyte'
    | 'byte'
    | 'short'
    | 'ushort'
    | 'int'
    | 'uint'
    | 'long'
    | 'ulong'
    | 'System.SByte'
    | 'System.Byte'
    | 'System.Int16'
    | 'System.UInt16'
    | 'System.Int32'
    | 'System.UInt32'
    | 'System.Int64'
    | 'System.UInt64'
    ;

@gafter Have your concerns been addressed by Nigel's PR?

@Nigel-Ecma An ANTLR question: If we use the grammar-only approach, that means we'll have duplicate terminals (as in sbyte, byte, ...), which are already present in the keyword rule. Recall that I introduced the following named lexical tokens to avoid that for other terminals:

DEFAULT  : 'default' ;
NULL     : 'null' ;
TRUE     : 'true' ;
FALSE    : 'false' ;
ASTERISK : '*' ;
SLASH    : '/' ;

which suggests we'd need 8 more for these duplicate type names.

@RexJaeschke RexJaeschke added the meeting: proposal There is an informal proposal in the issue, worth discussing in a meeting label Nov 25, 2021
@Nigel-Ecma
Copy link
Contributor

Nigel-Ecma commented Nov 25, 2021 via email

@jskeet
Copy link
Contributor

jskeet commented Nov 26, 2021

I was mostly reflecting that draft-v6 doesn't include some of the changes made in our previous repo, to be honest. There was a PR merged there that doesn't appear to have been merged here. I can try to find the details again, but it won't be before Monday.

@jskeet
Copy link
Contributor

jskeet commented Jan 19, 2022

Closing in favour of #412.

@jskeet jskeet closed this as completed Jan 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
meeting: proposal There is an informal proposal in the issue, worth discussing in a meeting
Projects
None yet
Development

No branches or pull requests

4 participants