Skip to content

Commit

Permalink
Fix printed paths for traits and impls.
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseth committed Oct 25, 2024
1 parent c6a8a76 commit 5594ab5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 24 deletions.
10 changes: 8 additions & 2 deletions ast/src/analyzed/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,18 @@ impl<T: Display> Display for Analyzed<T> {
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)
Expand Down
30 changes: 20 additions & 10 deletions ast/src/parsed/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,12 +565,17 @@ impl Display for FunctionDefinition {

impl<E: Display> Display for TraitDeclaration<E> {
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<E: Display> TraitDeclaration<E> {
/// 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
)
Expand Down Expand Up @@ -649,10 +654,15 @@ impl<Expr: Display> Display for SelectedExpressions<Expr> {

impl<E: Display> Display for StructDeclaration<E> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(
f,
"struct {}{} {{\n{}}}",
self.name,
write!(f, "{}", self.to_string_with_name(&self.name))
}
}

impl<E: Display> StructDeclaration<E> {
/// 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
Expand Down
28 changes: 16 additions & 12 deletions pil-analyzer/tests/parse_display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -941,33 +941,37 @@ fn typed_literals() {
#[test]
fn traits_and_impls() {
let input = "
namespace N;
trait X<T> {
f: -> T,
g: T -> T,
}
impl X<int> {
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<int> {
f: || 1,
g: |x| x + 1,
}
";
let expected = r#" trait X<T> {
let expected = r#"namespace N;
trait X<T> {
f: -> T,
g: T -> T,
}
impl X<int> {
f: || 1_int,
g: |x| x + 1_int,
}
impl X<()> {
impl N::X<()> {
f: || (),
g: |()| (),
}
let a: int = X::f::<int>();
namespace Q;
let a: int = N::X::f::<int>();
impl N::X<int> {
f: || 1_int,
g: |x| x + 1_int,
}
"#;
let analyzed = analyze_string(input);
assert_eq!(analyzed.to_string(), expected);
Expand Down

0 comments on commit 5594ab5

Please sign in to comment.