Skip to content

Commit c73ae49

Browse files
giacomocavalierilpil
authored andcommitted
improve definitions naming
1 parent 82b12de commit c73ae49

File tree

13 files changed

+166
-135
lines changed

13 files changed

+166
-135
lines changed

compiler-core/src/analyse.rs

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
GLEAM_CORE_PACKAGE_NAME,
99
ast::{
1010
self, Arg, BitArrayOption, CustomType, Definition, DefinitionLocation, Function,
11-
GroupedStatements, Import, ModuleConstant, Publicity, RecordConstructor,
11+
GroupedDefinitions, Import, ModuleConstant, Publicity, RecordConstructor,
1212
RecordConstructorArg, SrcSpan, Statement, TypeAlias, TypeAst, TypeAstConstructor,
1313
TypeAstFn, TypeAstHole, TypeAstTuple, TypeAstVar, TypedDefinition, TypedExpr,
1414
TypedFunction, TypedModule, UntypedArg, UntypedCustomType, UntypedFunction, UntypedImport,
@@ -223,71 +223,80 @@ impl<'a, A> ModuleAnalyzer<'a, A> {
223223
}
224224
.build();
225225

226-
let statements = GroupedStatements::new(module.into_iter_statements(self.target));
227-
let statements_count = statements.len();
226+
let definitions = GroupedDefinitions::new(module.into_iter_definitions(self.target));
227+
let definitions_count = definitions.len();
228228

229229
// Register any modules, types, and values being imported
230230
// We process imports first so that anything imported can be referenced
231231
// anywhere in the module.
232-
let mut env = Importer::run(self.origin, env, &statements.imports, &mut self.problems);
232+
let mut env = Importer::run(self.origin, env, &definitions.imports, &mut self.problems);
233233

234234
// Register types so they can be used in constructors and functions
235235
// earlier in the module.
236-
for t in &statements.custom_types {
237-
if let Err(error) = self.register_types_from_custom_type(t, &mut env) {
236+
for type_ in &definitions.custom_types {
237+
if let Err(error) = self.register_types_from_custom_type(type_, &mut env) {
238238
return self.all_errors(error);
239239
}
240240
}
241241

242-
let sorted_aliases = match sorted_type_aliases(&statements.type_aliases) {
243-
Ok(it) => it,
242+
let sorted_aliases = match sorted_type_aliases(&definitions.type_aliases) {
243+
Ok(sorted_aliases) => sorted_aliases,
244244
Err(error) => return self.all_errors(error),
245245
};
246-
for t in sorted_aliases {
247-
self.register_type_alias(t, &mut env);
246+
for type_alias in sorted_aliases {
247+
self.register_type_alias(type_alias, &mut env);
248248
}
249249

250-
for f in &statements.functions {
251-
self.register_value_from_function(f, &mut env);
250+
for function in &definitions.functions {
251+
self.register_value_from_function(function, &mut env);
252252
}
253253

254254
// Infer the types of each statement in the module
255-
let mut typed_statements = Vec::with_capacity(statements_count);
256-
for i in statements.imports {
257-
optionally_push(&mut typed_statements, self.analyse_import(i, &env));
255+
let mut typed_definitions = Vec::with_capacity(definitions_count);
256+
for import in definitions.imports {
257+
optionally_push(&mut typed_definitions, self.analyse_import(import, &env));
258258
}
259-
for t in statements.custom_types {
260-
optionally_push(&mut typed_statements, self.analyse_custom_type(t, &mut env));
259+
for type_ in definitions.custom_types {
260+
optionally_push(
261+
&mut typed_definitions,
262+
self.analyse_custom_type(type_, &mut env),
263+
);
261264
}
262-
for t in statements.type_aliases {
263-
typed_statements.push(analyse_type_alias(t, &mut env));
265+
for type_alias in definitions.type_aliases {
266+
typed_definitions.push(analyse_type_alias(type_alias, &mut env));
264267
}
265268

266-
// Sort functions and constants into dependency order for inference. Definitions that do
267-
// not depend on other definitions are inferred first, then ones that depend
268-
// on those, etc.
269+
// Sort functions and constants into dependency order for inference.
270+
// Definitions that do not depend on other definitions are inferred
271+
// first, then ones that depend on those, etc.
269272
let definition_groups =
270-
match into_dependency_order(statements.functions, statements.constants) {
271-
Ok(it) => it,
273+
match into_dependency_order(definitions.functions, definitions.constants) {
274+
Ok(definition_groups) => definition_groups,
272275
Err(error) => return self.all_errors(error),
273276
};
274277
let mut working_group = vec![];
275278

276279
for group in definition_groups {
277-
// A group may have multiple functions that depend on each other through
278-
// mutual recursion.
280+
// A group may have multiple functions that depend on each other
281+
// through mutual recursion.
279282

280283
for definition in group {
281-
let def = match definition {
282-
CallGraphNode::Function(f) => self.infer_function(f, &mut env),
283-
CallGraphNode::ModuleConstant(c) => self.infer_module_constant(c, &mut env),
284+
let definition = match definition {
285+
CallGraphNode::Function(function) => self.infer_function(function, &mut env),
286+
CallGraphNode::ModuleConstant(constant) => {
287+
self.infer_module_constant(constant, &mut env)
288+
}
284289
};
285-
working_group.push(def);
290+
working_group.push(definition);
286291
}
287292

288293
// Now that the entire group has been inferred, generalise their types.
289294
for inferred in working_group.drain(..) {
290-
typed_statements.push(generalise_statement(inferred, &self.module_name, &mut env));
295+
typed_definitions.push(generalise_definition(
296+
inferred,
297+
&self.module_name,
298+
&mut env,
299+
));
291300
}
292301
}
293302

@@ -335,7 +344,7 @@ impl<'a, A> ModuleAnalyzer<'a, A> {
335344
let module = ast::Module {
336345
documentation: documentation.clone(),
337346
name: self.module_name.clone(),
338-
definitions: typed_statements,
347+
definitions: typed_definitions,
339348
names: type_names,
340349
unused_definition_positions,
341350
type_info: ModuleInterface {
@@ -1686,19 +1695,19 @@ where
16861695
}
16871696
}
16881697

1689-
fn generalise_statement(
1690-
s: TypedDefinition,
1698+
fn generalise_definition(
1699+
definition: TypedDefinition,
16911700
module_name: &EcoString,
16921701
environment: &mut Environment<'_>,
16931702
) -> TypedDefinition {
1694-
match s {
1703+
match definition {
16951704
Definition::Function(function) => generalise_function(function, environment, module_name),
16961705
Definition::ModuleConstant(constant) => {
16971706
generalise_module_constant(constant, environment, module_name)
16981707
}
1699-
statement @ (Definition::TypeAlias(TypeAlias { .. })
1708+
definition @ (Definition::TypeAlias(TypeAlias { .. })
17001709
| Definition::CustomType(CustomType { .. })
1701-
| Definition::Import(Import { .. })) => statement,
1710+
| Definition::Import(Import { .. })) => definition,
17021711
}
17031712
}
17041713

compiler-core/src/ast.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,18 @@ pub type TypedModule = Module<type_::ModuleInterface, TypedDefinition>;
5050
pub type UntypedModule = Module<(), TargetedDefinition>;
5151

5252
#[derive(Debug, Clone, PartialEq, Eq)]
53-
pub struct Module<Info, Statements> {
53+
pub struct Module<Info, Definitions> {
5454
pub name: EcoString,
5555
pub documentation: Vec<EcoString>,
5656
pub type_info: Info,
57-
pub definitions: Vec<Statements>,
57+
pub definitions: Vec<Definitions>,
5858
pub names: Names,
5959
/// The source byte locations of definition that are unused.
6060
/// This is used in code generation to know when definitions can be safely omitted.
6161
pub unused_definition_positions: HashSet<u32>,
6262
}
6363

64-
impl<Info, Statements> Module<Info, Statements> {
64+
impl<Info, Definitions> Module<Info, Definitions> {
6565
pub fn erlang_name(&self) -> EcoString {
6666
module_erlang_name(&self.name)
6767
}
@@ -71,7 +71,7 @@ impl TypedModule {
7171
pub fn find_node(&self, byte_index: u32) -> Option<Located<'_>> {
7272
self.definitions
7373
.iter()
74-
.find_map(|statement| statement.find_node(byte_index))
74+
.find_map(|definition| definition.find_node(byte_index))
7575
}
7676

7777
pub fn find_statement(&self, byte_index: u32) -> Option<&TypedStatement> {
@@ -105,8 +105,8 @@ impl TargetedDefinition {
105105

106106
impl UntypedModule {
107107
pub fn dependencies(&self, target: Target) -> Vec<(EcoString, SrcSpan)> {
108-
self.iter_statements(target)
109-
.flat_map(|s| match s {
108+
self.iter_definitions(target)
109+
.flat_map(|definition| match definition {
110110
Definition::Import(Import {
111111
module, location, ..
112112
}) => Some((module.clone(), *location)),
@@ -115,18 +115,18 @@ impl UntypedModule {
115115
.collect()
116116
}
117117

118-
pub fn iter_statements(&self, target: Target) -> impl Iterator<Item = &UntypedDefinition> {
118+
pub fn iter_definitions(&self, target: Target) -> impl Iterator<Item = &UntypedDefinition> {
119119
self.definitions
120120
.iter()
121-
.filter(move |def| def.is_for(target))
122-
.map(|def| &def.definition)
121+
.filter(move |definition| definition.is_for(target))
122+
.map(|definition| &definition.definition)
123123
}
124124

125-
pub fn into_iter_statements(self, target: Target) -> impl Iterator<Item = UntypedDefinition> {
125+
pub fn into_iter_definitions(self, target: Target) -> impl Iterator<Item = UntypedDefinition> {
126126
self.definitions
127127
.into_iter()
128-
.filter(move |def| def.is_for(target))
129-
.map(|def| def.definition)
128+
.filter(move |definition| definition.is_for(target))
129+
.map(|definition| definition.definition)
130130
}
131131
}
132132

@@ -2846,20 +2846,20 @@ pub enum TodoKind {
28462846
}
28472847

28482848
#[derive(Debug, Default)]
2849-
pub struct GroupedStatements {
2849+
pub struct GroupedDefinitions {
28502850
pub functions: Vec<UntypedFunction>,
28512851
pub constants: Vec<UntypedModuleConstant>,
28522852
pub custom_types: Vec<UntypedCustomType>,
28532853
pub imports: Vec<UntypedImport>,
28542854
pub type_aliases: Vec<UntypedTypeAlias>,
28552855
}
28562856

2857-
impl GroupedStatements {
2858-
pub fn new(statements: impl IntoIterator<Item = UntypedDefinition>) -> Self {
2857+
impl GroupedDefinitions {
2858+
pub fn new(definitions: impl IntoIterator<Item = UntypedDefinition>) -> Self {
28592859
let mut this = Self::default();
28602860

2861-
for statement in statements {
2862-
this.add(statement)
2861+
for definition in definitions {
2862+
this.add(definition)
28632863
}
28642864

28652865
this
@@ -2882,11 +2882,11 @@ impl GroupedStatements {
28822882

28832883
fn add(&mut self, statement: UntypedDefinition) {
28842884
match statement {
2885-
Definition::Import(i) => self.imports.push(i),
2886-
Definition::Function(f) => self.functions.push(f),
2887-
Definition::TypeAlias(t) => self.type_aliases.push(t),
2888-
Definition::CustomType(c) => self.custom_types.push(c),
2889-
Definition::ModuleConstant(c) => self.constants.push(c),
2885+
Definition::Import(import) => self.imports.push(import),
2886+
Definition::Function(function) => self.functions.push(function),
2887+
Definition::TypeAlias(type_alias) => self.type_aliases.push(type_alias),
2888+
Definition::CustomType(custom_type) => self.custom_types.push(custom_type),
2889+
Definition::ModuleConstant(constant) => self.constants.push(constant),
28902890
}
28912891
}
28922892
}

compiler-core/src/ast/visit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,8 @@ pub fn visit_typed_module<'a, V>(v: &mut V, module: &'a TypedModule)
587587
where
588588
V: Visit<'a> + ?Sized,
589589
{
590-
for def in &module.definitions {
591-
v.visit_typed_definition(def);
590+
for definition in &module.definitions {
591+
v.visit_typed_definition(definition);
592592
}
593593
}
594594

compiler-core/src/ast_folder.rs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,51 +22,53 @@ use crate::{
2222
#[allow(dead_code)]
2323
pub trait UntypedModuleFolder: TypeAstFolder + UntypedExprFolder {
2424
/// You probably don't want to override this method.
25-
fn fold_module(&mut self, mut m: UntypedModule) -> UntypedModule {
26-
m.definitions = m
25+
fn fold_module(&mut self, mut module: UntypedModule) -> UntypedModule {
26+
module.definitions = module
2727
.definitions
2828
.into_iter()
29-
.map(|d| {
30-
let TargetedDefinition { definition, target } = d;
29+
.map(|definition| {
30+
let TargetedDefinition { definition, target } = definition;
3131
match definition {
32-
Definition::Function(f) => {
33-
let f = self.fold_function_definition(f, target);
34-
let definition = self.walk_function_definition(f);
32+
Definition::Function(function) => {
33+
let function = self.fold_function_definition(function, target);
34+
let definition = self.walk_function_definition(function);
3535
TargetedDefinition { definition, target }
3636
}
3737

38-
Definition::TypeAlias(a) => {
39-
let a = self.fold_type_alias(a, target);
40-
let definition = self.walk_type_alias(a);
38+
Definition::TypeAlias(type_alias) => {
39+
let type_alias = self.fold_type_alias(type_alias, target);
40+
let definition = self.walk_type_alias(type_alias);
4141
TargetedDefinition { definition, target }
4242
}
4343

44-
Definition::CustomType(t) => {
45-
let t = self.fold_custom_type(t, target);
46-
let definition = self.walk_custom_type(t);
44+
Definition::CustomType(custom_type) => {
45+
let custom_type = self.fold_custom_type(custom_type, target);
46+
let definition = self.walk_custom_type(custom_type);
4747
TargetedDefinition { definition, target }
4848
}
4949

50-
Definition::Import(i) => {
51-
let i = self.fold_import(i, target);
52-
let definition = self.walk_import(i);
50+
Definition::Import(import) => {
51+
let import = self.fold_import(import, target);
52+
let definition = self.walk_import(import);
5353
TargetedDefinition { definition, target }
5454
}
5555

56-
Definition::ModuleConstant(c) => {
57-
let c = self.fold_module_constant(c, target);
58-
let definition = self.walk_module_constant(c);
56+
Definition::ModuleConstant(constant) => {
57+
let constant = self.fold_module_constant(constant, target);
58+
let definition = self.walk_module_constant(constant);
5959
TargetedDefinition { definition, target }
6060
}
6161
}
6262
})
6363
.collect();
64-
m
64+
module
6565
}
6666

6767
/// You probably don't want to override this method.
6868
fn walk_function_definition(&mut self, mut function: UntypedFunction) -> UntypedDefinition {
69-
function.body = function.body.mapped(|s| self.fold_statement(s));
69+
function.body = function
70+
.body
71+
.mapped(|statement| self.fold_statement(statement));
7072
function.return_annotation = function
7173
.return_annotation
7274
.map(|type_| self.fold_type(type_));

compiler-core/src/build.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -272,26 +272,26 @@ impl Module {
272272

273273
self.ast.type_info.documentation = self.ast.documentation.clone();
274274

275-
// Order statements to avoid misassociating doc comments after the
275+
// Order definitions to avoid misassociating doc comments after the
276276
// order has changed during compilation.
277-
let mut statements: Vec<_> = self.ast.definitions.iter_mut().collect();
278-
statements.sort_by(|a, b| a.location().start.cmp(&b.location().start));
277+
let mut definitions: Vec<_> = self.ast.definitions.iter_mut().collect();
278+
definitions.sort_by(|a, b| a.location().start.cmp(&b.location().start));
279279

280280
// Doc Comments
281281
let mut doc_comments = self.extra.doc_comments.iter().peekable();
282-
for statement in &mut statements {
282+
for definition in &mut definitions {
283283
let (docs_start, docs): (u32, Vec<&str>) = doc_comments_before(
284284
&mut doc_comments,
285285
&self.extra,
286-
statement.location().start,
286+
definition.location().start,
287287
&self.code,
288288
);
289289
if !docs.is_empty() {
290290
let doc = docs.join("\n").into();
291-
statement.put_doc((docs_start, doc));
291+
definition.put_doc((docs_start, doc));
292292
}
293293

294-
if let Definition::CustomType(CustomType { constructors, .. }) = statement {
294+
if let Definition::CustomType(CustomType { constructors, .. }) = definition {
295295
for constructor in constructors {
296296
let (docs_start, docs): (u32, Vec<&str>) = doc_comments_before(
297297
&mut doc_comments,

0 commit comments

Comments
 (0)