Skip to content

Produce specific error for misplaced 'record' keyword#59622

Merged
RikkiGibson merged 7 commits intodotnet:mainfrom
jcouv:struct-record
Mar 1, 2022
Merged

Produce specific error for misplaced 'record' keyword#59622
RikkiGibson merged 7 commits intodotnet:mainfrom
jcouv:struct-record

Conversation

@jcouv
Copy link
Copy Markdown
Member

@jcouv jcouv commented Feb 17, 2022

Fixes #59534

@jcouv jcouv self-assigned this Feb 17, 2022
@ghost ghost added the Area-Compilers label Feb 17, 2022
@jcouv jcouv added the Feature - Records Records label Feb 17, 2022
@jcouv jcouv marked this pull request as ready for review February 17, 2022 19:00
@jcouv jcouv requested a review from a team as a code owner February 17, 2022 19:00
Diagnostic(ErrorCode.ERR_MisplacedRecord, "record").WithLocation(1, 8),
// (1,16): error CS1514: { expected
// struct record C(int X, int Y);
Diagnostic(ErrorCode.ERR_LbraceExpected, "(").WithLocation(1, 16),
Copy link
Copy Markdown
Contributor

@CyrusNajmabadi CyrusNajmabadi Feb 17, 2022

Choose a reason for hiding this comment

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

this is a pity. consider (if cheap) supporting this as a record, with misplaced struct. Once you see 'record' (even if it's errant) i think we should exercise the record-parsing path. #Closed

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

the primary reason for this is so that the tree doesn't go off the rails. that helps the entire experience.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I understand, but prefer to leave as-is.
It's not trivial because the keyword is struct (not record) and so we'd have to loosen assertions in constructTypeDeclaration(...) below which I'd rather not.
This will likely resolve when we allow parameter list on type declaration for primary constructors.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

That works for me! Thanks :)

{
var text = "class record C(int X, int Y);";
UsingTree(text, options: TestOptions.Regular10,
// (1,7): error CS9012: Unexpected keyword 'record'. Did you mean 'struct record' or 'class record'?
Copy link
Copy Markdown
Contributor

@CyrusNajmabadi CyrusNajmabadi Feb 17, 2022

Choose a reason for hiding this comment

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

i'm a bit confused by the error message. It says Did you mean 'struct record' or 'class record'? but the user did write class record. #Resolved

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

D'oh! Fixed

[Fact]
public void StructNamedRecord()
{
var source = "struct record { } ";
Copy link
Copy Markdown
Member

@Youssef1313 Youssef1313 Feb 17, 2022

Choose a reason for hiding this comment

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

struct record : IMyInterface? #Closed

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good catch. Thanks

@jcouv jcouv closed this Feb 18, 2022
@jcouv jcouv reopened this Feb 18, 2022
@jcouv
Copy link
Copy Markdown
Member Author

jcouv commented Feb 23, 2022

@333fred Could you take another look? We've updated the PR with Cyrus's commit to treat struct record and class record as record declarations (placing struct as leading trivia on record rather than record as trailing trivia on struct).

@jcouv
Copy link
Copy Markdown
Member Author

jcouv commented Feb 24, 2022

@333fred @dotnet/roslyn-compiler for review. Thanks

Copy link
Copy Markdown
Member

@333fred 333fred left a comment

Choose a reason for hiding this comment

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

Done review pass (commit 5)


namespace Microsoft.CodeAnalysis.CSharp.Syntax.InternalSyntax
{
using System.Diagnostics.CodeAnalysis;
Copy link
Copy Markdown
Member

@333fred 333fred Feb 24, 2022

Choose a reason for hiding this comment

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

Top of the file? #Resolved

{
var source = @"
interface I { }
struct record<T> : I { }
Copy link
Copy Markdown
Member

@333fred 333fred Feb 24, 2022

Choose a reason for hiding this comment

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

Add a non-generic version? #Resolved

}

[Fact]
public void StructNamedRecord()
Copy link
Copy Markdown
Member

@333fred 333fred Feb 24, 2022

Choose a reason for hiding this comment

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

Probably want class record versions of these tests as well. #Resolved

[Fact, CompilerTrait(CompilerFeature.RecordStructs)]
public void RecordStructParsing_WrongOrder()
[Fact, CompilerTrait(CompilerFeature.RecordStructs), WorkItem(59534, "https://github.com/dotnet/roslyn/issues/59534")]
public void RecordStructParsing_WrongOrder_CSharp10()
Copy link
Copy Markdown
Member

@333fred 333fred Feb 24, 2022

Choose a reason for hiding this comment

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

Consider making these theories over struct vs class. #Resolved

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think we have sufficient coverage for class case now (added a few tests)

Julien Couvreur added 2 commits February 26, 2022 18:01
@jcouv jcouv requested a review from cston February 27, 2022 19:22
}

[Fact, CompilerTrait(CompilerFeature.RecordStructs), WorkItem(59534, "https://github.com/dotnet/roslyn/issues/59534")]
public void StructNamedRecord_CSharp8()
Copy link
Copy Markdown
Contributor

@cston cston Feb 28, 2022

Choose a reason for hiding this comment

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

StructNamedRecord_CSharp8

Consider merging these three into a single test, either with [Theory] or with a verify(ParseOptions) helper method, and add a similar test for class record { }.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We have an IDE PR waiting for this to be merged, so I'd rather avoid one more iteration to combine two tests. Thanks

@jcouv jcouv enabled auto-merge (squash) March 1, 2022 01:57
@RikkiGibson RikkiGibson disabled auto-merge March 1, 2022 18:07
@RikkiGibson RikkiGibson merged commit 44956a2 into dotnet:main Mar 1, 2022
@ghost ghost added this to the Next milestone Mar 1, 2022
@jcouv jcouv deleted the struct-record branch March 1, 2022 18:16
@allisonchou allisonchou modified the milestones: Next, 17.2.P3 Mar 28, 2022
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.

IDE help after typing struct record Foo

7 participants