Skip to content

Commit

Permalink
Clean up error messages for address of to non-function-pointer types.
Browse files Browse the repository at this point in the history
* Correctly error on direct cast to non-func ptr type
* Use better error message test.

Fixes #44489.
  • Loading branch information
333fred committed May 28, 2020
1 parent b8cb1f8 commit ac10559
Show file tree
Hide file tree
Showing 18 changed files with 293 additions and 41 deletions.
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2352,6 +2352,11 @@ private void GenerateExplicitConversionErrors(
GenerateImplicitConversionError(diagnostics, operand.Syntax, conversion, operand, targetType);
return;
}
case BoundKind.UnconvertedAddressOfOperator:
{
diagnostics.Add(ErrorCode.ERR_AddressOfToNonFunctionPointer , syntax.Location, ((BoundUnconvertedAddressOfOperator)operand).Operand.Name, targetType);
return;
}
}

Debug.Assert((object)operand.Type != null);
Expand Down
21 changes: 11 additions & 10 deletions src/Compilers/CSharp/Portable/Binder/Binder_Statements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2138,12 +2138,12 @@ protected void GenerateImplicitConversionError(
}
case BoundKind.MethodGroup:
{
reportMethodGroupErrors((BoundMethodGroup)operand);
reportMethodGroupErrors((BoundMethodGroup)operand, fromFunctionPtr: false);
return;
}
case BoundKind.UnconvertedAddressOfOperator:
{
reportMethodGroupErrors(((BoundUnconvertedAddressOfOperator)operand).Operand);
reportMethodGroupErrors(((BoundUnconvertedAddressOfOperator)operand).Operand, fromFunctionPtr: true);
return;
}
case BoundKind.Literal:
Expand Down Expand Up @@ -2204,7 +2204,7 @@ protected void GenerateImplicitConversionError(

Debug.Assert(operand.HasAnyErrors && operand.Kind != BoundKind.UnboundLambda, "Missing a case in implicit conversion error reporting");

void reportMethodGroupErrors(BoundMethodGroup methodGroup)
void reportMethodGroupErrors(BoundMethodGroup methodGroup, bool fromFunctionPtr)
{
if (!Conversions.ReportDelegateOrFunctionPointerMethodGroupDiagnostics(this, methodGroup, targetType, diagnostics))
{
Expand All @@ -2230,19 +2230,20 @@ void reportMethodGroupErrors(BoundMethodGroup methodGroup)

switch (targetType.TypeKind)
{
case TypeKind.FunctionPointer:
if (operand.Kind == BoundKind.MethodGroup)
{
Error(diagnostics, ErrorCode.ERR_MissingAddressOf, location);
return;
}
case TypeKind.FunctionPointer when fromFunctionPtr:
errorCode = ErrorCode.ERR_MethFuncPtrMismatch;
break;
case TypeKind.FunctionPointer:
Error(diagnostics, ErrorCode.ERR_MissingAddressOf, location);
return;
case TypeKind.Delegate when fromFunctionPtr:
errorCode = ErrorCode.ERR_CannotConvertAddressOfToDelegate;
break;
case TypeKind.Delegate:
errorCode = ErrorCode.ERR_MethDelegateMismatch;
break;
default:
errorCode = ErrorCode.ERR_MethGrpToNonDel;
errorCode = fromFunctionPtr ? ErrorCode.ERR_AddressOfToNonFunctionPointer : ErrorCode.ERR_MethGrpToNonDel;
break;
}

Expand Down
8 changes: 7 additions & 1 deletion src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -6146,7 +6146,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<value>Cannot convert method group to function pointer (Are you missing a '&amp;'?)</value>
</data>
<data name="ERR_CannotUseReducedExtensionMethodInAddressOf" xml:space="preserve">
<value>Cannot use a an extension method with a receiver as the target of a '&amp;' operator.</value>
<value>Cannot use an extension method with a receiver as the target of a '&amp;' operator.</value>
</data>
<data name="ERR_CannotUseFunctionPointerAsFixedLocal" xml:space="preserve">
<value>The type of a local declared in a fixed statement cannot be a function pointer type.</value>
Expand All @@ -6160,4 +6160,10 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="OutIsNotValidForReturn" xml:space="preserve">
<value>'RefKind.Out' is not a valid ref kind for a return type.</value>
</data>
<data name="ERR_CannotConvertAddressOfToDelegate" xml:space="preserve">
<value>Cannot convert a &amp; method group '{0}' to delegate type '{0}'.</value>
</data>
<data name="ERR_AddressOfToNonFunctionPointer" xml:space="preserve">
<value>Cannot convert &amp; method group '{0}' to non-function pointer type '{1}'.</value>
</data>
</root>
2 changes: 2 additions & 0 deletions src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1806,6 +1806,8 @@ internal enum ErrorCode
ERR_InvalidFuncPointerReturnTypeModifier = 8797,
ERR_DupReturnTypeMod = 8798,
ERR_AddressOfMethodGroupInExpressionTree = 8799,
ERR_CannotConvertAddressOfToDelegate = 8800,
ERR_AddressOfToNonFunctionPointer = 8801,

#endregion
// Note: you will need to re-generate compiler code after adding warnings (eng\generate-compiler-code.cmd)
Expand Down
14 changes: 12 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<target state="new">'&amp;' on method groups cannot be used in expression trees</target>
<note />
</trans-unit>
<trans-unit id="ERR_AddressOfToNonFunctionPointer">
<source>Cannot convert &amp; method group '{0}' to non-function pointer type '{1}'.</source>
<target state="new">Cannot convert &amp; method group '{0}' to non-function pointer type '{1}'.</target>
<note />
</trans-unit>
<trans-unit id="ERR_AltInterpolatedVerbatimStringsNotAvailable">
<source>To use '@$' instead of '$@' for an interpolated verbatim string, please use language version '{0}' or greater.</source>
<target state="translated">Pokud chcete pro interpolovaný doslovný řetězec použít @$ místo $@, použijte verzi jazyka {0} nebo vyšší.</target>
Expand Down Expand Up @@ -117,14 +122,19 @@
<target state="translated">Chyba syntaxe příkazového řádku: {0} není platná hodnota možnosti {1}. Hodnota musí mít tvar {2}.</target>
<note />
</trans-unit>
<trans-unit id="ERR_CannotConvertAddressOfToDelegate">
<source>Cannot convert a &amp; method group '{0}' to delegate type '{0}'.</source>
<target state="new">Cannot convert a &amp; method group '{0}' to delegate type '{0}'.</target>
<note />
</trans-unit>
<trans-unit id="ERR_CannotUseFunctionPointerAsFixedLocal">
<source>The type of a local declared in a fixed statement cannot be a function pointer type.</source>
<target state="new">The type of a local declared in a fixed statement cannot be a function pointer type.</target>
<note />
</trans-unit>
<trans-unit id="ERR_CannotUseReducedExtensionMethodInAddressOf">
<source>Cannot use a an extension method with a receiver as the target of a '&amp;' operator.</source>
<target state="new">Cannot use a an extension method with a receiver as the target of a '&amp;' operator.</target>
<source>Cannot use an extension method with a receiver as the target of a '&amp;' operator.</source>
<target state="new">Cannot use an extension method with a receiver as the target of a '&amp;' operator.</target>
<note />
</trans-unit>
<trans-unit id="ERR_CantUseInOrOutInArglist">
Expand Down
14 changes: 12 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<target state="new">'&amp;' on method groups cannot be used in expression trees</target>
<note />
</trans-unit>
<trans-unit id="ERR_AddressOfToNonFunctionPointer">
<source>Cannot convert &amp; method group '{0}' to non-function pointer type '{1}'.</source>
<target state="new">Cannot convert &amp; method group '{0}' to non-function pointer type '{1}'.</target>
<note />
</trans-unit>
<trans-unit id="ERR_AltInterpolatedVerbatimStringsNotAvailable">
<source>To use '@$' instead of '$@' for an interpolated verbatim string, please use language version '{0}' or greater.</source>
<target state="translated">Um für eine interpolierte ausführliche Zeichenfolge "@$" anstelle von "$@" zu verwenden, benötigen Sie Sprachversion {0} oder höher.</target>
Expand Down Expand Up @@ -117,14 +122,19 @@
<target state="translated">Fehler in der Befehlszeilensyntax: "{0}" ist kein gültiger Wert für die Option "{1}". Der Wert muss im Format "{2}" vorliegen.</target>
<note />
</trans-unit>
<trans-unit id="ERR_CannotConvertAddressOfToDelegate">
<source>Cannot convert a &amp; method group '{0}' to delegate type '{0}'.</source>
<target state="new">Cannot convert a &amp; method group '{0}' to delegate type '{0}'.</target>
<note />
</trans-unit>
<trans-unit id="ERR_CannotUseFunctionPointerAsFixedLocal">
<source>The type of a local declared in a fixed statement cannot be a function pointer type.</source>
<target state="new">The type of a local declared in a fixed statement cannot be a function pointer type.</target>
<note />
</trans-unit>
<trans-unit id="ERR_CannotUseReducedExtensionMethodInAddressOf">
<source>Cannot use a an extension method with a receiver as the target of a '&amp;' operator.</source>
<target state="new">Cannot use a an extension method with a receiver as the target of a '&amp;' operator.</target>
<source>Cannot use an extension method with a receiver as the target of a '&amp;' operator.</source>
<target state="new">Cannot use an extension method with a receiver as the target of a '&amp;' operator.</target>
<note />
</trans-unit>
<trans-unit id="ERR_CantUseInOrOutInArglist">
Expand Down
14 changes: 12 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<target state="new">'&amp;' on method groups cannot be used in expression trees</target>
<note />
</trans-unit>
<trans-unit id="ERR_AddressOfToNonFunctionPointer">
<source>Cannot convert &amp; method group '{0}' to non-function pointer type '{1}'.</source>
<target state="new">Cannot convert &amp; method group '{0}' to non-function pointer type '{1}'.</target>
<note />
</trans-unit>
<trans-unit id="ERR_AltInterpolatedVerbatimStringsNotAvailable">
<source>To use '@$' instead of '$@' for an interpolated verbatim string, please use language version '{0}' or greater.</source>
<target state="translated">Para usar "@$" en lugar de "$@" para una cadena textual interpolada, use la versión "{0}" del lenguaje o una posterior.</target>
Expand Down Expand Up @@ -117,14 +122,19 @@
<target state="translated">Error de sintaxis de la línea de comandos: "{0}" no es un valor válido para la opción "{1}". El valor debe tener el formato "{2}".</target>
<note />
</trans-unit>
<trans-unit id="ERR_CannotConvertAddressOfToDelegate">
<source>Cannot convert a &amp; method group '{0}' to delegate type '{0}'.</source>
<target state="new">Cannot convert a &amp; method group '{0}' to delegate type '{0}'.</target>
<note />
</trans-unit>
<trans-unit id="ERR_CannotUseFunctionPointerAsFixedLocal">
<source>The type of a local declared in a fixed statement cannot be a function pointer type.</source>
<target state="new">The type of a local declared in a fixed statement cannot be a function pointer type.</target>
<note />
</trans-unit>
<trans-unit id="ERR_CannotUseReducedExtensionMethodInAddressOf">
<source>Cannot use a an extension method with a receiver as the target of a '&amp;' operator.</source>
<target state="new">Cannot use a an extension method with a receiver as the target of a '&amp;' operator.</target>
<source>Cannot use an extension method with a receiver as the target of a '&amp;' operator.</source>
<target state="new">Cannot use an extension method with a receiver as the target of a '&amp;' operator.</target>
<note />
</trans-unit>
<trans-unit id="ERR_CantUseInOrOutInArglist">
Expand Down
14 changes: 12 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<target state="new">'&amp;' on method groups cannot be used in expression trees</target>
<note />
</trans-unit>
<trans-unit id="ERR_AddressOfToNonFunctionPointer">
<source>Cannot convert &amp; method group '{0}' to non-function pointer type '{1}'.</source>
<target state="new">Cannot convert &amp; method group '{0}' to non-function pointer type '{1}'.</target>
<note />
</trans-unit>
<trans-unit id="ERR_AltInterpolatedVerbatimStringsNotAvailable">
<source>To use '@$' instead of '$@' for an interpolated verbatim string, please use language version '{0}' or greater.</source>
<target state="translated">Pour utiliser '@$' à la place de '$@' pour une chaîne verbatim interpolée, utilisez la version de langage '{0}' ou une version ultérieure.</target>
Expand Down Expand Up @@ -117,14 +122,19 @@
<target state="translated">Erreur de syntaxe de ligne de commande : '{0}' est une valeur non valide pour l'option '{1}'. La valeur doit se présenter sous la forme '{2}'.</target>
<note />
</trans-unit>
<trans-unit id="ERR_CannotConvertAddressOfToDelegate">
<source>Cannot convert a &amp; method group '{0}' to delegate type '{0}'.</source>
<target state="new">Cannot convert a &amp; method group '{0}' to delegate type '{0}'.</target>
<note />
</trans-unit>
<trans-unit id="ERR_CannotUseFunctionPointerAsFixedLocal">
<source>The type of a local declared in a fixed statement cannot be a function pointer type.</source>
<target state="new">The type of a local declared in a fixed statement cannot be a function pointer type.</target>
<note />
</trans-unit>
<trans-unit id="ERR_CannotUseReducedExtensionMethodInAddressOf">
<source>Cannot use a an extension method with a receiver as the target of a '&amp;' operator.</source>
<target state="new">Cannot use a an extension method with a receiver as the target of a '&amp;' operator.</target>
<source>Cannot use an extension method with a receiver as the target of a '&amp;' operator.</source>
<target state="new">Cannot use an extension method with a receiver as the target of a '&amp;' operator.</target>
<note />
</trans-unit>
<trans-unit id="ERR_CantUseInOrOutInArglist">
Expand Down
14 changes: 12 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<target state="new">'&amp;' on method groups cannot be used in expression trees</target>
<note />
</trans-unit>
<trans-unit id="ERR_AddressOfToNonFunctionPointer">
<source>Cannot convert &amp; method group '{0}' to non-function pointer type '{1}'.</source>
<target state="new">Cannot convert &amp; method group '{0}' to non-function pointer type '{1}'.</target>
<note />
</trans-unit>
<trans-unit id="ERR_AltInterpolatedVerbatimStringsNotAvailable">
<source>To use '@$' instead of '$@' for an interpolated verbatim string, please use language version '{0}' or greater.</source>
<target state="translated">Per usare '@$' invece di '$@' per una stringa verbatim interpolata, usare la versione '{0}' o versioni successive del linguaggio.</target>
Expand Down Expand Up @@ -117,14 +122,19 @@
<target state="translated">Errore di sintassi della riga di comando: '{0}' non è un valore valido per l'opzione '{1}'. Il valore deve essere espresso nel formato '{2}'.</target>
<note />
</trans-unit>
<trans-unit id="ERR_CannotConvertAddressOfToDelegate">
<source>Cannot convert a &amp; method group '{0}' to delegate type '{0}'.</source>
<target state="new">Cannot convert a &amp; method group '{0}' to delegate type '{0}'.</target>
<note />
</trans-unit>
<trans-unit id="ERR_CannotUseFunctionPointerAsFixedLocal">
<source>The type of a local declared in a fixed statement cannot be a function pointer type.</source>
<target state="new">The type of a local declared in a fixed statement cannot be a function pointer type.</target>
<note />
</trans-unit>
<trans-unit id="ERR_CannotUseReducedExtensionMethodInAddressOf">
<source>Cannot use a an extension method with a receiver as the target of a '&amp;' operator.</source>
<target state="new">Cannot use a an extension method with a receiver as the target of a '&amp;' operator.</target>
<source>Cannot use an extension method with a receiver as the target of a '&amp;' operator.</source>
<target state="new">Cannot use an extension method with a receiver as the target of a '&amp;' operator.</target>
<note />
</trans-unit>
<trans-unit id="ERR_CantUseInOrOutInArglist">
Expand Down
14 changes: 12 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<target state="new">'&amp;' on method groups cannot be used in expression trees</target>
<note />
</trans-unit>
<trans-unit id="ERR_AddressOfToNonFunctionPointer">
<source>Cannot convert &amp; method group '{0}' to non-function pointer type '{1}'.</source>
<target state="new">Cannot convert &amp; method group '{0}' to non-function pointer type '{1}'.</target>
<note />
</trans-unit>
<trans-unit id="ERR_AltInterpolatedVerbatimStringsNotAvailable">
<source>To use '@$' instead of '$@' for an interpolated verbatim string, please use language version '{0}' or greater.</source>
<target state="translated">挿入される逐語的文字列で '$@' の代わりに '@$' を使用するには、言語バージョン '{0}' 以上をご使用ください。</target>
Expand Down Expand Up @@ -117,14 +122,19 @@
<target state="translated">コマンドライン構文エラー: '{0}' は、'{1}' オプションの有効な値ではありません。値は '{2}' の形式にする必要があります。</target>
<note />
</trans-unit>
<trans-unit id="ERR_CannotConvertAddressOfToDelegate">
<source>Cannot convert a &amp; method group '{0}' to delegate type '{0}'.</source>
<target state="new">Cannot convert a &amp; method group '{0}' to delegate type '{0}'.</target>
<note />
</trans-unit>
<trans-unit id="ERR_CannotUseFunctionPointerAsFixedLocal">
<source>The type of a local declared in a fixed statement cannot be a function pointer type.</source>
<target state="new">The type of a local declared in a fixed statement cannot be a function pointer type.</target>
<note />
</trans-unit>
<trans-unit id="ERR_CannotUseReducedExtensionMethodInAddressOf">
<source>Cannot use a an extension method with a receiver as the target of a '&amp;' operator.</source>
<target state="new">Cannot use a an extension method with a receiver as the target of a '&amp;' operator.</target>
<source>Cannot use an extension method with a receiver as the target of a '&amp;' operator.</source>
<target state="new">Cannot use an extension method with a receiver as the target of a '&amp;' operator.</target>
<note />
</trans-unit>
<trans-unit id="ERR_CantUseInOrOutInArglist">
Expand Down
Loading

0 comments on commit ac10559

Please sign in to comment.