From 0025dd2285fd8056cc50072380260167b73f0719 Mon Sep 17 00:00:00 2001 From: Jens Geyer Date: Thu, 5 Nov 2026 00:07:57 +0100 Subject: [PATCH] THRIFT-4833: Mark generated netstd properties that hide inherited members as new Client: netstd A field named like a member of System.Exception, such as message or data, became a property that hides that member, with warning CS0114 or CS0108. Such properties, and those named like a member of System.Object, are now declared new. The Thrift.Compile projects treat CS0108, CS0109 and CS0114 as errors. Co-Authored-By: Claude Opus 5.5 --- .../src/thrift/generate/t_netstd_generator.cc | 29 +++- .../src/thrift/generate/t_netstd_generator.h | 5 +- .../Thrift.Compile.net10.csproj | 3 + .../Thrift.Compile.net8.csproj | 3 + .../Thrift.Compile.net9.csproj | 3 + .../Thrift.Compile.netstd2.csproj | 3 + .../Thrift.Compile.Tests/Thrift4833.thrift | 75 ++++++++++ .../DataModel/InheritedMemberNames.cs | 129 ++++++++++++++++++ 8 files changed, 244 insertions(+), 6 deletions(-) create mode 100644 lib/netstd/Tests/Thrift.Compile.Tests/Thrift4833.thrift create mode 100644 lib/netstd/Tests/Thrift.Tests/DataModel/InheritedMemberNames.cs diff --git a/compiler/cpp/src/thrift/generate/t_netstd_generator.cc b/compiler/cpp/src/thrift/generate/t_netstd_generator.cc index 92b0f6830df..83db8f143cd 100644 --- a/compiler/cpp/src/thrift/generate/t_netstd_generator.cc +++ b/compiler/cpp/src/thrift/generate/t_netstd_generator.cc @@ -1239,7 +1239,7 @@ void t_netstd_generator::generate_netstd_struct_definition(ostream& out, t_struc for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { generate_netstd_doc(out, *m_iter); - generate_property(out, *m_iter, true, true); + generate_property(out, *m_iter, true, true, is_exception); bool is_required = field_is_required((*m_iter)); if (is_required) { @@ -3379,12 +3379,12 @@ void t_netstd_generator::generate_serialize_list_element(ostream& out, t_list* t generate_serialize_field(out, &efield, "", false, false); } -void t_netstd_generator::generate_property(ostream& out, t_field* tfield, bool isPublic, bool generateIsset) +void t_netstd_generator::generate_property(ostream& out, t_field* tfield, bool isPublic, bool generateIsset, bool in_exception) { - generate_netstd_property(out, tfield, isPublic, generateIsset, "_"); + generate_netstd_property(out, tfield, isPublic, generateIsset, "_", in_exception); } -void t_netstd_generator::generate_netstd_property(ostream& out, t_field* tfield, bool isPublic, bool generateIsset, string fieldPrefix) +void t_netstd_generator::generate_netstd_property(ostream& out, t_field* tfield, bool isPublic, bool generateIsset, string fieldPrefix, bool in_exception) { if ((is_serialize_enabled() || is_wcf_enabled()) && isPublic) { @@ -3394,6 +3394,7 @@ void t_netstd_generator::generate_netstd_property(ostream& out, t_field* tfield, out << indent() << (isPublic ? "public " : "private ") + << (hides_inherited_member(prop_name(tfield), in_exception) ? "new " : "") << type_name(tfield->get_type()) << nullable_field_suffix(tfield) << " " @@ -3654,6 +3655,26 @@ string t_netstd_generator::get_isset_name(const string& str) { } +// Whether a member of that name hides one that every generated class inherits from System.Object, +// or that an exception inherits from System.Exception. Such a member is declared "new", which keeps +// the inherited one intact (CS0108, CS0114). Equals(), GetHashCode() and ToString() are left out, +// as the generated classes declare them themselves, and so is Finalize(), which cannot be hidden. +bool t_netstd_generator::hides_inherited_member(const string& name, bool in_exception) { + static const std::set object_members = { + "GetType", "MemberwiseClone", "ReferenceEquals" + }; + static const std::set exception_members = { + "Data", "GetBaseException", "GetObjectData", "HelpLink", "HResult", "InnerException", + "Message", "SerializeObjectState", "Source", "StackTrace", "TargetSite" + }; + + if (object_members.find(name) != object_members.end()) { + return true; + } + return in_exception && (exception_members.find(name) != exception_members.end()); +} + + string t_netstd_generator::prop_name(t_field* tfield, bool suppress_mapping) { string name(tfield->get_name()); if (suppress_mapping) { diff --git a/compiler/cpp/src/thrift/generate/t_netstd_generator.h b/compiler/cpp/src/thrift/generate/t_netstd_generator.h index e4a300be871..e18ce920fca 100644 --- a/compiler/cpp/src/thrift/generate/t_netstd_generator.h +++ b/compiler/cpp/src/thrift/generate/t_netstd_generator.h @@ -82,8 +82,8 @@ class t_netstd_generator : public t_oop_generator // additional files void generate_extensions_file(); - void generate_property(ostream& out, t_field* tfield, bool isPublic, bool generateIsset); - void generate_netstd_property(ostream& out, t_field* tfield, bool isPublic, bool includeIsset = true, string fieldPrefix = ""); + void generate_property(ostream& out, t_field* tfield, bool isPublic, bool generateIsset, bool in_exception = false); + void generate_netstd_property(ostream& out, t_field* tfield, bool isPublic, bool includeIsset = true, string fieldPrefix = "", bool in_exception = false); bool print_const_value(ostream& out, string name, t_type* type, t_const_value* value, bool in_static, bool defval = false, bool needtype = false); string render_const_value(ostream& out, string name, t_type* type, t_const_value* value); void print_const_constructor(ostream& out, vector consts); @@ -210,6 +210,7 @@ class t_netstd_generator : public t_oop_generator void cleanup_member_name_mapping(void* scope); string get_mapped_member_name(string oldname); string get_isset_name(const string& str); + bool hides_inherited_member(const string& name, bool in_exception); string get_deep_copy_method_call(t_type* ttype, bool is_not_null, bool& needs_typecast, string& suffix); string deep_copy_expression(const string& source, t_type* ttype, bool is_not_null, bool& needs_typecast, bool null_conditional = false); string extensions_class_name(t_program* program); diff --git a/lib/netstd/Tests/Thrift.Compile.Tests/Thrift.Compile.net10/Thrift.Compile.net10.csproj b/lib/netstd/Tests/Thrift.Compile.Tests/Thrift.Compile.net10/Thrift.Compile.net10.csproj index f51faf6d4a9..ed67ac463ee 100644 --- a/lib/netstd/Tests/Thrift.Compile.Tests/Thrift.Compile.net10/Thrift.Compile.net10.csproj +++ b/lib/netstd/Tests/Thrift.Compile.Tests/Thrift.Compile.net10/Thrift.Compile.net10.csproj @@ -31,6 +31,8 @@ false false enable + + $(WarningsAsErrors);CS0108;CS0109;CS0114 @@ -80,6 +82,7 @@ + diff --git a/lib/netstd/Tests/Thrift.Compile.Tests/Thrift.Compile.net8/Thrift.Compile.net8.csproj b/lib/netstd/Tests/Thrift.Compile.Tests/Thrift.Compile.net8/Thrift.Compile.net8.csproj index 4beeefdf8bf..38bc6ad5c18 100644 --- a/lib/netstd/Tests/Thrift.Compile.Tests/Thrift.Compile.net8/Thrift.Compile.net8.csproj +++ b/lib/netstd/Tests/Thrift.Compile.Tests/Thrift.Compile.net8/Thrift.Compile.net8.csproj @@ -31,6 +31,8 @@ false false enable + + $(WarningsAsErrors);CS0108;CS0109;CS0114 @@ -81,6 +83,7 @@ + diff --git a/lib/netstd/Tests/Thrift.Compile.Tests/Thrift.Compile.net9/Thrift.Compile.net9.csproj b/lib/netstd/Tests/Thrift.Compile.Tests/Thrift.Compile.net9/Thrift.Compile.net9.csproj index 555be8ff80a..7cb8069a4ce 100644 --- a/lib/netstd/Tests/Thrift.Compile.Tests/Thrift.Compile.net9/Thrift.Compile.net9.csproj +++ b/lib/netstd/Tests/Thrift.Compile.Tests/Thrift.Compile.net9/Thrift.Compile.net9.csproj @@ -31,6 +31,8 @@ false false enable + + $(WarningsAsErrors);CS0108;CS0109;CS0114 @@ -81,6 +83,7 @@ + diff --git a/lib/netstd/Tests/Thrift.Compile.Tests/Thrift.Compile.netstd2/Thrift.Compile.netstd2.csproj b/lib/netstd/Tests/Thrift.Compile.Tests/Thrift.Compile.netstd2/Thrift.Compile.netstd2.csproj index f7dea3c525a..87115f17027 100644 --- a/lib/netstd/Tests/Thrift.Compile.Tests/Thrift.Compile.netstd2/Thrift.Compile.netstd2.csproj +++ b/lib/netstd/Tests/Thrift.Compile.Tests/Thrift.Compile.netstd2/Thrift.Compile.netstd2.csproj @@ -30,6 +30,8 @@ false false false + + $(WarningsAsErrors);CS0108;CS0109;CS0114 @@ -72,6 +74,7 @@ + diff --git a/lib/netstd/Tests/Thrift.Compile.Tests/Thrift4833.thrift b/lib/netstd/Tests/Thrift.Compile.Tests/Thrift4833.thrift new file mode 100644 index 00000000000..d1d674357ef --- /dev/null +++ b/lib/netstd/Tests/Thrift.Compile.Tests/Thrift4833.thrift @@ -0,0 +1,75 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +// Testcase for THRIFT-4833 warning CS0114 "Xception.Message" hides inherited member "Exception.Message" +// +// A field becomes a property of the same name, starting with an uppercase letter. Every generated +// class inherits GetType(), MemberwiseClone() and ReferenceEquals() from System.Object, and an +// exception inherits the members of System.Exception as well. Finalize() cannot be hidden. + +namespace * Thrift4833 + +exception InheritedNames { + 1: string message + 2: string data + 3: string source + 4: string helpLink + 5: string stackTrace + 6: string innerException + 7: i32 hResult + 8: string targetSite + 9: string getBaseException + 10: string getObjectData + 11: string getType + 12: string serializeObjectState + 13: string memberwiseClone + 14: string referenceEquals + 15: string finalize +} + +exception RequiredInheritedNames { + 1: required string message + 2: required string data + 3: required string source + 4: required string helpLink + 5: required string stackTrace + 6: required string innerException + 7: required i32 hResult + 8: required string targetSite + 9: required string getBaseException + 10: required string getObjectData + 11: required string getType + 12: required string serializeObjectState + 13: required string memberwiseClone + 14: required string referenceEquals + 15: required string finalize +} + +struct ObjectNames { + 1: string getType + 2: string memberwiseClone + 3: string referenceEquals + 4: string finalize + 5: string message + 6: string data +} + +service InheritedNamesService { + string Call(1: string getType, 2: string memberwiseClone, 3: string referenceEquals, 4: string message) throws (1: InheritedNames getType) +} diff --git a/lib/netstd/Tests/Thrift.Tests/DataModel/InheritedMemberNames.cs b/lib/netstd/Tests/Thrift.Tests/DataModel/InheritedMemberNames.cs new file mode 100644 index 00000000000..1b84132bb56 --- /dev/null +++ b/lib/netstd/Tests/Thrift.Tests/DataModel/InheritedMemberNames.cs @@ -0,0 +1,129 @@ +// Licensed to the Apache Software Foundation(ASF) under one +// or more contributor license agreements.See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership.The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +using System; +using System.Collections; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Thrift4833; +using ThriftTest; +using ErrorData = Thrift5320.exceptions.ErrorData; +using TaskException = Thrift5320.exceptions.Task; + +namespace Thrift.Tests.DataModel +{ + // THRIFT-4833: a field whose name is that of an inherited member hides that member. The generated + // class returns the field, while the inherited member stays what it is, seen through the base class. + [TestClass] + public class InheritedMemberNamesTests + { + [TestMethod] + public void FieldNamedMessage_HidesExceptionMessage() + { + var xception = new Xception { ErrorCode = 1001, Message = "This is an Xception" }; + Assert.AreEqual("This is an Xception", xception.Message); + + Exception exception = xception; + Assert.AreEqual(((Exception)new Xception()).Message, exception.Message); + Assert.AreNotEqual(xception.Message, exception.Message); + } + + [TestMethod] + public void FieldNamedData_HidesExceptionData() + { + var data = new ErrorData { Messitsch = "details" }; + var task = new TaskException { Data = data }; + Assert.AreSame(data, task.Data); + + Exception exception = task; + Assert.IsInstanceOfType(exception.Data); + } + + [TestMethod] + public void FieldsNamedAfterInheritedMembers_HideThem() + { + var names = new InheritedNames + { + Message = "message", + Data = "data", + Source = "source", + HelpLink = "helpLink", + StackTrace = "stackTrace", + InnerException = "innerException", + HResult = 42, + TargetSite = "targetSite", + GetBaseException = "getBaseException", + GetObjectData = "getObjectData", + GetType = "getType", + SerializeObjectState = "serializeObjectState", + MemberwiseClone = "memberwiseClone", + ReferenceEquals = "referenceEquals", + Finalize = "finalize", + }; + + Exception thrown; + try + { + throw names; + } + catch (InheritedNames caught) + { + thrown = caught; + } + Assert.AreSame(names, thrown); + + // the fields + Assert.AreEqual("message", names.Message); + Assert.AreEqual("data", names.Data); + Assert.AreEqual("source", names.Source); + Assert.AreEqual("helpLink", names.HelpLink); + Assert.AreEqual("stackTrace", names.StackTrace); + Assert.AreEqual("innerException", names.InnerException); + Assert.AreEqual(42, names.HResult); + Assert.AreEqual("targetSite", names.TargetSite); + Assert.AreEqual("getBaseException", names.GetBaseException); + Assert.AreEqual("getObjectData", names.GetObjectData); + Assert.AreEqual("getType", names.GetType); + Assert.AreEqual("serializeObjectState", names.SerializeObjectState); + Assert.AreEqual("memberwiseClone", names.MemberwiseClone); + Assert.AreEqual("referenceEquals", names.ReferenceEquals); + Assert.AreEqual("finalize", names.Finalize); + + // the inherited members + Assert.AreEqual(((Exception)new InheritedNames()).Message, thrown.Message); + Assert.IsInstanceOfType(thrown.Data); + Assert.AreEqual(typeof(InheritedMemberNamesTests).Assembly.GetName().Name, thrown.Source); + Assert.IsNull(thrown.HelpLink); + Assert.IsNotNull(thrown.StackTrace); + Assert.Contains(nameof(FieldsNamedAfterInheritedMembers_HideThem), thrown.StackTrace); + Assert.IsNull(thrown.InnerException); + Assert.AreEqual(new Exception().HResult, thrown.HResult); + Assert.AreEqual(nameof(FieldsNamedAfterInheritedMembers_HideThem), thrown.TargetSite!.Name); + Assert.AreSame(thrown, thrown.GetBaseException()); + Assert.AreEqual(typeof(InheritedNames), thrown.GetType()); + } + + [TestMethod] + public void StructFieldsNamedAfterObjectMembers_HideThem() + { + var names = new ObjectNames { GetType = "getType", MemberwiseClone = "memberwiseClone", ReferenceEquals = "referenceEquals" }; + Assert.AreEqual("getType", names.GetType); + Assert.AreEqual("memberwiseClone", names.MemberwiseClone); + Assert.AreEqual("referenceEquals", names.ReferenceEquals); + Assert.AreEqual(typeof(ObjectNames), ((object)names).GetType()); + } + } +}