authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-16 20:23:15-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-16 20:23:15-04:00
log54820a3005f25e1e542d8add39f184ed1e1eddba
tree0a08d03caa189f49c063c3e028f2eb958e214e20
parent017ecc5148da3f3f50f5666d635c22dfb6bfffb2

fix source not being loaded when printing errors


1 files changed, 41 insertions(+), 30 deletions(-)

src-self-hosted/Module.zig+41-30
......@@ -307,7 +307,7 @@ pub const Scope = struct {
307307 /// Relative to the owning package's root_src_dir.
308308 /// Reference to external memory, not owned by ZIRModule.
309309 sub_file_path: []const u8,
310 source: union {
310 source: union(enum) {
311311 unloaded: void,
312312 bytes: [:0]const u8,
313313 },
......@@ -320,7 +320,7 @@ pub const Scope = struct {
320320 unloaded_success,
321321 unloaded_parse_failure,
322322 unloaded_sema_failure,
323 loaded_parse_failure,
323
324324 loaded_sema_failure,
325325 loaded_success,
326326 },
......@@ -334,21 +334,22 @@ pub const Scope = struct {
334334 => {},
335335
336336 .loaded_success => {
337 allocator.free(self.source.bytes);
338337 self.contents.module.deinit(allocator);
339338 allocator.destroy(self.contents.module);
340339 self.status = .unloaded_success;
341340 },
342341 .loaded_sema_failure => {
343 allocator.free(self.source.bytes);
344342 self.contents.module.deinit(allocator);
345343 allocator.destroy(self.contents.module);
346344 self.status = .unloaded_sema_failure;
347345 },
348 .loaded_parse_failure => {
349 allocator.free(self.source.bytes);
350 self.status = .unloaded_parse_failure;
346 }
347 switch (self.source) {
348 .bytes => |bytes| {
349 allocator.free(bytes);
350 self.source = .{ .unloaded = {} };
351351 },
352 .unloaded => {},
352353 }
353354 }
354355
......@@ -586,7 +587,7 @@ pub fn getAllErrorsAlloc(self: *Module) !AllErrors {
586587 while (it.next()) |kv| {
587588 const scope = kv.key;
588589 const err_msg = kv.value;
589 const source = scope.source.bytes;
590 const source = try self.getSource(scope);
590591 try AllErrors.add(&arena, &errors, scope.sub_file_path, source, err_msg.*);
591592 }
592593 }
......@@ -595,7 +596,7 @@ pub fn getAllErrorsAlloc(self: *Module) !AllErrors {
595596 while (it.next()) |kv| {
596597 const decl = kv.key;
597598 const err_msg = kv.value;
598 const source = decl.scope.source.bytes;
599 const source = try self.getSource(decl.scope);
599600 try AllErrors.add(&arena, &errors, decl.scope.sub_file_path, source, err_msg.*);
600601 }
601602 }
......@@ -604,7 +605,7 @@ pub fn getAllErrorsAlloc(self: *Module) !AllErrors {
604605 while (it.next()) |kv| {
605606 const decl = kv.key.owner_decl;
606607 const err_msg = kv.value;
607 const source = decl.scope.source.bytes;
608 const source = try self.getSource(decl.scope);
608609 try AllErrors.add(&arena, &errors, decl.scope.sub_file_path, source, err_msg.*);
609610 }
610611 }
......@@ -684,20 +685,29 @@ pub fn performAllTheWork(self: *Module) error{OutOfMemory}!void {
684685 };
685686}
686687
687fn getSrcModule(self: *Module, root_scope: *Scope.ZIRModule) !*zir.Module {
688 switch (root_scope.status) {
689 .never_loaded, .unloaded_success => {
690 try self.failed_files.ensureCapacity(self.failed_files.size + 1);
691
692 var keep_source = false;
688fn getSource(self: *Module, root_scope: *Scope.ZIRModule) ![:0]const u8 {
689 switch (root_scope.source) {
690 .unloaded => {
693691 const source = try self.root_pkg.root_src_dir.readFileAllocOptions(
694692 self.allocator,
695 self.root_pkg.root_src_path,
693 root_scope.sub_file_path,
696694 std.math.maxInt(u32),
697695 1,
698696 0,
699697 );
700 defer if (!keep_source) self.allocator.free(source);
698 root_scope.source = .{ .bytes = source };
699 return source;
700 },
701 .bytes => |bytes| return bytes,
702 }
703}
704
705fn getSrcModule(self: *Module, root_scope: *Scope.ZIRModule) !*zir.Module {
706 switch (root_scope.status) {
707 .never_loaded, .unloaded_success => {
708 try self.failed_files.ensureCapacity(self.failed_files.size + 1);
709
710 const source = try self.getSource(root_scope);
701711
702712 var keep_zir_module = false;
703713 const zir_module = try self.allocator.create(zir.Module);
......@@ -711,15 +721,11 @@ fn getSrcModule(self: *Module, root_scope: *Scope.ZIRModule) !*zir.Module {
711721 root_scope,
712722 try ErrorMsg.create(self.allocator, src_err_msg.byte_offset, "{}", .{src_err_msg.msg}),
713723 );
714 root_scope.status = .loaded_parse_failure;
715 root_scope.source = .{ .bytes = source };
716 keep_source = true;
724 root_scope.status = .unloaded_parse_failure;
717725 return error.AnalysisFail;
718726 }
719727
720728 root_scope.status = .loaded_success;
721 root_scope.source = .{ .bytes = source };
722 keep_source = true;
723729 root_scope.contents = .{ .module = zir_module };
724730 keep_zir_module = true;
725731
......@@ -728,10 +734,9 @@ fn getSrcModule(self: *Module, root_scope: *Scope.ZIRModule) !*zir.Module {
728734
729735 .unloaded_parse_failure,
730736 .unloaded_sema_failure,
731 .loaded_parse_failure,
732 .loaded_sema_failure,
733737 => return error.AnalysisFail,
734 .loaded_success => return root_scope.contents.module,
738
739 .loaded_success, .loaded_sema_failure => return root_scope.contents.module,
735740 }
736741}
737742
......@@ -760,10 +765,9 @@ fn analyzeRoot(self: *Module, root_scope: *Scope.ZIRModule) !void {
760765
761766 .unloaded_parse_failure,
762767 .unloaded_sema_failure,
763 .loaded_parse_failure,
768 .unloaded_success,
764769 .loaded_sema_failure,
765770 .loaded_success,
766 .unloaded_success,
767771 => {
768772 const src_module = try self.getSrcModule(root_scope);
769773
......@@ -2008,9 +2012,16 @@ fn coerceArrayPtrToSlice(self: *Module, scope: *Scope, dest_type: Type, inst: *I
20082012
20092013fn fail(self: *Module, scope: *Scope, src: usize, comptime format: []const u8, args: var) InnerError {
20102014 @setCold(true);
2011 try self.failed_decls.ensureCapacity(self.failed_decls.size + 1);
2012 try self.failed_files.ensureCapacity(self.failed_files.size + 1);
20132015 const err_msg = try ErrorMsg.create(self.allocator, src, format, args);
2016 return self.failWithOwnedErrorMsg(scope, src, err_msg);
2017}
2018
2019fn failWithOwnedErrorMsg(self: *Module, scope: *Scope, src: usize, err_msg: *ErrorMsg) InnerError {
2020 {
2021 errdefer err_msg.destroy(self.allocator);
2022 try self.failed_decls.ensureCapacity(self.failed_decls.size + 1);
2023 try self.failed_files.ensureCapacity(self.failed_files.size + 1);
2024 }
20142025 switch (scope.tag) {
20152026 .decl => {
20162027 const decl = scope.cast(Scope.DeclAnalysis).?.decl;