From 5594ab53083e564b9e21897106ebd157a200a91d Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 25 Oct 2024 15:22:38 +0000 Subject: [PATCH] Fix printed paths for traits and impls. --- ast/src/analyzed/display.rs | 10 ++++++++-- ast/src/parsed/display.rs | 30 +++++++++++++++++++---------- pil-analyzer/tests/parse_display.rs | 28 +++++++++++++++------------ 3 files changed, 44 insertions(+), 24 deletions(-) diff --git a/ast/src/analyzed/display.rs b/ast/src/analyzed/display.rs index 9803a26c4..6e70929f7 100644 --- a/ast/src/analyzed/display.rs +++ b/ast/src/analyzed/display.rs @@ -106,12 +106,18 @@ impl Display for Analyzed { Some(FunctionValueDefinition::TypeDeclaration( TypeDeclaration::Struct(struct_declaration), )) => { - writeln_indented(f, struct_declaration)?; + writeln_indented( + f, + struct_declaration.to_string_with_name(&name), + )?; } Some(FunctionValueDefinition::TraitDeclaration( trait_declaration, )) => { - writeln_indented(f, trait_declaration)?; + writeln_indented( + f, + trait_declaration.to_string_with_name(&name), + )?; } _ => { unreachable!("Invalid definition for symbol: {}", name) diff --git a/ast/src/parsed/display.rs b/ast/src/parsed/display.rs index ddd38b899..ac0959bb2 100644 --- a/ast/src/parsed/display.rs +++ b/ast/src/parsed/display.rs @@ -565,12 +565,17 @@ impl Display for FunctionDefinition { impl Display for TraitDeclaration { fn fmt(&self, f: &mut Formatter<'_>) -> Result { - write!( - f, - "trait {name}<{type_vars}> {{\n{functions}}}", - name = self.name, - type_vars = self.type_vars.iter().format(", "), - functions = indent( + write!(f, "{}", self.to_string_with_name(&self.name)) + } +} + +impl TraitDeclaration { + /// Formats the trait declaration, exchanging its name by the provided one. + pub fn to_string_with_name(&self, name: &str) -> String { + format!( + "trait {name}<{}> {{\n{}}}", + self.type_vars.iter().format(", "), + indent( self.functions.iter().map(|m| format!("{m},\n")).format(""), 1 ) @@ -649,10 +654,15 @@ impl Display for SelectedExpressions { impl Display for StructDeclaration { fn fmt(&self, f: &mut Formatter<'_>) -> Result { - write!( - f, - "struct {}{} {{\n{}}}", - self.name, + write!(f, "{}", self.to_string_with_name(&self.name)) + } +} + +impl StructDeclaration { + /// Formats the struct declaration, exchanging its name by the provided one. + pub fn to_string_with_name(&self, name: &str) -> String { + format!( + "struct {name}{} {{\n{}}}", type_vars_to_string(&self.type_vars), indent( self.fields diff --git a/pil-analyzer/tests/parse_display.rs b/pil-analyzer/tests/parse_display.rs index 5f380805f..51c40a68c 100644 --- a/pil-analyzer/tests/parse_display.rs +++ b/pil-analyzer/tests/parse_display.rs @@ -941,33 +941,37 @@ fn typed_literals() { #[test] fn traits_and_impls() { let input = " + namespace N; trait X { f: -> T, g: T -> T, } - impl X { - f: || 1, - g: |x| x + 1, - } impl X<()> { f: || (), g: |()| (), } - let a: int = X::f(); + namespace Q; + let a: int = N::X::f(); + impl N::X { + f: || 1, + g: |x| x + 1, + } "; - let expected = r#" trait X { + let expected = r#"namespace N; + trait X { f: -> T, g: T -> T, } - impl X { - f: || 1_int, - g: |x| x + 1_int, - } - impl X<()> { + impl N::X<()> { f: || (), g: |()| (), } - let a: int = X::f::(); +namespace Q; + let a: int = N::X::f::(); + impl N::X { + f: || 1_int, + g: |x| x + 1_int, + } "#; let analyzed = analyze_string(input); assert_eq!(analyzed.to_string(), expected);