Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Clone from TokenStream #1701

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -308,14 +308,15 @@ impl ProcMacroHostPlugin {
return all_none;
}
};
let original = token_stream.to_string();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
let original = token_stream.to_string();
let token_stream_string = 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 @@ -526,7 +527,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 @@ -536,10 +536,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 @@ -597,10 +598,11 @@ impl ProcMacroHostPlugin {
token_stream: TokenStream,
stable_ptr: SyntaxStablePtrId,
) -> PluginResult {
let original = token_stream.to_string();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: same as above

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 @@ -626,10 +628,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 @@ -1035,7 +1034,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
2 changes: 0 additions & 2 deletions scarb/tests/build_cairo_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,6 @@ fn can_implement_derive_macro() {
#[derive_macro]
pub fn custom_derive(token_stream: TokenStream) -> ProcMacroResult {
let name = token_stream
.clone()
.to_string()
.lines()
.find(|l| l.starts_with("struct"))
Expand Down Expand Up @@ -1278,7 +1277,6 @@ fn can_be_expanded() {
#[derive_macro]
pub fn custom_derive(token_stream: TokenStream) -> ProcMacroResult {
let name = token_stream
.clone()
.to_string()
.lines()
.find(|l| l.starts_with("struct"))
Expand Down
Loading