Skip to content

Commit

Permalink
report an error when failing to infer unannotated lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
kritzcreek committed Oct 8, 2024
1 parent 634d892 commit 32bc746
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 8 deletions.
4 changes: 4 additions & 0 deletions crates/cli/tests/check/cant_infer_lambda.nemo
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() -> i32 {
let clos = \(z) { z };
clos(1)
}
2 changes: 1 addition & 1 deletion crates/cli/tests/check/cant_return_from_global.nemo
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ global g = return 1
global g2 = {
let x = 3;
return x
}
}
25 changes: 25 additions & 0 deletions crates/cli/tests/snapshots/check@cant_infer_lambda.nemo.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
source: crates/cli/tests/lib.rs
info:
program: nemo
args:
- check
- tests/check/cant_infer_lambda.nemo
env:
RUST_BACKTRACE: "0"
input_file: crates/cli/tests/check/cant_infer_lambda.nemo
---
success: false
exit_code: 1
----- stdout -----
[27] Error: Can't infer type of unannotated lambda
╭─[tests/check/cant_infer_lambda.nemo:1:13]
│
2 │   let clos = \(z) { z };
 │
 │ ╰── Can't infer type of unannotated lambda
───╯


----- stderr -----
Error: "Check failed with 1 errors"
17 changes: 10 additions & 7 deletions crates/frontend/src/types/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ impl Scope {
}
}

// TyCtx contains top-level function, type, and import definitions.
// Because it is immutable once we've walked the top-level structure of a
// module we keep it in the Typechecker struct for easier access.
// The mutable part of the type checking "context" is called Scope in this module
// and passed as an explicit mutable reference throughout the checking process
/// TyCtx contains top-level function, type, and import definitions.
/// Because it is immutable once we've walked the top-level structure of a
/// module we keep it in the Typechecker struct for easier access.
/// The mutable part of the type checking "context" is called Scope in this module
/// and passed as an explicit mutable reference throughout the checking process
#[derive(Debug)]
struct TyCtx<'ctx> {
functions: HashMap<Symbol, FuncDef>,
Expand Down Expand Up @@ -1058,8 +1058,11 @@ impl Typechecker<'_> {
};
let (name, sym) = self.name_supply.local_idx(&name_tkn);
let ty = match param.ty() {
// TODO: Produce a type error. We can't infer lambdas
None => Ty::Error,
None => {
// TODO: report a single error spanning the entire param list
errors.report(&param, CantInferLambda);
Ty::Error
}
Some(t) => self.check_ty(errors, scope, &t),
};
builder.params(Some((name, ty.clone())));
Expand Down
3 changes: 3 additions & 0 deletions crates/frontend/src/types/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ pub enum TyErrorData {
MissingNode(String),
InvalidLiteral,
CantInferEmptyArray,
CantInferLambda,
CantInstantiateFunctionRef,
TypeParamInVariantStruct,
CantReturnFromGlobal,
Expand Down Expand Up @@ -204,6 +205,7 @@ fn code_for_error(err_data: &TyErrorData) -> i32 {
TyErrorData::CantReturnFromGlobal => 24,
TyErrorData::CantReassignCapturedVariable(_) => 25,
TyErrorData::CantInferTypeParam(_) => 26,
TyErrorData::CantInferLambda => 27,
}
}

Expand All @@ -216,6 +218,7 @@ fn error_label(err_data: &TyErrorData, ctx: &Ctx) -> String {
rhs.display(ctx)
),
TyErrorData::CantInferEmptyArray => "Can't infer type of an empty array".to_string(),
TyErrorData::CantInferLambda => "Can't infer type of unannotated lambda".to_string(),
TyErrorData::CantInstantiateFunctionRef => "Can't instantiate function reference. Only top-level functions may be polymorphic at this time.".to_string(),
TyErrorData::CantReturnFromGlobal => "Can't 'return' from a global definition.".to_string(),
TyErrorData::UnknownVar(v) => format!("Unknown variable {v}"),
Expand Down

0 comments on commit 32bc746

Please sign in to comment.