Skip to content

Commit bbcf8a3

Browse files
committed
More breadcrumbs for errors
1 parent 4dd9d0d commit bbcf8a3

File tree

9 files changed

+80
-14
lines changed

9 files changed

+80
-14
lines changed

compiler-core/checking/src/algorithm/binder.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::ExternalQueries;
55
use crate::algorithm::state::{CheckContext, CheckState};
66
use crate::algorithm::{kind, operator, term, unification};
77
use crate::core::{RowField, RowType, Type, TypeId};
8+
use crate::error::ErrorStep;
89

910
#[derive(Copy, Clone, Debug)]
1011
enum BinderMode {
@@ -20,7 +21,9 @@ pub fn infer_binder<Q>(
2021
where
2122
Q: ExternalQueries,
2223
{
23-
binder_core(state, context, binder_id, BinderMode::Infer)
24+
state.with_error_step(ErrorStep::InferringBinder(binder_id), |state| {
25+
binder_core(state, context, binder_id, BinderMode::Infer)
26+
})
2427
}
2528

2629
pub fn check_binder<Q>(
@@ -32,7 +35,9 @@ pub fn check_binder<Q>(
3235
where
3336
Q: ExternalQueries,
3437
{
35-
binder_core(state, context, binder_id, BinderMode::Check { expected_type })
38+
state.with_error_step(ErrorStep::CheckingBinder(binder_id), |state| {
39+
binder_core(state, context, binder_id, BinderMode::Check { expected_type })
40+
})
3641
}
3742

3843
fn binder_core<Q>(

compiler-core/checking/src/algorithm/kind.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::ExternalQueries;
1313
use crate::algorithm::state::{CheckContext, CheckState};
1414
use crate::algorithm::{substitute, transfer, unification};
1515
use crate::core::{ForallBinder, RowField, RowType, Type, TypeId, Variable};
16+
use crate::error::ErrorStep;
1617

1718
const MISSING_NAME: SmolStr = SmolStr::new_static("<MissingName>");
1819

@@ -21,6 +22,19 @@ pub fn infer_surface_kind<Q>(
2122
context: &CheckContext<Q>,
2223
id: lowering::TypeId,
2324
) -> QueryResult<(TypeId, TypeId)>
25+
where
26+
Q: ExternalQueries,
27+
{
28+
state.with_error_step(ErrorStep::InferringKind(id), |state| {
29+
infer_surface_kind_core(state, context, id)
30+
})
31+
}
32+
33+
fn infer_surface_kind_core<Q>(
34+
state: &mut CheckState,
35+
context: &CheckContext<Q>,
36+
id: lowering::TypeId,
37+
) -> QueryResult<(TypeId, TypeId)>
2438
where
2539
Q: ExternalQueries,
2640
{
@@ -518,9 +532,11 @@ pub fn check_surface_kind<Q>(
518532
where
519533
Q: ExternalQueries,
520534
{
521-
let (inferred_type, inferred_kind) = infer_surface_kind(state, context, id)?;
522-
let _ = unification::subtype(state, context, inferred_kind, kind)?;
523-
Ok((inferred_type, inferred_kind))
535+
state.with_error_step(ErrorStep::CheckingKind(id), |state| {
536+
let (inferred_type, inferred_kind) = infer_surface_kind_core(state, context, id)?;
537+
let _ = unification::subtype(state, context, inferred_kind, kind)?;
538+
Ok((inferred_type, inferred_kind))
539+
})
524540
}
525541

526542
pub fn check_type_variable_binding<Q>(

compiler-core/checking/src/algorithm/term.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::ExternalQueries;
77
use crate::algorithm::state::{CheckContext, CheckState};
88
use crate::algorithm::{binder, inspect, kind, operator, substitute, transfer, unification};
99
use crate::core::{RowField, RowType, Type, TypeId};
10-
use crate::error::ErrorKind;
10+
use crate::error::{ErrorKind, ErrorStep};
1111

1212
pub fn infer_equations<Q>(
1313
state: &mut CheckState,
@@ -245,16 +245,31 @@ pub fn check_expression<Q>(
245245
where
246246
Q: ExternalQueries,
247247
{
248-
let inferred = infer_expression(state, context, expr_id)?;
249-
let _ = unification::subtype(state, context, inferred, expected)?;
250-
Ok(inferred)
248+
state.with_error_step(ErrorStep::CheckingExpression(expr_id), |state| {
249+
let inferred = infer_expression_quiet(state, context, expr_id)?;
250+
let _ = unification::subtype(state, context, inferred, expected)?;
251+
Ok(inferred)
252+
})
251253
}
252254

253255
pub fn infer_expression<Q>(
254256
state: &mut CheckState,
255257
context: &CheckContext<Q>,
256258
expr_id: lowering::ExpressionId,
257259
) -> QueryResult<TypeId>
260+
where
261+
Q: ExternalQueries,
262+
{
263+
state.with_error_step(ErrorStep::InferringExpression(expr_id), |state| {
264+
infer_expression_quiet(state, context, expr_id)
265+
})
266+
}
267+
268+
fn infer_expression_quiet<Q>(
269+
state: &mut CheckState,
270+
context: &CheckContext<Q>,
271+
expr_id: lowering::ExpressionId,
272+
) -> QueryResult<TypeId>
258273
where
259274
Q: ExternalQueries,
260275
{

compiler-core/checking/src/error.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ pub enum ErrorStep {
99
TermDeclaration(indexing::TermItemId),
1010
TypeDeclaration(indexing::TypeItemId),
1111
ConstructorArgument(lowering::TypeId),
12+
13+
InferringKind(lowering::TypeId),
14+
CheckingKind(lowering::TypeId),
15+
16+
InferringBinder(lowering::BinderId),
17+
CheckingBinder(lowering::BinderId),
18+
19+
InferringExpression(lowering::ExpressionId),
20+
CheckingExpression(lowering::ExpressionId),
1221
}
1322

1423
#[derive(Debug, Clone, Copy, PartialEq, Eq)]

tests-integration/fixtures/checking/080_let_recursive_errors/Main.snap

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ threeWayConflict :: Int -> Int
1212
Types
1313

1414
Errors
15-
CannotUnify { Int, String } at [TermDeclaration(Idx::<TermItem>(1))]
16-
CannotUnify { String, Int } at [TermDeclaration(Idx::<TermItem>(1))]
17-
CannotUnify { ??? -> ???, ??? } at [TermDeclaration(Idx::<TermItem>(2))]
18-
CannotUnify { Int, String } at [TermDeclaration(Idx::<TermItem>(3))]
19-
CannotUnify { String, Int } at [TermDeclaration(Idx::<TermItem>(4))]
15+
CannotUnify { Int, String } at [TermDeclaration(Idx::<TermItem>(1)), InferringExpression(AstId<syntax::cst::Expression>(20))]
16+
CannotUnify { String, Int } at [TermDeclaration(Idx::<TermItem>(1)), InferringExpression(AstId<syntax::cst::Expression>(20))]
17+
CannotUnify { ??? -> ???, ??? } at [TermDeclaration(Idx::<TermItem>(2)), InferringExpression(AstId<syntax::cst::Expression>(61))]
18+
CannotUnify { Int, String } at [TermDeclaration(Idx::<TermItem>(3)), InferringExpression(AstId<syntax::cst::Expression>(82)), InferringExpression(AstId<syntax::cst::Expression>(102)), CheckingExpression(AstId<syntax::cst::Expression>(105))]
19+
CannotUnify { String, Int } at [TermDeclaration(Idx::<TermItem>(4)), InferringExpression(AstId<syntax::cst::Expression>(115)), InferringExpression(AstId<syntax::cst::Expression>(135)), CheckingExpression(AstId<syntax::cst::Expression>(138))]

tests-integration/tests/snapshots/checking__constrained_invalid_constraint.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ expression: checked.errors
1212
TypeDeclaration(
1313
Idx::<TypeItem>(1),
1414
),
15+
CheckingKind(
16+
AstId<syntax::cst::Type>(7),
17+
),
18+
CheckingKind(
19+
AstId<syntax::cst::Type>(8),
20+
),
1521
],
1622
},
1723
]

tests-integration/tests/snapshots/checking__constrained_invalid_type.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ expression: checked.errors
1212
TypeDeclaration(
1313
Idx::<TypeItem>(1),
1414
),
15+
CheckingKind(
16+
AstId<syntax::cst::Type>(14),
17+
),
18+
CheckingKind(
19+
AstId<syntax::cst::Type>(18),
20+
),
1521
],
1622
},
1723
]

tests-integration/tests/snapshots/checking__partial_synonym.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ expression: checked.errors
1111
TypeDeclaration(
1212
Idx::<TypeItem>(1),
1313
),
14+
CheckingKind(
15+
AstId<syntax::cst::Type>(9),
16+
),
1417
],
1518
},
1619
CheckError {
@@ -22,6 +25,9 @@ expression: checked.errors
2225
TypeDeclaration(
2326
Idx::<TypeItem>(1),
2427
),
28+
CheckingKind(
29+
AstId<syntax::cst::Type>(9),
30+
),
2531
],
2632
},
2733
CheckError {

tests-integration/tests/snapshots/checking__unification_fail.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ expression: checked.errors
1515
ConstructorArgument(
1616
AstId<syntax::cst::Type>(9),
1717
),
18+
CheckingKind(
19+
AstId<syntax::cst::Type>(9),
20+
),
1821
],
1922
},
2023
]

0 commit comments

Comments
 (0)