Fork of daniellemaywood.uk/gleam — Wasm codegen work
2

Configure Feed

Select the types of activity you want to include in your feed.

move type pretty printing to new arena

author
Giacomo Cavalieri
committer
Louis Pilfold
date (Jun 25, 2026, 1:58 PM +0100) commit 2f725f55 parent 4191c186 change-id tszxusmz
+78 -49
+76 -49
compiler-core/src/type_/pretty.rs
··· 2 2 // SPDX-FileCopyrightText: 2020 The Gleam contributors 3 3 4 4 use super::{Type, TypeVar}; 5 - use crate::{ 6 - docvec, 7 - pretty::{nil, *}, 8 - }; 9 5 use ecow::EcoString; 6 + use pretty_arena::*; 10 7 use std::sync::Arc; 11 8 12 9 #[cfg(test)] ··· 27 24 printed_types: im::HashMap<EcoString, EcoString>, 28 25 } 29 26 30 - impl Printer { 27 + impl<'a, 'doc> Printer { 31 28 pub fn new() -> Self { 32 29 Self::default() 33 30 } ··· 39 36 /// Render a Type as a well formatted string. 40 37 /// 41 38 pub fn pretty_print(&mut self, type_: &Type, initial_indent: usize) -> String { 42 - let mut buffer = String::with_capacity(initial_indent); 43 - for _ in 0..initial_indent { 44 - buffer.push(' '); 45 - } 46 - buffer 47 - .to_doc() 48 - .append(self.print(type_)) 49 - .nest(initial_indent as isize) 39 + let arena = DocumentArena::new(); 40 + 41 + EcoString::from(" ".repeat(initial_indent)) 42 + .to_doc(&arena) 43 + .append(&arena, self.print(&arena, type_)) 44 + .nest(&arena, initial_indent as isize) 50 45 .to_pretty_string(80) 51 46 } 52 47 53 48 // TODO: have this function return a Document that borrows from the Type. 54 49 // Is this possible? The lifetime would have to go through the Arc<Refcell<Type>> 55 50 // for TypeVar::Link'd types. 56 - pub fn print<'a>(&mut self, type_: &Type) -> Document<'a> { 51 + fn print(&mut self, arena: &'doc DocumentArena<'a, 'doc>, type_: &Type) -> Document<'a, 'doc> { 57 52 match type_ { 58 53 Type::Named { 59 54 name, ··· 62 57 .. 63 58 } => { 64 59 let doc = if self.name_clashes_if_unqualified(name, module) { 65 - qualify_type_name(module, name) 60 + qualify_type_name(arena, module, name) 66 61 } else { 67 62 let _ = self.printed_types.insert(name.clone(), module.clone()); 68 - name.to_doc() 63 + name.to_doc(arena) 69 64 }; 70 65 if arguments.is_empty() { 71 66 doc 72 67 } else { 73 - doc.append("(") 74 - .append(self.arguments_to_gleam_doc(arguments)) 75 - .append(")") 68 + docvec![ 69 + arena, 70 + doc, 71 + OPEN_PAREN_DOCUMENT, 72 + self.arguments_to_gleam_doc(arena, arguments), 73 + CLOSE_PAREN_DOCUMENT 74 + ] 76 75 } 77 76 } 78 77 79 - Type::Fn { arguments, return_ } => "fn(" 80 - .to_doc() 81 - .append(self.arguments_to_gleam_doc(arguments)) 82 - .append(") ->") 78 + Type::Fn { arguments, return_ } => FN_OPEN_PAREN_DOCUMENT 79 + .append(arena, self.arguments_to_gleam_doc(arena, arguments)) 80 + .append(arena, CLOSE_PAREN_SLIM_ARROW_DOCUMENT) 83 81 .append( 84 - break_("", " ") 85 - .append(self.print(return_)) 86 - .nest(INDENT) 87 - .group(), 82 + arena, 83 + BREAKABLE_SPACE_DOCUMENT 84 + .append(arena, self.print(arena, return_)) 85 + .nest(arena, INDENT) 86 + .group(arena), 88 87 ), 89 88 90 - Type::Var { type_, .. } => self.type_var_doc(&type_.borrow()), 89 + Type::Var { type_, .. } => self.type_var_doc(arena, &type_.borrow()), 91 90 92 - Type::Tuple { elements, .. } => { 93 - self.arguments_to_gleam_doc(elements).surround("#(", ")") 94 - } 91 + Type::Tuple { elements, .. } => self.arguments_to_gleam_doc(arena, elements).surround( 92 + arena, 93 + OPEN_TUPLE_DOCUMENT, 94 + CLOSE_PAREN_DOCUMENT, 95 + ), 95 96 } 96 97 } 97 98 ··· 103 104 } 104 105 } 105 106 106 - fn type_var_doc<'a>(&mut self, type_: &TypeVar) -> Document<'a> { 107 + fn type_var_doc( 108 + &mut self, 109 + arena: &'doc DocumentArena<'a, 'doc>, 110 + type_: &TypeVar, 111 + ) -> Document<'a, 'doc> { 107 112 match type_ { 108 - TypeVar::Link { type_, .. } => self.print(type_), 109 - TypeVar::Unbound { id, .. } | TypeVar::Generic { id, .. } => self.generic_type_var(*id), 113 + TypeVar::Link { type_, .. } => self.print(arena, type_), 114 + TypeVar::Unbound { id, .. } | TypeVar::Generic { id, .. } => { 115 + self.generic_type_var(arena, *id) 116 + } 110 117 } 111 118 } 112 119 113 - pub fn generic_type_var<'a>(&mut self, id: u64) -> Document<'a> { 120 + pub fn generic_type_var( 121 + &mut self, 122 + arena: &'doc DocumentArena<'a, 'doc>, 123 + id: u64, 124 + ) -> Document<'a, 'doc> { 114 125 match self.names.get(&id) { 115 126 Some(n) => { 116 127 let _ = self.printed_types.insert(n.clone(), "".into()); 117 - n.to_doc() 128 + n.to_doc(arena) 118 129 } 119 130 None => { 120 131 let n = self.next_letter(); 121 132 let _ = self.names.insert(id, n.clone()); 122 133 let _ = self.printed_types.insert(n.clone(), "".into()); 123 - n.to_doc() 134 + n.to_doc(arena) 124 135 } 125 136 } 126 137 } ··· 147 158 chars.into_iter().rev().collect() 148 159 } 149 160 150 - fn arguments_to_gleam_doc(&mut self, arguments: &[Arc<Type>]) -> Document<'static> { 161 + fn arguments_to_gleam_doc( 162 + &mut self, 163 + arena: &'doc DocumentArena<'a, 'doc>, 164 + arguments: &[Arc<Type>], 165 + ) -> Document<'a, 'doc> { 151 166 if arguments.is_empty() { 152 - return nil(); 167 + return EMPTY_DOCUMENT; 153 168 } 154 169 155 - let arguments = join( 156 - arguments.iter().map(|type_| self.print(type_).group()), 157 - break_(",", ", "), 170 + let arguments = arena.join( 171 + arguments 172 + .iter() 173 + .map(|type_| self.print(arena, type_).group(arena)), 174 + COMMA_BREAK_DOCUMENT, 158 175 ); 159 - break_("", "") 160 - .append(arguments) 161 - .nest(INDENT) 162 - .append(break_(",", "")) 163 - .group() 176 + 177 + EMPTY_BREAK_DOCUMENT 178 + .append(arena, arguments) 179 + .nest(arena, INDENT) 180 + .append(arena, TRAILING_COMMA_BREAK_DOCUMENT) 181 + .group(arena) 164 182 } 165 183 } 166 184 167 - fn qualify_type_name(module: &str, type_name: &str) -> Document<'static> { 168 - docvec![EcoString::from(module), ".", EcoString::from(type_name)] 185 + fn qualify_type_name<'a, 'doc>( 186 + arena: &'doc DocumentArena<'a, 'doc>, 187 + module: &str, 188 + type_name: &str, 189 + ) -> Document<'a, 'doc> { 190 + docvec![ 191 + arena, 192 + EcoString::from(module), 193 + ".", 194 + EcoString::from(type_name) 195 + ] 169 196 } 170 197 171 198 #[test]
+2
pretty-arena/src/lib.rs
··· 754 754 const_str!(VALUE_INSTANCE_OF_SPACE_DOCUMENT, "value instanceof ", 17); 755 755 const_str!(DOLLAR_DOCUMENT, "$", 1); 756 756 const_str!(CLOSE_PAREN_ARROW_DOCUMENT, ") =>", 4); 757 + const_str!(CLOSE_PAREN_SLIM_ARROW_DOCUMENT, ") ->", 4); 757 758 const_str!(SPACE_EQUAL_SPACE_OPEN_PAREN_DOCUMENT, " = (", 4); 758 759 const_str!(DOLLAR_CONST_SEMICOLON_DOCUMENT, "$const;", 7); 759 760 const_str!(DOLLAR_CONST_DOCUMENT, "$const", 6); ··· 934 935 const_str!(PUB_FN_SPACE_DOCUMENT, "pub fn ", 7); 935 936 const_str!(PUB_CONST_SPACE_DOCUMENT, "pub const ", 10); 936 937 const_str!(PUB_OPAQUE_TYPE_SPACE_DOCUMENT, "pub opaque type ", 16); 938 + const_str!(FN_OPEN_PAREN_DOCUMENT, "fn(", 3); 937 939 938 940 const_zero_width_str!(ZERO_WIDTH_CLOSED_SPAN_TAG_DOCUMENT, "</span>"); 939 941 const_zero_width_str!(ZERO_WIDTH_CLOSED_A_TAG_DOCUMENT, "</a>");