Skip to content

Commit b871f2a

Browse files
committed
AstGen: detect and error on files included in multiple packages
Previously, if a source file was referenced from multiple packages, it just became owned by the first one AstGen happened to reach; this was a problem, because it could lead to inconsistent behaviour in the compiler based on a race condition. This could be fixed by just analyzing such files multiple times - however, it was pointed out by Andrew that it might make more sense to enforce files being part of at most a single package. Having a file in multiple packages would not only impact compile times (due to Sema having to run multiple times on potentially a lot of code) but is also a confusing anti-pattern which more often than not is a mistake on the part of the user. Resolves: ziglang#13662
1 parent 8ccb9a6 commit b871f2a

File tree

3 files changed

+129
-10
lines changed

3 files changed

+129
-10
lines changed

src/Compilation.zig

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -617,14 +617,6 @@ pub const AllErrors = struct {
617617
note_i += 1;
618618
}
619619
}
620-
if (module_err_msg.src_loc.lazy == .entire_file) {
621-
try errors.append(.{
622-
.plain = .{
623-
.msg = try allocator.dupe(u8, module_err_msg.msg),
624-
},
625-
});
626-
return;
627-
}
628620

629621
const reference_trace = try allocator.alloc(Message, module_err_msg.reference_trace.len);
630622
for (reference_trace) |*reference, i| {
@@ -661,7 +653,7 @@ pub const AllErrors = struct {
661653
.column = @intCast(u32, err_loc.column),
662654
.notes = notes_buf[0..note_i],
663655
.reference_trace = reference_trace,
664-
.source_line = try allocator.dupe(u8, err_loc.source_line),
656+
.source_line = if (module_err_msg.src_loc.lazy == .entire_file) null else try allocator.dupe(u8, err_loc.source_line),
665657
},
666658
});
667659
}
@@ -3034,6 +3026,58 @@ pub fn performAllTheWork(
30343026
}
30353027
}
30363028

3029+
if (comp.bin_file.options.module) |mod| {
3030+
for (mod.import_table.values()) |file| {
3031+
if (file.multi_pkg) {
3032+
const err = err_blk: {
3033+
const notes = try mod.gpa.alloc(Module.ErrorMsg, file.references.items.len);
3034+
errdefer mod.gpa.free(notes);
3035+
3036+
for (notes) |*note, i| {
3037+
errdefer for (notes[0..i]) |*n| n.deinit(mod.gpa);
3038+
note.* = switch (file.references.items[i]) {
3039+
.import => |loc| try Module.ErrorMsg.init(
3040+
mod.gpa,
3041+
loc,
3042+
"imported from package {s}",
3043+
.{loc.file_scope.pkg.getName()},
3044+
),
3045+
.root => |pkg| try Module.ErrorMsg.init(
3046+
mod.gpa,
3047+
.{ .file_scope = file, .parent_decl_node = 0, .lazy = .entire_file },
3048+
"root of package {s}",
3049+
.{pkg.getName()},
3050+
),
3051+
};
3052+
}
3053+
errdefer for (notes) |*n| n.deinit(mod.gpa);
3054+
3055+
const err = try Module.ErrorMsg.create(
3056+
mod.gpa,
3057+
.{ .file_scope = file, .parent_decl_node = 0, .lazy = .entire_file },
3058+
"file exists in multiple packages",
3059+
.{},
3060+
);
3061+
err.notes = notes;
3062+
break :err_blk err;
3063+
};
3064+
errdefer err.destroy(mod.gpa);
3065+
try mod.failed_files.putNoClobber(mod.gpa, file, err);
3066+
}
3067+
}
3068+
3069+
// Now that we've reported the errors, we need to deal with
3070+
// dependencies. Any file referenced by a multi_pkg file should also be
3071+
// marked multi_pkg and have its status set to astgen_failure, as it's
3072+
// ambiguous which package they should be analyzed as a part of. We need
3073+
// to add this flag after reporting the errors however, as otherwise
3074+
// we'd get an error for every single downstream file, which wouldn't be
3075+
// very useful.
3076+
for (mod.import_table.values()) |file| {
3077+
if (file.multi_pkg) file.recursiveMarkMultiPkg(mod);
3078+
}
3079+
}
3080+
30373081
{
30383082
const outdated_and_deleted_decls_frame = tracy.namedFrame("outdated_and_deleted_decls");
30393083
defer outdated_and_deleted_decls_frame.end();
@@ -3458,7 +3502,15 @@ fn workerAstGenFile(
34583502
comp.mutex.lock();
34593503
defer comp.mutex.unlock();
34603504

3461-
break :blk mod.importFile(file, import_path) catch continue;
3505+
const res = mod.importFile(file, import_path) catch continue;
3506+
if (res.is_file) {
3507+
res.file.addReference(mod.*, .{ .import = .{
3508+
.file_scope = file,
3509+
.parent_decl_node = 0,
3510+
.lazy = .{ .token_abs = item.data.token },
3511+
} }) catch continue;
3512+
}
3513+
break :blk res;
34623514
};
34633515
if (import_result.is_new) {
34643516
log.debug("AstGen of {s} has import '{s}'; queuing AstGen of {s}", .{

src/Module.zig

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1895,6 +1895,10 @@ pub const File = struct {
18951895
zir: Zir,
18961896
/// Package that this file is a part of, managed externally.
18971897
pkg: *Package,
1898+
/// Whether this file is a part of multiple packages. This is an error condition which will be reported after AstGen.
1899+
multi_pkg: bool = false,
1900+
/// List of references to this file, used for multi-package errors.
1901+
references: std.ArrayListUnmanaged(Reference) = .{},
18981902

18991903
/// Used by change detection algorithm, after astgen, contains the
19001904
/// set of decls that existed in the previous ZIR but not in the new one.
@@ -1910,6 +1914,14 @@ pub const File = struct {
19101914
/// successful, this field is unloaded.
19111915
prev_zir: ?*Zir = null,
19121916

1917+
/// A single reference to a file.
1918+
const Reference = union(enum) {
1919+
/// The file is imported directly (i.e. not as a package) with @import.
1920+
import: SrcLoc,
1921+
/// The file is the root of a package.
1922+
root: *Package,
1923+
};
1924+
19131925
pub fn unload(file: *File, gpa: Allocator) void {
19141926
file.unloadTree(gpa);
19151927
file.unloadSource(gpa);
@@ -1942,6 +1954,7 @@ pub const File = struct {
19421954
log.debug("deinit File {s}", .{file.sub_file_path});
19431955
file.deleted_decls.deinit(gpa);
19441956
file.outdated_decls.deinit(gpa);
1957+
file.references.deinit(gpa);
19451958
if (file.root_decl.unwrap()) |root_decl| {
19461959
mod.destroyDecl(root_decl);
19471960
}
@@ -2062,6 +2075,44 @@ pub const File = struct {
20622075
else => true,
20632076
};
20642077
}
2078+
2079+
/// Add a reference to this file during AstGen.
2080+
pub fn addReference(file: *File, mod: Module, ref: Reference) !void {
2081+
try file.references.append(mod.gpa, ref);
2082+
2083+
const pkg = switch (ref) {
2084+
.import => |loc| loc.file_scope.pkg,
2085+
.root => |pkg| pkg,
2086+
};
2087+
if (pkg != file.pkg) file.multi_pkg = true;
2088+
}
2089+
2090+
/// Mark this file and every file referenced by it as multi_pkg and report an
2091+
/// astgen_failure error for them. AstGen must have completed in its entirety.
2092+
pub fn recursiveMarkMultiPkg(file: *File, mod: *Module) void {
2093+
file.multi_pkg = true;
2094+
file.status = .astgen_failure;
2095+
2096+
std.debug.assert(file.zir_loaded);
2097+
const imports_index = file.zir.extra[@enumToInt(Zir.ExtraIndex.imports)];
2098+
if (imports_index == 0) return;
2099+
const extra = file.zir.extraData(Zir.Inst.Imports, imports_index);
2100+
2101+
var import_i: u32 = 0;
2102+
var extra_index = extra.end;
2103+
while (import_i < extra.data.imports_len) : (import_i += 1) {
2104+
const item = file.zir.extraData(Zir.Inst.Imports.Item, extra_index);
2105+
extra_index = item.end;
2106+
2107+
const import_path = file.zir.nullTerminatedString(item.data.name);
2108+
if (mem.eql(u8, import_path, "builtin")) continue;
2109+
2110+
const res = mod.importFile(file, import_path) catch continue;
2111+
if (res.is_file and !res.file.multi_pkg) {
2112+
res.file.recursiveMarkMultiPkg(mod);
2113+
}
2114+
}
2115+
}
20652116
};
20662117

20672118
/// Represents the contents of a file loaded with `@embedFile`.
@@ -4867,6 +4918,7 @@ pub fn declareDeclDependency(mod: *Module, depender_index: Decl.Index, dependee_
48674918
pub const ImportFileResult = struct {
48684919
file: *File,
48694920
is_new: bool,
4921+
is_file: bool,
48704922
};
48714923

48724924
pub fn importPkg(mod: *Module, pkg: *Package) !ImportFileResult {
@@ -4886,6 +4938,7 @@ pub fn importPkg(mod: *Module, pkg: *Package) !ImportFileResult {
48864938
if (gop.found_existing) return ImportFileResult{
48874939
.file = gop.value_ptr.*,
48884940
.is_new = false,
4941+
.is_file = false,
48894942
};
48904943

48914944
const sub_file_path = try gpa.dupe(u8, pkg.root_src_path);
@@ -4909,9 +4962,11 @@ pub fn importPkg(mod: *Module, pkg: *Package) !ImportFileResult {
49094962
.pkg = pkg,
49104963
.root_decl = .none,
49114964
};
4965+
try new_file.addReference(mod.*, .{ .root = pkg });
49124966
return ImportFileResult{
49134967
.file = new_file,
49144968
.is_new = true,
4969+
.is_file = false,
49154970
};
49164971
}
49174972

@@ -4952,6 +5007,7 @@ pub fn importFile(
49525007
if (gop.found_existing) return ImportFileResult{
49535008
.file = gop.value_ptr.*,
49545009
.is_new = false,
5010+
.is_file = true,
49555011
};
49565012

49575013
const new_file = try gpa.create(File);
@@ -4997,6 +5053,7 @@ pub fn importFile(
49975053
return ImportFileResult{
49985054
.file = new_file,
49995055
.is_new = true,
5056+
.is_file = true,
50005057
};
50015058
}
50025059

src/Package.zig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,13 @@ pub fn addAndAdopt(parent: *Package, gpa: Allocator, name: []const u8, child: *P
124124
child.parent = parent;
125125
return parent.add(gpa, name, child);
126126
}
127+
128+
pub fn getName(pkg: *const Package) []const u8 {
129+
if (pkg.parent) |parent| {
130+
var it = parent.table.iterator();
131+
while (it.next()) |p| {
132+
if (p.value_ptr.* == pkg) return p.key_ptr.*;
133+
}
134+
unreachable;
135+
} else return "root";
136+
}

0 commit comments

Comments
 (0)