Advertisement
RIC21000

Zig LLVM compile error bug reproduction

Aug 19th, 2024
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 6.04 KB | None | 0 0
  1. const http = struct {
  2.     pub const Version = undefined;
  3.  
  4.     const Headers = undefined;
  5.  
  6.     pub const request_module = struct {
  7.         pub fn Request(comptime state_type: type, comptime path_arguments_type: type) type {
  8.             _ = state_type;
  9.             _ = path_arguments_type;
  10.             return struct {
  11.                 const Self = undefined;
  12.             };
  13.         }
  14.     };
  15.  
  16.     pub const StatusCode = undefined;
  17.  
  18.     pub const Request = request_module.Request;
  19.  
  20.     pub const Response = struct {
  21.         pub fn init(version: Version, status_code: StatusCode, headers: Headers, body: ?[]const u8) Response {
  22.             _ = version;
  23.             _ = status_code;
  24.             _ = headers;
  25.             _ = body;
  26.             @trap();
  27.         }
  28.     };
  29.  
  30.     pub const Method = enum {
  31.         get,
  32.     };
  33.  
  34.     const handler_module = struct {
  35.         pub const PathArgumentsLen = 200;
  36.         pub const PathArguments = stack_hashmap.StackStringHashMap(void, PathArgumentsLen);
  37.  
  38.         pub fn HandlerFn(comptime state_type: type) type {
  39.             return *const fn (req: *Request(state_type, void), args: []const []const u8) Response;
  40.         }
  41.  
  42.         pub fn MethodHandlers(comptime state_type: type) type {
  43.             return stack_hashmap.StackHashMap(Method, HandlerFn(state_type), std.meta.fields(Method).len);
  44.         }
  45.     };
  46.  
  47.     pub fn NodeAllocator(comptime state_type: type, comptime node_count: usize) type {
  48.         return struct {
  49.             data: [node_count]Node(state_type, node_count) = undefined,
  50.  
  51.             const Self = @This();
  52.  
  53.             pub fn init() Self {
  54.                 return .{};
  55.             }
  56.         };
  57.     }
  58.  
  59.     const stack_hashmap = struct {
  60.         pub fn StackStringHashMap(comptime V: type, comptime size: usize) type {
  61.             return StackHashMap([]const u8, V, size);
  62.         }
  63.  
  64.         pub fn StackHashMap(comptime K: type, comptime V: type, comptime size: usize) type {
  65.             return struct {
  66.                 keys: [size]K = undefined,
  67.  
  68.                 const Self = @This();
  69.  
  70.                 pub fn init() Self {
  71.                     return .{};
  72.                 }
  73.  
  74.                 /// This method fails when there is no memory available anymore
  75.                 pub fn put(self: *Self, key: []const u8, value: V) !void {
  76.                     _ = self;
  77.                     _ = key;
  78.                     _ = value;
  79.                     @trap();
  80.                 }
  81.             };
  82.         }
  83.     };
  84.  
  85.     const route_module = struct {
  86.         pub const Route = []RouteSegment;
  87.  
  88.         pub const RouteSegment = struct {};
  89.  
  90.         pub fn parse_route(route: []const u8, out: Route) Route {
  91.             var index: usize = 0;
  92.  
  93.             var split = std.mem.splitScalar(u8, route[1..], '/');
  94.  
  95.             while (split.next()) |segment| : (index += 1) {
  96.                 const is_arg = if (segment[0] == '$') true else blk: {
  97.                     break :blk false;
  98.                 };
  99.                 _ = is_arg;
  100.             }
  101.  
  102.             return out[0..index];
  103.         }
  104.     };
  105.  
  106.     pub fn Node(comptime state_type: type, comptime node_children_count: usize) type {
  107.         return struct {
  108.             name: []const u8,
  109.             is_arg: bool,
  110.             handlers: handler_module.MethodHandlers(state_type),
  111.             children: ?Children,
  112.  
  113.             const Self = @This();
  114.  
  115.             const Children = stack_hashmap.StackStringHashMap(*Self, node_children_count);
  116.  
  117.             pub fn put(
  118.                 comptime self: *Self,
  119.                 comptime node_allocator: *NodeAllocator(state_type, 400 * 400),
  120.                 comptime route: route_module.Route,
  121.                 comptime method: Method,
  122.                 comptime handler: *const fn (req: *anyopaque) Response,
  123.                 comptime arguments: *handler_module.PathArguments,
  124.             ) !void {
  125.                 //If this is the last segment of the route
  126.                 _ = route;
  127.                 _ = self;
  128.                 _ = method;
  129.                 _ = handler;
  130.                 _ = arguments;
  131.                 _ = node_allocator;
  132.             }
  133.         };
  134.     }
  135.     pub fn Router(comptime state_type: type, comptime node_count: usize) type {
  136.         return struct {
  137.             root_node: Node(state_type, node_count),
  138.             state: state_type,
  139.             node_allocator: NodeAllocator(state_type, node_count),
  140.  
  141.             const Self = @This();
  142.  
  143.             pub fn init(comptime state: state_type) Self {
  144.                 return .{
  145.                     .root_node = .{
  146.                         .is_arg = false,
  147.                         .name = "/",
  148.                         .handlers = handler_module.MethodHandlers(state_type).init(),
  149.                         .children = null,
  150.                     },
  151.                     .state = state,
  152.                     .node_allocator = NodeAllocator(state_type, node_count).init(),
  153.                 };
  154.             }
  155.  
  156.             pub fn add_route(comptime self: *Self, comptime route: []const u8, comptime method: Method, comptime handler: *const fn (req: *anyopaque) Response) !void {
  157.                 comptime var route_out: [300]route_module.RouteSegment = undefined;
  158.  
  159.                 const route_out_slice = route_module.parse_route(route, &route_out);
  160.  
  161.                 comptime var arguments = handler_module.PathArguments.init();
  162.  
  163.                 comptime try self.root_node.put(&self.node_allocator, route_out_slice, method, handler, &arguments);
  164.             }
  165.         };
  166.     }
  167. };
  168. const std = @import("std");
  169.  
  170. pub fn main() !void {
  171.     var buf: [4_294_967]u8 = undefined;
  172.  
  173.     var fba = std.heap.FixedBufferAllocator.init(&buf);
  174.  
  175.     const allocator = fba.allocator();
  176.     _ = allocator;
  177.  
  178.     comptime var router = http.Router(void, 400 * 400).init({});
  179.  
  180.     comptime try router.add_route("/posts/$user", http.Method.get, @ptrCast(@alignCast(&struct {
  181.         fn a(req: *http.Request(void, void)) http.Response {
  182.             _ = req;
  183.             @trap();
  184.         }
  185.     }.a)));
  186.  
  187.     const router_const = router;
  188.     _ = router_const;
  189. }
  190.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement