Skip to content
Merged
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
5 changes: 5 additions & 0 deletions Coder.Test/Languages/RustGeneratedSourceCompilesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,11 @@ private static SourceFile Exemplar()
Condition = "std::mem::size_of::<i32>() == 4",
Message = "an i32 is four bytes",
});
geometry.Members.Add(new CompileTimeAssertion
{
Condition = "std::mem::size_of::<u8>() == 1",
Message = "size {N} fits, brace {} here",
});

file.Members.Add(geometry);
return file;
Expand Down
12 changes: 12 additions & 0 deletions Coder.Test/Languages/RustGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,18 @@
StringAssert.Contains(code, "\"a Point is two i32s\");", StringComparison.Ordinal);
}

/// <summary>
/// Tests that braces in an assertion's message are doubled, because <c>assert!</c> reads its
/// message as a format string and a single brace would open a placeholder.
/// </summary>
[TestMethod]
public void CompileTimeAssertion_DoublesBracesInTheMessage()
{
CompileTimeAssertion assertion = new("true", "size {N} fits, brace {} here");

StringAssert.Contains(Generator.Generate(assertion), "\"size {{N}} fits, brace {{}} here\");", StringComparison.Ordinal);

Check warning on line 704 in Coder.Test/Languages/RustGeneratorTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.Contains' instead of 'StringAssert.Contains'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_Coder&issues=AaEeg_W-8xEq5PWoxYdH&open=AaEeg_W-8xEq5PWoxYdH&pullRequest=194
}

/// <summary>
/// Tests that an alias is a type alias.
/// </summary>
Expand Down
9 changes: 8 additions & 1 deletion Coder/Languages/RustGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@
/// The associated constants of <c>f64</c>, which need nothing imported.
/// </remarks>
protected override string SpellNonFiniteDouble(double value) =>
double.IsNaN(value) ? "f64::NAN" : value > 0 ? "f64::INFINITY" : "f64::NEG_INFINITY";

Check warning on line 344 in Coder/Languages/RustGenerator.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 344 in Coder/Languages/RustGenerator.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 344 in Coder/Languages/RustGenerator.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 344 in Coder/Languages/RustGenerator.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 344 in Coder/Languages/RustGenerator.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.

/// <inheritdoc/>
/// <remarks>
Expand Down Expand Up @@ -782,7 +782,7 @@
HashSet<string> reassigned = op.ReturnsBool
? []
: ReassignedNames(funcDecl.Body, [SelfName, .. funcDecl.Parameters.Select(OperandName)]);
string self = op.ReturnsBool ? "&self" : reassigned.Contains(SelfName) ? $"mut {SelfName}" : SelfName;

Check warning on line 785 in Coder/Languages/RustGenerator.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 785 in Coder/Languages/RustGenerator.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 785 in Coder/Languages/RustGenerator.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 785 in Coder/Languages/RustGenerator.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 785 in Coder/Languages/RustGenerator.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.
code.Write($"fn {op.Method}({self}");

foreach (Parameter parameter in funcDecl.Parameters)
Expand Down Expand Up @@ -1383,6 +1383,10 @@
/// <c>const _: () = assert!(…)</c>, which is Rust's own compile-time assertion and needs no
/// macro crate: a constant nobody names still has to be evaluated for the program to build, and
/// a failed <c>assert!</c> in that position is a compile error naming the message.
/// <para>
/// The message is <c>assert!</c>'s format string rather than a plain literal, so its braces are
/// doubled: a single <c>{N}</c> would be read as a placeholder naming a value that does not exist.
/// </para>
/// </remarks>
protected override void GenerateCompileTimeAssertion(CompileTimeAssertion assertion, CodeBlocker code)
{
Expand All @@ -1397,7 +1401,10 @@
{
code.WriteLine(",");
code.Indent();
code.Write($"\"{EscapeString(assertion.Message)}\"");
string message = EscapeString(assertion.Message)
.Replace("{", "{{", StringComparison.Ordinal)
.Replace("}", "}}", StringComparison.Ordinal);
code.Write($"\"{message}\"");
code.Outdent();
}

Expand Down
Loading