Skip to content

Commit

Permalink
Making full_path more consistent. (#7080)
Browse files Browse the repository at this point in the history
  • Loading branch information
orizi authored Jan 14, 2025
1 parent 882fa34 commit 2d0c700
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 43 deletions.
5 changes: 3 additions & 2 deletions crates/cairo-lang-executable/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@ pub fn compile_executable_in_prepared_db(
///
/// If the executable is not wrapping a function, returns the full path of the executable.
fn originating_function_path(db: &RootDatabase, wrapper: ConcreteFunctionWithBodyId) -> String {
let wrapper_name = wrapper.name(db);
let wrapper_full_path = wrapper.base_semantic_function(db).full_path(db.upcast());
let semantic = wrapper.base_semantic_function(db);
let wrapper_name = semantic.name(db);
let wrapper_full_path = semantic.full_path(db.upcast());
let Some(wrapped_name) = wrapper_name.strip_prefix(EXECUTABLE_PREFIX) else {
return wrapper_full_path;
};
Expand Down
50 changes: 29 additions & 21 deletions crates/cairo-lang-lowering/src/ids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use defs::ids::{ExternFunctionId, FreeFunctionId};
use semantic::items::functions::GenericFunctionId;
use semantic::substitution::{GenericSubstitution, SubstitutionRewriter};
use semantic::{ExprVar, Mutability};
use smol_str::SmolStr;
use {cairo_lang_defs as defs, cairo_lang_semantic as semantic};

use crate::Location;
Expand Down Expand Up @@ -169,10 +168,10 @@ impl ConcreteFunctionWithBodyLongId {
ConcreteFunctionWithBodyLongId::Generated(generated) => generated.parent,
}
}
pub fn name(&self, db: &dyn LoweringGroup) -> SmolStr {
pub fn full_path(&self, db: &dyn LoweringGroup) -> String {
match self {
ConcreteFunctionWithBodyLongId::Semantic(semantic) => semantic.name(db.upcast()),
ConcreteFunctionWithBodyLongId::Generated(generated) => generated.name(db),
ConcreteFunctionWithBodyLongId::Semantic(semantic) => semantic.full_path(db.upcast()),
ConcreteFunctionWithBodyLongId::Generated(generated) => generated.full_path(db),
}
}
}
Expand All @@ -192,8 +191,8 @@ impl ConcreteFunctionWithBodyId {
pub fn function_id(&self, db: &dyn LoweringGroup) -> Maybe<FunctionId> {
self.lookup_intern(db).function_id(db)
}
pub fn name(&self, db: &dyn LoweringGroup) -> SmolStr {
self.lookup_intern(db).name(db)
pub fn full_path(&self, db: &dyn LoweringGroup) -> String {
self.lookup_intern(db).full_path(db)
}
pub fn signature(&self, db: &dyn LoweringGroup) -> Maybe<Signature> {
let generic_signature = self.function_with_body_id(db).signature(db)?;
Expand Down Expand Up @@ -311,18 +310,15 @@ impl FunctionLongId {
FunctionLongId::Generated(generated) => generated.body(db).signature(db),
}
}
pub fn name(&self, db: &dyn LoweringGroup) -> SmolStr {
match *self {
FunctionLongId::Semantic(semantic) => semantic.name(db.upcast()),
FunctionLongId::Generated(generated) => generated.name(db),
}
pub fn full_path(&self, db: &dyn LoweringGroup) -> String {
format!("{:?}", self.debug(db))
}
/// Returns the full path of the relevant semantic function:
/// - If the function itself is semantic (non generated), its own full path.
/// - If the function is generated, then its (semantic) parent's full path.
pub fn semantic_full_path(&self, db: &dyn LoweringGroup) -> String {
match self {
FunctionLongId::Semantic(id) => id.full_name(db.upcast()),
FunctionLongId::Semantic(id) => id.full_path(db.upcast()),
FunctionLongId::Generated(generated) => generated.parent.full_path(db.upcast()),
}
}
Expand All @@ -334,8 +330,8 @@ impl FunctionId {
pub fn signature(&self, db: &dyn LoweringGroup) -> Maybe<Signature> {
self.lookup_intern(db).signature(db)
}
pub fn name(&self, db: &dyn LoweringGroup) -> SmolStr {
self.lookup_intern(db).name(db)
pub fn full_path(&self, db: &dyn LoweringGroup) -> String {
self.lookup_intern(db).full_path(db)
}
pub fn semantic_full_path(&self, db: &dyn LoweringGroup) -> String {
self.lookup_intern(db).semantic_full_path(db)
Expand Down Expand Up @@ -378,10 +374,8 @@ impl<'a> DebugWithDb<dyn LoweringGroup + 'a> for FunctionLongId {
db: &(dyn LoweringGroup + 'a),
) -> std::fmt::Result {
match self {
FunctionLongId::Semantic(semantic) => semantic.fmt(f, db),
FunctionLongId::Generated(generated) => {
write!(f, "{}", generated.name(db))
}
FunctionLongId::Semantic(semantic) => write!(f, "{:?}", semantic.debug(db)),
FunctionLongId::Generated(generated) => write!(f, "{:?}", generated.debug(db)),
}
}
}
Expand All @@ -406,13 +400,27 @@ impl GeneratedFunction {
let long_id = ConcreteFunctionWithBodyLongId::Generated(GeneratedFunction { parent, key });
long_id.intern(db)
}
pub fn name(&self, db: &dyn LoweringGroup) -> SmolStr {
pub fn full_path(&self, db: &dyn LoweringGroup) -> String {
match self.key {
GeneratedFunctionKey::Loop(expr_id) => {
format!("{}[expr{}]", self.parent.full_path(db.upcast()), expr_id.index())
}
GeneratedFunctionKey::TraitFunc(trait_func, _) => trait_func.full_path(db.upcast()),
}
}
}
impl<'a> DebugWithDb<dyn LoweringGroup + 'a> for GeneratedFunction {
fn fmt(
&self,
f: &mut std::fmt::Formatter<'_>,
db: &(dyn LoweringGroup + 'a),
) -> std::fmt::Result {
match self.key {
GeneratedFunctionKey::Loop(expr_id) => {
format!("{}[expr{}]", self.parent.full_path(db.upcast()), expr_id.index()).into()
write!(f, "{:?}[expr{}]", self.parent.debug(db), expr_id.index())
}
GeneratedFunctionKey::TraitFunc(trait_func, _) => {
format!("{:?}", trait_func.debug(db)).into()
write!(f, "{:?}", trait_func.debug(db))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/cairo-lang-semantic/src/items/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,7 @@ impl ConstantEvaluateContext<'_> {
_ => (diagnostic.into(), vec![]),
};
notes.push(DiagnosticNote::with_location(
format!("In `{}`", concrete_function.full_name(db)),
format!("In `{}`", concrete_function.full_path(db)),
location,
));
self.diagnostics.report(
Expand Down
24 changes: 6 additions & 18 deletions crates/cairo-lang-semantic/src/items/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,8 @@ impl FunctionId {
format!("{:?}", self.get_concrete(db).generic_function.name(db)).into()
}

pub fn full_name(&self, db: &dyn SemanticGroup) -> String {
self.get_concrete(db).full_name(db)
pub fn full_path(&self, db: &dyn SemanticGroup) -> String {
self.get_concrete(db).full_path(db)
}

/// Returns true if the function does not depend on any generics.
Expand Down Expand Up @@ -594,7 +594,7 @@ impl ConcreteFunctionWithBody {
self.function_with_body_id(db).name(db.upcast())
}
pub fn full_path(&self, db: &dyn SemanticGroup) -> String {
self.generic_function.full_path(db)
format!("{:?}", self.debug(db.elongate()))
}
}

Expand All @@ -604,7 +604,7 @@ impl DebugWithDb<dyn SemanticGroup> for ConcreteFunctionWithBody {
f: &mut std::fmt::Formatter<'_>,
db: &(dyn SemanticGroup + 'static),
) -> std::fmt::Result {
write!(f, "{:?}", self.generic_function.name(db.upcast()))?;
write!(f, "{}", self.generic_function.full_path(db))?;
fmt_generic_args(&self.generic_args, f, db)
}
}
Expand Down Expand Up @@ -693,20 +693,8 @@ impl ConcreteFunction {
.intern(db),
))
}
pub fn full_name(&self, db: &dyn SemanticGroup) -> String {
let maybe_generic_part = if !self.generic_args.is_empty() {
let mut generics = String::new();
for (i, arg) in self.generic_args.iter().enumerate() {
if i > 0 {
generics.push_str(", ");
}
generics.push_str(&arg.format(db));
}
format!("::<{generics}>")
} else {
"".to_string()
};
format!("{}{maybe_generic_part}", self.generic_function.format(db.upcast()))
pub fn full_path(&self, db: &dyn SemanticGroup) -> String {
format!("{:?}", self.debug(db.elongate()))
}
}
impl DebugWithDb<dyn SemanticGroup> for ConcreteFunction {
Expand Down
3 changes: 3 additions & 0 deletions crates/cairo-lang-semantic/src/items/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ impl ConcreteImplId {
pub fn name(&self, db: &dyn SemanticGroup) -> SmolStr {
self.impl_def_id(db).name(db.upcast())
}
pub fn full_path(&self, db: &dyn SemanticGroup) -> String {
format!("{:?}", self.debug(db.elongate()))
}
pub fn substitution(&self, db: &dyn SemanticGroup) -> Maybe<GenericSubstitution> {
Ok(GenericSubstitution::from_impl(ImplLongId::Concrete(*self).intern(db)).concat(
GenericSubstitution::new(
Expand Down
2 changes: 1 addition & 1 deletion crates/cairo-lang-semantic/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ impl DebugWithDb<dyn SemanticGroup> for TypeLongId {
write!(f, "{}::{}", impl_type_id.impl_id.name(db), impl_type_id.ty.name(def_db))
}
TypeLongId::Var(var) => write!(f, "?{}", var.id.0),
TypeLongId::Coupon(function_id) => write!(f, "{}::Coupon", function_id.full_name(db)),
TypeLongId::Coupon(function_id) => write!(f, "{}::Coupon", function_id.full_path(db)),
TypeLongId::Missing(_) => write!(f, "<missing>"),
TypeLongId::FixedSizeArray { type_id, size } => {
write!(f, "[{}; {:?}]", type_id.format(db), size.debug(db.elongate()))
Expand Down

0 comments on commit 2d0c700

Please sign in to comment.