From 997a731456d467252086bedf56d1d574d7433eed Mon Sep 17 00:00:00 2001 From: ilyalesokhin-starkware Date: Tue, 9 Apr 2024 15:57:43 +0300 Subject: [PATCH] Add phantom types to sierra-gen. (#5382) --- crates/cairo-lang-semantic/src/types.rs | 12 +++++++++++- crates/cairo-lang-sierra-generator/src/db.rs | 16 +++++++++++++++- .../src/replace_ids.rs | 3 ++- crates/cairo-lang-sierra-generator/src/types.rs | 7 +++++++ 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/crates/cairo-lang-semantic/src/types.rs b/crates/cairo-lang-semantic/src/types.rs index 094f024c181..789449050fd 100644 --- a/crates/cairo-lang-semantic/src/types.rs +++ b/crates/cairo-lang-semantic/src/types.rs @@ -5,7 +5,7 @@ use cairo_lang_defs::ids::{ }; use cairo_lang_diagnostics::{DiagnosticAdded, Maybe}; use cairo_lang_proc_macros::SemanticObject; -use cairo_lang_syntax::attribute::consts::MUST_USE_ATTR; +use cairo_lang_syntax::attribute::consts::{MUST_USE_ATTR, PHANTOM_ATTR}; use cairo_lang_syntax::node::ids::SyntaxStablePtrId; use cairo_lang_syntax::node::{ast, TypedStablePtr, TypedSyntaxNode}; use cairo_lang_utils::{define_short_id, try_extract_matches, OptionFrom}; @@ -219,6 +219,16 @@ impl ConcreteTypeId { ) } } + + /// Returns whether the type has the `#[phantom]` attribute. + pub fn is_phantom(&self, db: &dyn SemanticGroup) -> Maybe { + match self { + ConcreteTypeId::Struct(id) => id.has_attr(db, PHANTOM_ATTR), + ConcreteTypeId::Enum(id) => id.has_attr(db, PHANTOM_ATTR), + ConcreteTypeId::Extern(id) => id.has_attr(db, PHANTOM_ATTR), + } + } + /// Returns whether the type has the `#[must_use]` attribute. pub fn is_must_use(&self, db: &dyn SemanticGroup) -> Maybe { match self { diff --git a/crates/cairo-lang-sierra-generator/src/db.rs b/crates/cairo-lang-sierra-generator/src/db.rs index 43c767a5d61..fc4ba3749ff 100644 --- a/crates/cairo-lang-sierra-generator/src/db.rs +++ b/crates/cairo-lang-sierra-generator/src/db.rs @@ -25,6 +25,9 @@ pub enum SierraGeneratorTypeLongId { Regular(Arc), /// The long id for cycle breakers, such as `Box` and `Nullable`. CycleBreaker(semantic::TypeId), + /// This is a long id of a phantom type. + /// Phantom types have a one to one mapping from the semantic type to the sierra type. + Phantom(semantic::TypeId), } #[salsa::query_group(SierraGenDatabase)] @@ -198,6 +201,16 @@ fn get_type_info( zero_sized: false, })); } + SierraGeneratorTypeLongId::Phantom(ty) => { + let long_id = db.get_concrete_long_type_id(ty)?.as_ref().clone(); + return Ok(Arc::new(cairo_lang_sierra::extensions::types::TypeInfo { + long_id, + storable: false, + droppable: false, + duplicatable: false, + zero_sized: false, + })); + } }; let concrete_ty = cairo_lang_sierra::extensions::core::CoreType::specialize_by_id( &SierraSignatureSpecializationContext(db), @@ -219,6 +232,7 @@ pub fn sierra_concrete_long_id( ) -> Maybe> { match db.lookup_intern_concrete_type(concrete_type_id) { SierraGeneratorTypeLongId::Regular(long_id) => Ok(long_id), - SierraGeneratorTypeLongId::CycleBreaker(type_id) => db.get_concrete_long_type_id(type_id), + SierraGeneratorTypeLongId::Phantom(type_id) + | SierraGeneratorTypeLongId::CycleBreaker(type_id) => db.get_concrete_long_type_id(type_id), } } diff --git a/crates/cairo-lang-sierra-generator/src/replace_ids.rs b/crates/cairo-lang-sierra-generator/src/replace_ids.rs index 864ec7cd193..ac227235e30 100644 --- a/crates/cairo-lang-sierra-generator/src/replace_ids.rs +++ b/crates/cairo-lang-sierra-generator/src/replace_ids.rs @@ -105,7 +105,8 @@ impl SierraIdReplacer for DebugReplacer<'_> { id: &cairo_lang_sierra::ids::ConcreteTypeId, ) -> cairo_lang_sierra::ids::ConcreteTypeId { match self.db.lookup_intern_concrete_type(id.clone()) { - SierraGeneratorTypeLongId::CycleBreaker(ty) => ty.format(self.db.upcast()).into(), + SierraGeneratorTypeLongId::Phantom(ty) + | SierraGeneratorTypeLongId::CycleBreaker(ty) => ty.format(self.db.upcast()).into(), SierraGeneratorTypeLongId::Regular(long_id) => { let mut long_id = long_id.as_ref().clone(); self.replace_generic_args(&mut long_id.generic_args); diff --git a/crates/cairo-lang-sierra-generator/src/types.rs b/crates/cairo-lang-sierra-generator/src/types.rs index 43942bcb0c6..09428dec8b4 100644 --- a/crates/cairo-lang-sierra-generator/src/types.rs +++ b/crates/cairo-lang-sierra-generator/src/types.rs @@ -36,6 +36,13 @@ pub fn get_concrete_type_id( ) if db.is_self_referential(type_id)? => { Ok(db.intern_concrete_type(SierraGeneratorTypeLongId::CycleBreaker(type_id))) } + + semantic::TypeLongId::Concrete(concrete_type_id) + if concrete_type_id.is_phantom(db.upcast())? => + { + Ok(db.intern_concrete_type(SierraGeneratorTypeLongId::Phantom(type_id))) + } + _ => Ok(db.intern_concrete_type(SierraGeneratorTypeLongId::Regular( db.get_concrete_long_type_id(type_id)?, ))),