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

Mono: Replace exception strings with those stored in the resx file (#34056) #78341

Merged
merged 25 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
0d46864
swapping exception strings from resx file
databunks Nov 14, 2022
e276c22
Fixed byref naming/text value
databunks Nov 14, 2022
bf792d9
Fixed ByRef Arguments &
databunks Nov 15, 2022
9838ae5
fixes SRformat only used in exceptions & resx refs
databunks Nov 23, 2022
3eeec78
fixing small typo
databunks Nov 23, 2022
98392ba
de-duplication + suggested changes
databunks Dec 4, 2022
37a5ea7
Remaining strings fix
databunks Dec 16, 2022
7bd6987
global members merge conflict fix
databunks Dec 16, 2022
7f67478
Remaining Merge Conflict fixes
databunks Dec 16, 2022
95168c2
Merge branch 'main' into main
databunks Dec 16, 2022
cf88b72
Apply suggestions from code review
akoeplinger Jan 23, 2023
2af909b
Merge branch 'main' into databunks/main
akoeplinger Mar 20, 2023
4ee4128
Fix merge conflicts
akoeplinger Mar 20, 2023
061e2ea
PR feedback
akoeplinger Mar 20, 2023
44af1ba
Fix Argument_MissingDefaultConstructor usages
akoeplinger Mar 20, 2023
5f8c018
Fix remaining usages of exception strings
akoeplinger Mar 20, 2023
80dd633
Remove unused resources and fix one usage
akoeplinger Mar 20, 2023
1ef9d27
Apply suggestions from code review
akoeplinger Mar 20, 2023
637e1e1
Deduplicate some strings
akoeplinger Mar 20, 2023
01f8e4e
Fixup string
akoeplinger Mar 20, 2023
c1b501c
Fix errors in resource strings
akoeplinger Mar 20, 2023
f38e86e
Update src/libraries/System.Private.CoreLib/src/Resources/Strings.resx
akoeplinger Mar 20, 2023
27fa647
Merge branch main into databunks/main
akoeplinger Mar 21, 2023
5ad8622
Remove unused resource
akoeplinger Mar 21, 2023
6974e7c
A few more fixes
akoeplinger Mar 22, 2023
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 @@ -1189,12 +1189,12 @@ public virtual void ThrowException([DynamicallyAccessedMembers(DynamicallyAccess

if (!excType.IsSubclassOf(typeof(Exception)) && excType != typeof(Exception))
{
throw new ArgumentException(SR.Argument_NotExceptionType);
throw new ArgumentException(SR.Argument_NotExceptionType, nameof(excType));
}
ConstructorInfo? con = excType.GetConstructor(Type.EmptyTypes);
if (con == null)
{
throw new ArgumentException(SR.Argument_MissingDefaultConstructor);
throw new ArgumentException(SR.Arg_NoDefCTorWithoutTypeName, nameof(excType));
}
Emit(OpCodes.Newobj, con);
Emit(OpCodes.Throw);
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/debug/ee/funceval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1708,7 +1708,7 @@ void ResolveFuncEvalGenericArgInfo(DebuggerEval *pDE)
// If this is a new object operation, then we should have a .ctor.
if ((pDE->m_evalType == DB_IPCE_FET_NEW_OBJECT) && !pDE->m_md->IsCtor())
{
COMPlusThrow(kArgumentException, W("Argument_MissingDefaultConstructor"));
COMPlusThrow(kArgumentException, W("Arg_NoDefCTorWithoutTypeName"));
}

pDE->m_md->EnsureActive();
Expand Down
206 changes: 202 additions & 4 deletions src/libraries/System.Private.CoreLib/src/Resources/Strings.resx

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/mono/System.Private.CoreLib/src/Mono/HotReload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public static FieldStore Create (RuntimeTypeHandle type)
else if (t.IsClass || t.IsInterface)
loc = null;
else
throw new ArgumentException("EnC: Expected a primitive, valuetype, class or interface field");
throw new ArgumentException(SR.Arg_EnC);
/* FIXME: do we want FieldStore to be pinned? */
return new FieldStore(loc);
}
Expand Down
2 changes: 1 addition & 1 deletion src/mono/System.Private.CoreLib/src/Mono/RuntimeMarshal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal static string PtrToUtf8String(IntPtr ptr)
}
catch (NullReferenceException)
{
throw new ArgumentOutOfRangeException(nameof(ptr), "Value does not refer to a valid string.");
throw new ArgumentOutOfRangeException(nameof(ptr), SR.ArgumentOutOfRange_InvalidString);
}

return new string((sbyte*)ptr, 0, length, System.Text.Encoding.UTF8);
Expand Down
4 changes: 2 additions & 2 deletions src/mono/System.Private.CoreLib/src/System/Array.Mono.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ private static void Copy(Array sourceArray, int sourceIndex, Array destinationAr
throw new RankException(SR.Rank_MultiDimNotSupported);

if (sourceIndex < 0)
throw new ArgumentOutOfRangeException(nameof(sourceIndex), "Value has to be >= 0.");
throw new ArgumentOutOfRangeException(nameof(sourceIndex), SR.ArgumentOutOfRange_NeedNonNegNum);

if (destinationIndex < 0)
throw new ArgumentOutOfRangeException(nameof(destinationIndex), "Value has to be >= 0.");
throw new ArgumentOutOfRangeException(nameof(destinationIndex), SR.ArgumentOutOfRange_NeedNonNegNum);

var src = sourceArray;
var dst = destinationArray;
Expand Down
14 changes: 7 additions & 7 deletions src/mono/System.Private.CoreLib/src/System/ModuleHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public int MDStreamVersion
get
{
if (value == IntPtr.Zero)
throw new ArgumentNullException(string.Empty, "Invalid handle");
throw new ArgumentNullException(string.Empty, SR.Arg_InvalidHandle);
return RuntimeModule.GetMDStreamVersion(value);
}
}
Expand Down Expand Up @@ -68,10 +68,10 @@ public RuntimeTypeHandle ResolveTypeHandle(int typeToken)
public RuntimeTypeHandle ResolveTypeHandle(int typeToken, RuntimeTypeHandle[]? typeInstantiationContext, RuntimeTypeHandle[]? methodInstantiationContext)
{
if (value == IntPtr.Zero)
throw new ArgumentNullException(string.Empty, "Invalid handle");
throw new ArgumentNullException(string.Empty, SR.Arg_InvalidHandle);
IntPtr res = RuntimeModule.ResolveTypeToken(value, typeToken, ptrs_from_handles(typeInstantiationContext), ptrs_from_handles(methodInstantiationContext), out _);
if (res == IntPtr.Zero)
throw new TypeLoadException(string.Format("Could not load type '0x{0:x}' from assembly '0x{1:x}'", typeToken, value.ToInt64()));
throw new TypeLoadException(SR.Format(SR.ClassLoad_General_Hex, typeToken, value.ToInt64()));
else
return new RuntimeTypeHandle(res);
}
Expand All @@ -80,10 +80,10 @@ public RuntimeTypeHandle ResolveTypeHandle(int typeToken, RuntimeTypeHandle[]? t
public RuntimeMethodHandle ResolveMethodHandle(int methodToken, RuntimeTypeHandle[]? typeInstantiationContext, RuntimeTypeHandle[]? methodInstantiationContext)
{
if (value == IntPtr.Zero)
throw new ArgumentNullException(string.Empty, "Invalid handle");
throw new ArgumentNullException(string.Empty, SR.Arg_InvalidHandle);
IntPtr res = RuntimeModule.ResolveMethodToken(value, methodToken, ptrs_from_handles(typeInstantiationContext), ptrs_from_handles(methodInstantiationContext), out _);
if (res == IntPtr.Zero)
throw new Exception(string.Format("Could not load method '0x{0:x}' from assembly '0x{1:x}'", methodToken, value.ToInt64()));
throw new Exception(SR.Format(SR.ClassLoad_General_Hex, methodToken, value.ToInt64()));
else
return new RuntimeMethodHandle(res);
}
Expand All @@ -92,11 +92,11 @@ public RuntimeMethodHandle ResolveMethodHandle(int methodToken, RuntimeTypeHandl
public RuntimeFieldHandle ResolveFieldHandle(int fieldToken, RuntimeTypeHandle[]? typeInstantiationContext, RuntimeTypeHandle[]? methodInstantiationContext)
{
if (value == IntPtr.Zero)
throw new ArgumentNullException(string.Empty, "Invalid handle");
throw new ArgumentNullException(string.Empty, SR.Arg_InvalidHandle);

IntPtr res = RuntimeModule.ResolveFieldToken(value, fieldToken, ptrs_from_handles(typeInstantiationContext), ptrs_from_handles(methodInstantiationContext), out _);
if (res == IntPtr.Zero)
throw new Exception(string.Format("Could not load field '0x{0:x}' from assembly '0x{1:x}'", fieldToken, value.ToInt64()));
throw new Exception(SR.Format(SR.ClassLoad_General_Hex, fieldToken, value.ToInt64()));
else
return new RuntimeFieldHandle(res);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,11 @@ private void Initialize(ConstructorInfo con, object?[] constructorArgs,
{
Type t = fi.DeclaringType!;
if ((atype != t) && (!t.IsSubclassOf(atype)) && (!atype.IsSubclassOf(t)))
throw new ArgumentException("Field '" + fi.Name + "' does not belong to the same class as the constructor");
throw new ArgumentException(SR.Format(SR.Argument_FieldDoesNotBelongToConstructorClass, fi.Name));
if (!IsValidType(fi.FieldType))
throw new ArgumentException("Field '" + fi.Name + "' does not have a valid type.");
throw new ArgumentException(SR.Format(SR.Argument_FieldDoesNotHaveAValidType, fi.Name));
if (!IsValidValue(fi.FieldType, fieldValues[i]))
throw new ArgumentException("Field " + fi.Name + " is not a valid value.");
throw new ArgumentException(SR.Format(SR.Argument_FieldDoesNotHaveAValidValue, fi.Name));
// FIXME: Check enums and TypeBuilders as well
if (fieldValues[i] != null)
// IsEnum does not seem to work on TypeBuilders
Expand All @@ -215,7 +215,7 @@ private void Initialize(ConstructorInfo con, object?[] constructorArgs,
// MS.NET allows this
//
if (!fi.FieldType.IsArray)
throw new ArgumentException("Value of field '" + fi.Name + "' does not match field type: " + fi.FieldType);
throw new ArgumentException(SR.Format(SR.Argument_UnmatchedFieldValueAndType, fi.Name, fi.FieldType));
}
i++;
}
Expand All @@ -224,19 +224,19 @@ private void Initialize(ConstructorInfo con, object?[] constructorArgs,
foreach (PropertyInfo pi in namedProperties)
{
if (!pi.CanWrite)
throw new ArgumentException("Property '" + pi.Name + "' does not have a setter.");
throw new ArgumentException(SR.Format(SR.Argument_PropertyMissingSetter, pi.Name));
Type t = pi.DeclaringType!;
if ((atype != t) && (!t.IsSubclassOf(atype)) && (!atype.IsSubclassOf(t)))
throw new ArgumentException("Property '" + pi.Name + "' does not belong to the same class as the constructor");
throw new ArgumentException(SR.Format(SR.Argument_PropertyClassUnmatchedWithConstructor, pi.Name));
if (!IsValidType(pi.PropertyType))
throw new ArgumentException("Property '" + pi.Name + "' does not have a valid type.");
throw new ArgumentException(SR.Format(SR.Argument_PropertyInvalidType, pi.Name));
if (!IsValidValue(pi.PropertyType, propertyValues[i]))
throw new ArgumentException("Property " + pi.Name + " is not a valid value.");
throw new ArgumentException(SR.Format(SR.Argument_PropertyInvalidValue, pi.Name));
if (propertyValues[i] != null)
{
if (!(pi.PropertyType is TypeBuilder) && !pi.PropertyType.IsEnum && !pi.PropertyType.IsInstanceOfType(propertyValues[i]))
if (!pi.PropertyType.IsArray)
throw new ArgumentException("Value of property '" + pi.Name + "' does not match property type: " + pi.PropertyType + " -> " + propertyValues[i]);
throw new ArgumentException(SR.Format(SR.Argument_PropertyUnmatchingPropertyType, pi.Name, pi.PropertyType, propertyValues[i]));
}
i++;
}
Expand All @@ -248,17 +248,17 @@ private void Initialize(ConstructorInfo con, object?[] constructorArgs,
{
Type paramType = pi.ParameterType;
if (!IsValidType(paramType))
throw new ArgumentException("Parameter " + i + " does not have a valid type.");
throw new ArgumentException(SR.Format(SR.Argument_ParameterInvalidType, i));
if (!IsValidValue(paramType, constructorArgs[i]))
throw new ArgumentException("Parameter " + i + " is not a valid value.");
throw new ArgumentException(SR.Format(SR.Argument_ParameterInvalidValue, i));

if (constructorArgs[i] != null)
{
if (!(paramType is TypeBuilder) && !paramType.IsEnum && !paramType.IsInstanceOfType(constructorArgs[i]))
if (!paramType.IsArray)
throw new ArgumentException("Value of argument " + i + " does not match parameter type: " + paramType + " -> " + constructorArgs[i]);
throw new ArgumentException(SR.Format(SR.Argument_ParameterHasUnmatchedArgumentValue, i, paramType, constructorArgs[i]));
if (!IsValidParam(constructorArgs[i]!, paramType))
throw new ArgumentException("Cannot emit a CustomAttribute with argument of type " + constructorArgs[i]!.GetType() + ".");
throw new ArgumentException(SR.Format(SR.Argument_BadParameterTypeForCAB, constructorArgs[i]!.GetType()));
}
}
i++;
Expand Down Expand Up @@ -403,7 +403,7 @@ internal static UnmanagedMarshal get_umarshal(CustomAttributeBuilder customBuild
marshalCookie = decode_string(data, pos, out pos)!;
break;
default:
throw new Exception("Unknown MarshalAsAttribute field: " + named_name);
throw new Exception(SR.Format(SR.Exception_UnknownMarshalAsAttributeField, named_name));
}
}

Expand All @@ -420,7 +420,7 @@ internal static UnmanagedMarshal get_umarshal(CustomAttributeBuilder customBuild
#endif
case UnmanagedType.ByValArray:
if (!is_field)
throw new ArgumentException("Specified unmanaged type is only valid on fields");
throw new ArgumentException(SR.Argument_UnmanagedMemAccessorWrapAround);

return UnmanagedMarshal.DefineByValArray(sizeConst);
case UnmanagedType.ByValTStr:
Expand Down Expand Up @@ -451,7 +451,7 @@ private static Type elementTypeToType(int elementType) =>
0x0c => typeof(float),
0x0d => typeof(double),
0x0e => typeof(string),
_ => throw new Exception("Unknown element type '" + elementType + "'"),
_ => throw new Exception(SR.Format(SR.ArgumentException_InvalidArrayElementType, elementType)),
};

private static object? decode_cattr_value(Type t, byte[] data, int pos, out int rpos)
Expand Down Expand Up @@ -480,7 +480,7 @@ private static Type elementTypeToType(int elementType) =>
if (subtype >= 0x02 && subtype <= 0x0e)
return decode_cattr_value(elementTypeToType(subtype), data, pos, out rpos);
else
throw new Exception("Subtype '" + subtype + "' of type object not yet handled in decode_cattr_value");
throw new Exception(SR.Exception_UnhandledSubType);
default:
throw new Exception("FIXME: Type " + t + " not yet handled in decode_cattr_value.");
}
Expand Down Expand Up @@ -508,9 +508,9 @@ internal static CustomAttributeInfo decode_cattr(CustomAttributeBuilder customBu

// Prolog
if (data.Length < 2)
throw new Exception("Custom attr length is only '" + data.Length + "'");
throw new Exception(SR.Format(SR.Exception_InvalidCustomAttributeLength, data.Length));
if ((data[0] != 0x1) || (data[1] != 0x00))
throw new Exception("Prolog invalid");
throw new Exception(SR.Exception_InvalidProlog);
pos = 2;

ParameterInfo[] pi = GetParameters(ctor);
Expand Down Expand Up @@ -547,7 +547,7 @@ internal static CustomAttributeInfo decode_cattr(CustomAttributeBuilder customBu
/* Field */
FieldInfo? fi = ctor.DeclaringType!.GetField(name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
if (fi == null)
throw new Exception("Custom attribute type '" + ctor.DeclaringType + "' doesn't contain a field named '" + name + "'");
throw new Exception(SR.Format(SR.Exception_EmptyFieldForCustomAttributeType, ctor.DeclaringType, name));

object? val = decode_cattr_value(fi.FieldType, data, pos, out pos);
if (enum_type_name != null)
Expand All @@ -560,7 +560,7 @@ internal static CustomAttributeInfo decode_cattr(CustomAttributeBuilder customBu
}
else
// FIXME:
throw new Exception("Unknown named type: " + named_type);
throw new Exception(SR.Format(SR.Exception_UnknownNamedType, named_type));
}

return info;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public ILGenerator GetILGenerator(int streamSize) =>
}
catch (MethodAccessException mae)
{
throw new TargetInvocationException("Method cannot be invoked.", mae);
throw new TargetInvocationException(SR.TargetInvocation_MethodCannotBeInvoked, mae);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ public virtual void BeginCatchBlock(Type? exceptionType)
if (!InExceptionBlock)
throw new NotSupportedException(SR.Argument_NotInExceptionBlock);
if (exceptionType != null && exceptionType.IsUserType)
throw new NotSupportedException("User defined subclasses of System.Type are not yet supported.");
throw new NotSupportedException(SR.PlatformNotSupported_ITypeInfo);
if (ex_handlers![cur_block].LastClauseType() == ILExceptionBlock.FILTER_START)
{
if (exceptionType != null)
Expand Down Expand Up @@ -446,7 +446,7 @@ public virtual LocalBuilder DeclareLocal(Type localType, bool pinned)
{
ArgumentNullException.ThrowIfNull(localType);
if (localType.IsUserType)
throw new NotSupportedException("User defined subclasses of System.Type are not yet supported.");
throw new NotSupportedException(SR.PlatformNotSupported_ITypeInfo);
LocalBuilder res = new LocalBuilder(localType, this);
res.is_pinned = pinned;

Expand Down Expand Up @@ -797,7 +797,7 @@ public virtual void EmitCall(OpCode opcode, MethodInfo methodInfo, Type[]? optio
{
if ((methodInfo.CallingConvention & CallingConventions.VarArgs) == 0)
{
throw new InvalidOperationException("Method is not VarArgs method and optional types were passed");
throw new InvalidOperationException(SR.InvalidOperation_NotAVarArgCallingConvention);
}

int token = token_gen.GetToken(methodInfo, optionalParameterTypes);
Expand Down Expand Up @@ -901,7 +901,7 @@ public virtual void ThrowException([DynamicallyAccessedMembers(DynamicallyAccess
throw new ArgumentException(SR.Argument_NotExceptionType, nameof(excType));
ConstructorInfo? ctor = excType.GetConstructor(Type.EmptyTypes);
if (ctor == null)
throw new ArgumentException(SR.Argument_MissingDefaultConstructor, nameof(excType));
throw new ArgumentException(SR.Arg_NoDefCTorWithoutTypeName, nameof(excType));
Emit(OpCodes.Newobj, ctor);
Emit(OpCodes.Throw);
}
Expand All @@ -917,7 +917,7 @@ internal void label_fixup(MethodBase mb)
for (int i = 0; i < num_fixups; ++i)
{
if (labels![fixups![i].label_idx].addr < 0)
throw new ArgumentException(string.Format("Label #{0} is not marked in method `{1}'", fixups[i].label_idx + 1, mb.Name));
throw new ArgumentException(SR.Format(SR.Argument_LabelUnmarked, fixups[i].label_idx + 1, mb.Name));
// Diff is the offset from the end of the jump instruction to the address of the label
int diff = labels[fixups[i].label_idx].addr - (fixups[i].pos + fixups[i].offset);
if (fixups[i].offset == 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ public override bool ContainsGenericParameters
public override MethodInfo MakeGenericMethod(params Type[] typeArgs)
{
if (!_method.IsGenericMethodDefinition || (_typeArguments != null))
throw new InvalidOperationException("Method is not a generic method definition");
throw new InvalidOperationException(SR.Argument_NeedGenericMethodDefinition);

ArgumentNullException.ThrowIfNull(typeArgs);

if (_method.GetGenericArguments().Length != typeArgs.Length)
throw new ArgumentException("Incorrect length", nameof(typeArgs));
throw new ArgumentException(SR.Format(SR.Argument_NotEnoughGenArguments, _method.GetGenericArguments().Length, typeArgs.Length));

foreach (Type type in typeArgs)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ internal RuntimeAssemblyBuilder(AssemblyName n, AssemblyBuilderAccess access)
aname = (AssemblyName)n.Clone();

if (!Enum.IsDefined(typeof(AssemblyBuilderAccess), access))
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture,
throw new ArgumentException(SR.Format(CultureInfo.InvariantCulture,
"Argument value {0} is not valid.", (int)access),
nameof(access));

Expand Down
Loading