Skip to content

Fix code generation to add default constraint when needed#53318

Merged
CyrusNajmabadi merged 12 commits intodotnet:mainfrom
Youssef1313:gen-override-type-constraint
Jan 11, 2022
Merged

Fix code generation to add default constraint when needed#53318
CyrusNajmabadi merged 12 commits intodotnet:mainfrom
Youssef1313:gen-override-type-constraint

Conversation

@Youssef1313
Copy link
Copy Markdown
Member

Fixes #53012
Fixes #53003 (please confirm on this since the description of that issue isn't 100% clear to me).

@Youssef1313 Youssef1313 requested a review from a team as a code owner May 11, 2021 14:23
@ghost ghost added the Area-IDE label May 11, 2021
Comment on lines 209 to +211
return !method.ExplicitInterfaceImplementations.Any() && !method.IsOverride
? method.TypeParameters.GenerateConstraintClauses()
: default;
: GenerateDefaultConstraints(method);
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.

Are there any cases where this will be generating extra default constraints?

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.

i think this is fine.

@jinujoseph jinujoseph added the Community The pull request was submitted by a contributor who is not a Microsoft employee. label May 11, 2021
Comment on lines +216 to +217
var seenTypeParameters = new HashSet<string>();
var listOfClauses = new List<TypeParameterConstraintClauseSyntax>(method.TypeParameters.Length);
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.

pooled collections please :)

foreach (var parameter in method.Parameters)
{
if (parameter.Type is not ITypeParameterSymbol { NullableAnnotation: NullableAnnotation.Annotated } typeParameter ||
!seenTypeParameters.Add(parameter.Type.Name))
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.

oh please don't mix a check and a mutation in the same expression. separate out to two if blocks.

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.

@CyrusNajmabadi Will do. But curious if there is any particular reason?

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.

99.999% of all checks are non-mutating (true number :)). As such, it can be easy to miss and get confused by the odd case which looks like a normal if check but does mutate. I like uncommon cases to stick out so it is very easy to see and understand what is going on.

continue;
}

TypeParameterConstraintSyntax constraint;
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.

consider a switch or ternary. up to you though.

class D : I
{
void I.M<T1, T2, T3>(T1? a, T2 b, T1? c, T3? d)
where T1 : class
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's interesting. are we supposed to keep that in?

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.

@CyrusNajmabadi Yes. There are compile errors otherwise.

@Youssef1313
Copy link
Copy Markdown
Member Author

@CyrusNajmabadi Thanks for the fixes!

There is now:

CSC : error CS8103: Combined length of user strings used by the program exceeds allowed limit. Try to decrease use of string literals. [D:\a_work\1\s\src\Compilers\CSharp\Test\Emit\Microsoft.CodeAnalysis.CSharp.Emit.UnitTests.csproj]

I think it was fixed in #58754, can you re-run the failing checks? Thanks!

@CyrusNajmabadi
Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 4 pipeline(s).

@CyrusNajmabadi CyrusNajmabadi merged commit 6b0d35d into dotnet:main Jan 11, 2022
@ghost ghost added this to the Next milestone Jan 11, 2022
@Youssef1313 Youssef1313 deleted the gen-override-type-constraint branch January 11, 2022 19:35
@CyrusNajmabadi
Copy link
Copy Markdown
Contributor

Thanks @Youssef1313

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area-IDE Community The pull request was submitted by a contributor who is not a Microsoft employee.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GenerateOverride should sometimes add where T : default constraint Incorrect override snippet for type parameter

5 participants