Skip to content

Commit

Permalink
Fix enums/errors case names conflicting with parameters to top level …
Browse files Browse the repository at this point in the history
…type names

Signed-off-by: Kristupas Antanavičius <[email protected]>
  • Loading branch information
arg0d committed Oct 4, 2024
1 parent f68639f commit b8ed8aa
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 4 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions bindgen/templates/EnumTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public override void Write({{ type_name }} value, BigEndianStream stream) {
public record {{ variant.name()|class_name }}: {{ type_name }} {}
{% else -%}
public record {{ variant.name()|class_name }} (
{% for field in variant.fields() -%}
{{ field|type_name}} {{ field.name()|var_name }}{% if !loop.last %},{% endif %}
{%- for field in variant.fields() %}
{% call cs::enum_parameter_type_name(field|type_name, variant.name()|class_name) %} {{ field.name()|var_name }}{% if !loop.last %},{% endif %}
{%- endfor %}
) : {{ type_name }} {}
{%- endif %}
Expand Down
4 changes: 2 additions & 2 deletions bindgen/templates/ErrorTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ public class {{ variant.name()|exception_name }} : {{ type_name }} {}
public class {{ variant.name()|exception_name }} : {{ type_name }} {
// Members
{%- for field in variant.fields() %}
public {{ field|type_name}} {{ field.name()|var_name }};
public {% call cs::enum_parameter_type_name(field|type_name, variant.name()|exception_name) %} {{ field.name()|var_name }};
{%- endfor %}

// Constructor
public {{ variant.name()|exception_name }}(
{%- for field in variant.fields() %}
{{ field|type_name}} {{ field.name()|var_name }}{% if loop.last %}{% else %}, {% endif %}
{% call cs::enum_parameter_type_name(field|type_name, variant.name()|exception_name) %} {{ field.name()|var_name }}{% if loop.last %}{% else %}, {% endif %}
{%- endfor %}) {
{%- for field in variant.fields() %}
this.{{ field.name()|var_name }} = {{ field.name()|var_name }};
Expand Down
18 changes: 18 additions & 0 deletions bindgen/templates/macros.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,21 @@
{%- else %}
{%- endmatch %}
{%- endmacro %}

{#
// Break the following cycle, where `Rectangle` case name conflicts with top level `Rectangle` type name.
// If param type name matches the enum name, prefix the param type name with top level namespace.
// https://github.com/NordSecurity/uniffi-bindgen-cs/issues/60
// public record Rectangle(double @width, double @height) { }
// public record Shape
// { ____________
// ∨ ∧
// public record Rectangle(Rectangle @s) : Shape { }
// public record Ellipse(Ellipse @s) : Shape { }
// }
#}
{%- macro enum_parameter_type_name(param_type_name, enum_name) %}
{%- if param_type_name == enum_name %}{{ config.namespace() }}.{{ param_type_name }}
{%- else %}{{ param_type_name }}
{%- endif %}
{%- endmacro %}
17 changes: 17 additions & 0 deletions dotnet-tests/UniffiCS.BindingTests/TestRegressions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

using uniffi.issue_60;

namespace UniffiCS.BindingTests;

public class TestRegressions
{
[Fact]
public void TestIssue60()
{
new Shape.Rectangle(new Rectangle(1f, 2f));
new ShapeException.Rectangle(new Rectangle(1f, 2f));
}
}
1 change: 1 addition & 0 deletions fixtures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ crate-type = ["cdylib", "lib"]

[dependencies]
global-methods-class-name = { path = "global-methods-class-name" }
issue-60 = { path = "regressions/issue-60" }
null-to-empty-string = { path = "null-to-empty-string" }
uniffi-cs-custom-types-builtin = { path = "custom-types-builtin" }
uniffi-cs-disposable-fixture = { path = "disposable" }
Expand Down
17 changes: 17 additions & 0 deletions fixtures/regressions/issue-60/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "issue-60"
version = "1.0.0"
edition = "2021"
publish = false

[lib]
crate-type = ["lib", "cdylib"]
name = "issue_60"

[dependencies]
thiserror = "1.0"
uniffi = {path = "../../../3rd-party/uniffi-rs/uniffi", features=["build"]}
uniffi_macros = {path = "../../../3rd-party/uniffi-rs/uniffi_macros"}

[build-dependencies]
uniffi = {path = "../../../3rd-party/uniffi-rs/uniffi", features=["bindgen-tests"]}
45 changes: 45 additions & 0 deletions fixtures/regressions/issue-60/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use std::fmt;

#[derive(uniffi::Enum)]
pub enum Shape {
Rectangle { s: Rectangle },
Ellipse { s: Ellipse },
}

#[derive(Debug, thiserror::Error, uniffi::Error)]
pub enum ShapeError {
#[error("Rectangle: {s}")]
Rectangle { s: Rectangle },
#[error("Ellipse: {s}")]
Ellipse { s: Ellipse },
}

#[derive(uniffi::Record, Debug)]
pub struct Rectangle {
width: f64,
height: f64,
}

#[derive(uniffi::Record, Debug)]
pub struct Ellipse {
x_radius: f64,
y_radius: f64,
}

impl fmt::Display for Rectangle {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "width: {}, height: {}", self.width, self.height)
}
}

impl fmt::Display for Ellipse {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "x_radius: {}, y_radius: {}", self.x_radius, self.y_radius)
}
}

uniffi::setup_scaffolding!();
1 change: 1 addition & 0 deletions fixtures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ mod uniffi_fixtures {
uniffi_cs_disposable::uniffi_reexport_scaffolding!();
uniffi_cs_optional_parameters::uniffi_reexport_scaffolding!();
stringify::uniffi_reexport_scaffolding!();
issue_60::uniffi_reexport_scaffolding!();
}

0 comments on commit b8ed8aa

Please sign in to comment.