Tip replacing <- with : for static values

This commit is contained in:
Agustin Zubiaga 2023-05-09 23:37:24 -03:00
parent 088193c93b
commit 71a2990e21
7 changed files with 51 additions and 3 deletions

View File

@ -289,6 +289,7 @@ pub fn constrain_expr<'a>(
let fn_reason = Reason::FnCall {
name: opt_symbol,
arity: args.len() as u8,
called_via: *called_via,
};
let fn_con = constrain_expr(arena, env, call_expr, fn_expected, region);

View File

@ -315,7 +315,7 @@ pub fn desugar_expr<'a>(arena: &'a Bump, loc_expr: &'a Loc<Expr<'a>>) -> &'a Loc
let args = std::slice::from_ref(arena.alloc(apply));
apply = arena.alloc(Loc {
value: Apply(desugared_expr, args, *called_via),
value: Apply(desugared_expr, args, CalledVia::RecordBuilder),
region: loc_expr.region,
});
}

View File

@ -466,6 +466,7 @@ pub fn constrain_expr(
let fn_reason = Reason::FnCall {
name: opt_symbol,
arity: loc_args.len() as u8,
called_via: *called_via,
};
let fn_con = constrain_expr(

View File

@ -88,6 +88,10 @@ pub enum CalledVia {
/// This call is the result of desugaring string interpolation,
/// e.g. "\(first) \(last)" is transformed into Str.concat (Str.concat first " ") last.
StringInterpolation,
/// This call is the result of desugaring a Record Builder field.
/// e.g. succeed { a <- get "a" } is transformed into (get "a") (succeed \a -> { a })
RecordBuilder,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]

View File

@ -3566,6 +3566,7 @@ pub enum Reason {
FnCall {
name: Option<Symbol>,
arity: u8,
called_via: CalledVia,
},
LowLevelOpArg {
op: LowLevel,

View File

@ -1138,7 +1138,11 @@ fn to_expr_report<'b>(
),
}
}
Reason::FnCall { name, arity } => match describe_wanted_function(&found) {
Reason::FnCall {
name,
arity,
called_via,
} => match describe_wanted_function(&found) {
DescribedFunction::NotAFunction(tag) => {
let this_value = match name {
None => alloc.text("This value"),
@ -1174,7 +1178,21 @@ fn to_expr_report<'b>(
)),
]),
alloc.region(lines.convert_region(expr_region)),
alloc.reflow("Are there any missing commas? Or missing parentheses?"),
match called_via {
CalledVia::RecordBuilder => {
alloc.concat([
alloc.tip(),
alloc.reflow("Replace "),
alloc.keyword("<-"),
alloc.reflow(" with "),
alloc.keyword(":"),
alloc.reflow(" to assign the field directly.")
])
}
_ => {
alloc.reflow("Are there any missing commas? Or missing parentheses?")
}
}
]),
};

View File

@ -10225,6 +10225,29 @@ I recommend using camelCase. It's the standard style in Roc code!
"###
);
test_report!(
record_builder_apply_non_function,
indoc!(
r#"
succeed = \_ -> crash ""
succeed {
a <- "a",
}
"#
),
@r###"
TOO MANY ARGS /code/proj/Main.roc
This value is not a function, but it was given 1 argument:
7 a <- "a",
^^^
Tip: Replace `<-` with `:` to assign the field directly.
"###
);
test_report!(
destructure_assignment_introduces_no_variables_nested,
indoc!(