Skip to content

Commit 980c76e

Browse files
authored
clang: error.Error (#48)
1 parent 370c6d6 commit 980c76e

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

internal/clang/struct.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,16 @@ func (g *Generator) emitMethodCall(sel *ast.SelectorExpr, call *ast.CallExpr) {
218218
named = recv.(*types.Named)
219219
}
220220

221+
// Error interface: err.Error() → errors_Error(err).
222+
// so_Error is a plain pointer (struct so_Error_*) without a vtable,
223+
// so it can't be dispatched through the generic interface path.
224+
if isErrorType(named) && sel.Sel.Name == "Error" {
225+
fmt.Fprintf(w, "errors_Error(")
226+
g.emitExpr(sel.X)
227+
fmt.Fprintf(w, ")")
228+
return
229+
}
230+
221231
// Interface method dispatch: s.Perim(2) → s.Perim(s.self, 2)
222232
if isInterfaceType(named) {
223233
g.emitExpr(sel.X)

internal/compiler/builtin/builtin.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,12 @@ static inline const char* errors_cstr(so_Error err) {
414414
return err ? err->msg : "<nil>";
415415
}
416416

417+
// errors_Error returns the error message as a so_String. Backs err.Error().
418+
static inline so_String errors_Error(so_Error err) {
419+
const char* s = errors_cstr(err);
420+
return (so_String){s, (so_int)strlen(s)};
421+
}
422+
417423
// panic aborts the program with the given message.
418424
#define so_panic(msg) \
419425
do { \

0 commit comments

Comments
 (0)