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

Add cairo edition to TokenStreamMetadata (#1704) #1832

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
1 change: 1 addition & 0 deletions plugins/cairo-lang-macro-stable/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub struct StableTokenStream {
pub struct StableTokenStreamMetadata {
pub original_file_path: Option<NonNull<c_char>>,
pub file_id: Option<NonNull<c_char>>,
pub edition: Option<NonNull<c_char>>,
}

/// Auxiliary data returned by the procedural macro.
Expand Down
13 changes: 11 additions & 2 deletions plugins/cairo-lang-macro/src/types/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,17 @@ impl TokenStreamMetadata {
pub fn into_stable(self) -> StableTokenStreamMetadata {
let original_file_path = self
.original_file_path
.and_then(|path| NonNull::new(CString::new(path).unwrap().into_raw()));
.and_then(|value| NonNull::new(CString::new(value).unwrap().into_raw()));
let file_id = self
.file_id
.and_then(|path| NonNull::new(CString::new(path).unwrap().into_raw()));
.and_then(|value| NonNull::new(CString::new(value).unwrap().into_raw()));
let edition = self
.edition
.and_then(|value| NonNull::new(CString::new(value).unwrap().into_raw()));
StableTokenStreamMetadata {
original_file_path,
file_id,
edition,
}
}

Expand All @@ -259,9 +263,11 @@ impl TokenStreamMetadata {
.original_file_path
.map(|raw| from_raw_cstr(raw.as_ptr()));
let file_id = metadata.file_id.map(|raw| from_raw_cstr(raw.as_ptr()));
let edition = metadata.edition.map(|raw| from_raw_cstr(raw.as_ptr()));
Self {
original_file_path,
file_id,
edition,
}
}

Expand All @@ -279,6 +285,9 @@ impl TokenStreamMetadata {
if let Some(raw) = metadata.file_id {
free_raw_cstring(raw.as_ptr());
}
if let Some(raw) = metadata.edition {
free_raw_cstring(raw.as_ptr());
}
}
}

Expand Down
5 changes: 4 additions & 1 deletion plugins/cairo-lang-macro/src/types/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ pub struct TokenStreamMetadata {
///
/// It is guaranteed, that the `file_id` will be unique for each file.
pub file_id: Option<String>,
/// Cairo edition defined for the token stream.
pub edition: Option<String>,
}

impl TokenStream {
Expand Down Expand Up @@ -102,10 +104,11 @@ impl Display for TokenStream {

impl TokenStreamMetadata {
#[doc(hidden)]
pub fn new(file_path: impl ToString, file_id: impl ToString) -> Self {
pub fn new(file_path: impl ToString, file_id: impl ToString, edition: impl ToString) -> Self {
Self {
original_file_path: Some(file_path.to_string()),
file_id: Some(file_id.to_string()),
edition: Some(edition.to_string()),
}
}
}
Expand Down
16 changes: 11 additions & 5 deletions scarb/src/compiler/plugin/proc_macro/host.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::compiler::plugin::proc_macro::{
Expansion, ExpansionKind, ProcMacroInstance, TokenStreamBuilder,
};
use crate::core::{Config, Package, PackageId};
use crate::core::{edition_variant, Config, Package, PackageId};
use anyhow::{ensure, Result};
use cairo_lang_defs::ids::{ModuleItemId, TopLevelLanguageElementId};
use cairo_lang_defs::patcher::{PatchBuilder, RewriteNode};
Expand All @@ -11,6 +11,7 @@ use cairo_lang_defs::plugin::{
};
use cairo_lang_defs::plugin::{InlineMacroExprPlugin, InlinePluginResult, PluginDiagnostic};
use cairo_lang_diagnostics::ToOption;
use cairo_lang_filesystem::db::Edition;
use cairo_lang_filesystem::ids::CodeMapping;
use cairo_lang_macro::{
AuxData, Diagnostic, FullPathMarker, ProcMacroResult, Severity, TokenStream,
Expand Down Expand Up @@ -844,11 +845,16 @@ impl ProcMacroHostPlugin {
.or_insert(markers);
}

fn calculate_metadata(db: &dyn SyntaxGroup, item_ast: ast::ModuleItem) -> TokenStreamMetadata {
fn calculate_metadata(
db: &dyn SyntaxGroup,
item_ast: ast::ModuleItem,
edition: Edition,
) -> TokenStreamMetadata {
let stable_ptr = item_ast.clone().stable_ptr().untyped();
let file_path = stable_ptr.file_id(db).full_path(db.upcast());
let file_id = short_hash(file_path.clone());
TokenStreamMetadata::new(file_path, file_id)
let edition = edition_variant(edition);
TokenStreamMetadata::new(file_path, file_id, edition)
}
}

Expand Down Expand Up @@ -938,9 +944,9 @@ impl MacroPlugin for ProcMacroHostPlugin {
&self,
db: &dyn SyntaxGroup,
item_ast: ast::ModuleItem,
_metadata: &MacroPluginMetadata<'_>,
metadata: &MacroPluginMetadata<'_>,
) -> PluginResult {
let stream_metadata = Self::calculate_metadata(db, item_ast.clone());
let stream_metadata = Self::calculate_metadata(db, item_ast.clone(), metadata.edition);

// Handle inner functions.
if let InnerAttrExpansionResult::Some(result) = self.expand_inner_attr(db, item_ast.clone())
Expand Down
3 changes: 3 additions & 0 deletions scarb/tests/build_cairo_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,9 @@ fn can_read_token_stream_metadata() {
file_id: Some(
"[..]",
),
edition: Some(
"[..]",
),
}
[..]Finished `dev` profile target(s) in [..]
"#});
Expand Down
Loading