Skip to content

Commit

Permalink
Add cairo edition to TokenStreamMetadata (#1704)
Browse files Browse the repository at this point in the history
commit-id:83c7db1f

---

**Stack**:
- #1734
- #1722
- #1704⚠️ *Part of a stack created by [spr](https://github.com/ejoffe/spr). Do
not merge manually using the UI - doing so may have unexpected results.*
  • Loading branch information
maciektr committed Nov 14, 2024
1 parent 7502c67 commit 293aa94
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
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 @@ -813,11 +814,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 @@ -894,9 +900,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 @@ -527,6 +527,9 @@ fn can_read_token_stream_metadata() {
file_id: Some(
"[..]",
),
edition: Some(
"[..]",
),
}
[..]Finished `dev` profile target(s) in [..]
"#});
Expand Down

0 comments on commit 293aa94

Please sign in to comment.