Skip to content

Commit

Permalink
Remove Clone from TokenStream
Browse files Browse the repository at this point in the history
commit-id:c34c8d99
  • Loading branch information
maciektr committed Oct 28, 2024
1 parent 308f593 commit ecf7f5f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
2 changes: 1 addition & 1 deletion plugins/cairo-lang-macro/src/types/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fmt::Display;
/// An abstract stream of Cairo tokens.
///
/// This is both input and part of an output of a procedural macro.
#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct TokenStream {
pub tokens: Vec<TokenTree>,
pub metadata: TokenStreamMetadata,
Expand Down
23 changes: 11 additions & 12 deletions scarb/src/compiler/plugin/proc_macro/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,14 +307,15 @@ impl ProcMacroHostPlugin {
return all_none;
}
};
let original = token_stream.to_string();

let result = self.instance(input.package_id).generate_code(
input.expansion.name.clone(),
args.clone(),
token_stream.clone(),
args,
token_stream,
);

let expanded = context.register_result(token_stream.to_string(), input, result, stable_ptr);
let expanded = context.register_result(original, input, result, stable_ptr);
item_builder.add_modified(RewriteNode::Mapped {
origin: func.as_syntax_node().span(db),
node: Box::new(RewriteNode::Text(expanded.to_string())),
Expand Down Expand Up @@ -525,7 +526,6 @@ impl ProcMacroHostPlugin {
let mut token_stream_builder = TokenStreamBuilder::new(db);
token_stream_builder.add_node(item_ast.as_syntax_node());
token_stream_builder.with_metadata(stream_metadata.clone());
let token_stream = token_stream_builder.build();
let mut aux_data = EmittedAuxData::default();
let mut all_diagnostics: Vec<Diagnostic> = Vec::new();

Expand All @@ -535,10 +535,11 @@ impl ProcMacroHostPlugin {

let mut derived_code = PatchBuilder::new(db, &item_ast);
for derive in derives {
let token_stream = token_stream_builder.build();
let result = self.instance(derive.package_id).generate_code(
derive.expansion.name.clone(),
TokenStream::empty(),
token_stream.clone(),
token_stream,
);

// Register diagnostics.
Expand Down Expand Up @@ -596,10 +597,11 @@ impl ProcMacroHostPlugin {
token_stream: TokenStream,
stable_ptr: SyntaxStablePtrId,
) -> PluginResult {
let original = token_stream.to_string();
let result = self.instance(input.package_id).generate_code(
input.expansion.name.clone(),
args.clone(),
token_stream.clone(),
args,
token_stream,
);

// Handle token stream.
Expand All @@ -625,10 +627,7 @@ impl ProcMacroHostPlugin {
// In essence, `code: None, remove_original_item: false` means `ProcMacroHost` will not be
// called again for this AST item.
// This optimization limits the number of generated nodes a bit.
if last
&& result.aux_data.is_none()
&& token_stream.to_string() == result.token_stream.to_string()
{
if last && result.aux_data.is_none() && original == result.token_stream.to_string() {
return PluginResult {
code: None,
remove_original_item: false,
Expand Down Expand Up @@ -1029,7 +1028,7 @@ impl InlineMacroExprPlugin for ProcMacroInlinePlugin {
);
// Handle diagnostics.
let diagnostics = into_cairo_diagnostics(result.diagnostics, stable_ptr);
let token_stream = result.token_stream.clone();
let token_stream = result.token_stream;
if token_stream.is_empty() {
// Remove original code
InlinePluginResult {
Expand Down
4 changes: 2 additions & 2 deletions scarb/src/compiler/plugin/proc_macro/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl<'a> TokenStreamBuilder<'a> {
self.metadata = Some(metadata);
}

pub fn build(self) -> TokenStream {
pub fn build(&self) -> TokenStream {
let mut result: Vec<TokenTree> = Vec::default();
for node in self.nodes.iter() {
let leaves = node.tokens(self.db);
Expand All @@ -35,7 +35,7 @@ impl<'a> TokenStreamBuilder<'a> {
result.extend(tokens);
}

match self.metadata {
match self.metadata.as_ref() {
Some(metadata) => TokenStream::new(result.clone()).with_metadata(metadata.clone()),
None => TokenStream::new(result.clone()),
}
Expand Down

0 comments on commit ecf7f5f

Please sign in to comment.