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,...@@ -2400,25 +2400,40 @@ pub fn analyzeSlice(self: *Module, scope: *Scope, src: usize, array_ptr: *Inst,
2400}2400}
24012401
2402pub fn analyzeImport(self: *Module, scope: *Scope, src: usize, target_string: []const u8) !*Scope.File {2402pub fn analyzeImport(self: *Module, scope: *Scope, src: usize, target_string: []const u8) !*Scope.File {
2403 // TODO if (package_table.get(target_string)) |pkg|2403 // TODO scope.getCurPkg();
2404 if (self.import_table.get(target_string)) |some| {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);
2405 return some;2416 return some;
2406 }2417 }
24072418
2408 // TODO check for imports outside of pkg path2419 if (found_pkg == null) {
2409 if (false) return error.ImportOutsidePkgPath;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
2411 // TODO Scope.Container arena for ty and sub_file_path2428 // TODO Scope.Container arena for ty and sub_file_path
2412 const struct_payload = try self.gpa.create(Type.Payload.EmptyStruct);2429 const struct_payload = try self.gpa.create(Type.Payload.EmptyStruct);
2413 errdefer self.gpa.destroy(struct_payload);2430 errdefer self.gpa.destroy(struct_payload);
2414 const file_scope = try self.gpa.create(Scope.File);2431 const file_scope = try self.gpa.create(Scope.File);
2415 errdefer self.gpa.destroy(file_scope);2432 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
2419 struct_payload.* = .{ .scope = &file_scope.root_container };2434 struct_payload.* = .{ .scope = &file_scope.root_container };
2420 file_scope.* = .{2435 file_scope.* = .{
2421 .sub_file_path = file_path,2436 .sub_file_path = resolved_path,
2422 .source = .{ .unloaded = {} },2437 .source = .{ .unloaded = {} },
2423 .contents = .{ .not_available = {} },2438 .contents = .{ .not_available = {} },
2424 .status = .never_loaded,2439 .status = .never_loaded,
src/zir_sema.zig+3-3
...@@ -1208,9 +1208,9 @@ fn analyzeInstImport(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerErr...@@ -1208,9 +1208,9 @@ fn analyzeInstImport(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerErr
1208 const operand = try resolveConstString(mod, scope, inst.positionals.operand);1208 const operand = try resolveConstString(mod, scope, inst.positionals.operand);
12091209
1210 const file_scope = mod.analyzeImport(scope, inst.base.src, operand) catch |err| switch (err) {1210 const file_scope = mod.analyzeImport(scope, inst.base.src, operand) catch |err| switch (err) {
1211 // error.ImportOutsidePkgPath => {1211 error.ImportOutsidePkgPath => {
1212 // return mod.fail(scope, inst.base.src, "import of file outside package path: '{}'", .{operand});1212 return mod.fail(scope, inst.base.src, "import of file outside package path: '{}'", .{operand});
1213 // },1213 },
1214 error.FileNotFound => {1214 error.FileNotFound => {
1215 return mod.fail(scope, inst.base.src, "unable to find '{}'", .{operand});1215 return mod.fail(scope, inst.base.src, "unable to find '{}'", .{operand});
1216 },1216 },