Skip to content

Commit

Permalink
refactor: replace "spread" with "record update" naming (gh-3737)
Browse files Browse the repository at this point in the history
  • Loading branch information
george-grec authored and lpil committed Oct 25, 2024
1 parent 48504fd commit 9ee71a1
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 49 deletions.
2 changes: 1 addition & 1 deletion compiler-core/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1286,7 +1286,7 @@ impl<T> HasLocation for CallArg<T> {
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RecordUpdateSpread {
pub struct RecordBeingUpdated {
pub base: Box<UntypedExpr>,
pub location: SrcSpan,
}
Expand Down
6 changes: 3 additions & 3 deletions compiler-core/src/ast/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ pub enum TypedExpr {
RecordUpdate {
location: SrcSpan,
type_: Arc<Type>,
spread: Box<Self>,
record: Box<Self>,
args: Vec<TypedRecordUpdateArg>,
},

Expand Down Expand Up @@ -281,10 +281,10 @@ impl TypedExpr {
.find_map(|arg| arg.find_node(byte_index))
.or_else(|| self.self_if_contains_location(byte_index)),

Self::RecordUpdate { spread, args, .. } => args
Self::RecordUpdate { record, args, .. } => args
.iter()
.find_map(|arg| arg.find_node(byte_index))
.or_else(|| spread.find_node(byte_index))
.or_else(|| record.find_node(byte_index))
.or_else(|| self.self_if_contains_location(byte_index)),
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler-core/src/ast/untyped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ pub enum UntypedExpr {
RecordUpdate {
location: SrcSpan,
constructor: Box<Self>,
spread: RecordUpdateSpread,
record: RecordBeingUpdated,
arguments: Vec<UntypedRecordUpdateArg>,
},

Expand Down
12 changes: 6 additions & 6 deletions compiler-core/src/ast/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,10 @@ pub trait Visit<'ast> {
&mut self,
location: &'ast SrcSpan,
type_: &'ast Arc<Type>,
spread: &'ast TypedExpr,
record: &'ast TypedExpr,
args: &'ast [TypedRecordUpdateArg],
) {
visit_typed_expr_record_update(self, location, type_, spread, args);
visit_typed_expr_record_update(self, location, type_, record, args);
}

fn visit_typed_expr_negate_bool(&mut self, location: &'ast SrcSpan, value: &'ast TypedExpr) {
Expand Down Expand Up @@ -714,9 +714,9 @@ where
TypedExpr::RecordUpdate {
location,
type_,
spread,
record,
args,
} => v.visit_typed_expr_record_update(location, type_, spread, args),
} => v.visit_typed_expr_record_update(location, type_, record, args),
TypedExpr::NegateBool { location, value } => {
v.visit_typed_expr_negate_bool(location, value)
}
Expand Down Expand Up @@ -970,12 +970,12 @@ pub fn visit_typed_expr_record_update<'a, V>(
v: &mut V,
_location: &'a SrcSpan,
_typ: &'a Arc<Type>,
spread: &'a TypedExpr,
record: &'a TypedExpr,
args: &'a [TypedRecordUpdateArg],
) where
V: Visit<'a> + ?Sized,
{
v.visit_typed_expr(spread);
v.visit_typed_expr(record);
for arg in args {
v.visit_typed_record_update_arg(arg);
}
Expand Down
14 changes: 7 additions & 7 deletions compiler-core/src/ast_folder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
analyse::Inferred,
ast::{
AssignName, Assignment, BinOp, CallArg, Constant, Definition, FunctionLiteralKind, Pattern,
RecordUpdateSpread, SrcSpan, Statement, TargetedDefinition, TodoKind, TypeAst,
RecordBeingUpdated, SrcSpan, Statement, TargetedDefinition, TodoKind, TypeAst,
TypeAstConstructor, TypeAstFn, TypeAstHole, TypeAstTuple, TypeAstVar, UntypedArg,
UntypedAssignment, UntypedClause, UntypedConstant, UntypedConstantBitArraySegment,
UntypedCustomType, UntypedDefinition, UntypedExpr, UntypedExprBitArraySegment,
Expand Down Expand Up @@ -320,9 +320,9 @@ pub trait UntypedExprFolder: TypeAstFolder + UntypedConstantFolder + PatternFold
UntypedExpr::RecordUpdate {
location,
constructor,
spread,
record,
arguments,
} => self.fold_record_update(location, constructor, spread, arguments),
} => self.fold_record_update(location, constructor, record, arguments),

UntypedExpr::NegateBool { location, value } => self.fold_negate_bool(location, value),

Expand Down Expand Up @@ -522,7 +522,7 @@ pub trait UntypedExprFolder: TypeAstFolder + UntypedConstantFolder + PatternFold
UntypedExpr::RecordUpdate {
location,
constructor,
spread,
record,
arguments,
} => {
let constructor = Box::new(self.fold_expr(*constructor));
Expand All @@ -536,7 +536,7 @@ pub trait UntypedExprFolder: TypeAstFolder + UntypedConstantFolder + PatternFold
UntypedExpr::RecordUpdate {
location,
constructor,
spread,
record,
arguments,
}
}
Expand Down Expand Up @@ -800,13 +800,13 @@ pub trait UntypedExprFolder: TypeAstFolder + UntypedConstantFolder + PatternFold
&mut self,
location: SrcSpan,
constructor: Box<UntypedExpr>,
spread: RecordUpdateSpread,
record: RecordBeingUpdated,
arguments: Vec<UntypedRecordUpdateArg>,
) -> UntypedExpr {
UntypedExpr::RecordUpdate {
location,
constructor,
spread,
record,
arguments,
}
}
Expand Down
4 changes: 2 additions & 2 deletions compiler-core/src/call_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,9 @@ impl<'a> CallGraphBuilder<'a> {
}

UntypedExpr::RecordUpdate {
spread, arguments, ..
record, arguments, ..
} => {
self.expression(&spread.base);
self.expression(&record.base);
for argument in arguments {
self.expression(&argument.value);
}
Expand Down
6 changes: 3 additions & 3 deletions compiler-core/src/erlang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1656,11 +1656,11 @@ fn docs_args_call<'a>(
}

fn record_update<'a>(
spread: &'a TypedExpr,
record: &'a TypedExpr,
args: &'a [TypedRecordUpdateArg],
env: &mut Env<'a>,
) -> Document<'a> {
let expr_doc = maybe_block_expr(spread, env);
let expr_doc = maybe_block_expr(record, env);

args.iter().fold(expr_doc, |tuple_doc, arg| {
// Increment the index by 2, because the first element
Expand Down Expand Up @@ -1844,7 +1844,7 @@ fn expr<'a>(expression: &'a TypedExpr, env: &mut Env<'a>) -> Document<'a> {

TypedExpr::RecordAccess { record, index, .. } => tuple_index(record, index + 1, env),

TypedExpr::RecordUpdate { spread, args, .. } => record_update(spread, args, env),
TypedExpr::RecordUpdate { record, args, .. } => record_update(record, args, env),

TypedExpr::Case {
subjects, clauses, ..
Expand Down
2 changes: 1 addition & 1 deletion compiler-core/src/erlang/tests/pipes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn pipe_in_case_subject() {

// https://github.com/gleam-lang/gleam/issues/1379
#[test]
fn pipe_in_spread() {
fn pipe_in_record_update() {
assert_erl!(
"pub type X {
X(a: Int, b: Int)
Expand Down
10 changes: 5 additions & 5 deletions compiler-core/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1025,11 +1025,11 @@ impl<'comments> Formatter<'comments> {
}
UntypedExpr::RecordUpdate {
constructor,
spread,
record,
arguments: args,
location,
..
} => self.record_update(constructor, spread, args, location),
} => self.record_update(constructor, record, args, location),
};
commented(document, comments)
}
Expand Down Expand Up @@ -1334,14 +1334,14 @@ impl<'comments> Formatter<'comments> {
pub fn record_update<'a>(
&mut self,
constructor: &'a UntypedExpr,
spread: &'a RecordUpdateSpread,
record: &'a RecordBeingUpdated,
args: &'a [UntypedRecordUpdateArg],
location: &SrcSpan,
) -> Document<'a> {
use std::iter::once;
let constructor_doc = self.expr(constructor);
let comments = self.pop_comments(spread.base.location().start);
let spread_doc = commented("..".to_doc().append(self.expr(&spread.base)), comments);
let comments = self.pop_comments(record.base.location().start);
let spread_doc = commented("..".to_doc().append(self.expr(&record.base)), comments);
let arg_docs = args
.iter()
.map(|a| self.record_update_arg(a).group())
Expand Down
2 changes: 1 addition & 1 deletion compiler-core/src/javascript/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ impl<'module> Generator<'module> {
TypedExpr::Fn { args, body, .. } => self.fn_(args, body),

TypedExpr::RecordAccess { record, label, .. } => self.record_access(record, label),
TypedExpr::RecordUpdate { spread, args, .. } => self.record_update(spread, args),
TypedExpr::RecordUpdate { record, args, .. } => self.record_update(record, args),

TypedExpr::Var {
name, constructor, ..
Expand Down
8 changes: 4 additions & 4 deletions compiler-core/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ use crate::analyse::Inferred;
use crate::ast::{
Arg, ArgNames, AssignName, Assignment, AssignmentKind, BinOp, BitArrayOption, BitArraySegment,
CallArg, Clause, ClauseGuard, Constant, CustomType, Definition, Function, FunctionLiteralKind,
HasLocation, Import, Module, ModuleConstant, Pattern, Publicity, RecordConstructor,
RecordConstructorArg, RecordUpdateSpread, SrcSpan, Statement, TargetedDefinition, TodoKind,
HasLocation, Import, Module, ModuleConstant, Pattern, Publicity, RecordBeingUpdated,
RecordConstructor, RecordConstructorArg, SrcSpan, Statement, TargetedDefinition, TodoKind,
TypeAlias, TypeAst, TypeAstConstructor, TypeAstFn, TypeAstHole, TypeAstTuple, TypeAstVar,
UnqualifiedImport, UntypedArg, UntypedClause, UntypedClauseGuard, UntypedConstant,
UntypedDefinition, UntypedExpr, UntypedModule, UntypedPattern, UntypedRecordUpdateArg,
Expand Down Expand Up @@ -845,7 +845,7 @@ where
// Record update
let base = self.expect_expression()?;
let base_e = base.location().end;
let spread = RecordUpdateSpread {
let record = RecordBeingUpdated {
base: Box::new(base),
location: SrcSpan {
start: dot_s,
Expand All @@ -865,7 +865,7 @@ where
expr = UntypedExpr::RecordUpdate {
location: SrcSpan { start, end },
constructor: Box::new(expr),
spread,
record,
arguments: args,
};
} else {
Expand Down
30 changes: 15 additions & 15 deletions compiler-core/src/type_/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
ast::{
Arg, Assignment, AssignmentKind, BinOp, BitArrayOption, BitArraySegment, CallArg, Clause,
ClauseGuard, Constant, FunctionLiteralKind, HasLocation, ImplicitCallArgOrigin, Layer,
RecordUpdateSpread, SrcSpan, Statement, TodoKind, TypeAst, TypedArg, TypedAssignment,
RecordBeingUpdated, SrcSpan, Statement, TodoKind, TypeAst, TypedArg, TypedAssignment,
TypedClause, TypedClauseGuard, TypedConstant, TypedExpr, TypedMultiPattern, TypedStatement,
UntypedArg, UntypedAssignment, UntypedClause, UntypedClauseGuard, UntypedConstant,
UntypedConstantBitArraySegment, UntypedExpr, UntypedExprBitArraySegment,
Expand Down Expand Up @@ -394,9 +394,9 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
UntypedExpr::RecordUpdate {
location,
constructor,
spread,
record,
arguments: args,
} => self.infer_record_update(*constructor, spread, args, location),
} => self.infer_record_update(*constructor, record, args, location),

UntypedExpr::NegateBool { location, value } => self.infer_negate_bool(location, *value),

Expand Down Expand Up @@ -2295,7 +2295,7 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
fn infer_record_update(
&mut self,
constructor: UntypedExpr,
spread: RecordUpdateSpread,
record: RecordBeingUpdated,
args: Vec<UntypedRecordUpdateArg>,
location: SrcSpan,
) -> Result<TypedExpr, Error> {
Expand Down Expand Up @@ -2361,12 +2361,12 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
}
};

let spread = self.infer(*spread.base)?;
let record = self.infer(*record.base)?;
let return_type = self.instantiate(retrn.clone(), &mut hashmap![]);

// Check that the spread variable unifies with the return type of the constructor
unify(return_type, spread.type_())
.map_err(|e| convert_unify_error(e, spread.location()))?;
// Check that the record variable unifies with the return type of the constructor
unify(return_type, record.type_())
.map_err(|e| convert_unify_error(e, record.location()))?;

let args: Vec<TypedRecordUpdateArg> = args
.iter()
Expand All @@ -2377,18 +2377,18 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
location,
}| {
let value = self.infer(value.clone())?;
let spread_field = self.infer_known_record_expression_access(
spread.clone(),
let record_field = self.infer_known_record_expression_access(
record.clone(),
label.clone(),
*location,
FieldAccessUsage::Other,
)?;

// Check that the update argument unifies with the corresponding
// field in the record contained within the spread variable. We
// need to check the spread, and not the constructor, in order
// field in the record contained within the record variable. We
// need to check the record, and not the constructor, in order
// to handle polymorphic types.
unify(spread_field.type_(), value.type_())
unify(record_field.type_(), value.type_())
.map_err(|e| convert_unify_error(e, value.location()))?;

match field_map.fields.get(label) {
Expand Down Expand Up @@ -2418,8 +2418,8 @@ impl<'a, 'b> ExprTyper<'a, 'b> {

Ok(TypedExpr::RecordUpdate {
location,
type_: spread.type_(),
spread: Box::new(spread),
type_: record.type_(),
record: Box::new(record),
args,
})
}
Expand Down

0 comments on commit 9ee71a1

Please sign in to comment.