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

Disallow pointer and restricted types for record parameters #48925

Merged
merged 7 commits into from
Oct 31, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5936,7 +5936,7 @@ private BoundExpression BindDynamicMemberAccess(
for (int i = 0; i < typeArgumentsWithAnnotations.Length; ++i)
{
var typeArgument = typeArgumentsWithAnnotations[i];
if ((typeArgument.Type.IsPointerOrFunctionPointer()) || typeArgument.Type.IsRestrictedType())
if (typeArgument.Type.IsPointerOrFunctionPointer() || typeArgument.Type.IsRestrictedType())
{
// "The type '{0}' may not be used as a type argument"
Error(diagnostics, ErrorCode.ERR_BadTypeArgument, typeArgumentsSyntax[i], typeArgument.Type);
Expand Down
3 changes: 3 additions & 0 deletions src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -6576,6 +6576,9 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="WRN_AnalyzerReferencesFramework_Title" xml:space="preserve">
<value>The loaded assembly references .NET Framework, which is not supported.</value>
</data>
<data name="ERR_BadFieldTypeInRecord" xml:space="preserve">
<value>The type '{0}' may not be used for a field of a record.</value>
</data>
<data name="ERR_FunctionPointersCannotBeCalledWithNamedArguments" xml:space="preserve">
<value>A function pointer cannot be called with named arguments.</value>
</data>
Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1923,8 +1923,8 @@ internal enum ErrorCode
ERR_FunctionPointersCannotBeCalledWithNamedArguments = 8905,

ERR_EqualityContractRequiresGetter = 8906,

WRN_UnreadRecordParameter = 8907,
ERR_BadFieldTypeInRecord = 8908,

#endregion diagnostics introduced for C# 9.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,27 @@ internal override void GenerateMethodBody(TypeCompilationState compilationState,

// field1 == other.field1 && ... && fieldN == other.fieldN
var fields = ArrayBuilder<FieldSymbol>.GetInstance();
bool foundBadField = false;
foreach (var f in ContainingType.GetFieldsToEmit())
{
if (!f.IsStatic)
{
fields.Add(f);

var parameterType = f.Type;
if (parameterType.IsUnsafe())
{
diagnostics.Add(ErrorCode.ERR_BadFieldTypeInRecord, f.Locations.FirstOrNone(), parameterType);
foundBadField = true;
}
else if (parameterType.IsRestrictedType())
{
// We'll have reported a diagnostic elsewhere (SourceMemberFieldSymbol.TypeChecks)
foundBadField = true;
}
}
}
if (fields.Count > 0)
if (fields.Count > 0 && !foundBadField)
{
retExpr = MethodBodySynthesizer.GenerateFieldEquals(
retExpr,
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@
<target state="translated">V asynchronním příkazu foreach nejde použít kolekce dynamického typu.</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadFieldTypeInRecord">
<source>The type '{0}' may not be used for a field of a record.</source>
<target state="new">The type '{0}' may not be used for a field of a record.</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadFuncPointerArgCount">
<source>Function pointer '{0}' does not take {1} arguments</source>
<target state="new">Function pointer '{0}' does not take {1} arguments</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@
<target state="translated">Eine Sammlung des dynamic-Typs kann in einem asynchronen foreach nicht verwendet werden.</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadFieldTypeInRecord">
<source>The type '{0}' may not be used for a field of a record.</source>
<target state="new">The type '{0}' may not be used for a field of a record.</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadFuncPointerArgCount">
<source>Function pointer '{0}' does not take {1} arguments</source>
<target state="new">Function pointer '{0}' does not take {1} arguments</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@
<target state="translated">No se puede usar una colección de tipo dinámico en una instrucción foreach asincrónica.</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadFieldTypeInRecord">
<source>The type '{0}' may not be used for a field of a record.</source>
<target state="new">The type '{0}' may not be used for a field of a record.</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadFuncPointerArgCount">
<source>Function pointer '{0}' does not take {1} arguments</source>
<target state="new">Function pointer '{0}' does not take {1} arguments</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@
<target state="translated">Impossible d'utiliser une collection de type dynamique dans un foreach asynchrone</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadFieldTypeInRecord">
<source>The type '{0}' may not be used for a field of a record.</source>
<target state="new">The type '{0}' may not be used for a field of a record.</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadFuncPointerArgCount">
<source>Function pointer '{0}' does not take {1} arguments</source>
<target state="new">Function pointer '{0}' does not take {1} arguments</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@
<target state="translated">Non è possibile usare una raccolta di tipo dinamico in un'istruzione foreach asincrona</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadFieldTypeInRecord">
<source>The type '{0}' may not be used for a field of a record.</source>
<target state="new">The type '{0}' may not be used for a field of a record.</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadFuncPointerArgCount">
<source>Function pointer '{0}' does not take {1} arguments</source>
<target state="new">Function pointer '{0}' does not take {1} arguments</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@
<target state="translated">非同期 foreach では動的な型のコレクションを使用できません</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadFieldTypeInRecord">
<source>The type '{0}' may not be used for a field of a record.</source>
<target state="new">The type '{0}' may not be used for a field of a record.</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadFuncPointerArgCount">
<source>Function pointer '{0}' does not take {1} arguments</source>
<target state="new">Function pointer '{0}' does not take {1} arguments</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@
<target state="translated">비동기 foreach에는 동적 형식 컬렉션을 사용할 수 없습니다.</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadFieldTypeInRecord">
<source>The type '{0}' may not be used for a field of a record.</source>
<target state="new">The type '{0}' may not be used for a field of a record.</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadFuncPointerArgCount">
<source>Function pointer '{0}' does not take {1} arguments</source>
<target state="new">Function pointer '{0}' does not take {1} arguments</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@
<target state="translated">Nie można użyć kolekcji typu dynamicznego w asynchronicznej instrukcji foreach</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadFieldTypeInRecord">
<source>The type '{0}' may not be used for a field of a record.</source>
<target state="new">The type '{0}' may not be used for a field of a record.</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadFuncPointerArgCount">
<source>Function pointer '{0}' does not take {1} arguments</source>
<target state="new">Function pointer '{0}' does not take {1} arguments</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@
<target state="translated">Não é possível usar uma coleção do tipo dinâmico em uma foreach assíncrona</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadFieldTypeInRecord">
<source>The type '{0}' may not be used for a field of a record.</source>
<target state="new">The type '{0}' may not be used for a field of a record.</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadFuncPointerArgCount">
<source>Function pointer '{0}' does not take {1} arguments</source>
<target state="new">Function pointer '{0}' does not take {1} arguments</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@
<target state="translated">Не удается использовать коллекцию динамического типа в асинхронном операторе foreach</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadFieldTypeInRecord">
<source>The type '{0}' may not be used for a field of a record.</source>
<target state="new">The type '{0}' may not be used for a field of a record.</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadFuncPointerArgCount">
<source>Function pointer '{0}' does not take {1} arguments</source>
<target state="new">Function pointer '{0}' does not take {1} arguments</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@
<target state="translated">Zaman uyumsuz bir foreach içinde dinamik tür koleksiyonu oluşturulamaz</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadFieldTypeInRecord">
<source>The type '{0}' may not be used for a field of a record.</source>
<target state="new">The type '{0}' may not be used for a field of a record.</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadFuncPointerArgCount">
<source>Function pointer '{0}' does not take {1} arguments</source>
<target state="new">Function pointer '{0}' does not take {1} arguments</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@
<target state="translated">无法在异步 foreach 中使用动态类型集合</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadFieldTypeInRecord">
<source>The type '{0}' may not be used for a field of a record.</source>
<target state="new">The type '{0}' may not be used for a field of a record.</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadFuncPointerArgCount">
<source>Function pointer '{0}' does not take {1} arguments</source>
<target state="new">Function pointer '{0}' does not take {1} arguments</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@
<target state="translated">無法在非同步 foreach 中使用動態類型的集合</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadFieldTypeInRecord">
<source>The type '{0}' may not be used for a field of a record.</source>
<target state="new">The type '{0}' may not be used for a field of a record.</target>
<note />
</trans-unit>
<trans-unit id="ERR_BadFuncPointerArgCount">
<source>Function pointer '{0}' does not take {1} arguments</source>
<target state="new">Function pointer '{0}' does not take {1} arguments</target>
Expand Down
Loading