From d196887b3f4583e7bd4c431b1f5ff9b6af85d783 Mon Sep 17 00:00:00 2001 From: Sam Zhou Date: Fri, 27 Oct 2023 20:39:59 -0700 Subject: [PATCH] [errors] Tweak some error names and messages (#1115) --- .../samlang-core/src/checker/checker_tests.rs | 56 +++++++-------- .../samlang-core/src/checker/main_checker.rs | 18 +++-- crates/samlang-core/src/errors.rs | 72 +++++++++---------- crates/samlang-core/src/integration_tests.rs | 16 ++--- crates/samlang-core/src/services/api_tests.rs | 8 +-- .../samlang-core/src/services/server_state.rs | 6 +- 6 files changed, 90 insertions(+), 86 deletions(-) diff --git a/crates/samlang-core/src/checker/checker_tests.rs b/crates/samlang-core/src/checker/checker_tests.rs index deb8ab57f..0d24b7ad6 100644 --- a/crates/samlang-core/src/checker/checker_tests.rs +++ b/crates/samlang-core/src/checker/checker_tests.rs @@ -883,7 +883,7 @@ Found 1 error. r#" Error ----------------------------------- DUMMY.sam:1:6-1:17 -Cannot find member `helloWorld2` on `Test`. +Cannot resolve member `helloWorld2` on `Test`. 1| Test.helloWorld2 ^^^^^^^^^^^ @@ -949,7 +949,7 @@ Found 1 error. r#" Error ------------------------------------ DUMMY.sam:1:6-1:9 -Cannot find member `Foo` on `Test`. +Cannot resolve member `Foo` on `Test`. 1| Test.Foo(true) ^^^ @@ -965,7 +965,7 @@ Found 1 error. r#" Error ------------------------------------ DUMMY.sam:1:6-1:9 -Cannot find member `Bar` on `Test`. +Cannot resolve member `Bar` on `Test`. 1| Test.Bar(42) ^^^ @@ -1063,7 +1063,7 @@ Found 2 errors. r#" Error ------------------------------------ DUMMY.sam:1:1-1:7 -Class `Test44` is not resolved. +Cannot resolve class `Test44`. 1| Test44.Bar(42) ^^^^^^ @@ -1079,7 +1079,7 @@ Found 1 error. r#" Error ----------------------------------- DUMMY.sam:1:7-1:11 -Cannot find member `Tars` on `Test2`. +Cannot resolve member `Tars` on `Test2`. 1| Test2.Tars(42) ^^^^ @@ -1143,7 +1143,7 @@ Found 1 error. r#" Error ---------------------------------- DUMMY.sam:1:20-1:24 -Cannot find member `bazz` on `Test`. +Cannot resolve member `bazz` on `Test`. 1| Test.init(true, 3).bazz ^^^^ @@ -1159,7 +1159,7 @@ Found 1 error. r#" Error ---------------------------------- DUMMY.sam:1:35-1:38 -Cannot find member `bar` on `Test3`. +Cannot resolve member `bar` on `Test3`. 1| { let _ = (t3: Test3) -> t3.bar; } ^^^ @@ -1175,7 +1175,7 @@ Found 1 error. r#" Error ---------------------------------- DUMMY.sam:1:17-1:20 -Cannot find member `foo` on `Test2`. +Cannot resolve member `foo` on `Test2`. 1| Test2.Foo(true).foo ^^^ @@ -2756,7 +2756,7 @@ let _ = (t: Test2) -> if let Foo111(_) = t then 1 else 2; r#" Error ---------------------------------- DUMMY.sam:1:32-1:35 -Cannot find member `bar` on `Test`. +Cannot resolve member `bar` on `Test`. 1| { let _ = (t: Test) -> if let {bar, boo} = t then 1 else 2; ^^^ @@ -2764,7 +2764,7 @@ Cannot find member `bar` on `Test`. Error ---------------------------------- DUMMY.sam:1:37-1:40 -Cannot find member `boo` on `Test`. +Cannot resolve member `boo` on `Test`. 1| { let _ = (t: Test) -> if let {bar, boo} = t then 1 else 2; ^^^ @@ -2788,7 +2788,7 @@ Cannot access member of `Test2` at index 1. Error ---------------------------------- DUMMY.sam:5:30-5:36 -Cannot find member `Foo111` on `Test2`. +Cannot resolve member `Foo111` on `Test2`. 5| let _ = (t: Test2) -> if let Foo111(_) = t then 1 else 2; ^^^^^^ @@ -2806,7 +2806,7 @@ Found 5 errors. r#" Error ---------------------------------- DUMMY.sam:1:13-1:16 -Cannot find member `Foo` on `int`. +Cannot resolve member `Foo` on `int`. 1| match (3) { Foo(_) -> 1, Bar(s) -> 2 } ^^^ @@ -2814,7 +2814,7 @@ Cannot find member `Foo` on `int`. Error ---------------------------------- DUMMY.sam:1:26-1:29 -Cannot find member `Bar` on `int`. +Cannot resolve member `Bar` on `int`. 1| match (3) { Foo(_) -> 1, Bar(s) -> 2 } ^^^ @@ -2830,7 +2830,7 @@ Found 2 errors. r#" Error ---------------------------------- DUMMY.sam:1:30-1:33 -Cannot find member `Foo` on `Test`. +Cannot resolve member `Foo` on `Test`. 1| match (Test.init(true, 3)) { Foo(_) -> 1, Bar(s) -> 2, } ^^^ @@ -2838,7 +2838,7 @@ Cannot find member `Foo` on `Test`. Error ---------------------------------- DUMMY.sam:1:43-1:46 -Cannot find member `Bar` on `Test`. +Cannot resolve member `Bar` on `Test`. 1| match (Test.init(true, 3)) { Foo(_) -> 1, Bar(s) -> 2, } ^^^ @@ -2863,7 +2863,7 @@ The match is not exhausive. The following variants have not been handled: Error ---------------------------------- DUMMY.sam:1:50-1:53 -Cannot find member `Baz` on `Test2`. +Cannot resolve member `Baz` on `Test2`. 1| { let _ = (t: Test2) -> match (t) { Foo(_) -> 1, Baz(s) -> 2, }; } ^^^ @@ -3007,7 +3007,7 @@ Found 2 errors. r#" Error ---------------------------------- DUMMY.sam:1:10-1:11 -Cannot find member `b` on `A`. +Cannot resolve member `b` on `A`. 1| {let {a, b as c} = A.init();} ^ @@ -3023,7 +3023,7 @@ Found 1 error. r#" Error ------------------------------------ DUMMY.sam:1:7-1:8 -Cannot find member `a` on `C`. +Cannot resolve member `a` on `C`. 1| {let {a, b as c} = C.init();} ^ @@ -3031,7 +3031,7 @@ Cannot find member `a` on `C`. Error ---------------------------------- DUMMY.sam:1:10-1:11 -Cannot find member `b` on `C`. +Cannot resolve member `b` on `C`. 1| {let {a, b as c} = C.init();} ^ @@ -3047,7 +3047,7 @@ Found 2 errors. r#" Error ------------------------------------ DUMMY.sam:1:7-1:8 -Cannot find member `a` on `int`. +Cannot resolve member `a` on `int`. 1| {let {a, b as c} = 1;} ^ @@ -3055,7 +3055,7 @@ Cannot find member `a` on `int`. Error ---------------------------------- DUMMY.sam:1:10-1:11 -Cannot find member `b` on `int`. +Cannot resolve member `b` on `int`. 1| {let {a, b as c} = 1;} ^ @@ -3071,7 +3071,7 @@ Found 2 errors. r#" Error ---------------------------------- DUMMY.sam:1:10-1:11 -Cannot find member `d` on `A`. +Cannot resolve member `d` on `A`. 1| {let {a, d as c} = A.init();} ^ @@ -3251,7 +3251,7 @@ Found 5 errors. r#" Error --------------------------------------- A.sam:1:1-1:20 -Module `K` is not resolved. +Cannot resolve module `K`. 1| import { Z } from K ^^^^^^^^^^^^^^^^^^^ @@ -3614,7 +3614,7 @@ Type parameter name mismatch. Expected exact match of ``. Error ------------------------------------ A.sam:38:16-38:18 -Name `TA` is not resolved. +Cannot resolve name `TA`. 38| method f1(a: TA1, b: TB1): TC // has bound mismatch ^^ @@ -3654,7 +3654,7 @@ Type parameter name mismatch. Expected exact match of ``. Error ------------------------------------ A.sam:50:34-50:35 -Name `T` is not resolved. +Cannot resolve name `T`. 50| interface WithBound { method f(): int } ^ @@ -3662,7 +3662,7 @@ Name `T` is not resolved. Error ------------------------------------ A.sam:51:47-51:48 -Name `T` is not resolved. +Cannot resolve name `T`. 51| interface WithBound2 : WithBound { method f(): int } ^ @@ -3670,7 +3670,7 @@ Name `T` is not resolved. Error ------------------------------------ A.sam:52:19-52:25 -Name `DumDum` is not resolved. +Cannot resolve name `DumDum`. 52| class Z : DumDum {} // error ^^^^^^ @@ -3837,7 +3837,7 @@ Error --------------------- bounded-generics.sam:28:20-28:30 Error --------------------- bounded-generics.sam:29:21-29:22 -Name `T` is not resolved. +Cannot resolve name `T`. 29| class ImplTArg : T {} // error: T not resolved ^ diff --git a/crates/samlang-core/src/checker/main_checker.rs b/crates/samlang-core/src/checker/main_checker.rs index 2f8eae0b4..224f9e266 100644 --- a/crates/samlang-core/src/checker/main_checker.rs +++ b/crates/samlang-core/src/checker/main_checker.rs @@ -508,7 +508,7 @@ fn check_member_with_unresolved_tparams( }); (partially_checked_expr, vec![]) } else { - cx.error_set.report_member_missing_error( + cx.error_set.report_cannot_resolve_member_error( expression.field_name.loc, Description::NominalType { name: class_id, type_args: vec![] }, expression.field_name.name, @@ -940,7 +940,7 @@ fn check_match( let mapping_data_types = match unused_mappings.remove(&tag.name) { Some(types) => types, None => { - cx.error_set.report_member_missing_error( + cx.error_set.report_cannot_resolve_member_error( tag.loc, checked_matched_type.to_description(), tag.name, @@ -1113,7 +1113,7 @@ fn check_destructuring_pattern( { if let Some((field_type, is_public)) = field_mappings.get(&field_name.name) { if !is_public { - cx.error_set.report_member_missing_error( + cx.error_set.report_cannot_resolve_member_error( field_name.loc, pattern_type.to_description(), field_name.name, @@ -1131,7 +1131,7 @@ fn check_destructuring_pattern( }); continue; } - cx.error_set.report_member_missing_error( + cx.error_set.report_cannot_resolve_member_error( field_name.loc, pattern_type.to_description(), field_name.name, @@ -1208,7 +1208,7 @@ fn check_matching_pattern( { if let Some((field_type, is_public)) = field_mappings.get(&field_name.name) { if !is_public { - cx.error_set.report_member_missing_error( + cx.error_set.report_cannot_resolve_member_error( field_name.loc, pattern_type.to_description(), field_name.name, @@ -1226,7 +1226,7 @@ fn check_matching_pattern( }); continue; } - cx.error_set.report_member_missing_error( + cx.error_set.report_cannot_resolve_member_error( field_name.loc, pattern_type.to_description(), field_name.name, @@ -1254,7 +1254,11 @@ fn check_matching_pattern( let Some((tag_order, resolved_enum)) = cx.resolve_enum_definitions(pattern_type).into_iter().find_position(|e| e.name == tag.name) else { - cx.error_set.report_member_missing_error(tag.loc, pattern_type.to_description(), tag.name); + cx.error_set.report_cannot_resolve_member_error( + tag.loc, + pattern_type.to_description(), + tag.name, + ); let type_ = Rc::new(Type::Any(Reason::new(*loc, Some(*loc)), false)); return pattern::MatchingPattern::Variant(pattern::VariantPattern { loc: *loc, diff --git a/crates/samlang-core/src/errors.rs b/crates/samlang-core/src/errors.rs index e4a87d013..431bb7427 100644 --- a/crates/samlang-core/src/errors.rs +++ b/crates/samlang-core/src/errors.rs @@ -527,6 +527,7 @@ mod stackable_error_tests { #[derive(Debug, PartialEq, Eq, PartialOrd, Ord)] pub(crate) enum ErrorDetail { CannotResolveClass { module_reference: ModuleReference, name: PStr }, + CannotResolveMember { parent: Description, member: PStr }, CannotResolveModule { module_reference: ModuleReference }, CannotResolveName { name: PStr }, CyclicTypeDefinition { type_: Description }, @@ -535,7 +536,6 @@ pub(crate) enum ErrorDetail { IncompatibleSubType { lower: Description, upper: Description }, IncompatibleTypeKind { lower: Description, upper: Description }, InvalidSyntax(String), - MemberMissing { parent: Description, member: PStr }, MissingClassMemberDefinitions { missing_definitions: Vec }, MissingExport { module_reference: ModuleReference, name: PStr }, NameAlreadyBound { name: PStr, old_loc: Location }, @@ -549,19 +549,26 @@ impl ErrorDetail { fn push_to_printable_stream<'a>(&'a self, printable_stream: &mut PrintableStream<'a>) { match self { ErrorDetail::CannotResolveClass { module_reference: _, name } => { - printable_stream.push_text("Class `"); + printable_stream.push_text("Cannot resolve class `"); printable_stream.push_pstr(name); - printable_stream.push_text("` is not resolved."); + printable_stream.push_text("`."); + } + ErrorDetail::CannotResolveMember { parent, member } => { + printable_stream.push_text("Cannot resolve member `"); + printable_stream.push_pstr(member); + printable_stream.push_text("` on `"); + printable_stream.push_description(parent); + printable_stream.push_text("`."); } ErrorDetail::CannotResolveModule { module_reference } => { - printable_stream.push_text("Module `"); + printable_stream.push_text("Cannot resolve module `"); printable_stream.push_mod_ref(module_reference); - printable_stream.push_text("` is not resolved."); + printable_stream.push_text("`."); } ErrorDetail::CannotResolveName { name } => { - printable_stream.push_text("Name `"); + printable_stream.push_text("Cannot resolve name `"); printable_stream.push_pstr(name); - printable_stream.push_text("` is not resolved."); + printable_stream.push_text("`."); } ErrorDetail::CyclicTypeDefinition { type_ } => { printable_stream.push_text("Type `"); @@ -597,13 +604,6 @@ impl ErrorDetail { ErrorDetail::InvalidSyntax(reason) => { printable_stream.push_text(reason); } - ErrorDetail::MemberMissing { parent, member } => { - printable_stream.push_text("Cannot find member `"); - printable_stream.push_pstr(member); - printable_stream.push_text("` on `"); - printable_stream.push_description(parent); - printable_stream.push_text("`."); - } ErrorDetail::MissingClassMemberDefinitions { missing_definitions } => { printable_stream.push_text("The following members must be implemented for the class:"); for tag in missing_definitions { @@ -848,6 +848,15 @@ impl ErrorSet { self.errors.insert(CompileTimeError { location, detail }); } + pub(crate) fn report_cannot_resolve_member_error( + &mut self, + loc: Location, + parent: Description, + member: PStr, + ) { + self.report_error(loc, ErrorDetail::CannotResolveMember { parent, member }) + } + pub(crate) fn report_cannot_resolve_module_error( &mut self, loc: Location, @@ -912,15 +921,6 @@ impl ErrorSet { self.report_error(loc, ErrorDetail::InvalidSyntax(reason)) } - pub(crate) fn report_member_missing_error( - &mut self, - loc: Location, - parent: Description, - member: PStr, - ) { - self.report_error(loc, ErrorDetail::MemberMissing { parent, member }) - } - pub(crate) fn report_missing_class_member_definition_error( &mut self, loc: Location, @@ -1028,7 +1028,7 @@ mod tests { assert_eq!( r#"Error ------------------------------------ DUMMY.sam:0:0-0:0 -Module `DUMMY` is not resolved. +Cannot resolve module `DUMMY`. Found 1 error. @@ -1037,7 +1037,7 @@ Found 1 error. error_set.pretty_print_error_messages_no_frame(&heap) ); assert_eq!( - (Location::dummy(), "Module `DUMMY` is not resolved.".to_string(), vec![]), + (Location::dummy(), "Cannot resolve module `DUMMY`.".to_string(), vec![]), error_set.errors()[0].to_ide_format(&heap) ); @@ -1069,13 +1069,13 @@ Found 1 error. r#" Error ------------------------------------ DUMMY.sam:0:0-0:0 -Module `DUMMY` is not resolved. +Cannot resolve module `DUMMY`. Error ------------------------------------------------------ Very/Very/Very/Very/Very/Very/Very/Very/Very/Very/Very/Very/Very/Very/Very/Long.sam:0:0-0:0 -Name `global` is not resolved. +Cannot resolve name `global`. Found 2 errors."# @@ -1104,7 +1104,7 @@ Found 2 errors."# builder.bool_type().to_description(), ); error_set.report_invalid_syntax_error(Location::dummy(), "bad code".to_string()); - error_set.report_member_missing_error( + error_set.report_cannot_resolve_member_error( Location::dummy(), Description::NominalType { name: heap.alloc_str_for_test("Foo"), type_args: vec![] }, heap.alloc_str_for_test("bar"), @@ -1146,12 +1146,17 @@ Found 2 errors."# let expected_errors = r#" Error ------------------------------------ DUMMY.sam:0:0-0:0 -Class `global` is not resolved. +Cannot resolve class `global`. + + +Error ------------------------------------ DUMMY.sam:0:0-0:0 + +Cannot resolve member `bar` on `Foo`. Error ------------------------------------ DUMMY.sam:0:0-0:0 -Module `DUMMY` is not resolved. +Cannot resolve module `DUMMY`. Error ------------------------------------ DUMMY.sam:0:0-0:0 @@ -1179,11 +1184,6 @@ Error ------------------------------------ DUMMY.sam:0:0-0:0 bad code -Error ------------------------------------ DUMMY.sam:0:0-0:0 - -Cannot find member `bar` on `Foo`. - - Error ------------------------------------ DUMMY.sam:0:0-0:0 The following members must be implemented for the class: @@ -1234,7 +1234,7 @@ There is not enough context information to decide the type of this expression. Error ------------------------------------------------------ Very/Very/Very/Very/Very/Very/Very/Very/Very/Very/Very/Very/Very/Very/Very/Long.sam:0:0-0:0 -Name `global` is not resolved. +Cannot resolve name `global`. Found 17 errors. diff --git a/crates/samlang-core/src/integration_tests.rs b/crates/samlang-core/src/integration_tests.rs index 97ef947e3..4890c482b 100644 --- a/crates/samlang-core/src/integration_tests.rs +++ b/crates/samlang-core/src/integration_tests.rs @@ -486,7 +486,7 @@ class Main { let expected_errors = r#" Error ---------------- access-private-member.sam:12:15-12:16 -Cannot find member `b` on `A`. +Cannot resolve member `b` on `A`. 12| let _ = A.b(); ^ @@ -634,7 +634,7 @@ Error --------------------- bounded-generics.sam:28:20-28:30 Error --------------------- bounded-generics.sam:29:21-29:22 -Name `T` is not resolved. +Cannot resolve name `T`. 29| class ImplTArg : T {} // error: T not resolved ^ @@ -658,7 +658,7 @@ Error ---------------- call-interface-function.sam:3:33-3:36 Error ---------------- call-interface-function.sam:3:45-3:48 -Class `Foo` is not resolved. +Cannot resolve class `Foo`. 3| class Ouch { function call(foo: Foo): int = Foo.bar() } ^^^ @@ -934,7 +934,7 @@ Error ------------ illegal-binary-operations.sam:27:35-27:64 Error --------- illegal-private-field-access.sam:15:13-15:14 -Cannot find member `b` on `Fields`. +Cannot resolve member `b` on `Fields`. 15| let {a, b} = f; ^ @@ -942,7 +942,7 @@ Cannot find member `b` on `Fields`. Error --------- illegal-private-field-access.sam:17:15-17:16 -Cannot find member `b` on `Fields`. +Cannot resolve member `b` on `Fields`. 17| let _ = f.b; ^ @@ -1015,7 +1015,7 @@ Name `a` collides with a previously defined name at [1]. Error --------------------------- illegal-this.sam:5:13-5:17 -Name `this` is not resolved. +Cannot resolve name `this`. 5| let _ = this; ^^^^ @@ -1109,7 +1109,7 @@ Error ------------------------ simple-mismatch.sam:4:30-4:34 Error ------------------------- undefined-type.sam:3:20-3:30 -Name `HelloWorld` is not resolved. +Cannot resolve name `HelloWorld`. 3| function main(): HelloWorld = 1 ^^^^^^^^^^ @@ -1135,7 +1135,7 @@ Error ------------------------- undefined-type.sam:3:33-3:34 Error --------------------- undefined-variable.sam:3:26-3:36 -Name `helloWorld` is not resolved. +Cannot resolve name `helloWorld`. 3| function main(): Str = helloWorld ^^^^^^^^^^ diff --git a/crates/samlang-core/src/services/api_tests.rs b/crates/samlang-core/src/services/api_tests.rs index d5a74bc07..bdf031bdd 100644 --- a/crates/samlang-core/src/services/api_tests.rs +++ b/crates/samlang-core/src/services/api_tests.rs @@ -264,7 +264,7 @@ class Test2(val a: int) { r#" Error ---------------------------------- Test1.sam:3:10-3:11 -Cannot find member `c` on `int`. +Cannot resolve member `c` on `int`. 3| let {c, b} = 1 + 2; ^ @@ -272,7 +272,7 @@ Cannot find member `c` on `int`. Error ---------------------------------- Test1.sam:3:13-3:14 -Cannot find member `b` on `int`. +Cannot resolve member `b` on `int`. 3| let {c, b} = 1 + 2; ^ @@ -280,7 +280,7 @@ Cannot find member `b` on `int`. Error ------------------------------------ Test1.sam:5:5-5:6 -Name `a` is not resolved. +Cannot resolve name `a`. 5| a + b + c ^ @@ -339,7 +339,7 @@ class Test1(val a: int) { r#" Error -------------------------------- Test1.sam:12:15-12:16 -Name `c` is not resolved. +Cannot resolve name `c`. 12| let _ = c; ^ diff --git a/crates/samlang-core/src/services/server_state.rs b/crates/samlang-core/src/services/server_state.rs index a5b282a74..4acf7a0e0 100644 --- a/crates/samlang-core/src/services/server_state.rs +++ b/crates/samlang-core/src/services/server_state.rs @@ -501,7 +501,7 @@ class Test2 { r#" Error ------------------------------------ Test.sam:2:1-2:18 -Module `B` is not resolved. +Cannot resolve module `B`. 2| import {A} from B ^^^^^^^^^^^^^^^^^ @@ -528,7 +528,7 @@ Name `A` collides with a previously defined name at [1]. r#" Error ------------------------------------ Test.sam:1:1-1:18 -Module `A` is not resolved. +Cannot resolve module `A`. 1| import {A} from A ^^^^^^^^^^^^^^^^^ @@ -555,7 +555,7 @@ Name `A` collides with a previously defined name at [1]. r#" Error ------------------------------------ Test.sam:1:1-1:18 -Module `A` is not resolved. +Cannot resolve module `A`. 1| import {A} from A ^^^^^^^^^^^^^^^^^