Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 4 additions & 2 deletions crates/csharp/src/function.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -167,6 +167,7 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
.blocks
.drain(self.blocks.len() - cases.len()..)
.collect::<Vec<_>>();
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");
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 11 additions & 1 deletion crates/csharp/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
(
Expand All @@ -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});
}}
"
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 5 additions & 2 deletions crates/test/src/cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,14 @@ impl LanguageMethods for Cpp {

fn should_fail_verify(
&self,
_name: &str,
name: &str,
config: &crate::config::WitConfig,
_args: &[String],
) -> bool {
config.async_
return match name {
"issue1514-6.wit" => true,
_ => false,
} || config.async_;
}

fn prepare(&self, runner: &mut Runner) -> anyhow::Result<()> {
Expand Down
12 changes: 12 additions & 0 deletions tests/codegen/issue1514-6.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package demo:poc;

interface i {
variant v {
v,
}
f: func(x: v);
}

world w {
export i;
}
Loading