From 86863d4967f49643f8d09cb63dc6739879b43fbb Mon Sep 17 00:00:00 2001 From: Ellen Arteca Date: Tue, 3 Dec 2024 13:41:23 -0800 Subject: [PATCH] moving to use `stringifier` --- tool/src/kotlin/formatter.rs | 28 ++++------------------------ tool/src/kotlin/mod.rs | 24 +++++++++++++----------- 2 files changed, 17 insertions(+), 35 deletions(-) diff --git a/tool/src/kotlin/formatter.rs b/tool/src/kotlin/formatter.rs index 281762f93..58fdb3d6d 100644 --- a/tool/src/kotlin/formatter.rs +++ b/tool/src/kotlin/formatter.rs @@ -16,11 +16,10 @@ pub(super) struct KotlinFormatter<'tcx> { docs_url_gen: &'tcx DocsUrlGenerator, } -const INVALID_RAW_METHOD_NAMES: &[&str] = &[ +const INVALID_METHOD_NAMES: &[&str] = &[ "new", "static", "default", "private", "internal", "toString", ]; const DISALLOWED_CORE_TYPES: &[&str] = &["Object", "String"]; -const OVERRIDE_METHOD_SIGS: &[&str] = &["fun toString(): String"]; impl<'tcx> KotlinFormatter<'tcx> { pub fn new( @@ -113,37 +112,18 @@ impl<'tcx> KotlinFormatter<'tcx> { } } - pub fn fmt_method_name<'a>( - &self, - method: &'a hir::Method, - should_be_overridden: bool, - ) -> Cow<'a, str> { + pub fn fmt_method_name<'a>(&self, method: &'a hir::Method) -> Cow<'a, str> { // TODO(#60): handle other keywords let name = method.name.as_str().to_lower_camel_case(); let name = method.attrs.rename.apply(name.into()); - if INVALID_RAW_METHOD_NAMES.contains(&&*name) && !should_be_overridden { + if INVALID_METHOD_NAMES.contains(&&*name) { format!("{name}_").into() } else { name } } - pub fn method_should_be_overridden( - &self, - method: &hir::Method, - params: &str, - return_type: &str, - ) -> bool { - let method_sig = format!( - "fun {}({}): {}", - &&*method.name.as_str().to_lower_camel_case(), - params, - return_type - ); - OVERRIDE_METHOD_SIGS.contains(&&*method_sig) - } - pub fn fmt_trait_method_name<'a>(&self, method: &'a hir::Callback) -> Cow<'a, str> { if method.name.is_none() { panic!("Trait methods need a name"); @@ -154,7 +134,7 @@ impl<'tcx> KotlinFormatter<'tcx> { } else { name.into() }; - if INVALID_RAW_METHOD_NAMES.contains(&&*name) { + if INVALID_METHOD_NAMES.contains(&&*name) { format!("{name}_").into() } else { name diff --git a/tool/src/kotlin/mod.rs b/tool/src/kotlin/mod.rs index ff73892d6..650103a4e 100644 --- a/tool/src/kotlin/mod.rs +++ b/tool/src/kotlin/mod.rs @@ -35,7 +35,7 @@ pub(crate) fn attr_support() -> BackendAttrSupport { a.named_constructors = false; // TODO a.fallible_constructors = false; // TODO a.accessors = false; - a.stringifiers = false; // TODO + a.stringifiers = true; a.comparators = false; // TODO a.iterators = true; a.iterables = true; @@ -1188,18 +1188,18 @@ returnVal.option() ?: return null panic!("Can only have one iterable method per opaque struct") } } + Some(SpecialMethod::Stringifier) => { + if !special_methods.has_stringifier { + special_methods.has_stringifier = true; + format!("override fun toString(): String") + } else { + panic!("Can only have one stringifier method per opaque struct") + } + } _ => { - let should_be_overridden = - self.formatter - .method_should_be_overridden(method, ¶ms, return_ty.as_ref()); format!( - "{}fun {}({}): {return_ty}", - if should_be_overridden { - "override " - } else { - "" - }, - self.formatter.fmt_method_name(method, should_be_overridden), + "fun {}({}): {return_ty}", + self.formatter.fmt_method_name(method), params ) } @@ -1911,6 +1911,7 @@ struct SpecialMethods { iterator_type: Option, indexer_type: Option, iterable_type: Option, + has_stringifier: bool, } struct IndexerType { @@ -1931,6 +1932,7 @@ impl SpecialMethodsImpl { iterator_type, indexer_type, iterable_type, + has_stringifier: _, }: SpecialMethods, ) -> Self { let interfaces = iterator_type