remove suffixed from Expr::Var

This commit is contained in:
Luke Boswell 2024-04-27 13:06:19 +10:00
parent 1640ee1321
commit 2fe03e6c91
No known key found for this signature in database
GPG key ID: F6DB3C9DB47377B0
15 changed files with 60 additions and 167 deletions

View file

@ -748,10 +748,11 @@ fn can_annotation_help(
for loc_var in *loc_vars {
let var = match loc_var.value {
Pattern::Identifier {
ident: name,
suffixed: _,
} if name.chars().next().unwrap().is_lowercase() => name,
Pattern::Identifier { ident: name, .. }
if name.chars().next().unwrap().is_lowercase() =>
{
name
}
_ => unreachable!("I thought this was validated during parsing"),
};
let var_name = Lowercase::from(var);

View file

@ -547,11 +547,7 @@ fn canonicalize_claimed_ability_impl<'a>(
}
AssignedField::RequiredValue(label, _spaces, value) => {
let impl_ident = match value.value {
ast::Expr::Var {
module_name,
ident,
suffixed: _,
} => {
ast::Expr::Var { module_name, ident } => {
if module_name.is_empty() {
ident
} else {
@ -2574,10 +2570,9 @@ fn to_pending_alias_or_opaque<'a>(
for loc_var in vars.iter() {
match loc_var.value {
ast::Pattern::Identifier {
ident: name,
suffixed: _,
} if name.chars().next().unwrap().is_lowercase() => {
ast::Pattern::Identifier { ident: name, .. }
if name.chars().next().unwrap().is_lowercase() =>
{
let lowercase = Lowercase::from(name);
can_rigids.push(Loc {
value: lowercase,

View file

@ -37,12 +37,10 @@ fn to_encoder<'a>(env: &mut Env<'a>, at_opaque: &'a str) -> ast::Expr<'a> {
alloc_expr(ast::Expr::Var {
module_name: "Encode",
ident: "toEncoder",
suffixed: 0,
}),
&*env.arena.alloc([&*alloc_expr(ast::Expr::Var {
module_name: "",
ident: payload,
suffixed: 0,
})]),
roc_module::called_via::CalledVia::Space,
));
@ -67,23 +65,19 @@ fn decoder<'a>(env: &mut Env<'a>, at_opaque: &'a str) -> ast::Expr<'a> {
alloc_expr(ast::Expr::Var {
module_name: "Decode",
ident: "decodeWith",
suffixed: 0,
}),
env.arena.alloc([
&*alloc_expr(ast::Expr::Var {
module_name: "",
ident: bytes,
suffixed: 0,
}),
alloc_expr(ast::Expr::Var {
module_name: "Decode",
ident: "decoder",
suffixed: 0,
}),
alloc_expr(ast::Expr::Var {
module_name: "",
ident: fmt,
suffixed: 0,
}),
]),
CalledVia::Space,
@ -94,7 +88,6 @@ fn decoder<'a>(env: &mut Env<'a>, at_opaque: &'a str) -> ast::Expr<'a> {
alloc_expr(ast::Expr::Var {
module_name: "Decode",
ident: "mapResult",
suffixed: 0,
}),
env.arena.alloc([
&*alloc_expr(call_decode_with),
@ -130,7 +123,6 @@ fn decoder<'a>(env: &mut Env<'a>, at_opaque: &'a str) -> ast::Expr<'a> {
alloc_expr(ast::Expr::Var {
module_name: "Decode",
ident: "custom",
suffixed: 0,
}),
env.arena.alloc([&*alloc_expr(custom_closure)]),
CalledVia::Space,
@ -165,18 +157,15 @@ fn hash<'a>(env: &mut Env<'a>, at_opaque: &'a str) -> ast::Expr<'a> {
alloc_expr(ast::Expr::Var {
module_name: "Hash",
ident: "hash",
suffixed: 0,
}),
&*env.arena.alloc([
&*alloc_expr(ast::Expr::Var {
module_name: "",
ident: hasher,
suffixed: 0,
}),
&*alloc_expr(ast::Expr::Var {
module_name: "",
ident: payload,
suffixed: 0,
}),
]),
roc_module::called_via::CalledVia::Space,
@ -234,18 +223,15 @@ fn is_eq<'a>(env: &mut Env<'a>, at_opaque: &'a str) -> ast::Expr<'a> {
alloc_expr(ast::Expr::Var {
module_name: "Bool",
ident: "isEq",
suffixed: 0,
}),
&*env.arena.alloc([
&*alloc_expr(ast::Expr::Var {
module_name: "",
ident: payload1,
suffixed: 0,
}),
&*alloc_expr(ast::Expr::Var {
module_name: "",
ident: payload2,
suffixed: 0,
}),
]),
roc_module::called_via::CalledVia::Space,
@ -286,12 +272,10 @@ fn to_inspector<'a>(env: &mut Env<'a>, at_opaque: &'a str) -> ast::Expr<'a> {
alloc_expr(ast::Expr::Var {
module_name: "Inspect",
ident: "toInspector",
suffixed: 0,
}),
&*env.arena.alloc([&*alloc_expr(ast::Expr::Var {
module_name: "",
ident: payload,
suffixed: 0,
})]),
roc_module::called_via::CalledVia::Space,
));
@ -306,7 +290,6 @@ fn to_inspector<'a>(env: &mut Env<'a>, at_opaque: &'a str) -> ast::Expr<'a> {
alloc_expr(ast::Expr::Var {
module_name: "Inspect",
ident: "tag",
suffixed: 0,
}),
&*env.arena.alloc([&*opaque_name, &*to_inspector_list]),
roc_module::called_via::CalledVia::Space,
@ -319,14 +302,12 @@ fn to_inspector<'a>(env: &mut Env<'a>, at_opaque: &'a str) -> ast::Expr<'a> {
alloc_expr(ast::Expr::Var {
module_name: "Inspect",
ident: "apply",
suffixed: 0,
}),
&*env.arena.alloc([
&*opaque_inspector,
&*alloc_expr(ast::Expr::Var {
module_name: "",
ident: fmt,
suffixed: 0,
}),
]),
roc_module::called_via::CalledVia::Space,
@ -348,7 +329,6 @@ fn to_inspector<'a>(env: &mut Env<'a>, at_opaque: &'a str) -> ast::Expr<'a> {
alloc_expr(ast::Expr::Var {
module_name: "Inspect",
ident: "custom",
suffixed: 0,
}),
env.arena.alloc([&*custom_closure]),
CalledVia::Space,

View file

@ -57,11 +57,7 @@ fn new_op_call_expr<'a>(
let args = arena.alloc([left, right]);
let loc_expr = arena.alloc(Loc {
value: Expr::Var {
module_name,
ident,
suffixed: 0,
},
value: Expr::Var { module_name, ident },
region: loc_op.region,
});
@ -604,12 +600,10 @@ pub fn desugar_expr<'a>(
Negate => Var {
module_name: ModuleName::NUM,
ident: "neg",
suffixed: 0,
},
Not => Var {
module_name: ModuleName::BOOL,
ident: "not",
suffixed: 0,
},
};
let loc_fn_var = arena.alloc(Loc { region, value });
@ -707,7 +701,6 @@ pub fn desugar_expr<'a>(
let inspect_fn = Var {
module_name: ModuleName::INSPECT,
ident: "toStr",
suffixed: 0,
};
let loc_inspect_fn_var = arena.alloc(Loc {
value: inspect_fn,
@ -762,7 +755,6 @@ pub fn desugar_expr<'a>(
Expr::Var {
module_name: ModuleName::TASK,
ident: "ok",
suffixed: 0,
},
)),
arena.alloc(apply_args),
@ -858,7 +850,6 @@ fn desugar_field<'a>(
value: Var {
module_name: "",
ident: loc_str.value,
suffixed: 0,
},
region: loc_str.region,
};
@ -1039,7 +1030,6 @@ fn record_builder_arg<'a>(
value: Expr::Var {
module_name: "",
ident: arena.alloc("#".to_owned() + label.value),
suffixed: 0,
},
});

View file

@ -1019,11 +1019,9 @@ pub fn canonicalize_expr<'a>(
(expr, output)
}
}
ast::Expr::Var {
module_name,
ident,
suffixed: _, // TODO should we use suffixed here?
} => canonicalize_var_lookup(env, var_store, scope, module_name, ident, region),
ast::Expr::Var { module_name, ident } => {
canonicalize_var_lookup(env, var_store, scope, module_name, ident, region)
}
ast::Expr::Underscore(name) => {
// we parse underscores, but they are not valid expression syntax

View file

@ -10,8 +10,6 @@ use roc_parse::ast::{is_expr_suffixed, Pattern, ValueDef, WhenBranch};
use roc_region::all::{Loc, Region};
use std::cell::Cell;
static DEBUG_PRINT_UNWRAPPED: bool = false;
thread_local! {
// we use a thread_local here so that tests consistently give the same pattern
static SUFFIXED_ANSWER_COUNTER: Cell<usize> = Cell::new(0);
@ -33,7 +31,6 @@ fn next_suffixed_answer_pattern(arena: &Bump) -> (Expr, Pattern) {
Expr::Var {
module_name: "",
ident: answer_ident,
suffixed: 0,
},
Pattern::Identifier {
ident: answer_ident.as_str(),
@ -103,17 +100,6 @@ pub fn unwrap_suffixed_expression<'a>(
init_unwrapped_err(arena, unwrapped_sub_expr, maybe_def_pat)
}
Expr::Var {
module_name,
ident,
suffixed,
} => {
// TODO remove this when we remove suffixed as no longer used
debug_assert!(suffixed == 0);
Ok(loc_expr)
}
Expr::Defs(..) => unwrap_suffixed_expression_defs_help(arena, loc_expr, maybe_def_pat),
Expr::Apply(..) => {
@ -149,9 +135,12 @@ pub fn unwrap_suffixed_expression<'a>(
}
};
if DEBUG_PRINT_UNWRAPPED && is_expr_suffixed(&loc_expr.value) {
dbg!(&maybe_def_pat, &loc_expr, &unwrapped_expression);
}
// KEEP THIS HERE FOR DEBUGGING
// USEFUL TO SEE THE UNWRAPPING
// OF AST NODES AS THEY DESCEND
// if is_expr_suffixed(&loc_expr.value) {
// dbg!(&maybe_def_pat, &loc_expr, &unwrapped_expression);
// }
unwrapped_expression
}
@ -732,8 +721,7 @@ pub fn apply_task_await<'a>(
// If the pattern and the new are matching answers then we don't need to unwrap anything
// e.g. `Task.await foo \#!a1 -> Task.ok #!a1` is the same as `foo`
dbg!(&loc_pat, &loc_new);
if dbg!(is_matching_intermediate_answer(loc_pat, loc_new)) {
if is_matching_intermediate_answer(loc_pat, loc_new) {
return loc_arg;
}
@ -758,7 +746,6 @@ pub fn apply_task_await<'a>(
value: Var {
module_name: ModuleName::TASK,
ident: "await",
suffixed: 0,
},
}),
arena.alloc(task_await_apply_args),

File diff suppressed because one or more lines are too long

View file

@ -172,11 +172,7 @@ impl<'a> Formattable for Expr<'a> {
Str(literal) => {
fmt_str_literal(buf, *literal, indent);
}
Var {
module_name,
ident,
suffixed,
} => {
Var { module_name, ident } => {
buf.indent(indent);
if !module_name.is_empty() {
buf.push_str(module_name);
@ -184,11 +180,6 @@ impl<'a> Formattable for Expr<'a> {
}
buf.push_str(ident);
let count: u8 = *suffixed;
for _ in 0..count {
buf.push('!');
}
}
Underscore(name) => {
buf.indent(indent);

View file

@ -695,15 +695,7 @@ impl<'a> RemoveSpaces<'a> for Expr<'a> {
Expr::Record(a) => Expr::Record(a.remove_spaces(arena)),
Expr::RecordBuilder(a) => Expr::RecordBuilder(a.remove_spaces(arena)),
Expr::Tuple(a) => Expr::Tuple(a.remove_spaces(arena)),
Expr::Var {
module_name,
ident,
suffixed,
} => Expr::Var {
module_name,
ident,
suffixed,
},
Expr::Var { module_name, ident } => Expr::Var { module_name, ident },
Expr::Underscore(a) => Expr::Underscore(a),
Expr::Tag(a) => Expr::Tag(a),
Expr::OpaqueRef(a) => Expr::OpaqueRef(a),

View file

@ -9,7 +9,6 @@ use bumpalo::Bump;
use roc_collections::soa::{EitherIndex, Index, Slice};
use roc_error_macros::internal_error;
use roc_module::called_via::{BinOp, CalledVia, UnaryOp};
use roc_module::ident::ModuleName;
use roc_region::all::{Loc, Position, Region};
#[derive(Debug, Clone, PartialEq, Eq)]
@ -291,7 +290,6 @@ pub enum Expr<'a> {
Var {
module_name: &'a str, // module_name will only be filled if the original Roc code stated something like `5 + SomeModule.myVar`, module_name will be blank if it was `5 + myVar`
ident: &'a str,
suffixed: u8, // how many `!` suffixes, for example `doTheThing!!` executes a Task that returns a Task
},
Underscore(&'a str),
@ -358,17 +356,6 @@ pub enum Expr<'a> {
}
impl Expr<'_> {
pub fn increment_var_suffix(&mut self, count: u8) {
match self {
Expr::Var { suffixed, .. } => {
*suffixed += count;
}
_ => {
internal_error!("increment_var_suffix called on non-Var expression");
}
}
}
pub fn get_region_spanning_binops(&self) -> Region {
match self {
Expr::BinOps(firsts, last) => {
@ -398,7 +385,7 @@ pub fn split_loc_exprs_around<'a>(
pub fn is_expr_suffixed(expr: &Expr) -> bool {
match expr {
// expression without arguments, `read!`
Expr::Var { suffixed, .. } => false,
Expr::Var { .. } => false,
Expr::TaskAwaitBang(..) => true,
@ -449,11 +436,7 @@ pub fn is_expr_suffixed(expr: &Expr) -> bool {
}
Expr::Float(_) => false,
Expr::Num(_) => false,
Expr::NonBase10Int {
string,
base,
is_negative,
} => false,
Expr::NonBase10Int { .. } => false,
Expr::Str(_) => false,
Expr::SingleQuote(_) => false,
Expr::RecordAccess(a, _) => is_expr_suffixed(a),
@ -1684,13 +1667,11 @@ impl<'a> Expr<'a> {
pub const REPL_OPAQUE_FUNCTION: Self = Expr::Var {
module_name: "",
ident: "<function>",
suffixed: 0,
};
pub const REPL_RUNTIME_CRASH: Self = Expr::Var {
module_name: "",
ident: "*",
suffixed: 0,
};
pub fn loc_ref(&'a self, region: Region) -> Loc<&'a Self> {

View file

@ -244,7 +244,7 @@ fn parse_ident_seq<'a>(
state: State<'a>,
min_indent: u32,
) -> ParseResult<'a, Loc<Expr<'a>>, EExpr<'a>> {
let (p, loc_ident, state) =
let (_, loc_ident, state) =
loc!(assign_or_destructure_identifier()).parse(arena, state, min_indent)?;
let expr = ident_to_expr(arena, loc_ident.value);
let (_p, suffixes, state) = record_field_access_chain()
@ -2184,18 +2184,14 @@ fn expr_to_pattern_help<'a>(arena: &'a Bump, expr: &Expr<'a>) -> Result<Pattern<
}
let mut pat = match expr.item {
Expr::Var {
module_name,
ident,
suffixed,
} => {
Expr::Var { module_name, ident } => {
if module_name.is_empty() {
Pattern::Identifier { ident, suffixed }
Pattern::Identifier { ident, suffixed: 0 }
} else {
Pattern::QualifiedIdentifier {
module_name,
ident,
suffixed,
suffixed: 0,
}
}
}
@ -2830,18 +2826,14 @@ fn ident_to_expr<'a>(arena: &'a Bump, src: Ident<'a>) -> Expr<'a> {
Ident::Access {
module_name,
parts,
suffixed,
suffixed: _,
} => {
let mut iter = parts.iter();
// The first value in the iterator is the variable name,
// e.g. `foo` in `foo.bar.baz`
let mut answer = match iter.next() {
Some(Accessor::RecordField(ident)) => Expr::Var {
module_name,
ident,
suffixed,
},
Some(Accessor::RecordField(ident)) => Expr::Var { module_name, ident },
Some(Accessor::TupleIndex(_)) => {
// TODO: make this state impossible to represent in Ident::Access,
// by splitting out parts[0] into a separate field with a type of `&'a str`,

View file

@ -51,12 +51,12 @@ pub fn loc_pattern_help<'a>() -> impl Parser<'a, Loc<Pattern<'a>>, EPattern<'a>>
let pattern_state = state.clone();
// Return early with the suffixed statement
match pattern.value {
Pattern::Identifier { suffixed, .. } if suffixed > 0 => {
return Ok((MadeProgress, pattern, pattern_state))
}
_ => {}
}
// match pattern.value {
// Pattern::Identifier { .. } if suffixed > 0 => {
// return Ok((MadeProgress, pattern, pattern_state))
// }
// _ => {}
// }
let (pattern_spaces, state) =
match space0_e(EPattern::AsKeyword).parse(arena, state, min_indent) {
@ -408,7 +408,7 @@ fn loc_ident_pattern_help<'a>(
region: loc_ident.region,
value: Pattern::Identifier {
ident: var,
suffixed,
suffixed: 0,
},
},
state,
@ -424,7 +424,7 @@ fn loc_ident_pattern_help<'a>(
value: Pattern::QualifiedIdentifier {
module_name,
ident: var,
suffixed,
suffixed: 0,
},
},
state,

View file

@ -175,7 +175,6 @@ mod test_parse {
let expr = arena.alloc(Var {
module_name: "",
ident: "name",
suffixed: 0,
});
bumpalo::vec![in arena;
@ -192,7 +191,6 @@ mod test_parse {
let expr = arena.alloc(Var {
module_name: "",
ident: "name",
suffixed: 0,
});
bumpalo::vec![in arena;
@ -238,7 +236,6 @@ mod test_parse {
let expr = arena.alloc(Var {
module_name: "",
ident: "name",
suffixed: 0,
});
bumpalo::vec![in arena;
@ -254,13 +251,11 @@ mod test_parse {
let expr1 = arena.alloc(Var {
module_name: "",
ident: "name",
suffixed: 0,
});
let expr2 = arena.alloc(Var {
module_name: "",
ident: "project",
suffixed: 0,
});
bumpalo::vec![in arena;
@ -281,13 +276,11 @@ mod test_parse {
let expr1 = arena.alloc(Var {
module_name: "",
ident: "name",
suffixed: 0,
});
let expr2 = arena.alloc(Var {
module_name: "",
ident: "project",
suffixed: 0,
});
bumpalo::vec![in arena;

View file

@ -741,7 +741,6 @@ mod test_snapshots {
let expr = arena.alloc(Var {
module_name: "",
ident: "name",
suffixed: 0,
});
bumpalo::vec![in arena;
@ -758,7 +757,6 @@ mod test_snapshots {
let expr = arena.alloc(Var {
module_name: "",
ident: "name",
suffixed: 0,
});
bumpalo::vec![in arena;
@ -774,7 +772,6 @@ mod test_snapshots {
let expr = arena.alloc(Var {
module_name: "",
ident: "name",
suffixed: 0,
});
bumpalo::vec![in arena;
@ -790,13 +787,11 @@ mod test_snapshots {
let expr1 = arena.alloc(Var {
module_name: "",
ident: "name",
suffixed: 0,
});
let expr2 = arena.alloc(Var {
module_name: "",
ident: "project",
suffixed: 0,
});
bumpalo::vec![in arena;

View file

@ -791,7 +791,6 @@ fn addr_to_ast<'a, M: ReplAppMemory>(
let box_box = env.arena.alloc(Loc::at_zero(Expr::Var {
module_name: "Box",
ident: "box",
suffixed: 0,
}));
let box_box_arg = &*env.arena.alloc(Loc::at_zero(inner_expr));
let box_box_args = env.arena.alloc([box_box_arg]);
@ -1355,7 +1354,6 @@ fn bool_to_ast<'a>(env: &Env<'a, '_>, value: bool, content: &Content) -> Expr<'a
Alias(Symbol::BOOL_BOOL, _, _, _) => Expr::Var {
module_name: "Bool",
ident: if value { "true" } else { "false" },
suffixed: 0,
},
Alias(_, _, var, _) => {
let content = env.subs.get_content_without_compacting(*var);