From 287de8f49956da3b5915ec292e2005c0f020b279 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Mon, 5 Dec 2022 17:38:27 +0100 Subject: [PATCH] Fix issue with aliasing `@`-macros. --- src/compiler/parse_global.c | 12 +++++++++++- src/version.h | 2 +- test/test_suite/define/test_at.c3 | 17 +++++++++++++++++ test/test_suite/define/test_at_alias.c3 | 16 ++++++++++++++++ test/test_suite2/define/test_at.c3 | 17 +++++++++++++++++ test/test_suite2/define/test_at_alias.c3 | 16 ++++++++++++++++ 6 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 test/test_suite/define/test_at.c3 create mode 100644 test/test_suite/define/test_at_alias.c3 create mode 100644 test/test_suite2/define/test_at.c3 create mode 100644 test/test_suite2/define/test_at_alias.c3 diff --git a/src/compiler/parse_global.c b/src/compiler/parse_global.c index bb78dc5ed..d1a633241 100644 --- a/src/compiler/parse_global.c +++ b/src/compiler/parse_global.c @@ -1747,7 +1747,7 @@ static inline Decl *parse_define_ident(ParseContext *c, Visibility visibility) // 2. At this point we expect an ident or a const token. // since the Type is handled. TokenType alias_type = c->tok; - if (alias_type != TOKEN_IDENT && alias_type != TOKEN_CONST_IDENT) + if (alias_type != TOKEN_IDENT && alias_type != TOKEN_CONST_IDENT && alias_type != TOKEN_AT_IDENT) { if (token_is_any_type(alias_type)) { @@ -1799,6 +1799,16 @@ static inline Decl *parse_define_ident(ParseContext *c, Visibility visibility) SEMA_ERROR_HERE("Expected a constant name here."); return poisoned_decl; } + if (alias_type == TOKEN_IDENT && c->tok == TOKEN_AT_IDENT) + { + SEMA_ERROR(decl, "A name with '@' prefix cannot be aliased to a name without '@', try adding a '@' before '%s'.", decl->name); + return poisoned_decl; + } + if (alias_type == TOKEN_AT_IDENT && c->tok == TOKEN_IDENT) + { + SEMA_ERROR(decl, "An alias cannot use '@' if the aliased identifier doesn't, please remove the '@' symbol."); + return poisoned_decl; + } SEMA_ERROR_HERE("Expected a function or variable name here."); return poisoned_decl; } diff --git a/src/version.h b/src/version.h index 3c5126f2f..551b6af5c 100644 --- a/src/version.h +++ b/src/version.h @@ -1 +1 @@ -#define COMPILER_VERSION "0.3.105" \ No newline at end of file +#define COMPILER_VERSION "0.3.106" \ No newline at end of file diff --git a/test/test_suite/define/test_at.c3 b/test/test_suite/define/test_at.c3 new file mode 100644 index 000000000..520e9c591 --- /dev/null +++ b/test/test_suite/define/test_at.c3 @@ -0,0 +1,17 @@ +module foo; +import std::io; + +macro @hello(Type thing) { + io::printfln("%d", $sizeof(thing)); +} + +module bar; + +import private foo; + +define intHello = foo::@hello; // #error: cannot be aliased +define @intHello = foo::hello; // #error: cannot use + +fn void main(char[][] args) { + @intHello(42); +} \ No newline at end of file diff --git a/test/test_suite/define/test_at_alias.c3 b/test/test_suite/define/test_at_alias.c3 new file mode 100644 index 000000000..f0414ff44 --- /dev/null +++ b/test/test_suite/define/test_at_alias.c3 @@ -0,0 +1,16 @@ +module foo; +import std::io; + +macro @hello(Type thing) { + io::printfln("%d", $sizeof(thing)); +} + +module bar; + +import private foo; + +define @intHello = foo::@hello; + +fn void main(char[][] args) { + @intHello(42); +} \ No newline at end of file diff --git a/test/test_suite2/define/test_at.c3 b/test/test_suite2/define/test_at.c3 new file mode 100644 index 000000000..520e9c591 --- /dev/null +++ b/test/test_suite2/define/test_at.c3 @@ -0,0 +1,17 @@ +module foo; +import std::io; + +macro @hello(Type thing) { + io::printfln("%d", $sizeof(thing)); +} + +module bar; + +import private foo; + +define intHello = foo::@hello; // #error: cannot be aliased +define @intHello = foo::hello; // #error: cannot use + +fn void main(char[][] args) { + @intHello(42); +} \ No newline at end of file diff --git a/test/test_suite2/define/test_at_alias.c3 b/test/test_suite2/define/test_at_alias.c3 new file mode 100644 index 000000000..f0414ff44 --- /dev/null +++ b/test/test_suite2/define/test_at_alias.c3 @@ -0,0 +1,16 @@ +module foo; +import std::io; + +macro @hello(Type thing) { + io::printfln("%d", $sizeof(thing)); +} + +module bar; + +import private foo; + +define @intHello = foo::@hello; + +fn void main(char[][] args) { + @intHello(42); +} \ No newline at end of file