From aa62d9b79030de2707cb56cde4b3f62f04592f36 Mon Sep 17 00:00:00 2001 From: yowl Date: Wed, 11 Feb 2026 23:13:13 -0500 Subject: [PATCH 1/3] If the variant case and variant name have the same c# names, then add an underscore to the case name --- crates/csharp/src/function.rs | 6 ++++-- crates/csharp/src/interface.rs | 12 +++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/crates/csharp/src/function.rs b/crates/csharp/src/function.rs index 1f70a7f1f..075393b9a 100644 --- a/crates/csharp/src/function.rs +++ b/crates/csharp/src/function.rs @@ -1,5 +1,5 @@ use crate::csharp_ident::ToCSharpIdent; -use crate::interface::{InterfaceGenerator, ParameterType}; +use crate::interface::{InterfaceGenerator, ParameterType, variant_new_func_name}; use crate::world_generator::CSharp; use heck::ToUpperCamelCase; use std::fmt::Write; @@ -167,6 +167,7 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> { .blocks .drain(self.blocks.len() - cases.len()..) .collect::>(); + let variant_name = self.interface_gen.type_name_with_qualifier(ty, false); let ty = self.interface_gen.type_name_with_qualifier(ty, true); let generics_position = ty.find('<'); let lifted = self.locals.tmp("lifted"); @@ -198,7 +199,8 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> { String::new() }; - let method = case_name.to_csharp_ident_upper(); + let method = + variant_new_func_name(&variant_name, &case_name.to_csharp_ident_upper()); let call = if let Some(position) = generics_position { let (ty, generics) = ty.split_at(position); diff --git a/crates/csharp/src/interface.rs b/crates/csharp/src/interface.rs index adf5d383b..4b405c596 100644 --- a/crates/csharp/src/interface.rs +++ b/crates/csharp/src/interface.rs @@ -1409,6 +1409,7 @@ impl<'a> CoreInterfaceGenerator<'a> for InterfaceGenerator<'a> { .map(|case| { let case_name = case.name.to_csharp_ident(); let tag = case.name.to_csharp_ident_upper(); + let method_name = variant_new_func_name(&name, &tag); let (parameter, argument) = if let Some(ty) = self.non_empty_type(case.ty.as_ref()) { ( @@ -1420,7 +1421,7 @@ impl<'a> CoreInterfaceGenerator<'a> for InterfaceGenerator<'a> { }; format!( - "{access} static {name} {tag}({parameter}) {{ + "{access} static {name} {method_name}({parameter}) {{ return new {name}(Tags.{tag}, {argument}); }} " @@ -1571,6 +1572,15 @@ impl<'a> CoreInterfaceGenerator<'a> for InterfaceGenerator<'a> { } } +// Handles the tag being the same name as the variant, which would cause a method with the same name as the type in C# which is not valid. +pub fn variant_new_func_name(variant_name: &String, tag: &String) -> String { + if *tag == *variant_name { + format!("{tag}_") // Underscores are not valid in wit identifiers so this should be safe. + } else { + tag.clone() + } +} + #[derive(Copy, Clone, PartialEq, Eq, Debug)] pub(crate) enum ParameterType { ABI, From 70647863cb90db607392432a467ee3c6ac0389f2 Mon Sep 17 00:00:00 2001 From: yowl Date: Thu, 12 Feb 2026 20:22:04 +0000 Subject: [PATCH 2/3] Add test for variant case with same name as variant --- tests/codegen/issue1514-6.wit | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 tests/codegen/issue1514-6.wit diff --git a/tests/codegen/issue1514-6.wit b/tests/codegen/issue1514-6.wit new file mode 100644 index 000000000..309e7b837 --- /dev/null +++ b/tests/codegen/issue1514-6.wit @@ -0,0 +1,12 @@ +package demo:poc; + +interface i { + variant v { + v, + } + f: func(x: v); +} + +world w { + export i; +} \ No newline at end of file From d9c215480b74d26ab835eefbded9480c51858ba7 Mon Sep 17 00:00:00 2001 From: yowl Date: Thu, 12 Feb 2026 20:46:24 +0000 Subject: [PATCH 3/3] exclude known c++ failure --- crates/test/src/cpp.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/test/src/cpp.rs b/crates/test/src/cpp.rs index 5253de14d..3e3f0358f 100644 --- a/crates/test/src/cpp.rs +++ b/crates/test/src/cpp.rs @@ -54,7 +54,8 @@ impl LanguageMethods for Cpp { | "resources-with-futures.wit" | "resources-with-streams.wit" | "streams.wit" - | "async-resource-func.wit" => true, + | "async-resource-func.wit" + | "issue1514-6.wit" => true, _ => false, } }