Merge pull request #70483 from raulsntos/dotnet/forbid-init-properties

C#: Disallow init-only properties
This commit is contained in:
Ignacio Roldán Etcheverry 2022-12-23 21:13:32 +01:00 committed by GitHub
commit a545c29ace
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 7 deletions

View File

@ -295,8 +295,8 @@ namespace Godot.SourceGenerators
foreach (var property in properties) foreach (var property in properties)
{ {
// TODO: We should still restore read-only properties after reloading assembly. Two possible ways: reflection or turn RestoreGodotObjectData into a constructor overload. // TODO: We should still restore read-only properties after reloading assembly. Two possible ways: reflection or turn RestoreGodotObjectData into a constructor overload.
// Ignore properties without a getter or without a setter. Godot properties must be both readable and writable. // Ignore properties without a getter, without a setter or with an init-only setter. Godot properties must be both readable and writable.
if (property.IsWriteOnly || property.IsReadOnly) if (property.IsWriteOnly || property.IsReadOnly || property.SetMethod!.IsInitOnly)
continue; continue;
var marshalType = MarshalUtils.ConvertManagedTypeToMarshalType(property.Type, typeCache); var marshalType = MarshalUtils.ConvertManagedTypeToMarshalType(property.Type, typeCache);

View File

@ -158,7 +158,7 @@ namespace Godot.SourceGenerators
// Generate SetGodotClassPropertyValue // Generate SetGodotClassPropertyValue
bool allPropertiesAreReadOnly = godotClassFields.All(fi => fi.FieldSymbol.IsReadOnly) && bool allPropertiesAreReadOnly = godotClassFields.All(fi => fi.FieldSymbol.IsReadOnly) &&
godotClassProperties.All(pi => pi.PropertySymbol.IsReadOnly); godotClassProperties.All(pi => pi.PropertySymbol.IsReadOnly || pi.PropertySymbol.SetMethod!.IsInitOnly);
if (!allPropertiesAreReadOnly) if (!allPropertiesAreReadOnly)
{ {
@ -168,7 +168,7 @@ namespace Godot.SourceGenerators
isFirstEntry = true; isFirstEntry = true;
foreach (var property in godotClassProperties) foreach (var property in godotClassProperties)
{ {
if (property.PropertySymbol.IsReadOnly) if (property.PropertySymbol.IsReadOnly || property.PropertySymbol.SetMethod!.IsInitOnly)
continue; continue;
GeneratePropertySetter(property.PropertySymbol.Name, GeneratePropertySetter(property.PropertySymbol.Name,
@ -407,7 +407,7 @@ namespace Godot.SourceGenerators
return null; return null;
} }
if (propertySymbol.SetMethod == null) if (propertySymbol.SetMethod == null || propertySymbol.SetMethod.IsInitOnly)
{ {
// This should never happen, as we filtered ReadOnly properties, but just in case. // This should never happen, as we filtered ReadOnly properties, but just in case.
Common.ReportExportedMemberIsReadOnly(context, propertySymbol); Common.ReportExportedMemberIsReadOnly(context, propertySymbol);

View File

@ -138,14 +138,14 @@ namespace Godot.SourceGenerators
} }
// TODO: We should still restore read-only properties after reloading assembly. Two possible ways: reflection or turn RestoreGodotObjectData into a constructor overload. // TODO: We should still restore read-only properties after reloading assembly. Two possible ways: reflection or turn RestoreGodotObjectData into a constructor overload.
// Ignore properties without a getter or without a setter. Godot properties must be both readable and writable. // Ignore properties without a getter, without a setter or with an init-only setter. Godot properties must be both readable and writable.
if (property.IsWriteOnly) if (property.IsWriteOnly)
{ {
Common.ReportExportedMemberIsWriteOnly(context, property); Common.ReportExportedMemberIsWriteOnly(context, property);
continue; continue;
} }
if (property.IsReadOnly) if (property.IsReadOnly || property.SetMethod!.IsInitOnly)
{ {
Common.ReportExportedMemberIsReadOnly(context, property); Common.ReportExportedMemberIsReadOnly(context, property);
continue; continue;