Merge pull request #11867 from nektro/patch-2

stage2: ensure builtin packages are always available
This commit is contained in:
Andrew Kelley 2022-07-28 15:20:49 -07:00 committed by GitHub
commit e6b3eae490
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 21 deletions

View File

@ -1494,31 +1494,14 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
);
errdefer test_pkg.destroy(gpa);
try test_pkg.add(gpa, "builtin", builtin_pkg);
try test_pkg.add(gpa, "root", test_pkg);
try test_pkg.add(gpa, "std", std_pkg);
break :root_pkg test_pkg;
} else main_pkg;
errdefer if (options.is_test) root_pkg.destroy(gpa);
var other_pkg_iter = main_pkg.table.valueIterator();
while (other_pkg_iter.next()) |pkg| {
try pkg.*.add(gpa, "builtin", builtin_pkg);
try pkg.*.add(gpa, "std", std_pkg);
}
try main_pkg.addAndAdopt(gpa, "builtin", builtin_pkg);
try main_pkg.add(gpa, "root", root_pkg);
try main_pkg.addAndAdopt(gpa, "std", std_pkg);
try std_pkg.add(gpa, "builtin", builtin_pkg);
try std_pkg.add(gpa, "root", root_pkg);
try std_pkg.add(gpa, "std", std_pkg);
try builtin_pkg.add(gpa, "std", std_pkg);
try builtin_pkg.add(gpa, "builtin", builtin_pkg);
const main_pkg_in_std = m: {
const std_path = try std.fs.path.resolve(arena, &[_][]const u8{
std_pkg.root_src_directory.path orelse ".",

View File

@ -4673,6 +4673,15 @@ pub fn importFile(
cur_file: *File,
import_string: []const u8,
) !ImportFileResult {
if (std.mem.eql(u8, import_string, "std")) {
return mod.importPkg(mod.main_pkg.table.get("std").?);
}
if (std.mem.eql(u8, import_string, "builtin")) {
return mod.importPkg(mod.main_pkg.table.get("builtin").?);
}
if (std.mem.eql(u8, import_string, "root")) {
return mod.importPkg(mod.root_pkg);
}
if (cur_file.pkg.table.get(import_string)) |pkg| {
return mod.importPkg(pkg);
}

View File

@ -4674,10 +4674,6 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr
error.OutOfMemory => return error.OutOfMemory,
else => unreachable, // we pass null for root_src_dir_path
};
const std_pkg = mod.main_pkg.table.get("std").?;
const builtin_pkg = mod.main_pkg.table.get("builtin").?;
try c_import_pkg.add(sema.gpa, "builtin", builtin_pkg);
try c_import_pkg.add(sema.gpa, "std", std_pkg);
const result = mod.importPkg(c_import_pkg) catch |err|
return sema.fail(&child_block, src, "C import failed: {s}", .{@errorName(err)});

View File

@ -858,6 +858,12 @@ fn buildOutputType(
) catch |err| {
fatal("Failed to add package at path {s}: {s}", .{ pkg_path.?, @errorName(err) });
};
if (mem.eql(u8, pkg_name.?, "std") or mem.eql(u8, pkg_name.?, "root") or mem.eql(u8, pkg_name.?, "builtin")) {
fatal("unable to add package '{s}' -> '{s}': conflicts with builtin package", .{ pkg_name.?, pkg_path.? });
} else if (cur_pkg.table.get(pkg_name.?)) |prev| {
fatal("unable to add package '{s}' -> '{s}': already exists as '{s}", .{ pkg_name.?, pkg_path.?, prev.root_src_path });
}
try cur_pkg.addAndAdopt(gpa, pkg_name.?, new_cur_pkg);
cur_pkg = new_cur_pkg;
} else if (mem.eql(u8, arg, "--pkg-end")) {