Skip to content

Craft Anything: understand how any software works, and craft your own.

Rust

C# operators keep their CLR names (#456)

Alex Joaquim Pereiraa3e6bd1b13b0
diff --git a/crates/formats/src/dotnet/csharp.rs b/crates/formats/src/dotnet/csharp.rs
index ce3e5766..f121cccc 100644
--- a/crates/formats/src/dotnet/csharp.rs
+++ b/crates/formats/src/dotnet/csharp.rs
@@ -275,6 +275,45 @@ 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 {
+ "++" => "op_CheckedIncrement",
+ "--" => "op_CheckedDecrement",
+ "*" => "op_CheckedMultiply",
+ "/" => "op_CheckedDivision",
+ _ => "operator",
+ };
+ }
+ match symbol {
+ "==" => "op_Equality",
+ "!=" => "op_Inequality",
+ "*" => "op_Multiply",
+ "/" => "op_Division",
+ "%" => "op_Modulus",
+ "&" => "op_BitwiseAnd",
+ "|" => "op_BitwiseOr",
+ "^" => "op_ExclusiveOr",
+ "!" => "op_LogicalNot",
+ "~" => "op_OnesComplement",
+ "++" => "op_Increment",
+ "--" => "op_Decrement",
+ "<<" => "op_LeftShift",
+ ">>" => "op_RightShift",
+ ">>>" => "op_UnsignedRightShift",
+ "<" => "op_LessThan",
+ ">" => "op_GreaterThan",
+ "<=" => "op_LessThanOrEqual",
+ ">=" => "op_GreaterThanOrEqual",
+ "true" => "op_True",
+ "false" => "op_False",
+ _ => "operator",
+ }
+}
+
/// Split at top-level commas.
fn split_commas(ts: &[T]) -> Vec<&[T]> {
let mut out = Vec::new();
@@ -525,12 +564,34 @@ impl Scanner {
let op_at = rest.iter().position(|t| ident(t) == Some("operator"));
if let Some(op) = op_at {
let p = rest.iter().skip(op).position(|t| is_p(t, '(')).map(|x| x + op);
- let params = p
+ let params: Vec<String> = p
.and_then(|p| matching(rest, p, '(', ')').map(|c| (p, c)))
.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(&[]));
- self.push(CsMember { kind: "method", type_name: full, name: "operator".into(), generic_arity: 0, params, ty: ret, line });
+ 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 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() }
+ } else {
+ match (checked.unwrap_or(&symbol), checked.is_some(), 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.
+ ("+", true, 1) => "operator".to_string(),
+ ("+", true, _) => "op_CheckedAddition".to_string(),
+ ("-", false, 1) => "op_UnaryNegation".to_string(),
+ ("-", false, _) => "op_Subtraction".to_string(),
+ ("-", true, 1) => "op_CheckedUnaryNegation".to_string(),
+ ("-", true, _) => "op_CheckedSubtraction".to_string(),
+ _ => operator_name(&symbol).to_string(),
+ }
+ };
+ let ty = if conversion { checked.unwrap_or(&symbol).to_string() } else { ret };
+ self.push(CsMember { kind: "method", type_name: full, name, generic_arity: 0, params, ty, line });
return;
}
if let Some(p) = paren.filter(|&p| p < eq) {
@@ -792,35 +853,27 @@ pub fn scan(src: &str) -> CsScan {
/// Simplify a type for comparison: drop namespaces, map system type names to C# keywords, remove
/// spaces.
pub fn norm_type(s: &str) -> String {
+ norm_type_impl(s, false)
+}
+
+/// Simplify a type for comparison but keep namespaces, so `A.Result` and `B.Result` stay distinct.
+/// `global::` is dropped and metadata's nested-type separator becomes a dot, so a qualified source
+/// name matches the metadata name.
+fn norm_type_qualified(s: &str) -> String {
+ norm_type_impl(&s.replace("global::", "").replace('/', "."), true)
+}
+
+fn norm_type_impl(s: &str, keep_namespace: bool) -> String {
let mut out = String::new();
let mut word = String::new();
let flush = |word: &mut String, out: &mut String| {
if !word.is_empty() {
- let last = word.rsplit('.').next().unwrap_or(word).to_string();
- let last = last.split('`').next().unwrap_or("").to_string();
- let mapped = match last.as_str() {
- "Void" => "void",
- "Boolean" => "bool",
- "Char" => "char",
- "SByte" => "sbyte",
- "Byte" => "byte",
- "Int16" => "short",
- "UInt16" => "ushort",
- "Int32" => "int",
- "UInt32" => "uint",
- "Int64" => "long",
- "UInt64" => "ulong",
- "Single" => "float",
- "Double" => "double",
- "String" => "string",
- "Object" => "object",
- "IntPtr" => "nint",
- "UIntPtr" => "nuint",
- "Decimal" => "decimal",
- "dynamic" => "object",
- other => other,
- };
- out.push_str(mapped);
+ let name = strip_arity(word);
+ let simple = name.rsplit('.').next().unwrap_or(&name);
+ let alias = cs_alias(simple);
+ // A built-in keeps its C# keyword in both modes; other names keep the namespace only
+ // when asked, so `A.Result` and `B.Result` stay distinct.
+ out.push_str(alias.unwrap_or(if keep_namespace { name.as_str() } else { simple }));
word.clear();
}
};
@@ -842,6 +895,101 @@ pub fn norm_type(s: &str) -> String {
out.replace("ref", "ref ").replace("ref ", "ref ")
}
+/// The C# keyword for a CLR built-in type name, if it has one.
+fn cs_alias(name: &str) -> Option<&'static str> {
+ Some(match name {
+ "Void" => "void",
+ "Boolean" => "bool",
+ "Char" => "char",
+ "SByte" => "sbyte",
+ "Byte" => "byte",
+ "Int16" => "short",
+ "UInt16" => "ushort",
+ "Int32" => "int",
+ "UInt32" => "uint",
+ "Int64" => "long",
+ "UInt64" => "ulong",
+ "Single" => "float",
+ "Double" => "double",
+ "String" => "string",
+ "Object" => "object",
+ "IntPtr" => "nint",
+ "UIntPtr" => "nuint",
+ "Decimal" => "decimal",
+ "dynamic" => "object",
+ _ => return None,
+ })
+}
+
+/// Remove generic-arity markers (`` `1 ``) from a metadata type name.
+fn strip_arity(s: &str) -> String {
+ let mut out = String::with_capacity(s.len());
+ let mut skip_digits = false;
+ for c in s.chars() {
+ if c == '`' {
+ skip_digits = true;
+ } else if skip_digits && c.is_ascii_digit() {
+ // Part of the arity marker.
+ } else {
+ skip_digits = false;
+ out.push(c);
+ }
+ }
+ out
+}
+
+/// The outermost type name of a normalized type, ignoring generic arguments and suffixes.
+fn terminal_name(s: &str) -> &str {
+ let head = s.split(['<', '[', '?', '*']).next().unwrap_or(s);
+ head.rsplit('.').next().unwrap_or(head)
+}
+
+/// True when the outermost type is written with a namespace, so that namespace is part of its
+/// identity. Generic arguments and suffixes don't count.
+fn is_qualified_type(s: &str) -> bool {
+ s.split(['<', '[', '?', '*']).next().unwrap_or(s).contains('.')
+}
+
+/// Compare conversion targets. A source name is resolved against the declaring type, its enclosing
+/// types and its namespace, so `Outer.Inner` in `N.C` matches metadata `N.Outer/Inner`. A qualified
+/// source name that does not resolve is never equated by its last segment. The fallback for a short
+/// name is best-effort: the scanner does not track `using` directives, so a unique same-kind,
+/// same-arity target with the same terminal name is accepted; distinct targets are never conflated.
+fn conversion_targets_match(want: &str, got: &str, declaring: &str, targets: &[(&str, &str, usize)], op_name: &str, arity: usize) -> bool {
+ let want_qualified = norm_type_qualified(want);
+ let got_qualified = norm_type_qualified(got);
+ if want_qualified == got_qualified {
+ return true;
+ }
+ // `global::` is absolute: never expand it against the declaring scope or its terminal name.
+ if got.trim_start().starts_with("global::") {
+ return false;
+ }
+ let mut scope = declaring;
+ loop {
+ if format!("{scope}.{got_qualified}") == want_qualified {
+ return true;
+ }
+ match scope.rsplit_once('.') {
+ Some((parent, _)) => scope = parent,
+ None => break,
+ }
+ }
+ if is_qualified_type(&got_qualified) {
+ return false;
+ }
+ let want_terminal = terminal_name(&want_qualified);
+ let mut same_terminal: Vec<String> = targets
+ .iter()
+ .filter(|(name, _, params)| *name == op_name && *params == arity)
+ .map(|(_, target, _)| norm_type_qualified(target))
+ .filter(|target| terminal_name(target) == want_terminal)
+ .collect();
+ same_terminal.sort();
+ same_terminal.dedup();
+ same_terminal.len() == 1 && norm_type(&got.replace("global::", "").replace('/', ".")) == norm_type(&want.replace("global::", "").replace('/', "."))
+}
+
/// Normalise a metadata type name (`Ns.Outer/Inner`1`) to the dotted form without arity.
fn norm_type_name(s: &str) -> (String, usize) {
let mut arity = 0;
@@ -936,7 +1084,13 @@ pub fn reconcile(members: &Members, scan: &CsScan) -> Reconciliation {
r.missing_types.push(ty.full_name.clone());
}
let props: BTreeSet<&str> = ty.properties.iter().map(|p| p.name.as_str()).chain(ty.events.iter().map(|e| e.name.as_str())).collect();
- let mut check = |kind: &'static str, name: &str, params: Vec<String>, meta_text: String, token: String, r: &mut Reconciliation| {
+ let conversion_targets: Vec<(&str, &str, usize)> = ty
+ .methods
+ .iter()
+ .filter(|m| matches!(m.name.as_str(), "op_Implicit" | "op_Explicit" | "op_CheckedExplicit"))
+ .map(|m| (m.name.as_str(), m.return_type.as_str(), m.params.len()))
+ .collect();
+ let mut check = |kind: &'static str, name: &str, params: Vec<String>, ret: &str, meta_text: String, token: String, r: &mut Reconciliation| {
let mm = |source: Option<&CsMember>, reason: Option<String>| MemberMatch {
kind,
type_name: ty.full_name.clone(),
@@ -948,11 +1102,18 @@ pub fn reconcile(members: &Members, scan: &CsScan) -> Reconciliation {
reason,
};
let cands = src.get(&(tname.clone(), key_name(name).to_string())).cloned().unwrap_or_default();
+ let arity = params.len();
let want: Vec<String> = params.iter().map(|p| norm_type(p)).collect();
- if let Some((i, s)) = cands.iter().find(|(i, s)| !used.contains(i) && s.params.iter().map(|p| norm_type(p)).collect::<Vec<_>>() == want) {
+ // A conversion's target type is part of its identity: two op_Implicit methods can
+ // share their parameters and differ only in the type they convert to.
+ let want_ret = matches!(name, "op_Implicit" | "op_Explicit" | "op_CheckedExplicit").then_some(ret);
+ let same_ret = |s: &CsMember| want_ret.is_none_or(|want| conversion_targets_match(want, &s.ty, &tname, &conversion_targets, name, arity));
+ if let Some((i, s)) =
+ cands.iter().find(|(i, s)| !used.contains(i) && same_ret(s) && s.params.iter().map(|p| norm_type(p)).collect::<Vec<_>>() == want)
+ {
used.insert(*i);
r.matched.push(mm(Some(s), None));
- } else if let Some((i, s)) = cands.iter().find(|(i, s)| !used.contains(i) && s.params.len() == want.len()) {
+ } else if let Some((i, s)) = cands.iter().find(|(i, s)| !used.contains(i) && same_ret(s) && s.params.len() == want.len()) {
used.insert(*i);
let got: Vec<String> = s.params.iter().map(|p| norm_type(p)).collect();
r.signature_mismatch.push(mm(Some(s), Some(format!("parameters ({}) vs ({})", want.join(", "), got.join(", ")))));
@@ -970,16 +1131,16 @@ pub fn reconcile(members: &Members, scan: &CsScan) -> Reconciliation {
};
for m in &ty.methods {
let params: Vec<String> = m.params.iter().map(|p| p.ty.clone()).collect();
- check("method", &m.name, params, m.signature.clone(), m.token.clone(), &mut r);
+ check("method", &m.name, params, &m.return_type, m.signature.clone(), m.token.clone(), &mut r);
}
for f in &ty.fields {
- check("field", &f.name, vec![], format!("{} {}", f.ty, f.name), f.token.clone(), &mut r);
+ check("field", &f.name, vec![], "", format!("{} {}", f.ty, f.name), f.token.clone(), &mut r);
}
for p in &ty.properties {
- check("property", &p.name, vec![], format!("{} {}", p.ty, p.name), String::new(), &mut r);
+ check("property", &p.name, vec![], "", format!("{} {}", p.ty, p.name), String::new(), &mut r);
}
for e in &ty.events {
- check("event", &e.name, vec![], format!("event {} {}", e.ty, e.name), String::new(), &mut r);
+ check("event", &e.name, vec![], "", format!("event {} {}", e.ty, e.name), String::new(), &mut r);
}
}
for t in &scan.types {
@@ -1007,7 +1168,7 @@ pub fn reconcile(members: &Members, scan: &CsScan) -> Reconciliation {
#[cfg(test)]
mod tests {
use super::*;
- use crate::dotnet::model::{MethodInfo, PropertyInfo, TypeInfo};
+ use crate::dotnet::model::{MethodInfo, Param, PropertyInfo, TypeInfo};
#[test]
fn scans_declarations() {
@@ -1028,6 +1189,7 @@ public sealed class Box<T> : IDisposable where T : class
public static Box<T> Create<U, V>(ref int x, out string y, params object[] rest) where U : struct { return null; }
void IDisposable.Dispose() { }
public static bool operator ==(Box<T> a, Box<T> b) => true;
+ public static bool operator !=(Box<T> a, Box<T> b) => false;
public enum Mode { A, B = 2, C }
public record Point(int X, int Y);
}
@@ -1044,7 +1206,8 @@ public sealed class Box<T> : IDisposable where T : class
assert!(names.contains(&("constructor", ".cctor")));
assert!(names.contains(&("method", "Finalize")));
assert!(names.contains(&("method", "IDisposable.Dispose")));
- assert!(names.contains(&("method", "operator")));
+ assert!(names.contains(&("method", "op_Equality")), "{names:?}");
+ assert!(names.contains(&("method", "op_Inequality")), "{names:?}");
assert!(names.contains(&("field", "C")));
let create = s.members.iter().find(|m| m.name == "Create").unwrap();
assert_eq!(create.generic_arity, 2);
@@ -1173,6 +1336,227 @@ public class Container {
assert!(r.missing.is_empty(), "synthetic delegate methods are not missing: {:?}", r.missing);
}
+ #[test]
+ fn equality_operators_match_their_clr_names() {
+ let mut c = ty("C", "class", &["op_Equality", "op_Inequality"], &[]);
+ for m in &mut c.methods {
+ m.params = vec![Param { name: "a".into(), ty: "C".into() }, Param { name: "b".into(), ty: "C".into() }];
+ }
+ let members = Members { types: vec![c], total_types: 1, total_methods: 2, total_fields: 0, truncated: false, warnings: vec![] };
+ let s = scan("class C { public static bool operator ==(C a, C b) => true; public static bool operator !=(C a, C b) => false; }");
+ let r = reconcile(&members, &s);
+ assert!(r.missing.is_empty(), "{:?}", r.missing);
+ assert!(r.extra.is_empty(), "{:?}", r.extra);
+ assert_eq!(r.matched.iter().filter(|m| m.kind == "method").count(), 2, "{:?}", r.matched);
+ }
+
+ #[test]
+ fn other_operators_get_their_clr_names() {
+ let s = scan(
+ "class C {
+ public static C operator +(C a, C b) => a;
+ public static C operator -(C a) => a;
+ public static implicit operator int(C c) => 0;
+ public static explicit operator C(int i) => null;
+ public static bool operator true(C c) => true;
+ public static C operator checked *(C a, C b) => a;
+ public static C operator checked +(C a, C b) => a;
+ public static C operator checked -(C a) => a;
+ public static C operator >>>(C a, int b) => a;
+ public static explicit operator checked int(C c) => 0;
+ }",
+ );
+ let names: Vec<&str> = s.members.iter().map(|m| m.name.as_str()).collect();
+ for want in [
+ "op_Addition",
+ "op_UnaryNegation",
+ "op_Implicit",
+ "op_Explicit",
+ "op_True",
+ "op_CheckedMultiply",
+ "op_CheckedAddition",
+ "op_CheckedUnaryNegation",
+ "op_UnsignedRightShift",
+ "op_CheckedExplicit",
+ ] {
+ assert!(names.contains(&want), "{want} missing: {names:?}");
+ }
+ }
+
+ #[test]
+ fn a_checked_unary_plus_is_not_a_clr_operator() {
+ // Not valid C#; the scanner must not invent op_CheckedAddition for it.
+ let s = scan("class C { public static C operator checked +(C a) => a; }");
+ assert_eq!(s.members.first().map(|m| m.name.as_str()), Some("operator"));
+ }
+
+ #[test]
+ fn arithmetic_and_conversion_operators_reconcile() {
+ let mut c = ty("C", "class", &["op_Addition", "op_Implicit", "op_Implicit"], &[]);
+ for m in &mut c.methods {
+ m.params = match m.name.as_str() {
+ "op_Addition" => vec![Param { name: "a".into(), ty: "C".into() }, Param { name: "b".into(), ty: "C".into() }],
+ _ => vec![Param { name: "c".into(), ty: "C".into() }],
+ };
+ }
+ c.methods[1].return_type = "System.Int32".into();
+ c.methods[2].return_type = "System.String".into();
+ let members = Members { types: vec![c], total_types: 1, total_methods: 3, total_fields: 0, truncated: false, warnings: vec![] };
+ let s = scan(
+ "class C {
+ public static C operator +(C a, C b) => a;
+ public static implicit operator string(C c) => \"\";
+ public static implicit operator int(C c) => 0;
+ }",
+ );
+ let r = reconcile(&members, &s);
+ assert!(r.missing.is_empty(), "{:?}", r.missing);
+ assert!(r.extra.is_empty(), "{:?}", r.extra);
+ assert_eq!(r.matched.iter().filter(|m| m.kind == "method").count(), 3, "{:?}", r.matched);
+ // The int conversion matches the int declaration (line 4), not the first op_Implicit.
+ assert_eq!(r.matched[1].line, Some(4), "{:?}", r.matched[1]);
+ assert_eq!(r.matched[2].line, Some(3), "{:?}", r.matched[2]);
+ }
+
+ #[test]
+ fn conversion_targets_in_different_namespaces_are_distinct() {
+ let mut c = ty("C", "class", &["op_Implicit", "op_Implicit"], &[]);
+ for m in &mut c.methods {
+ m.params = vec![Param { name: "c".into(), ty: "C".into() }];
+ }
+ c.methods[0].return_type = "A.Result".into();
+ c.methods[1].return_type = "B.Result".into();
+ let members = Members { types: vec![c], total_types: 1, total_methods: 2, total_fields: 0, truncated: false, warnings: vec![] };
+ let s = scan(
+ "class C {
+ public static implicit operator B.Result(C c) => null;
+ public static implicit operator A.Result(C c) => null;
+ }",
+ );
+ let r = reconcile(&members, &s);
+ assert!(r.missing.is_empty(), "{:?}", r.missing);
+ assert!(r.extra.is_empty(), "{:?}", r.extra);
+ let lines: Vec<Option<u32>> = r.matched.iter().map(|m| m.line).collect();
+ assert_eq!(lines, vec![Some(3), Some(2)], "A.Result must not match B.Result: {:?}", r.matched);
+ }
+
+ #[test]
+ fn a_nested_conversion_target_matches_metadata_separators() {
+ let mut c = ty("C", "class", &["op_Implicit"], &[]);
+ c.methods[0].params = vec![Param { name: "c".into(), ty: "C".into() }];
+ c.methods[0].return_type = "A.Outer/Inner".into();
+ let members = Members { types: vec![c], total_types: 1, total_methods: 1, total_fields: 0, truncated: false, warnings: vec![] };
+ let s = scan("class C { public static implicit operator A.Outer.Inner(C c) => null; }");
+ let r = reconcile(&members, &s);
+ assert!(r.missing.is_empty(), "{:?}", r.missing);
+ assert!(r.extra.is_empty(), "{:?}", r.extra);
+ assert_eq!(r.matched.first().and_then(|m| m.line), Some(1), "{:?}", r.matched);
+ }
+
+ #[test]
+ fn a_short_nested_conversion_target_matches_metadata_separators() {
+ let mut c = ty("C", "class", &["op_Implicit"], &[]);
+ c.methods[0].params = vec![Param { name: "c".into(), ty: "C".into() }];
+ c.methods[0].return_type = "N.Outer/Inner".into();
+ let members = Members { types: vec![c], total_types: 1, total_methods: 1, total_fields: 0, truncated: false, warnings: vec![] };
+ let s = scan("class C { public static implicit operator Inner(C c) => null; }");
+ let r = reconcile(&members, &s);
+ assert!(r.missing.is_empty(), "{:?}", r.missing);
+ assert!(r.extra.is_empty(), "{:?}", r.extra);
+ assert_eq!(r.matched.first().and_then(|m| m.line), Some(1), "{:?}", r.matched);
+ }
+
+ #[test]
+ fn qualified_generic_conversion_targets_alias_builtin_arguments() {
+ let mut c = ty("C", "class", &["op_Implicit"], &[]);
+ c.methods[0].params = vec![Param { name: "c".into(), ty: "C".into() }];
+ c.methods[0].return_type = "System.Collections.Generic.List<System.Int32>".into();
+ let members = Members { types: vec![c], total_types: 1, total_methods: 1, total_fields: 0, truncated: false, warnings: vec![] };
+ let s = scan("class C { public static implicit operator System.Collections.Generic.List<int>(C c) => null; }");
+ let r = reconcile(&members, &s);
+ assert!(r.missing.is_empty(), "{:?}", r.missing);
+ assert!(r.extra.is_empty(), "{:?}", r.extra);
+ assert_eq!(r.matched.first().and_then(|m| m.line), Some(1), "{:?}", r.matched);
+ }
+
+ #[test]
+ fn a_short_conversion_target_matches_the_qualified_metadata_name() {
+ let mut c = ty("C", "class", &["op_Implicit"], &[]);
+ c.methods[0].params = vec![Param { name: "c".into(), ty: "C".into() }];
+ c.methods[0].return_type = "A.Result".into();
+ let members = Members { types: vec![c], total_types: 1, total_methods: 1, total_fields: 0, truncated: false, warnings: vec![] };
+ let s = scan("class C { public static implicit operator Result(C c) => null; }");
+ let r = reconcile(&members, &s);
+ assert!(r.missing.is_empty(), "{:?}", r.missing);
+ assert!(r.extra.is_empty(), "{:?}", r.extra);
+ assert_eq!(r.matched.first().and_then(|m| m.line), Some(1), "{:?}", r.matched);
+ }
+
+ #[test]
+ fn a_namespace_relative_conversion_target_matches() {
+ let mut c = ty("N.C", "class", &["op_Implicit"], &[]);
+ c.methods[0].params = vec![Param { name: "c".into(), ty: "N.C".into() }];
+ c.methods[0].return_type = "N.Outer/Inner".into();
+ let members = Members { types: vec![c], total_types: 1, total_methods: 1, total_fields: 0, truncated: false, warnings: vec![] };
+ let s = scan("namespace N { class C { public static implicit operator Outer.Inner(C c) => null; } }");
+ let r = reconcile(&members, &s);
+ assert!(r.missing.is_empty(), "{:?}", r.missing);
+ assert!(r.extra.is_empty(), "{:?}", r.extra);
+ assert_eq!(r.matched.first().and_then(|m| m.line), Some(1), "{:?}", r.matched);
+ }
+
+ #[test]
+ fn an_enclosing_type_relative_conversion_target_matches() {
+ let mut c = ty("N.C", "class", &["op_Implicit"], &[]);
+ c.methods[0].params = vec![Param { name: "c".into(), ty: "N.C".into() }];
+ c.methods[0].return_type = "N.C/Outer/Inner".into();
+ let members = Members { types: vec![c], total_types: 1, total_methods: 1, total_fields: 0, truncated: false, warnings: vec![] };
+ let s = scan("namespace N { class C { public static implicit operator Outer.Inner(C c) => null; } }");
+ let r = reconcile(&members, &s);
+ assert!(r.missing.is_empty(), "{:?}", r.missing);
+ assert!(r.extra.is_empty(), "{:?}", r.extra);
+ assert_eq!(r.matched.first().and_then(|m| m.line), Some(1), "{:?}", r.matched);
+ }
+
+ #[test]
+ fn an_unrelated_qualified_conversion_target_does_not_match() {
+ let mut c = ty("N.C", "class", &["op_Implicit"], &[]);
+ c.methods[0].params = vec![Param { name: "c".into(), ty: "N.C".into() }];
+ c.methods[0].return_type = "N.Other/Inner".into();
+ let members = Members { types: vec![c], total_types: 1, total_methods: 1, total_fields: 0, truncated: false, warnings: vec![] };
+ let s = scan("namespace N { class C { public static implicit operator Outer.Inner(C c) => null; } }");
+ let r = reconcile(&members, &s);
+ assert_eq!(r.missing.len(), 1, "{:?}", r.missing);
+ assert_eq!(r.extra.len(), 1, "{:?}", r.extra);
+ assert!(r.matched.is_empty(), "{:?}", r.matched);
+ }
+
+ #[test]
+ fn a_short_name_does_not_equate_distinct_nested_targets() {
+ let targets = [("op_Implicit", "N.Other/Inner", 1), ("op_Implicit", "N.C/Inner", 1)];
+ assert!(conversion_targets_match("N.C/Inner", "Inner", "N.C", &targets, "op_Implicit", 1));
+ assert!(!conversion_targets_match("N.Other/Inner", "Inner", "N.C", &targets, "op_Implicit", 1));
+ }
+
+ #[test]
+ fn conversion_ambiguity_is_per_operator_kind_and_outer_name() {
+ // An op_Explicit target with the same terminal name cannot make an op_Implicit ambiguous.
+ let kinds = [("op_Implicit", "N.Foo", 1), ("op_Explicit", "M.Foo", 1)];
+ assert!(conversion_targets_match("N.Foo", "Foo", "C", &kinds, "op_Implicit", 1));
+ // Generic arguments share a terminal name; the outer names still decide.
+ let generic = [("op_Implicit", "N.Box<Q.Item>", 1), ("op_Implicit", "M.Other<Q.Item>", 1)];
+ assert!(conversion_targets_match("N.Box<Q.Item>", "Box<Q.Item>", "C", &generic, "op_Implicit", 1));
+ }
+
+ #[test]
+ fn a_global_qualified_conversion_target_is_absolute() {
+ let targets = [("op_Implicit", "A.Result", 1), ("op_Implicit", "Result", 1)];
+ assert!(conversion_targets_match("A.Result", "global::A.Result", "N.C", &targets, "op_Implicit", 1));
+ assert!(!conversion_targets_match("N.A.Result", "global::A.Result", "N.C", &targets, "op_Implicit", 1));
+ assert!(conversion_targets_match("Result", "global::Result", "N.C", &targets, "op_Implicit", 1));
+ assert!(!conversion_targets_match("A.Result", "global::Result", "N.C", &targets, "op_Implicit", 1));
+ }
+
#[test]
fn junk_does_not_panic() {
for src in ["", "{{{{", "}}}}", "class", "class X {", "\"unterminated", "'", "/*", "@\"", "$\"\"\"", "namespace ;", "class X { void (", "enum E { , }"]