Skip to content
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

Error for using positional member that is hidden #52660

Merged
merged 11 commits into from
Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,17 @@
return a ?? (b ? 1 : 2);
}
```

3. https://github.com/dotnet/roslyn/issues/52630 In C# 9 (.NET 5, Visual Studio 16.9), it is possible that a record uses a hidden member from a base type as a positional member. In Visual Studio 16.10, this is now an error:
```csharp
record Base
{
public int I { get; init; }
}
record Derived(int I) // The positional member 'Base.I' found corresponding to this parameter is hidden.
: Base
{
public int I() { return 0; }
}
```

3 changes: 3 additions & 0 deletions src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -6606,4 +6606,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="ERR_InheritingFromRecordWithSealedToString" xml:space="preserve">
<value>Inheriting from a record with a sealed 'Object.ToString' is not supported in C# {0}. Please use language version '{1}' or greater.</value>
</data>
<data name="ERR_HiddenPositionalMember" xml:space="preserve">
<value>The positional member '{0}' found corresponding to this parameter is hidden.</value>
</data>
</root>
3 changes: 2 additions & 1 deletion src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1934,7 +1934,8 @@ internal enum ErrorCode

#region diagnostics introduced for C# 10.0

ERR_InheritingFromRecordWithSealedToString = 8912
ERR_InheritingFromRecordWithSealedToString = 8912,
ERR_HiddenPositionalMember = 8913,

#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3410,8 +3410,11 @@ private void AddSynthesizedRecordMembersIfNecessary(MembersAndInitializersBuilde
var memberSignatures = s_duplicateRecordMemberSignatureDictionary.Allocate();
var membersSoFar = builder.GetNonTypeMembers(declaredMembersAndInitializers);
var members = ArrayBuilder<Symbol>.GetInstance(membersSoFar.Count + 1);
var memberNames = PooledHashSet<string>.GetInstance();
foreach (var member in membersSoFar)
{
memberNames.Add(member.Name);

switch (member)
{
case FieldSymbol:
Expand Down Expand Up @@ -3467,6 +3470,7 @@ private void AddSynthesizedRecordMembersIfNecessary(MembersAndInitializersBuilde
addToStringMethod(printMembers);

memberSignatures.Free();
memberNames.Free();

// We put synthesized record members first so that errors about conflicts show up on user-defined members rather than all
// going to the record declaration
Expand Down Expand Up @@ -3735,6 +3739,11 @@ ImmutableArray<PropertySymbol> addProperties(ImmutableArray<ParameterSymbol> rec
{
// Deconstruct() is specified to simply assign from this property to the corresponding out parameter.
existingOrAddedMembers.Add(prop);

if (isInherited)
{
validateNotHidden(prop, param);
Copy link
Member

@Youssef1313 Youssef1313 Apr 17, 2021

Choose a reason for hiding this comment

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

If prop is abstract, it won't be validated, is that intended? i.e:

abstract record Base
{
    public abstract int I { get; init; }
}

record Derived(int I) : Base
{
    public int I() { return 0; }
}

#Resolved

Copy link
Member Author

@jcouv jcouv Apr 17, 2021

Choose a reason for hiding this comment

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

In that scenario Derived defined a concrete property I and a method I() (that's an error). The positional member is Derived.I so we didn't pick a hidden member, so the new error doesn't come into play.
Added tests to illustrate. Thanks for the scenario. #Resolved

}
}
}
else
Expand Down Expand Up @@ -3762,6 +3771,14 @@ void addProperty(SynthesizedRecordPropertySymbol property)
}

return existingOrAddedMembers.ToImmutableAndFree();

void validateNotHidden(Symbol symbol, ParameterSymbol param)
{
if (memberNames.Contains(symbol.Name) || !this.GetTypeMembers(symbol.Name).IsEmpty)
{
diagnostics.Add(ErrorCode.ERR_HiddenPositionalMember, param.Locations[0], symbol);
}
}
}

void addObjectEquals(MethodSymbol thisEquals)
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading