Skip to content
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
14 changes: 9 additions & 5 deletions compiler/noirc_frontend/src/hir/type_check/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,11 +434,15 @@ impl<'a> From<&'a TypeCheckError> for Diagnostic {
let msg = format!("Expected {expected_count} generic{expected_plural} from this function, but {actual_count} {actual_plural} provided");
Diagnostic::simple_error(msg, "".into(), *span)
},
TypeCheckError::MacroReturningNonExpr { typ, span } => Diagnostic::simple_error(
format!("Expected macro call to return a `Quoted` but found a(n) `{typ}`"),
"Macro calls must return quoted values, otherwise there is no code to insert".into(),
*span,
),
TypeCheckError::MacroReturningNonExpr { typ, span } => {
let mut error = Diagnostic::simple_error(
format!("Expected macro call to return a `Quoted` but found a(n) `{typ}`"),
"Macro calls must return quoted values, otherwise there is no code to insert.".into(),
*span,
);
error.add_secondary("Hint: remove the `!` from the end of the function name.".to_string(), *span);
error
},
TypeCheckError::UnsupportedTurbofishUsage { span } => {
let msg = "turbofish (`::<_>`) usage at this position isn't supported yet";
Diagnostic::simple_error(msg.to_string(), "".to_string(), *span)
Expand Down
34 changes: 33 additions & 1 deletion tooling/lsp/src/requests/code_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ use lsp_types::{
};
use noirc_errors::Span;
use noirc_frontend::{
ast::{ConstructorExpression, ItemVisibility, NoirTraitImpl, Path, UseTree, Visitor},
ast::{
CallExpression, ConstructorExpression, ItemVisibility, MethodCallExpression, NoirTraitImpl,
Path, UseTree, Visitor,
},
graph::CrateId,
hir::def_map::{CrateDefMap, LocalModuleId, ModuleId},
node_interner::NodeInterner,
Expand All @@ -29,6 +32,7 @@ use super::{process_request, to_lsp_location};
mod fill_struct_fields;
mod implement_missing_members;
mod import_or_qualify;
mod remove_bang_from_call;
mod remove_unused_import;
mod tests;

Expand Down Expand Up @@ -250,4 +254,32 @@ impl<'a> Visitor for CodeActionFinder<'a> {

true
}

fn visit_call_expression(&mut self, call: &CallExpression, span: Span) -> bool {
if !self.includes_span(span) {
return false;
}

if call.is_macro_call {
self.remove_bang_from_call(call.func.span);
}

true
}

fn visit_method_call_expression(
&mut self,
method_call: &MethodCallExpression,
span: Span,
) -> bool {
if !self.includes_span(span) {
return false;
}

if method_call.is_macro_call {
self.remove_bang_from_call(method_call.method_name.span());
}

true
}
}
97 changes: 97 additions & 0 deletions tooling/lsp/src/requests/code_action/remove_bang_from_call.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
use lsp_types::TextEdit;
use noirc_errors::{Location, Span};
use noirc_frontend::{node_interner::ReferenceId, QuotedType, Type};

use crate::byte_span_to_range;

use super::CodeActionFinder;

impl<'a> CodeActionFinder<'a> {
pub(super) fn remove_bang_from_call(&mut self, span: Span) {
// If we can't find the referenced function, there's nothing we can do
let Some(ReferenceId::Function(func_id)) =
self.interner.find_referenced(Location::new(span, self.file))
else {
return;
};

// If the return type is Quoted, all is good
let func_meta = self.interner.function_meta(&func_id);
if let Type::Quoted(QuotedType::Quoted) = func_meta.return_type() {
return;
}

// The `!` comes right after the name
let byte_span = span.end() as usize..span.end() as usize + 1;
let Some(range) = byte_span_to_range(self.files, self.file, byte_span) else {
return;
};

let title = "Remove `!` from call".to_string();
let text_edit = TextEdit { range, new_text: "".to_string() };

let code_action = self.new_quick_fix(title, text_edit);
self.code_actions.push(code_action);
}
}

#[cfg(test)]
mod tests {
use tokio::test;

use crate::requests::code_action::tests::assert_code_action;

#[test]
async fn test_removes_bang_from_call() {
let title = "Remove `!` from call";

let src = r#"
fn foo() {}

fn main() {
fo>|<o!();
}
"#;

let expected = r#"
fn foo() {}

fn main() {
foo();
}
"#;

assert_code_action(title, src, expected).await;
}

#[test]
async fn test_removes_bang_from_method_call() {
let title = "Remove `!` from call";

let src = r#"
struct Foo {}

impl Foo {
fn foo(self) {}
}

fn bar(foo: Foo) {
foo.fo>|<o!();
}
"#;

let expected = r#"
struct Foo {}

impl Foo {
fn foo(self) {}
}

fn bar(foo: Foo) {
foo.foo();
}
"#;

assert_code_action(title, src, expected).await;
}
}