Commit 4838fce7 authored by Benjamin REED's avatar Benjamin REED

not really working build, started work on assets system but got distracted by bug in compiler

parent 8b8d5c51
import std::collections::map;
import std::collections::list;
fault AssetError {
ASSET_TYPE_NOT_REGISTERED,
INDEX_OUT_OF_BOUNDS,
}
distinct AssetId = iptr;
def AssetList = List(<any>);
def AssetMap = HashMap(<AssetId, AssetList>);
struct Assets {
AssetMap assets;
}
fn Assets* Assets.init(Assets* self) {
AssetMap map;
map.new_init();
return Assets {
.assets = map,
};
}
// TODO: require that Assets is init-ed (initted? (i-knitted???))
macro Assets.insert(&self, $Type, value) {
AssetId id = (AssetId)$Type.typeid;
if (self.assets.has_key(id)) {
self.assets[id]!.push((any)&value);
} else {
AssetList list; list.new_init();
list.push((any)&value);
self.assets[id] = list;
}
}
macro Assets.get(&self, $Type, idx) {
AssetId id = (AssetId)$Type.typeid;
if (!self.assets.has_key(id)) {
return AssetError.ASSET_TYPE_NOT_REGISTERED?;
}
if (self.assets[id]!.len() <= idx) {
return AssetError.INDEX_OUT_OF_BOUNDS;
}
return (($Type)self.assets[id]!.get(idx));
}
\ No newline at end of file
# VERSION 0.6.4 (Pre-release, Oct 28 2024 21:38:50), compiler target: x86_64-pc-linux-gnu.
The following code correctly identifies that the macro doesn't resolve to a good(?) return type, however on WSL Debian the compiler also produces a segfault and hangs for ten to thirty seconds.
To be clear, it works and finds the error, the bug I think is the segfault followed by some kind of loop which prevents ctrl-C quitting and even seems to resist SIGKILL, although it may be that I have simply been failing to identify its PID correctly.
(The bug is a missing question mark in the second return statement, preventing the function from resolving a single return type)
```
macro Assets.get(&self, $Type, idx) {
AssetId id = (AssetId)$Type.typeid;
if (!self.assets.has_key(id)) {
return AssetError.ASSET_TYPE_NOT_REGISTERED?;
}
if (self.assets[id]!.len() <= idx) {
return AssetError.INDEX_OUT_OF_BOUNDS; // <-- '?' here to resolve return type as an optional
}
return (($Type)self.assets[id]!.get(idx));
}
```
The bug does not occur on Windows 10, instead the program correctly exits after printing the error:
```
48: return AssetError.INDEX_OUT_OF_BOUNDS;
49: }
50:
51: return (($Type)self.assets[id]!.get(idx));
^
(//wsl.localhost/Debian/home/.../assets.c3:51:9) Error: Cannot find a common parent type of 'Image*' and 'AssetError'
13:
14: assets.insert(Image, img)!!;
15:
16: Image imgtwo = assets.get(Image*, 0);
^^^^^^^^^^^^^^^^^^^^^
(//wsl.localhost/Debian/home/.../test.c3:16:17) Note: Inlined from here.
```
Using GDB I found the segfault to occur in sema_expr.c on line 1959, but I couldn't find the actual source of the error, or anything that made it hit an infinite loop. Even in GDB I am unable to send SIGINT to the program and pause it.
(The line where it apparently segfaults:)
```
// 4. Find the max of the old and new
Type *max = type_find_max_type(common_type, rtype);
// 5. No match -> error.
if (!max)
{
ASSERT0(return_stmt); // <-- line 1959
SEMA_ERROR(return_stmt, "Cannot find a common parent type of %s and %s");
// ...
return NULL;
}
```
From what I can find, the compiler doesn't handle interrupts (?), so the problem is more likely very platform specific.
The ASSERT0() macro seems to end up in the 'exit_compiler' function (which also references ASSERT0, which could lead to a circular infinite calling of the exit_compiler function?) which uses longjmp and I'm afraid I do not know how longjmp works 🥲 so I can't really see if that's where the issue is, although I doubt it.
Apologies if this is too platform-specific. (and for very long issue)
\ No newline at end of file
import std::io;
import assets;
struct Image {
int width, height;
}
fn void main() {
Assets assets; assets.init();
Image img = {500, 200};
assets.insert(Image, img)!!;
Image imgtwo = assets.get(Image*, 0);
io::printfn("Image is %d by %d", imgtwo.width, imgtwo.height);
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment