Skip to content

[hackathon] Support nice syntax for ValueArray in most type contexts.#57286

Closed
VSadov wants to merge 2 commits intodotnet:demos/lowlevelhackathonfrom
VSadov:ValueArrayTypeSyntax
Closed

[hackathon] Support nice syntax for ValueArray in most type contexts.#57286
VSadov wants to merge 2 commits intodotnet:demos/lowlevelhackathonfrom
VSadov:ValueArrayTypeSyntax

Conversation

@VSadov
Copy link
Member

@VSadov VSadov commented Oct 20, 2021

This change allows writing code with struct arrays in terms ofType[length] :

class QuadTree
{
    public readonly int Depth;
    private QuadTree[4] _nodes;  //field

    public ref readonly QuadTree[4] Nodes => ref _nodes;  // ref return type

    public int[4] DepthMask()    // byval return type
    {
        int[4] mask = default;   // local variable
        for(int i = 0; i < mask.Length; i++)
        {
            mask[i] = _nodes[i].Depth;
        }

        return mask;
    }
}

@cston - I have also changed ValueArray<T, Size> Roslyn classification from SpecialType to WellKnownType.
I do not have a strong opinion on which it should be, but WellKnown is a lot easier to test in the absence of the actual type by providing a mock implementation.

@VSadov VSadov requested a review from a team as a code owner October 20, 2021 23:14
@ghost ghost added the Area-Compilers label Oct 20, 2021
@VSadov
Copy link
Member Author

VSadov commented Oct 20, 2021

@cston @stephentoub @davidwrighton - please take a look.

// https://github.com/dotnet/roslyn/issues/32464
// Should capture invalid dimensions for use in `SemanticModel` and `IOperation`.
Error(diagnostics, ErrorCode.ERR_ArraySizeInDeclaration, rankSpecifier);
var size0 = dimension[0];
Copy link
Member Author

Choose a reason for hiding this comment

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

Most interesting part of the change is here.
Here I intercept already parsed elementType[constExpr] and instead of giving a ERR_ArraySizeInDeclaration error, I bind it to a ValueArray type of corresponding length.

The rest is supporting changes, tests, etc...

@VSadov
Copy link
Member Author

VSadov commented Oct 20, 2021

This is how it looks in IDE.
i.e. - tooltips still show the underlying type. Probably easy to fix, but I think it is good enough for a prototype.

image

@VSadov VSadov changed the title Support nice syntax for ValueArray in most type contexts. [hackathon] Support nice syntax for ValueArray in most type contexts. Oct 20, 2021
receiverOpt = null;
}
else if (symbol.ContainingType?.IsValueArrayType() == true)
else if (symbol.ContainingType?.IsValueArrayType(symbol.DeclaringCompilation ) == true)
Copy link
Contributor

@cston cston Oct 23, 2021

Choose a reason for hiding this comment

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

symbol.DeclaringCompilation

This will be null for metadata symbols. Consider using Binder.Compilation instead. Same comment below.

}
else
{
arrSize = sizeExpr.ConstantValue.UInt64Value;
Copy link
Contributor

Choose a reason for hiding this comment

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

UInt64Value

Do we need to check for < 0?

public static bool IsValueArrayType(this TypeSymbol type, CSharpCompilation compilation)
{
if (type.OriginalDefinition.SpecialType != SpecialType.System_ValueArray_TR)
if ((object)type.OriginalDefinition != compilation?.GetWellKnownType(WellKnownType.System_ValueArray_TR))
Copy link
Contributor

Choose a reason for hiding this comment

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

compilation?

Consider requiring compilation to be non-null.

@jcouv
Copy link
Member

jcouv commented Nov 15, 2021

Marked as draft to make some room in our review queue. Ping when we should take another look. Thanks

@jcouv
Copy link
Member

jcouv commented Nov 15, 2021

@VSadov Is there a csharplang proposal to move forward with this new syntax? It looks nice indeed.

@VSadov
Copy link
Member Author

VSadov commented Nov 26, 2021

this PR no longer needs to be open

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.

3 participants