Skip to content

Commit 4f4bfb7

Browse files
Consistent formatting of generic args. (#7944)
1 parent e22cf51 commit 4f4bfb7

File tree

6 files changed

+59
-31
lines changed

6 files changed

+59
-31
lines changed

crates/cairo-lang-semantic/src/expr/fmt.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,26 @@ impl Upcast<dyn DefsGroup + 'static> for ExprFormatter<'_> {
2121
self.db
2222
}
2323
}
24+
25+
/// A wrapper around std::fmt::Formatter that counts the number of characters written so far.
26+
pub struct CountingWriter<'a, 'b> {
27+
inner: &'a mut std::fmt::Formatter<'b>,
28+
count: usize,
29+
}
30+
31+
impl<'a, 'b> CountingWriter<'a, 'b> {
32+
pub fn new(inner: &'a mut std::fmt::Formatter<'b>) -> Self {
33+
Self { inner, count: 0 }
34+
}
35+
36+
pub fn count(&self) -> usize {
37+
self.count
38+
}
39+
}
40+
41+
impl<'a, 'b> std::fmt::Write for CountingWriter<'a, 'b> {
42+
fn write_str(&mut self, s: &str) -> std::fmt::Result {
43+
self.count += s.len();
44+
self.inner.write_str(s)
45+
}
46+
}

crates/cairo-lang-semantic/src/items/functions.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::fmt::Write;
12
use std::sync::Arc;
23

34
use cairo_lang_debug::DebugWithDb;
@@ -31,6 +32,7 @@ use crate::corelib::{fn_traits, unit_ty};
3132
use crate::db::SemanticGroup;
3233
use crate::diagnostic::{SemanticDiagnosticKind, SemanticDiagnostics, SemanticDiagnosticsBuilder};
3334
use crate::expr::compute::Environment;
35+
use crate::expr::fmt::CountingWriter;
3436
use crate::resolve::{Resolver, ResolverData};
3537
use crate::substitution::GenericSubstitution;
3638
use crate::types::resolve_type;
@@ -589,6 +591,7 @@ impl DebugWithDb<dyn SemanticGroup> for ConcreteFunctionWithBody {
589591
f: &mut std::fmt::Formatter<'_>,
590592
db: &(dyn SemanticGroup + 'static),
591593
) -> std::fmt::Result {
594+
let f = &mut CountingWriter::new(f);
592595
write!(f, "{}", self.generic_function.full_path(db))?;
593596
fmt_generic_args(&self.generic_args, f, db)
594597
}
@@ -688,6 +691,7 @@ impl DebugWithDb<dyn SemanticGroup> for ConcreteFunction {
688691
f: &mut std::fmt::Formatter<'_>,
689692
db: &(dyn SemanticGroup + 'static),
690693
) -> std::fmt::Result {
694+
let f = &mut CountingWriter::new(f);
691695
write!(f, "{}", self.generic_function.format(db))?;
692696
fmt_generic_args(&self.generic_args, f, db)
693697
}

crates/cairo-lang-semantic/src/items/generics.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::fmt::Write;
12
use std::hash::Hash;
23
use std::sync::Arc;
34

@@ -25,6 +26,7 @@ use crate::db::SemanticGroup;
2526
use crate::diagnostic::{
2627
NotFoundItemType, SemanticDiagnosticKind, SemanticDiagnostics, SemanticDiagnosticsBuilder,
2728
};
29+
use crate::expr::fmt::CountingWriter;
2830
use crate::expr::inference::InferenceId;
2931
use crate::expr::inference::canonic::ResultNoErrEx;
3032
use crate::lookup_item::LookupItemEx;
@@ -638,16 +640,24 @@ fn impl_generic_param_semantic(
638640
/// Formats a list of generic arguments.
639641
pub fn fmt_generic_args(
640642
generic_args: &[GenericArgumentId],
641-
f: &mut std::fmt::Formatter<'_>,
643+
f: &mut CountingWriter<'_, '_>,
642644
db: &(dyn SemanticGroup + 'static),
643645
) -> std::fmt::Result {
644-
if !generic_args.is_empty() {
646+
let mut generic_args = generic_args.iter();
647+
if let Some(first) = generic_args.next() {
648+
// Soft limit for the number of chars in the formatted type.
649+
const CHARS_BOUND: usize = 500;
645650
write!(f, "::<")?;
646-
for (i, arg) in generic_args.iter().enumerate() {
647-
if i > 0 {
648-
write!(f, ", ")?;
651+
write!(f, "{}", &first.format(db))?;
652+
653+
for arg in generic_args {
654+
write!(f, ", ")?;
655+
if f.count() > CHARS_BOUND {
656+
// If the formatted type is becoming too long, add short version of arguments.
657+
write!(f, "{}", &arg.short_name(db))?;
658+
} else {
659+
write!(f, "{}", &arg.format(db))?;
649660
}
650-
write!(f, "{}", arg.format(db))?;
651661
}
652662
write!(f, ">")?;
653663
}

crates/cairo-lang-semantic/src/items/imp.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::collections::BTreeSet;
2+
use std::fmt::Write;
23
use std::hash::Hash;
34
use std::sync::Arc;
45
use std::{mem, panic, vec};
@@ -66,6 +67,7 @@ use crate::db::{SemanticGroup, get_resolver_data_options};
6667
use crate::diagnostic::SemanticDiagnosticKind::{self, *};
6768
use crate::diagnostic::{NotFoundItemType, SemanticDiagnostics, SemanticDiagnosticsBuilder};
6869
use crate::expr::compute::{ComputationContext, ContextFunction, Environment, compute_root_expr};
70+
use crate::expr::fmt::CountingWriter;
6971
use crate::expr::inference::canonic::ResultNoErrEx;
7072
use crate::expr::inference::conform::InferenceConform;
7173
use crate::expr::inference::infers::InferenceEmbeddings;
@@ -113,8 +115,9 @@ impl DebugWithDb<dyn SemanticGroup> for ConcreteImplLongId {
113115
f: &mut std::fmt::Formatter<'_>,
114116
db: &(dyn SemanticGroup + 'static),
115117
) -> std::fmt::Result {
118+
let mut f = CountingWriter::new(f);
116119
write!(f, "{}", self.impl_def_id.full_path(db))?;
117-
fmt_generic_args(&self.generic_args, f, db)
120+
fmt_generic_args(&self.generic_args, &mut f, db)
118121
}
119122
}
120123
impl ConcreteImplId {

crates/cairo-lang-semantic/src/items/trt.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::fmt::Write;
12
use std::sync::Arc;
23

34
use cairo_lang_debug::DebugWithDb;
@@ -34,6 +35,7 @@ use crate::db::{SemanticGroup, get_resolver_data_options};
3435
use crate::diagnostic::SemanticDiagnosticKind::{self, *};
3536
use crate::diagnostic::{NotFoundItemType, SemanticDiagnostics, SemanticDiagnosticsBuilder};
3637
use crate::expr::compute::{ComputationContext, ContextFunction, Environment, compute_root_expr};
38+
use crate::expr::fmt::CountingWriter;
3739
use crate::expr::inference::InferenceId;
3840
use crate::expr::inference::canonic::ResultNoErrEx;
3941
use crate::items::feature_kind::HasFeatureKind;
@@ -60,8 +62,9 @@ impl DebugWithDb<dyn SemanticGroup> for ConcreteTraitLongId {
6062
f: &mut std::fmt::Formatter<'_>,
6163
db: &(dyn SemanticGroup + 'static),
6264
) -> std::fmt::Result {
65+
let mut f = CountingWriter::new(f);
6366
write!(f, "{}", self.trait_id.full_path(db))?;
64-
fmt_generic_args(&self.generic_args, f, db)
67+
fmt_generic_args(&self.generic_args, &mut f, db)
6568
}
6669
}
6770

crates/cairo-lang-semantic/src/types.rs

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::fmt::Write;
2+
13
use cairo_lang_debug::DebugWithDb;
24
use cairo_lang_defs::diagnostic_utils::StableLocation;
35
use cairo_lang_defs::ids::{
@@ -27,10 +29,12 @@ use crate::diagnostic::{NotFoundItemType, SemanticDiagnostics, SemanticDiagnosti
2729
use crate::expr::compute::{
2830
ComputationContext, ContextFunction, Environment, compute_expr_semantic,
2931
};
32+
use crate::expr::fmt::CountingWriter;
3033
use crate::expr::inference::canonic::ResultNoErrEx;
3134
use crate::expr::inference::{InferenceData, InferenceError, InferenceId, TypeVar};
3235
use crate::items::attribute::SemanticQueryAttrs;
3336
use crate::items::constant::{ConstValue, ConstValueId, resolve_const_expr_and_evaluate};
37+
use crate::items::generics::fmt_generic_args;
3438
use crate::items::imp::{ImplId, ImplLookupContext};
3539
use crate::resolve::{ResolutionContext, ResolvedConcreteItem, Resolver};
3640
use crate::substitution::SemanticRewriter;
@@ -305,28 +309,7 @@ impl ConcreteTypeId {
305309
}
306310
}
307311
pub fn format(&self, db: &dyn SemanticGroup) -> String {
308-
let generic_type_format = self.generic_type(db).format(db);
309-
let mut generic_args = self.generic_args(db).into_iter();
310-
if let Some(first) = generic_args.next() {
311-
// Soft limit for the number of chars in the formatted type.
312-
const CHARS_BOUND: usize = 500;
313-
let mut f = generic_type_format;
314-
f.push_str("::<");
315-
f.push_str(&first.format(db));
316-
for arg in generic_args {
317-
f.push_str(", ");
318-
if f.len() > CHARS_BOUND {
319-
// If the formatted type is becoming too long, add short version of arguments.
320-
f.push_str(&arg.short_name(db));
321-
} else {
322-
f.push_str(&arg.format(db));
323-
}
324-
}
325-
f.push('>');
326-
f
327-
} else {
328-
generic_type_format
329-
}
312+
format!("{:?}", self.debug(db.elongate()))
330313
}
331314

332315
/// Returns whether the type has the `#[must_use]` attribute.
@@ -354,7 +337,9 @@ impl DebugWithDb<dyn SemanticGroup> for ConcreteTypeId {
354337
f: &mut std::fmt::Formatter<'_>,
355338
db: &(dyn SemanticGroup + 'static),
356339
) -> std::fmt::Result {
357-
write!(f, "{}", self.format(db))
340+
let f = &mut CountingWriter::new(f);
341+
write!(f, "{}", self.generic_type(db).format(db))?;
342+
fmt_generic_args(&self.generic_args(db), f, db)
358343
}
359344
}
360345

0 commit comments

Comments
 (0)