Fix bug with defer assignment in macro #1807.

This commit is contained in:
Christoffer Lerno
2025-01-11 20:07:16 +01:00
parent 8a9edc02b6
commit f2e69f8fdc
7 changed files with 201 additions and 28 deletions

View File

@@ -0,0 +1,11 @@
macro void @foo(int #a)
{
var x = float.#a; // #error: cannot already be resolved
}
fn void main()
{
int inf;
@foo(inf);
}

View File

@@ -0,0 +1,45 @@
import std::io;
struct Version {
uint major;
uint minor;
}
fn void main()
{
String s = "123.456";
Version v;
v.major = @read_int(s);
s = s[1..];
v.minor = @read_int(s);
io::printfn("Version{.major = %d, .minor = %d}", v.major, v.minor);
}
<*
@require values::@is_lvalue(#s)
*>
macro int @read_int(String #s)
{
int res = 0;
int i = 0;
String s = #s;
defer #s = s[i..]; // commenting out this lines leads to a successful compilation
while (i < s.len && s[i].is_space())
{
++i;
}
if (i >= s.len) return res;
char c;
while (i < s.len && (c = s[i]).is_digit())
{
res = (10 * res) + (c - '0');
++i;
}
return res;
}