Skip to content

Commit

Permalink
Fix source positions in error messsages (#1729)
Browse files Browse the repository at this point in the history
Close #1728.

# Todo

- [ ] Remove the `Type::set_position()` function.

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
raviqqe and mergify[bot] authored Dec 30, 2022
1 parent 8f98505 commit 65760ea
Show file tree
Hide file tree
Showing 14 changed files with 761 additions and 307 deletions.
48 changes: 40 additions & 8 deletions lib/hir-mir/src/built_in_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ pub fn compile(
.function_type()
.ok_or_else(|| AnalysisError::TypeNotInferred(position.clone()))?;
let function_type = type_canonicalizer::canonicalize_function(function_type, context.types())?
.ok_or_else(|| AnalysisError::FunctionExpected(function_type.clone()))?;
.ok_or_else(|| {
AnalysisError::FunctionExpected(
call.function().position().clone(),
function_type.clone(),
)
})?;
let arguments = call
.arguments()
.iter()
Expand All @@ -49,7 +54,12 @@ pub fn compile(
BuiltInFunctionName::Delete => {
let map_type =
type_canonicalizer::canonicalize_map(function_type.result(), context.types())?
.ok_or_else(|| AnalysisError::MapExpected(function_type.result().clone()))?;
.ok_or_else(|| {
AnalysisError::MapExpected(
call.position().clone(),
function_type.result().clone(),
)
})?;
let mir_map_type = type_::compile_map(context)?;

mir::ir::Call::new(
Expand Down Expand Up @@ -85,17 +95,23 @@ pub fn compile(
BuiltInFunctionName::Error => error_type::compile_error(arguments[0].clone()),
BuiltInFunctionName::Keys => {
let argument_type = &function_type.arguments()[0];
let argument = &call.arguments()[0];

compile_map_iteration(
context,
&call.arguments()[0],
argument,
&context
.configuration()?
.map_type
.iteration
.key_function_name,
type_canonicalizer::canonicalize_map(argument_type, context.types())?
.ok_or_else(|| AnalysisError::MapExpected(argument_type.clone()))?
.ok_or_else(|| {
AnalysisError::MapExpected(
argument.position().clone(),
argument_type.clone(),
)
})?
.key(),
position,
)?
Expand All @@ -105,7 +121,12 @@ pub fn compile(

let list_type =
type_canonicalizer::canonicalize_list(function_type.result(), context.types())?
.ok_or_else(|| AnalysisError::ListExpected(function_type.result().clone()))?;
.ok_or_else(|| {
AnalysisError::ListExpected(
call.position().clone(),
function_type.result().clone(),
)
})?;
let any_list_type =
types::List::new(types::Any::new(position.clone()), position.clone());

Expand Down Expand Up @@ -168,7 +189,12 @@ pub fn compile(
let function_type = &function_type.arguments()[0];
let function_type =
type_canonicalizer::canonicalize_function(function_type, context.types())?
.ok_or_else(|| AnalysisError::FunctionExpected(function_type.clone()))?;
.ok_or_else(|| {
AnalysisError::FunctionExpected(
call.arguments()[0].position().clone(),
function_type.clone(),
)
})?;
let result_type = function_type.result();
let any_type = Type::from(types::Any::new(position.clone()));
let thunk_type =
Expand Down Expand Up @@ -236,17 +262,23 @@ pub fn compile(
}
BuiltInFunctionName::Values => {
let argument_type = &function_type.arguments()[0];
let argument = &call.arguments()[0];

compile_map_iteration(
context,
&call.arguments()[0],
argument,
&context
.configuration()?
.map_type
.iteration
.value_function_name,
type_canonicalizer::canonicalize_map(argument_type, context.types())?
.ok_or_else(|| AnalysisError::MapExpected(argument_type.clone()))?
.ok_or_else(|| {
AnalysisError::MapExpected(
argument.position().clone(),
argument_type.clone(),
)
})?
.value(),
position,
)?
Expand Down
35 changes: 23 additions & 12 deletions lib/hir-mir/src/downcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ pub fn compile(
let from = type_canonicalizer::canonicalize(from, context.types())?;

if !from.is_variant() {
return Err(AnalysisError::VariantExpected(from).into());
return Err(AnalysisError::VariantExpected(position.clone(), from).into());
} else if !type_subsumption_checker::check(to, &from, context.types())? {
return Err(AnalysisError::TypesNotMatched(to.clone(), from).into());
return Err(AnalysisError::TypesNotMatched {
found: (expression.position().clone(), to.clone()),
expected: (from.position().clone(), from),
}
.into());
}

Ok(
Expand Down Expand Up @@ -137,15 +141,18 @@ mod tests {
&types::Any::new(Position::fake()).into(),
&Variable::new("x", Position::fake()).into(),
),
Err(AnalysisError::TypesNotMatched(
types::Any::new(Position::fake()).into(),
types::Union::new(
types::None::new(Position::fake()),
types::Number::new(Position::fake()),
Position::fake()
)
.into(),
)
Err(AnalysisError::TypesNotMatched {
found: (Position::fake(), types::Any::new(Position::fake()).into()),
expected: (
Position::fake(),
types::Union::new(
types::None::new(Position::fake()),
types::Number::new(Position::fake()),
Position::fake()
)
.into()
),
}
.into())
);
}
Expand All @@ -158,7 +165,11 @@ mod tests {
&types::None::new(Position::fake()).into(),
&None::new(Position::fake()).into(),
),
Err(AnalysisError::VariantExpected(types::None::new(Position::fake()).into()).into())
Err(AnalysisError::VariantExpected(
Position::fake(),
types::None::new(Position::fake()).into()
)
.into())
);
}
}
22 changes: 17 additions & 5 deletions lib/hir-mir/src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ pub fn compile(
mir::ir::Call::new(
type_::compile(context, type_)?
.into_function()
.ok_or_else(|| AnalysisError::FunctionExpected(type_.clone()))?,
.ok_or_else(|| {
AnalysisError::FunctionExpected(
call.function().position().clone(),
type_.clone(),
)
})?,
compile(call.function())?,
call.arguments()
.iter()
Expand Down Expand Up @@ -125,6 +130,7 @@ pub fn compile(
Expression::RecordConstruction(construction) => {
let field_types = record_field_resolver::resolve(
construction.type_(),
construction.type_().position(),
context.types(),
context.records(),
)?;
Expand All @@ -148,10 +154,15 @@ pub fn compile(

mir::ir::RecordField::new(
type_::compile(context, type_)?.into_record().unwrap(),
record_field_resolver::resolve(type_, context.types(), context.records())?
.iter()
.position(|field_type| field_type.name() == deconstruction.field_name())
.unwrap(),
record_field_resolver::resolve(
type_,
deconstruction.record().position(),
context.types(),
context.records(),
)?
.iter()
.position(|field_type| field_type.name() == deconstruction.field_name())
.unwrap(),
compile(deconstruction.record())?,
)
.into()
Expand All @@ -168,6 +179,7 @@ pub fn compile(
Ok(mir::ir::RecordUpdateField::new(
record_field_resolver::resolve(
update.type_(),
update.type_().position(),
context.types(),
context.records(),
)?
Expand Down
5 changes: 4 additions & 1 deletion lib/hir-mir/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ pub fn compile(context: &Context, module: &Module) -> Result<mir::ir::Module, Co
type_::compile(context, declaration.type_())?
.into_function()
.ok_or_else(|| {
AnalysisError::FunctionExpected(declaration.type_().clone())
AnalysisError::FunctionExpected(
declaration.position().clone(),
declaration.type_().clone(),
)
})?,
compile_calling_convention(declaration.calling_convention()),
))
Expand Down
10 changes: 7 additions & 3 deletions lib/hir-mir/src/transformation/equal_operation/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ fn transform_canonical(
position,
)?,
Type::Any(_) | Type::Error(_) | Type::Function(_) => {
return Err(AnalysisError::TypeNotComparable(type_.clone()).into())
return Err(AnalysisError::TypeNotComparable(position.clone(), type_.clone()).into())
}
})
}
Expand Down Expand Up @@ -364,7 +364,11 @@ mod tests {
&Variable::new("y", Position::fake()).into(),
&Position::fake()
),
Err(AnalysisError::TypeNotComparable(types::Any::new(Position::fake()).into()).into())
Err(AnalysisError::TypeNotComparable(
Position::fake(),
types::Any::new(Position::fake()).into()
)
.into())
);
}

Expand All @@ -381,7 +385,7 @@ mod tests {
&Variable::new("y", Position::fake()).into(),
&Position::fake()
),
Err(AnalysisError::TypeNotComparable(function_type.into()).into())
Err(AnalysisError::TypeNotComparable(Position::fake(), function_type.into()).into())
);
}
}
6 changes: 3 additions & 3 deletions lib/hir-mir/src/transformation/hash_calculation/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ pub fn transform(
position,
)?,
Type::Any(_) | Type::Error(_) | Type::Function(_) => {
return Err(AnalysisError::TypeNotComparable(type_.clone()).into())
return Err(AnalysisError::TypeNotComparable(position.clone(), type_.clone()).into())
}
})
}
Expand Down Expand Up @@ -287,7 +287,7 @@ mod tests {
&any_type.clone().into(),
&Position::fake(),
),
Err(AnalysisError::TypeNotComparable(any_type.into()).into())
Err(AnalysisError::TypeNotComparable(Position::fake(), any_type.into()).into())
);
}

Expand All @@ -303,7 +303,7 @@ mod tests {
&function_type.clone().into(),
&Position::fake(),
),
Err(AnalysisError::TypeNotComparable(function_type.into()).into())
Err(AnalysisError::TypeNotComparable(Position::fake(), function_type.into()).into())
);
}
}
Loading

0 comments on commit 65760ea

Please sign in to comment.