diff --git a/crates/formats/src/dotnet/csharp.rs b/crates/formats/src/dotnet/csharp.rs index d76b72ce..da94f90c 100644 --- a/crates/formats/src/dotnet/csharp.rs +++ b/crates/formats/src/dotnet/csharp.rs @@ -297,12 +297,12 @@ fn join(ts: &[T]) -> String { s } -/// The CLR name of a C# operator, from the symbol after `operator`. Checked `++`, `--`, `*` and -/// `/` have their own names; `+` and `-` depend on how many parameters they take, so the caller -/// names those, and `implicit`/`explicit` conversions are named from the modifiers. -fn operator_name(symbol: &str) -> &'static str { - if let Some(base) = symbol.strip_prefix("checked").map(str::trim_start) { - return match base { +/// The CLR name of a C# operator, from the symbol after `operator` (and optional `checked` keyword). +/// Checked `++`, `--`, `*` and `/` have their own names; `+` and `-` depend on how many parameters +/// they take, so the caller names those, and `implicit`/`explicit` conversions are named from the modifiers. +fn operator_name(symbol: &str, checked: bool) -> &'static str { + if checked { + return match symbol { "++" => "op_CheckedIncrement", "--" => "op_CheckedDecrement", "*" => "op_CheckedMultiply", @@ -591,15 +591,16 @@ impl Scanner { .map(|(p, c)| split_commas(rest.get(p + 1..c).unwrap_or(&[])).into_iter().map(param_type).collect()) .unwrap_or_default(); let ret = join(rest.get(..op).unwrap_or(&[])); - let symbol = p.and_then(|p| rest.get(op + 1..p)).map(join).unwrap_or_default(); - let checked = symbol.strip_prefix("checked").map(str::trim_start); + let op_tokens = p.and_then(|p| rest.get(op + 1..p)).unwrap_or(&[]); + let is_checked = op_tokens.first().is_some_and(|t| kw(t) == Some("checked")); + let symbol = join(if is_checked { op_tokens.get(1..).unwrap_or(&[]) } else { op_tokens }); let conversion = mods.iter().any(|m| matches!(m.as_str(), "implicit" | "explicit")); let name = if mods.iter().any(|m| m.as_str() == "implicit") { "op_Implicit".to_string() } else if mods.iter().any(|m| m.as_str() == "explicit") { - if checked.is_some() { "op_CheckedExplicit".to_string() } else { "op_Explicit".to_string() } + if is_checked { "op_CheckedExplicit".to_string() } else { "op_Explicit".to_string() } } else { - match (checked.unwrap_or(&symbol), checked.is_some(), params.len()) { + match (symbol.as_str(), is_checked, params.len()) { ("+", false, 1) => "op_UnaryPlus".to_string(), ("+", false, _) => "op_Addition".to_string(), // Checked unary `+` isn't a C# operator: only the binary form is checked. @@ -609,10 +610,10 @@ impl Scanner { ("-", false, _) => "op_Subtraction".to_string(), ("-", true, 1) => "op_CheckedUnaryNegation".to_string(), ("-", true, _) => "op_CheckedSubtraction".to_string(), - _ => operator_name(&symbol).to_string(), + _ => operator_name(&symbol, is_checked).to_string(), } }; - let ty = if conversion { checked.unwrap_or(&symbol).to_string() } else { ret }; + let ty = if conversion { symbol } else { ret }; self.push(CsMember { kind: "method", type_name: full, name, generic_arity: 0, params, ty, line }); return; } diff --git a/crates/formats/tests/csharp_verbatim_identifiers.rs b/crates/formats/tests/csharp_verbatim_identifiers.rs index b37296bf..33d30f70 100644 --- a/crates/formats/tests/csharp_verbatim_identifiers.rs +++ b/crates/formats/tests/csharp_verbatim_identifiers.rs @@ -147,3 +147,52 @@ fn verbatim_type_and_delegate_identifiers_are_not_treated_as_reserved_keywords() scanned.members ); } + +/// Verbatim `@checked` conversion targets keep type name `checked` instead of matching the `checked` operator keyword. +#[test] +fn verbatim_checked_conversion_target_keeps_type_name() { + let src = r#" + namespace Sample { + public class @checked { } + public class Box { + public static explicit operator @checked(Box value) => null; + public static implicit operator @checked(int value) => null; + } + } + "#; + let scanned = scan(src); + assert!( + scanned.members.iter().any(|m| m.type_name == "Sample.Box" && m.name == "op_Explicit" && m.ty == "checked"), + "explicit conversion to `@checked` must scan as `op_Explicit` with target type `checked`: {:?}", + scanned.members + ); + assert!( + scanned.members.iter().any(|m| m.type_name == "Sample.Box" && m.name == "op_Implicit" && m.ty == "checked"), + "implicit conversion to `@checked` must scan as `op_Implicit` with target type `checked`: {:?}", + scanned.members + ); +} + +/// Non-verbatim `checked` operator keyword continues to produce `op_Checked*` operators. +#[test] +fn non_verbatim_checked_operator_keyword_scans_checked_operators() { + let src = r#" + namespace Sample { + public class Box { + public static explicit operator checked int(Box value) => 0; + public static Box operator checked +(Box a, Box b) => a; + } + } + "#; + let scanned = scan(src); + assert!( + scanned.members.iter().any(|m| m.type_name == "Sample.Box" && m.name == "op_CheckedExplicit" && m.ty == "int"), + "real `checked` explicit conversion must scan as `op_CheckedExplicit` with target type `int`: {:?}", + scanned.members + ); + assert!( + scanned.members.iter().any(|m| m.type_name == "Sample.Box" && m.name == "op_CheckedAddition"), + "real `checked` addition must scan as `op_CheckedAddition`: {:?}", + scanned.members + ); +} diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index 78c02e2d..eb476b1e 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -2753,7 +2753,7 @@ Owner: open · Status: in progress — claimed: Afloat16, branch bugfix/r3.12-dotnet-user-string-surrogate-limit-20261105 - [x] Treat `@`-prefixed C# verbatim identifiers (`@namespace`, `@class`, `@record`, `@event`, `@operator`, `@ref`) as identifiers rather than keywords when scanning declarations. — claimed: Plastic-time, branch bugfix/r3.12-csharp-verbatim-identifiers -- [ ] Check the token `kw` flag before stripping `checked` in C# conversion and checked operators so `@checked` keeps its target type name. — claimed: Plastic-time, branch bugfix/r3.12-csharp-verbatim-checked-conversion +- [x] Check the token `kw` flag before stripping `checked` in C# conversion and checked operators so `@checked` keeps its target type name. — claimed: Plastic-time, branch bugfix/r3.12-csharp-verbatim-checked-conversion - [x] Reject CIL branch and switch destinations outside the method body without wrapping signed displacements. — claimed: Afloat16, branch bugfix/r3.12-cil-branch-target-ranges-20261111 - [x] Validate CIL method exception-section kinds, extents and complete clause records before reading exception handlers. diff --git a/docs/STATUS.md b/docs/STATUS.md index 75b0bf25..362653e9 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -223,7 +223,7 @@ coverage and secrets passed. The companion CLI was built with locked dependencie | Application graph caps | An application graph counts the unique edges its `4 × max_nodes` cap drops, records an `edge_cap` limitation beside `node_cap`, and `js_app_map` marks coverage partial when either cap cut the graph. | Analysis regressions cover a dense graph (16 mutually importing modules, edges capped at 4 × max_nodes) and a graph within both caps; engine regressions check partial coverage and the `edge_cap`/`node_cap` limitations. | | Application graph import facts | `js_app` resolves every import `ModuleFacts` retained (up to its per-file list limit) instead of the first 2,000, and aggregates every `truncated` list into one `facts_truncated` limitation naming the kinds and the number of files; `js_app_map` marks coverage partial when facts were cut. | Analysis regressions cover a dependency reached only past 2,000 imports and a 5,001st import cut at the facts limit; an engine regression checks the limitation and partial coverage. The capped cases fail on the former code. | | Preserve complete Unicode characters in shortened .NET strings | .NET user-string limits preserve supplementary Unicode characters. | Synthetic byte fixtures in `dotnet_user_string_surrogate_limit` integration regressions. Cover supplementary characters at several limits, a zero-character limit, and unpaired surrogates followed by valid characters. | -| C# verbatim identifiers | `@`-prefixed C# verbatim identifiers (`@namespace`, `@class`, `@record`, `@operator`, `@event`, `@delegate`, `@public`) scan and reconcile as identifier names rather than triggering keyword or modifier rules. | Three regressions in `csharp_verbatim_identifiers` (two failing before the fix) plus a compilation check with the .NET Framework C# compiler (`csc`) verifying metadata member names. | +| C# verbatim identifiers | `@`-prefixed C# verbatim identifiers (`@namespace`, `@class`, `@record`, `@operator`, `@event`, `@delegate`, `@public`, `@checked`) scan and reconcile as identifier names rather than triggering keyword or modifier rules, including conversion targets after `operator`. | Five regressions in `csharp_verbatim_identifiers` (including `@checked` conversion target vs. `checked` operator keyword, three failing before their fixes) plus a compilation check with the .NET Framework C# compiler (`csc`) verifying metadata member names. | | CIL branch destinations | Relative branch and switch destinations must lie inside the method body and cannot wrap through a negative displacement. An invalid destination stops decoding before that instruction, retaining the valid prefix. | Five regression groups cover short and long branches, signed extremes, partial switch validity, backward and forward controls, truncated operands and instruction budgets. Three invalid-destination groups fail on the original decoder. | | CIL exception section framing | Method exception handlers come only from the defined exception-table section kind, a complete declared section, and whole clause records. Unknown section kinds are diagnosed and skipped while linked sections remain readable. | Six synthetic small/fat section regressions cover valid handlers, unknown kinds, cut headers, truncated declared extents, partial clauses and a following valid table. Five cases fail against the original parser. No managed program was executed. | | CIL nonfinite method fingerprints | Normalized CIL hashes distinguish positive infinity, negative infinity and NaN bits instead of serializing every nonfinite operand as null. Single-precision NaNs retain their original bits before widening; decoded values and JSON output stay unchanged. | Nine bytecode regressions cover both precisions, signaling/quiet NaN collisions, payload/sign distinctions, fixed fingerprints, numeric display, JSON compatibility and finite instruction equality. Finite and infinity encodings retain their existing hashes. No managed program was executed. |