Skip to content

Commit 8d391ec

Browse files
committed
internal: refactor mismatched args count diagnostic
1 parent bccf77f commit 8d391ec

File tree

4 files changed

+279
-280
lines changed

4 files changed

+279
-280
lines changed

crates/hir/src/diagnostics.rs

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ diagnostics![
3535
BreakOutsideOfLoop,
3636
InactiveCode,
3737
MacroError,
38+
MismatchedArgCount,
3839
MissingFields,
3940
MissingUnsafe,
4041
NoSuchField,
@@ -143,36 +144,13 @@ impl Diagnostic for ReplaceFilterMapNextWithFindMap {
143144
}
144145
}
145146

146-
// Diagnostic: mismatched-arg-count
147-
//
148-
// This diagnostic is triggered if a function is invoked with an incorrect amount of arguments.
149147
#[derive(Debug)]
150148
pub struct MismatchedArgCount {
151-
pub file: HirFileId,
152-
pub call_expr: AstPtr<ast::Expr>,
149+
pub call_expr: InFile<AstPtr<ast::Expr>>,
153150
pub expected: usize,
154151
pub found: usize,
155152
}
156153

157-
impl Diagnostic for MismatchedArgCount {
158-
fn code(&self) -> DiagnosticCode {
159-
DiagnosticCode("mismatched-arg-count")
160-
}
161-
fn message(&self) -> String {
162-
let s = if self.expected == 1 { "" } else { "s" };
163-
format!("Expected {} argument{}, found {}", self.expected, s, self.found)
164-
}
165-
fn display_source(&self) -> InFile<SyntaxNodePtr> {
166-
InFile { file_id: self.file, value: self.call_expr.clone().into() }
167-
}
168-
fn as_any(&self) -> &(dyn Any + Send + 'static) {
169-
self
170-
}
171-
fn is_experimental(&self) -> bool {
172-
true
173-
}
174-
}
175-
176154
#[derive(Debug)]
177155
pub struct RemoveThisSemicolon {
178156
pub file: HirFileId,

crates/hir/src/lib.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,12 +1176,9 @@ impl Function {
11761176
}
11771177
BodyValidationDiagnostic::MismatchedArgCount { call_expr, expected, found } => {
11781178
match source_map.expr_syntax(call_expr) {
1179-
Ok(source_ptr) => sink.push(MismatchedArgCount {
1180-
file: source_ptr.file_id,
1181-
call_expr: source_ptr.value,
1182-
expected,
1183-
found,
1184-
}),
1179+
Ok(source_ptr) => acc.push(
1180+
MismatchedArgCount { call_expr: source_ptr, expected, found }.into(),
1181+
),
11851182
Err(SyntheticSyntax) => (),
11861183
}
11871184
}

crates/ide/src/diagnostics.rs

Lines changed: 2 additions & 250 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
mod break_outside_of_loop;
88
mod inactive_code;
99
mod macro_error;
10+
mod mismatched_arg_count;
1011
mod missing_fields;
1112
mod missing_unsafe;
1213
mod no_such_field;
@@ -224,6 +225,7 @@ pub(crate) fn diagnostics(
224225
AnyDiagnostic::MacroError(d) => macro_error::macro_error(&ctx, &d),
225226
AnyDiagnostic::MissingFields(d) => missing_fields::missing_fields(&ctx, &d),
226227
AnyDiagnostic::MissingUnsafe(d) => missing_unsafe::missing_unsafe(&ctx, &d),
228+
AnyDiagnostic::MismatchedArgCount(d) => mismatched_arg_count::mismatched_arg_count(&ctx, &d),
227229
AnyDiagnostic::NoSuchField(d) => no_such_field::no_such_field(&ctx, &d),
228230
AnyDiagnostic::UnimplementedBuiltinMacro(d) => unimplemented_builtin_macro::unimplemented_builtin_macro(&ctx, &d),
229231
AnyDiagnostic::UnresolvedExternCrate(d) => unresolved_extern_crate::unresolved_extern_crate(&ctx, &d),
@@ -836,256 +838,6 @@ fn x(a: S) {
836838
)
837839
}
838840

839-
#[test]
840-
fn simple_free_fn_zero() {
841-
check_diagnostics(
842-
r#"
843-
fn zero() {}
844-
fn f() { zero(1); }
845-
//^^^^^^^ Expected 0 arguments, found 1
846-
"#,
847-
);
848-
849-
check_diagnostics(
850-
r#"
851-
fn zero() {}
852-
fn f() { zero(); }
853-
"#,
854-
);
855-
}
856-
857-
#[test]
858-
fn simple_free_fn_one() {
859-
check_diagnostics(
860-
r#"
861-
fn one(arg: u8) {}
862-
fn f() { one(); }
863-
//^^^^^ Expected 1 argument, found 0
864-
"#,
865-
);
866-
867-
check_diagnostics(
868-
r#"
869-
fn one(arg: u8) {}
870-
fn f() { one(1); }
871-
"#,
872-
);
873-
}
874-
875-
#[test]
876-
fn method_as_fn() {
877-
check_diagnostics(
878-
r#"
879-
struct S;
880-
impl S { fn method(&self) {} }
881-
882-
fn f() {
883-
S::method();
884-
} //^^^^^^^^^^^ Expected 1 argument, found 0
885-
"#,
886-
);
887-
888-
check_diagnostics(
889-
r#"
890-
struct S;
891-
impl S { fn method(&self) {} }
892-
893-
fn f() {
894-
S::method(&S);
895-
S.method();
896-
}
897-
"#,
898-
);
899-
}
900-
901-
#[test]
902-
fn method_with_arg() {
903-
check_diagnostics(
904-
r#"
905-
struct S;
906-
impl S { fn method(&self, arg: u8) {} }
907-
908-
fn f() {
909-
S.method();
910-
} //^^^^^^^^^^ Expected 1 argument, found 0
911-
"#,
912-
);
913-
914-
check_diagnostics(
915-
r#"
916-
struct S;
917-
impl S { fn method(&self, arg: u8) {} }
918-
919-
fn f() {
920-
S::method(&S, 0);
921-
S.method(1);
922-
}
923-
"#,
924-
);
925-
}
926-
927-
#[test]
928-
fn method_unknown_receiver() {
929-
// note: this is incorrect code, so there might be errors on this in the
930-
// future, but we shouldn't emit an argument count diagnostic here
931-
check_diagnostics(
932-
r#"
933-
trait Foo { fn method(&self, arg: usize) {} }
934-
935-
fn f() {
936-
let x;
937-
x.method();
938-
}
939-
"#,
940-
);
941-
}
942-
943-
#[test]
944-
fn tuple_struct() {
945-
check_diagnostics(
946-
r#"
947-
struct Tup(u8, u16);
948-
fn f() {
949-
Tup(0);
950-
} //^^^^^^ Expected 2 arguments, found 1
951-
"#,
952-
)
953-
}
954-
955-
#[test]
956-
fn enum_variant() {
957-
check_diagnostics(
958-
r#"
959-
enum En { Variant(u8, u16), }
960-
fn f() {
961-
En::Variant(0);
962-
} //^^^^^^^^^^^^^^ Expected 2 arguments, found 1
963-
"#,
964-
)
965-
}
966-
967-
#[test]
968-
fn enum_variant_type_macro() {
969-
check_diagnostics(
970-
r#"
971-
macro_rules! Type {
972-
() => { u32 };
973-
}
974-
enum Foo {
975-
Bar(Type![])
976-
}
977-
impl Foo {
978-
fn new() {
979-
Foo::Bar(0);
980-
Foo::Bar(0, 1);
981-
//^^^^^^^^^^^^^^ Expected 1 argument, found 2
982-
Foo::Bar();
983-
//^^^^^^^^^^ Expected 1 argument, found 0
984-
}
985-
}
986-
"#,
987-
);
988-
}
989-
990-
#[test]
991-
fn varargs() {
992-
check_diagnostics(
993-
r#"
994-
extern "C" {
995-
fn fixed(fixed: u8);
996-
fn varargs(fixed: u8, ...);
997-
fn varargs2(...);
998-
}
999-
1000-
fn f() {
1001-
unsafe {
1002-
fixed(0);
1003-
fixed(0, 1);
1004-
//^^^^^^^^^^^ Expected 1 argument, found 2
1005-
varargs(0);
1006-
varargs(0, 1);
1007-
varargs2();
1008-
varargs2(0);
1009-
varargs2(0, 1);
1010-
}
1011-
}
1012-
"#,
1013-
)
1014-
}
1015-
1016-
#[test]
1017-
fn arg_count_lambda() {
1018-
check_diagnostics(
1019-
r#"
1020-
fn main() {
1021-
let f = |()| ();
1022-
f();
1023-
//^^^ Expected 1 argument, found 0
1024-
f(());
1025-
f((), ());
1026-
//^^^^^^^^^ Expected 1 argument, found 2
1027-
}
1028-
"#,
1029-
)
1030-
}
1031-
1032-
#[test]
1033-
fn cfgd_out_call_arguments() {
1034-
check_diagnostics(
1035-
r#"
1036-
struct C(#[cfg(FALSE)] ());
1037-
impl C {
1038-
fn new() -> Self {
1039-
Self(
1040-
#[cfg(FALSE)]
1041-
(),
1042-
)
1043-
}
1044-
1045-
fn method(&self) {}
1046-
}
1047-
1048-
fn main() {
1049-
C::new().method(#[cfg(FALSE)] 0);
1050-
}
1051-
"#,
1052-
);
1053-
}
1054-
1055-
#[test]
1056-
fn cfgd_out_fn_params() {
1057-
check_diagnostics(
1058-
r#"
1059-
fn foo(#[cfg(NEVER)] x: ()) {}
1060-
1061-
struct S;
1062-
1063-
impl S {
1064-
fn method(#[cfg(NEVER)] self) {}
1065-
fn method2(#[cfg(NEVER)] self, arg: u8) {}
1066-
fn method3(self, #[cfg(NEVER)] arg: u8) {}
1067-
}
1068-
1069-
extern "C" {
1070-
fn fixed(fixed: u8, #[cfg(NEVER)] ...);
1071-
fn varargs(#[cfg(not(NEVER))] ...);
1072-
}
1073-
1074-
fn main() {
1075-
foo();
1076-
S::method();
1077-
S::method2(0);
1078-
S::method3(S);
1079-
S.method3();
1080-
unsafe {
1081-
fixed(0);
1082-
varargs(1, 2, 3);
1083-
}
1084-
}
1085-
"#,
1086-
)
1087-
}
1088-
1089841
#[test]
1090842
fn missing_semicolon() {
1091843
check_diagnostics(

0 commit comments

Comments
 (0)