authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-10-06 13:22:33+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-10-30 15:58:11+02:00
log8421b8a8983b5531b1cc299461f7747c829b054a
treeb16b34089d6883ca585c3ceadb0c40db5cb0956f
parent72343ffd06dbbc95424d74c93188ba4a6aa74c49
signaturelock-open Commit is signed but in an unrecognized format.

stage2: detect import outside file path


2 files changed, 25 insertions(+), 10 deletions(-)

src/Module.zig+22-7
......@@ -2400,25 +2400,40 @@ pub fn analyzeSlice(self: *Module, scope: *Scope, src: usize, array_ptr: *Inst,
24002400}
24012401
24022402pub fn analyzeImport(self: *Module, scope: *Scope, src: usize, target_string: []const u8) !*Scope.File {
2403 // TODO if (package_table.get(target_string)) |pkg|
2404 if (self.import_table.get(target_string)) |some| {
2403 // TODO scope.getCurPkg();
2404 const cur_pkg = self.root_pkg;
2405 const cur_pkg_dir_path = cur_pkg.root_src_directory.path orelse ".";
2406 const found_pkg = cur_pkg.table.get(target_string);
2407
2408 const resolved_path = if (found_pkg) |pkg|
2409 try std.fs.path.resolve(self.gpa, &[_][]const u8{ pkg.root_src_directory.path orelse ".", pkg.root_src_path })
2410 else
2411 try std.fs.path.resolve(self.gpa, &[_][]const u8{ cur_pkg_dir_path, target_string });
2412 errdefer self.gpa.free(resolved_path);
2413
2414 if (self.import_table.get(resolved_path)) |some| {
2415 self.gpa.free(resolved_path);
24052416 return some;
24062417 }
24072418
2408 // TODO check for imports outside of pkg path
2409 if (false) return error.ImportOutsidePkgPath;
2419 if (found_pkg == null) {
2420 const resolved_root_path = try std.fs.path.resolve(self.gpa, &[_][]const u8{cur_pkg_dir_path});
2421 defer self.gpa.free(resolved_root_path);
2422
2423 if (!mem.startsWith(u8, resolved_path, resolved_root_path)) {
2424 return error.ImportOutsidePkgPath;
2425 }
2426 }
24102427
24112428 // TODO Scope.Container arena for ty and sub_file_path
24122429 const struct_payload = try self.gpa.create(Type.Payload.EmptyStruct);
24132430 errdefer self.gpa.destroy(struct_payload);
24142431 const file_scope = try self.gpa.create(Scope.File);
24152432 errdefer self.gpa.destroy(file_scope);
2416 const file_path = try self.gpa.dupe(u8, target_string);
2417 errdefer self.gpa.free(file_path);
24182433
24192434 struct_payload.* = .{ .scope = &file_scope.root_container };
24202435 file_scope.* = .{
2421 .sub_file_path = file_path,
2436 .sub_file_path = resolved_path,
24222437 .source = .{ .unloaded = {} },
24232438 .contents = .{ .not_available = {} },
24242439 .status = .never_loaded,
src/zir_sema.zig+3-3
......@@ -1208,9 +1208,9 @@ fn analyzeInstImport(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerErr
12081208 const operand = try resolveConstString(mod, scope, inst.positionals.operand);
12091209
12101210 const file_scope = mod.analyzeImport(scope, inst.base.src, operand) catch |err| switch (err) {
1211 // error.ImportOutsidePkgPath => {
1212 // return mod.fail(scope, inst.base.src, "import of file outside package path: '{}'", .{operand});
1213 // },
1211 error.ImportOutsidePkgPath => {
1212 return mod.fail(scope, inst.base.src, "import of file outside package path: '{}'", .{operand});
1213 },
12141214 error.FileNotFound => {
12151215 return mod.fail(scope, inst.base.src, "unable to find '{}'", .{operand});
12161216 },