Skip to content

Commit

Permalink
frontend: Remove Callee case from MethodLookup
Browse files Browse the repository at this point in the history
  • Loading branch information
dinfuehr committed Nov 15, 2024
1 parent 7e068cb commit 83219e9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 50 deletions.
29 changes: 15 additions & 14 deletions dora-frontend/src/typeck/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,31 +275,32 @@ fn check_expr_call_fct(
type_params: SourceTypeArray,
arguments: CallArguments,
) -> SourceType {
let fct = ck.sa.fct(fct_id);

if !fct_accessible_from(ck.sa, fct_id, ck.module_id) {
let msg = ErrorMessage::NotAccessible;
ck.sa.report(ck.file_id, e.span, msg);
}

let arg_types = arguments.assume_all_positional(ck);

let lookup = MethodLookup::new(ck.sa, ck.file_id, ck.type_param_definition)
.span(e.span)
.callee(fct_id)
.args(&arg_types)
.fct_type_params(&type_params)
.find();

let ty = if lookup.find() {
let call_type = CallType::Fct(fct_id, type_params.clone());
ck.analysis.map_calls.insert(e.id, Arc::new(call_type));

lookup.found_ret().unwrap()
let ty = if typeparamck::check(
ck.sa,
&ck.type_param_definition,
fct,
&type_params,
ck.file_id,
e.span,
) {
check_args_compatible_fct(ck, fct, arguments, &type_params, None);
specialize_type(ck.sa, fct.return_type(), &type_params)
} else {
ty_error()
};

ck.analysis.set_ty(e.id, ty.clone());

let call_type = CallType::Fct(fct_id, type_params.clone());
ck.analysis.map_calls.insert(e.id, Arc::new(call_type));

ty
}

Expand Down
10 changes: 0 additions & 10 deletions dora-frontend/src/typeck/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ impl MethodLookupResult {
enum LookupKind {
Method(SourceType),
Static(SourceType),
Callee(FctDefinitionId),
}

pub struct MethodLookup<'a> {
Expand Down Expand Up @@ -94,11 +93,6 @@ impl<'a> MethodLookup<'a> {
}
}

pub fn callee(mut self, fct_id: FctDefinitionId) -> MethodLookup<'a> {
self.kind = Some(LookupKind::Callee(fct_id));
self
}

pub fn parent(mut self, parent: FctParent) -> MethodLookup<'a> {
self.fct_parent = Some(parent);
self
Expand Down Expand Up @@ -146,8 +140,6 @@ impl<'a> MethodLookup<'a> {
let mut result = MethodLookupResult::new();

let fct_id = match kind {
LookupKind::Callee(fct_id) => Some(fct_id),

LookupKind::Method(ref obj) => {
let name = self.name.expect("name not set");
self.find_method(&mut result, obj.clone(), name, false)
Expand All @@ -168,7 +160,6 @@ impl<'a> MethodLookup<'a> {
let name = self.sa.interner.str(name).to_string();

let msg = match kind {
LookupKind::Callee(_) => unreachable!(),
LookupKind::Method(ref obj) => {
let type_name = self.ty_name(obj);

Expand All @@ -195,7 +186,6 @@ impl<'a> MethodLookup<'a> {
LookupKind::Method(_) | LookupKind::Static(_) => {
result.found_container_type_params.clone().unwrap()
}
_ => SourceTypeArray::empty(),
};

let fct_tps: SourceTypeArray = if let Some(fct_tps) = self.fct_tps {
Expand Down
43 changes: 17 additions & 26 deletions dora-frontend/src/typeck/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,30 +439,22 @@ fn type_function_params() {
"
fn foo() {}
fn f() { foo(1i32); }",
(3, 18),
ErrorMessage::ParamTypesIncompatible("foo".into(), vec![], vec!["Int32".into()]),
(3, 22),
ErrorMessage::SuperfluousArgument,
);
err(
"
fn foo(a: Int32) {}
fn f() { foo(true); }",
(3, 18),
ErrorMessage::ParamTypesIncompatible(
"foo".into(),
vec!["Int32".into()],
vec!["Bool".into()],
),
(3, 22),
ErrorMessage::WrongTypeForArgument("Int32".into(), "Bool".into()),
);
err(
"
fn foo(a: Int32, b: Bool) {}
fn f() { foo(1i32, 2i32); }",
(3, 18),
ErrorMessage::ParamTypesIncompatible(
"foo".into(),
vec!["Int32".into(), "Bool".into()],
vec!["Int32".into(), "Int32".into()],
),
(3, 28),
ErrorMessage::WrongTypeForArgument("Bool".into(), "Int32".into()),
);
}

Expand Down Expand Up @@ -1198,8 +1190,8 @@ fn test_new_call_fct() {
fn test_new_call_fct_wrong_params() {
err(
"fn g() {} fn f() { g(1i32); }",
(1, 20),
ErrorMessage::ParamTypesIncompatible("g".into(), Vec::new(), vec!["Int32".into()]),
(1, 22),
ErrorMessage::SuperfluousArgument,
);
}

Expand Down Expand Up @@ -2544,8 +2536,8 @@ fn variadic_parameter() {
f(true);
}
",
(4, 13),
ErrorMessage::ParamTypesIncompatible("f".into(), vec!["Int32".into()], vec!["Bool".into()]),
(4, 15),
ErrorMessage::WrongTypeForArgument("Int32".into(), "Bool".into()),
);
ok("
fn f(x: Int32, y: Int32...) {}
Expand All @@ -2563,11 +2555,7 @@ fn variadic_parameter() {
}
",
(4, 13),
ErrorMessage::ParamTypesIncompatible(
"f".into(),
vec!["Int32".into(), "Int32".into()],
Vec::new(),
),
ErrorMessage::MissingArguments(1, 0),
);
err(
"fn f(x: Int32..., y: Int32) {}",
Expand Down Expand Up @@ -2649,8 +2637,8 @@ fn check_wrong_number_type_params() {
fn foo() { bar[Int32](false); }
fn bar[T](x: T) {}
",
(2, 24),
ErrorMessage::ParamTypesIncompatible("bar".into(), vec!["T".into()], vec!["Bool".into()]),
(2, 35),
ErrorMessage::WrongTypeForArgument("Int32".into(), "Bool".into()),
);
}

Expand Down Expand Up @@ -4672,7 +4660,10 @@ fn call_with_named_arguments() {
f(1, 2, y = 3);
}
",
&[((4, 21), ErrorMessage::UnexpectedNamedArgument)],
&[
((4, 21), ErrorMessage::UnexpectedNamedArgument),
((4, 21), ErrorMessage::SuperfluousArgument),
],
);
}

Expand Down

0 comments on commit 83219e9

Please sign in to comment.