Skip to content

Commit

Permalink
Made auto-generated copied structs properly mapped.
Browse files Browse the repository at this point in the history
Minor patcher refactor for support.
  • Loading branch information
orizi committed Aug 27, 2024
1 parent 26ce43c commit c6fedaf
Show file tree
Hide file tree
Showing 17 changed files with 421 additions and 398 deletions.
20 changes: 19 additions & 1 deletion crates/cairo-lang-defs/src/patcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub enum RewriteNode {
node: Box<RewriteNode>,
},
Text(String),
TextAndMapping(String, Vec<CodeMapping>),
}
impl RewriteNode {
pub fn new_trimmed(syntax_node: SyntaxNode) -> Self {
Expand Down Expand Up @@ -123,7 +124,9 @@ impl RewriteNode {
extract_matches!(self, RewriteNode::Modified)
}
RewriteNode::Modified(modified) => modified,
RewriteNode::Text(_) => panic!("A text node can't be modified"),
RewriteNode::Text(_) | RewriteNode::TextAndMapping(_, _) => {
panic!("A text node can't be modified")
}
RewriteNode::Mapped { .. } => panic!("A mapped node can't be modified"),
}
}
Expand Down Expand Up @@ -261,6 +264,12 @@ impl<'a> PatchBuilder<'a> {
(self.code, self.code_mappings)
}

/// Builds the patcher into a rewrite node enabling adding it to other patchers.
pub fn into_rewrite_node(self) -> RewriteNode {
let (code, mappings) = self.build();
RewriteNode::TextAndMapping(code, mappings)
}

pub fn add_char(&mut self, c: char) {
self.code.push(c);
}
Expand All @@ -284,6 +293,15 @@ impl<'a> PatchBuilder<'a> {
}
}
RewriteNode::Text(s) => self.add_str(s.as_str()),
RewriteNode::TextAndMapping(s, mappings) => {
let mapping_fix = TextWidth::from_str(&self.code);
self.add_str(&s);
self.code_mappings.extend(mappings.into_iter().map(|mut mapping| {
mapping.span.start = mapping.span.start.add_width(mapping_fix);
mapping.span.end = mapping.span.end.add_width(mapping_fix);
mapping
}));
}
}
}

Expand Down
4 changes: 1 addition & 3 deletions crates/cairo-lang-starknet/src/plugin/derive/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ pub fn handle_store_derive(
// a sub-pointers implementation.
let store_trait_code = handle_struct_store(db, struct_ast)?;
let sub_pointers_code = if !struct_ast.members(db).elements(db).is_empty() {
let (sub_pointers_code, _) =
handle_storage_interface_struct(db, struct_ast, metadata);
RewriteNode::Text(sub_pointers_code)
handle_storage_interface_struct(db, struct_ast, metadata).into_rewrite_node()
} else {
RewriteNode::Text("".to_string())
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,9 @@ impl MyTraitSafeDispatcherImpl of MyTraitSafeDispatcherTrait<MyTraitSafeDispatch
}


lib.cairo:1:16
lib.cairo:1:1
#[derive(Drop, starknet::Store)]
^*************^
^******************************^
impls:

impl MyTypeSubPointersDrop of core::traits::Drop::<MyTypeSubPointers>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,17 @@ pub trait HasComponent<TContractState> {



lib.cairo:1:1
#[starknet::component]
^********************^
lib.cairo:5:5
#[storage]
^********^
impls:

impl EventDrop of core::traits::Drop::<Event>;


lib.cairo:1:1
#[starknet::component]
^********************^
lib.cairo:5:5
#[storage]
^********^
starknet_derive:

impl EventIsEvent of starknet::Event<Event> {
Expand Down Expand Up @@ -303,17 +303,17 @@ pub trait HasComponent<TContractState> {



lib.cairo:1:1
#[starknet::component]
^********************^
lib.cairo:5:5
#[storage]
^********^
impls:

impl EventDrop of core::traits::Drop::<Event>;


lib.cairo:1:1
#[starknet::component]
^********************^
lib.cairo:5:5
#[storage]
^********^
starknet_derive:

impl EventIsEvent of starknet::Event<Event> {
Expand Down Expand Up @@ -483,17 +483,17 @@ pub trait HasComponent<TContractState> {



lib.cairo:1:1
#[starknet::component]
^********************^
lib.cairo:5:5
#[storage]
^********^
impls:

impl EventDrop of core::traits::Drop::<Event>;


lib.cairo:1:1
#[starknet::component]
^********************^
lib.cairo:5:5
#[storage]
^********^
starknet_derive:

impl EventIsEvent of starknet::Event<Event> {
Expand Down Expand Up @@ -659,17 +659,17 @@ pub trait HasComponent<TContractState> {



lib.cairo:1:1
#[starknet::component]
^********************^
lib.cairo:5:5
#[storage]
^********^
impls:

impl EventDrop of core::traits::Drop::<Event>;


lib.cairo:1:1
#[starknet::component]
^********************^
lib.cairo:5:5
#[storage]
^********^
starknet_derive:

impl EventIsEvent of starknet::Event<Event> {
Expand Down Expand Up @@ -835,17 +835,17 @@ pub trait HasComponent<TContractState> {



lib.cairo:1:1
#[starknet::component]
^********************^
lib.cairo:5:5
#[storage]
^********^
impls:

impl EventDrop of core::traits::Drop::<Event>;


lib.cairo:1:1
#[starknet::component]
^********************^
lib.cairo:5:5
#[storage]
^********^
starknet_derive:

impl EventIsEvent of starknet::Event<Event> {
Expand Down Expand Up @@ -1015,17 +1015,17 @@ pub trait HasComponent<TContractState> {



lib.cairo:1:1
#[starknet::component]
^********************^
lib.cairo:5:5
#[storage]
^********^
impls:

impl EventDrop of core::traits::Drop::<Event>;


lib.cairo:1:1
#[starknet::component]
^********************^
lib.cairo:5:5
#[storage]
^********^
starknet_derive:

impl EventIsEvent of starknet::Event<Event> {
Expand Down Expand Up @@ -1191,17 +1191,17 @@ pub trait HasComponent<TContractState> {



lib.cairo:1:1
#[starknet::component]
^********************^
lib.cairo:5:5
#[storage]
^********^
impls:

impl EventDrop of core::traits::Drop::<Event>;


lib.cairo:1:1
#[starknet::component]
^********************^
lib.cairo:5:5
#[storage]
^********^
starknet_derive:

impl EventIsEvent of starknet::Event<Event> {
Expand Down Expand Up @@ -1363,17 +1363,17 @@ pub trait HasComponent<TContractState> {



lib.cairo:1:1
#[starknet::component]
^********************^
lib.cairo:5:5
#[storage]
^********^
impls:

impl EventDrop of core::traits::Drop::<Event>;


lib.cairo:1:1
#[starknet::component]
^********************^
lib.cairo:5:5
#[storage]
^********^
starknet_derive:

impl EventIsEvent of starknet::Event<Event> {
Expand Down Expand Up @@ -1552,17 +1552,17 @@ pub impl MyImpl<
> of MyTrait<TContractState> {
}

lib.cairo:1:1
#[starknet::component]
^********************^
lib.cairo:5:5
#[storage]
^********^
impls:

impl EventDrop of core::traits::Drop::<Event>;


lib.cairo:1:1
#[starknet::component]
^********************^
lib.cairo:5:5
#[storage]
^********^
starknet_derive:

impl EventIsEvent of starknet::Event<Event> {
Expand Down Expand Up @@ -1776,17 +1776,17 @@ pub trait HasComponent<TContractState> {



lib.cairo:1:1
#[starknet::component]
^********************^
lib.cairo:5:5
#[storage]
^********^
impls:

impl EventDrop of core::traits::Drop::<Event>;


lib.cairo:1:1
#[starknet::component]
^********************^
lib.cairo:5:5
#[storage]
^********^
starknet_derive:

impl EventIsEvent of starknet::Event<Event> {
Expand Down Expand Up @@ -1956,17 +1956,17 @@ pub trait HasComponent<TContractState> {



lib.cairo:1:1
#[starknet::component]
^********************^
lib.cairo:5:5
#[storage]
^********^
impls:

impl EventDrop of core::traits::Drop::<Event>;


lib.cairo:1:1
#[starknet::component]
^********************^
lib.cairo:5:5
#[storage]
^********^
starknet_derive:

impl EventIsEvent of starknet::Event<Event> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -681,17 +681,17 @@ pub impl MyImpl<
}
}

lib.cairo:1:1
#[starknet::component]
^********************^
lib.cairo:5:5
#[storage]
^********^
impls:

impl EventDrop of core::traits::Drop::<Event>;


lib.cairo:1:1
#[starknet::component]
^********************^
lib.cairo:5:5
#[storage]
^********^
starknet_derive:

impl EventIsEvent of starknet::Event<Event> {
Expand Down
Loading

0 comments on commit c6fedaf

Please sign in to comment.