Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(diag): Better error messages when trying to call a non-callable type #119

Merged
merged 1 commit into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions docs/lang_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,26 @@ let rotated = Point::rotate_180deg::<f64>(&point);
// Not `Point::<f64>::rotate_180deg(&point);`
```

If the method has the same name as a field, the field has precedence

```rust
struct Foo {
bar: i32
}

impl Foo {
fn bar(self: &Foo) -> i32 {
self.bar
}
}

let foo = Foo { bar: 42 };
println!("{}", foo.bar()); // compile error - i32 is not callable

// Method can be called explicitely using the associated function syntax
println!("{}", Foo::bar(&foo));
```

## Type attributes

Named types can have attributes.
Expand Down
4 changes: 4 additions & 0 deletions src/alumina-boot/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ pub enum CodeDiagnostic {
TupleIndexOutOfBounds,
#[error("function or static expected")]
FunctionOrStaticExpectedHere,
#[error("`{}` is not callable", .0)]
NotCallable(String),
#[error("`{}` is not callable (hint: fields have precedence over methods, if there is a method with the same name, you can call the it explicitly with `Type::method(...)`)", .0)]
NotCallableFieldConfusion(String),
#[error("unexpected generic arguments (is this a method that needs to be called?)")]
UnexpectedGenericArgs,
#[error("could not resolve item `{}`", .0)]
Expand Down
10 changes: 8 additions & 2 deletions src/alumina-boot/src/ir/mono/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,11 +742,17 @@ impl<'a, 'ast, 'ir> super::Mono<'a, 'ast, 'ir> {
(&fn_arg_types[..], fun.return_type, callee)
}
_ => {
bail!(self, CodeDiagnostic::FunctionOrStaticExpectedHere);
bail!(
self,
CodeDiagnostic::NotCallable(self.ctx.type_name(callee.ty)?)
);
}
},
_ => {
bail!(self, CodeDiagnostic::FunctionOrStaticExpectedHere);
bail!(
self,
CodeDiagnostic::NotCallable(self.ctx.type_name(callee.ty)?)
);
}
};

Expand Down
33 changes: 26 additions & 7 deletions src/alumina-boot/src/ir/mono/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4997,6 +4997,8 @@ impl<'a, 'ast, 'ir> Mono<'a, 'ast, 'ir> {
// as function pointers, but we are also able to call things that cannot be turned into a function
// pointer, such as methods, UFCS free functions and compiler intrinsics.
let ast_callee = callee;
let mut possible_field_confusion = false;

let callee = match &callee.kind {
ast::ExprKind::Fn(ast::FnKind::Normal(item), generic_args) => {
if let ast::Item::Intrinsic(intrinsic) = item.get() {
Expand Down Expand Up @@ -5054,7 +5056,10 @@ impl<'a, 'ast, 'ir> Mono<'a, 'ast, 'ir> {
ast_span,
)? {
Some(result) => return Ok(result),
None => self.lower_expr(callee, None)?,
None => {
possible_field_confusion = true;
self.lower_expr(callee, None)?
}
}
}
_ => self.lower_expr(callee, None)?,
Expand All @@ -5063,6 +5068,24 @@ impl<'a, 'ast, 'ir> Mono<'a, 'ast, 'ir> {
let mut varargs = false;
let mut self_arg = None;

macro_rules! not_callable {
() => {
if possible_field_confusion {
bail!(
self,
CodeDiagnostic::NotCallableFieldConfusion(
self.ctx.type_name(callee.ty).unwrap()
)
);
} else {
bail!(
self,
CodeDiagnostic::NotCallable(self.ctx.type_name(callee.ty).unwrap())
);
}
};
}

let fn_arg_types: Vec<_>;
let (arg_types, return_type, callee) = match callee.ty {
ir::Ty::FunctionPointer(arg_types, return_type) => (*arg_types, *return_type, callee),
Expand All @@ -5088,13 +5111,9 @@ impl<'a, 'ast, 'ir> Mono<'a, 'ast, 'ir> {

(&fn_arg_types[..], fun.return_type, callee)
}
_ => {
bail!(self, CodeDiagnostic::FunctionOrStaticExpectedHere);
}
_ => not_callable!(),
},
_ => {
bail!(self, CodeDiagnostic::FunctionOrStaticExpectedHere);
}
_ => not_callable!(),
};

if !varargs && (arg_types.len() != args.len()) {
Expand Down
4 changes: 4 additions & 0 deletions tests/diag/not_callable.alu
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//! exit_code: 1
fn main() {
1(); // diag: error(not_callable): "`i32` is not callable"
}
15 changes: 15 additions & 0 deletions tests/diag/not_callable_field_confusion.alu
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//! exit_code: 1
struct Foo {
bar: i32
}

impl Foo {
fn bar(self: &Foo) -> i32 {
self.bar
}
}

fn main() {
let foo: Foo;
foo.bar(); // diag: error(not_callable_field_confusion): "`i32` is not callable (hint: fields have precedence over methods, if there is a method with the same name, you can call the it explicitly with `Type::method(...)`)"
}
Loading