From 4028eaa0430eb3244562c2c58bb07932d7db5113 Mon Sep 17 00:00:00 2001 From: "wangjie.wjdew" Date: Mon, 13 Nov 2023 18:49:21 +0800 Subject: [PATCH] fix: escape keywords when generate code --- pilota-build/src/codegen/mod.rs | 6 +++++- pilota-build/src/codegen/thrift/mod.rs | 2 +- pilota-build/src/middle/context.rs | 24 ++++++++++++------------ pilota-build/src/middle/resolver.rs | 2 -- pilota-build/src/middle/ty.rs | 7 ++++++- 5 files changed, 24 insertions(+), 17 deletions(-) diff --git a/pilota-build/src/codegen/mod.rs b/pilota-build/src/codegen/mod.rs index b4315464..9d1f4c79 100644 --- a/pilota-build/src/codegen/mod.rs +++ b/pilota-build/src/codegen/mod.rs @@ -340,7 +340,11 @@ where pub fn get_init_service(&self, def_id: DefId) -> (String, String) { let service_name = self.rust_name(def_id); let mod_prefix = self.mod_path(def_id); - let service_path = format!("{}::{}", mod_prefix.join("::"), service_name); + let service_path = format!( + "{}::{}", + mod_prefix.iter().map(|item| item.to_string()).join("::"), + service_name + ); tracing::debug!("service_path: {}", service_path); let methods = self.service_methods(def_id); diff --git a/pilota-build/src/codegen/thrift/mod.rs b/pilota-build/src/codegen/thrift/mod.rs index 715b7716..42340d1f 100644 --- a/pilota-build/src/codegen/thrift/mod.rs +++ b/pilota-build/src/codegen/thrift/mod.rs @@ -93,7 +93,7 @@ impl ThriftBackend { fn codegen_impl_message( &self, - def_id: DefId, + _def_id: DefId, name: Symbol, encode: String, size: String, diff --git a/pilota-build/src/middle/context.rs b/pilota-build/src/middle/context.rs index 07edcbe8..9a9ff00d 100644 --- a/pilota-build/src/middle/context.rs +++ b/pilota-build/src/middle/context.rs @@ -871,18 +871,18 @@ impl Context { info } - pub fn def_id_info(&self, def_id: DefId) -> FastStr { - let file_path = self - .file(self.node(def_id).unwrap().file_id) - .unwrap() - .package - .clone(); - file_path - .iter() - .chain(&[self.node(def_id).unwrap().name()]) - .join("::") - .into() - } + // pub fn def_id_info(&self, def_id: DefId) -> FastStr { + // let file_path = self + // .file(self.node(def_id).unwrap().file_id) + // .unwrap() + // .package + // .clone(); + // file_path + // .iter() + // .chain(&[self.node(def_id).unwrap().name()]) + // .join("::") + // .into() + // } pub fn config(&self, crate_id: &CrateId) -> &serde_yaml::Value { let main_file = crate_id.main_file; diff --git a/pilota-build/src/middle/resolver.rs b/pilota-build/src/middle/resolver.rs index fb8956f7..d9064a03 100644 --- a/pilota-build/src/middle/resolver.rs +++ b/pilota-build/src/middle/resolver.rs @@ -24,8 +24,6 @@ pub trait PathResolver: Sync + Send { _ => panic!(), }; - // keyword escape - let name = name.to_string().into(); segs.push(name); } diff --git a/pilota-build/src/middle/ty.rs b/pilota-build/src/middle/ty.rs index b776d4c5..f5cb740d 100644 --- a/pilota-build/src/middle/ty.rs +++ b/pilota-build/src/middle/ty.rs @@ -1,6 +1,7 @@ use std::fmt::Display; pub use std::sync::Arc; +use itertools::Itertools; pub use TyKind::*; use super::context::tls::with_cx; @@ -141,7 +142,11 @@ impl CodegenTy { .into() } CodegenTy::Adt(def) => with_cx(|cx| { - let path = cx.item_path(def.did).join("::"); + let path = cx + .item_path(def.did) + .iter() + .map(|item| item.to_string()) + .join("::"); format!("::{path}").into() }),