From e32a1768b937249ce7a1d0a3797ef1ebd4a8a573 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Inf=C3=BChr?= Date: Fri, 15 Nov 2024 14:17:35 +0100 Subject: [PATCH] frontend: Remove code_methodXXX test helpers --- dora-frontend/src/generator/tests.rs | 90 +++++++++++----------------- dora-frontend/src/stdlib_lookup.rs | 4 +- 2 files changed, 37 insertions(+), 57 deletions(-) diff --git a/dora-frontend/src/generator/tests.rs b/dora-frontend/src/generator/tests.rs index f8febdac4..2caaee645 100644 --- a/dora-frontend/src/generator/tests.rs +++ b/dora-frontend/src/generator/tests.rs @@ -38,29 +38,6 @@ fn position(code: &'static str) -> Vec<(u32, u32)> { }) } -fn code_method(code: &'static str) -> Vec { - code_method_with_class_name(code, "Foo") -} - -fn code_method_with_class_name(code: &'static str, class_name: &'static str) -> Vec { - test::check_valid(code, |sa| { - let fct_id = cls_method_by_name(sa, class_name, "f", false) - .unwrap_or_else(|| panic!("no function `f` in Class `{}`.", class_name)); - let fct = generate_fct_id(sa, fct_id); - build(&fct) - }) -} - -fn code_method_with_struct_name(code: &'static str, struct_name: &'static str) -> Vec { - test::check_valid(code, |sa| { - let fct_id = struct_method_by_name(sa, struct_name, "f", false) - .unwrap_or_else(|| panic!("no function `f` in Class `{}`.", struct_name)); - let fct = generate_fct_id(sa, fct_id); - build(&fct) - }) -} - -#[allow(unused)] fn setup(code: &'static str) -> Sema { let args: SemaFlags = SemaFlags::for_test(code); let mut sa = Sema::new(args); @@ -71,7 +48,6 @@ fn setup(code: &'static str) -> Sema { sa } -#[allow(unused)] fn bc(sa: &Sema, path: &str) -> Vec { let fct_id = lookup_fct(sa, path); let fct = generate_fct_id(sa, fct_id); @@ -1096,14 +1072,16 @@ fn gen_expr_assign() { #[test] fn gen_expr_self() { - let result = code_method("class Foo impl Foo { fn f(): Foo { return self; } }"); + let sa = setup("class Foo impl Foo { fn f(): Foo { return self; } }"); + let result = bc(&sa, "::Foo#f"); let expected = vec![Ret(r(0))]; assert_eq!(expected, result); } #[test] fn gen_expr_self_assign() { - let result = code_method("class Foo impl Foo { fn f() { let x = self; } }"); + let sa = setup("class Foo impl Foo { fn f() { let x = self; } }"); + let result = bc(&sa, "::Foo#f"); let expected = vec![Mov(r(1), r(0)), Ret(r(2))]; assert_eq!(expected, result); } @@ -3103,168 +3081,168 @@ fn gen_position_new_object_with_multiple_args() { #[test] fn gen_self_for_bool() { - let result = code_method_with_struct_name( + let sa = setup( "trait MyId { fn f(): Self; } impl MyId for Bool { fn f(): Bool { return self; } } ", - "Bool", ); + let result = bc(&sa, "::MyId for std::primitives::Bool#f"); let expected = vec![Ret(r(0))]; assert_eq!(expected, result); } #[test] fn gen_self_for_uint8() { - let result = code_method_with_struct_name( + let sa = setup( "trait MyId { fn f(): Self; } impl MyId for UInt8 { fn f(): UInt8 { return self; } } ", - "UInt8", ); + let result = bc(&sa, "::MyId for std::primitives::UInt8#f"); let expected = vec![Ret(r(0))]; assert_eq!(expected, result); } #[test] -fn gen_self_for_int() { - let result = code_method_with_struct_name( +fn gen_self_for_int32() { + let sa = setup( "trait MyId { fn f(): Self; } impl MyId for Int32 { fn f(): Int32 { return self; } } ", - "Int32", ); + let result = bc(&sa, "::MyId for std::primitives::Int32#f"); let expected = vec![Ret(r(0))]; assert_eq!(expected, result); } #[test] fn gen_self_for_int64() { - let result = code_method_with_struct_name( + let sa = setup( "trait MyId { fn f(): Self; } impl MyId for Int64 { fn f(): Int64 { return self; } } ", - "Int64", ); + let result = bc(&sa, "::MyId for std::primitives::Int64#f"); let expected = vec![Ret(r(0))]; assert_eq!(expected, result); } #[test] fn gen_self_for_float32() { - let result = code_method_with_struct_name( + let sa = setup( "trait MyId { fn f(): Self; } impl MyId for Float32 { fn f(): Float32 { return self; } } ", - "Float32", ); + let result = bc(&sa, "::MyId for std::primitives::Float32#f"); let expected = vec![Ret(r(0))]; assert_eq!(expected, result); } #[test] fn gen_self_for_float64() { - let result = code_method_with_struct_name( + let sa = setup( "trait MyId { fn f(): Self; } impl MyId for Float64 { fn f(): Float64 { return self; } } ", - "Float64", ); + let result = bc(&sa, "::MyId for std::primitives::Float64#f"); let expected = vec![Ret(r(0))]; assert_eq!(expected, result); } #[test] fn gen_self_for_string() { - let result = code_method_with_class_name( + let sa = setup( "trait MyId { fn f(): Self; } impl MyId for String { fn f(): String { return self; } } ", - "String", ); + let result = bc(&sa, "::MyId for std::string::String#f"); let expected = vec![Ret(r(0))]; assert_eq!(expected, result); } #[test] fn gen_self_assign_for_bool() { - let result = code_method_with_struct_name( + let sa = setup( "trait MyId { fn f(); } impl MyId for Bool { fn f() { let x = self; } } ", - "Bool", ); + let result = bc(&sa, "::MyId for std::primitives::Bool#f"); let expected = vec![Mov(r(1), r(0)), Ret(r(2))]; assert_eq!(expected, result); } #[test] fn gen_self_assign_for_uint8() { - let result = code_method_with_struct_name( + let sa = setup( "trait MyId { fn f(); } impl MyId for UInt8 { fn f() { let x = self; } } ", - "UInt8", ); + let result = bc(&sa, "::MyId for std::primitives::UInt8#f"); let expected = vec![Mov(r(1), r(0)), Ret(r(2))]; assert_eq!(expected, result); } #[test] -fn gen_self_assign_for_int() { - let result = code_method_with_struct_name( +fn gen_self_assign_for_int32() { + let sa = setup( "trait MyId { fn f(); } impl MyId for Int32 { fn f() { let x = self; } } ", - "Int32", ); + let result = bc(&sa, "::MyId for std::primitives::Int32#f"); let expected = vec![Mov(r(1), r(0)), Ret(r(2))]; assert_eq!(expected, result); } #[test] fn gen_self_assign_for_int64() { - let result = code_method_with_struct_name( + let sa = setup( "trait MyId { fn f(); } impl MyId for Int64 { fn f() { let x = self; } } ", - "Int64", ); + let result = bc(&sa, "::MyId for std::primitives::Int64#f"); let expected = vec![Mov(r(1), r(0)), Ret(r(2))]; assert_eq!(expected, result); } #[test] fn gen_self_assign_for_float32() { - let result = code_method_with_struct_name( + let sa = setup( "trait MyId { fn f(); } impl MyId for Float32 { fn f() { let x = self; } } ", - "Float32", ); + let result = bc(&sa, "::MyId for std::primitives::Float32#f"); let expected = vec![Mov(r(1), r(0)), Ret(r(2))]; assert_eq!(expected, result); } #[test] fn gen_self_assign_for_float64() { - let result = code_method_with_struct_name( + let sa = setup( "trait MyId { fn f(); } impl MyId for Float64 { fn f() { let x = self; } } ", - "Float64", ); + let result = bc(&sa, "::MyId for std::primitives::Float64#f"); let expected = vec![Mov(r(1), r(0)), Ret(r(2))]; assert_eq!(expected, result); } #[test] fn gen_self_assign_for_string() { - let result = code_method_with_class_name( + let sa = setup( "trait MyId { fn f(); } impl MyId for String { fn f() { let x = self; } } ", - "String", ); + let result = bc(&sa, "::MyId for std::string::String#f"); let expected = vec![Mov(r(1), r(0)), Ret(r(2))]; assert_eq!(expected, result); } diff --git a/dora-frontend/src/stdlib_lookup.rs b/dora-frontend/src/stdlib_lookup.rs index 2c765e4cb..ee2fcc876 100644 --- a/dora-frontend/src/stdlib_lookup.rs +++ b/dora-frontend/src/stdlib_lookup.rs @@ -259,7 +259,9 @@ pub fn resolve_path(sa: &Sema, name: &str) -> SymbolKind { }; let package_name = path.next().expect("missing package"); - let mut sym = if let Some(package_id) = sa.package_names.get(package_name) { + let mut sym = if package_name == "" { + SymbolKind::Module(sa.program_module_id()) + } else if let Some(package_id) = sa.package_names.get(package_name) { let package = &sa.packages[*package_id]; SymbolKind::Module(package.top_level_module_id()) } else {