ladybird icon indicating copy to clipboard operation
ladybird copied to clipboard

AK: Rework windows error types to store the code

Open R-Goc opened this issue 7 months ago • 6 comments

This commit makes windows errors store the code instead of being a formatted string literat. This will allow comparing against the error, and checkign what it is. The formatting is now done in the formatter, and moved to an implementation file to avoid polluting headers with windows.h.

R-Goc avatar Apr 28 '25 10:04 R-Goc

what happened in this CI? I see the error but I don't understand why it happens on only these two builds, or why it happens tbh.

R-Goc avatar Apr 28 '25 13:04 R-Goc

This is a difference in behavior in clang and gcc. Gcc is unable to tell that all control flow paths are handled. Adding a default case fixes it, but breaks -Wswitch. Adding unreachable fixes it. repro: https://godbolt.org/z/1n4WPna4r

R-Goc avatar Apr 28 '25 13:04 R-Goc

Compare view is useless due to rebase, but everything looks fine. I'll compile locally and check.

R-Goc avatar May 08 '25 11:05 R-Goc

This is the reason that I did that weird thing with the format reference:

D:\lib\ladybird\AK\Format.cpp(1102,39): error: call to non-static member function without an object argument
 1102 |         return Formatter<StringView>::format(builder, string->view());
      |                ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~
D:\lib\ladybird\AK\Format.cpp(1116,41): error: call to non-static member function without an object argument
 1116 |         return Formatter<FormatString>::format(builder, "Error {:08x} when formatting code {:08x}"sv, format_error, code);
      |                ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~
D:\lib\ladybird\AK\Format.cpp(1121,35): error: call to non-static member function without an object argument
 1121 |     return Formatter<StringView>::format(builder, string_in_map.view());
      |            ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~
3 errors generated.

It simply doesn't compile this way. Edit: fixed typo, in code, makes the error cleaner.

R-Goc avatar May 08 '25 12:05 R-Goc

Doing this solves the issue, but exposes it in a header.

diff --git a/AK/Format.cpp b/AK/Format.cpp
index 61508cb5cd..6526cb0f9f 100644
--- a/AK/Format.cpp
+++ b/AK/Format.cpp
@@ -1092,7 +1092,7 @@ void vout(FILE* file, StringView fmtstr, TypeErasedFormatParams& params, bool ne
     }
 }
 #if defined(AK_OS_WINDOWS)
-static ErrorOr<void> format_windows_error(FormatBuilder& builder, Error const& error)
+ErrorOr<void> Formatter<Error>::format_windows_error(FormatBuilder& builder, Error const& error)
 {
     thread_local HashMap<u32, ByteString> windows_errors;

@@ -1113,7 +1113,7 @@ static ErrorOr<void> format_windows_error(FormatBuilder& builder, Error const& e
         nullptr);
     if (size == 0) {
         auto format_error = GetLastError();
-        return Formatter::<FormatString>::format(builder, "Error {:08x} when formatting code {:08x}"sv, format_error, code);
+        return Formatter<FormatString>::format(builder, "Error {:08x} when formatting code {:08x}"sv, format_error, code);
     }

     auto& string_in_map = windows_errors.ensure(code, [message, size] { return ByteString { message, size }; });
diff --git a/AK/Format.h b/AK/Format.h
index d7514fad44..ff36e2d4eb 100644
--- a/AK/Format.h
+++ b/AK/Format.h
@@ -738,6 +738,7 @@ struct Formatter<FormatString> : Formatter<StringView> {
 template<>
 struct Formatter<Error> : Formatter<FormatString> {
     ErrorOr<void> format(FormatBuilder& builder, Error const& error);
+    ErrorOr<void> format_windows_error(FormatBuilder& builder, Error const& error);
 };

 template<typename T, typename ErrorType>

R-Goc avatar May 08 '25 12:05 R-Goc

Should I push it or is there another way around it?

R-Goc avatar May 08 '25 13:05 R-Goc