aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorVexu <git@vexu.eu>2020-10-06 13:22:33 +0300
committerVexu <git@vexu.eu>2020-10-30 15:58:11 +0200
commit8421b8a8983b5531b1cc299461f7747c829b054a (patch)
treeb16b34089d6883ca585c3ceadb0c40db5cb0956f /src
parent72343ffd06dbbc95424d74c93188ba4a6aa74c49 (diff)
downloadzig-8421b8a8983b5531b1cc299461f7747c829b054a.tar.gz
zig-8421b8a8983b5531b1cc299461f7747c829b054a.zip
stage2: detect import outside file path
Diffstat (limited to 'src')
-rw-r--r--src/Module.zig29
-rw-r--r--src/zir_sema.zig6
2 files changed, 25 insertions, 10 deletions
diff --git a/src/Module.zig b/src/Module.zig
index c9c77991e8..425165c8be 100644
--- a/src/Module.zig
+++ b/src/Module.zig
@@ -2400,25 +2400,40 @@ pub fn analyzeSlice(self: *Module, scope: *Scope, src: usize, array_ptr: *Inst,
}
pub fn analyzeImport(self: *Module, scope: *Scope, src: usize, target_string: []const u8) !*Scope.File {
- // TODO if (package_table.get(target_string)) |pkg|
- if (self.import_table.get(target_string)) |some| {
+ // TODO scope.getCurPkg();
+ const cur_pkg = self.root_pkg;
+ const cur_pkg_dir_path = cur_pkg.root_src_directory.path orelse ".";
+ const found_pkg = cur_pkg.table.get(target_string);
+
+ const resolved_path = if (found_pkg) |pkg|
+ try std.fs.path.resolve(self.gpa, &[_][]const u8{ pkg.root_src_directory.path orelse ".", pkg.root_src_path })
+ else
+ try std.fs.path.resolve(self.gpa, &[_][]const u8{ cur_pkg_dir_path, target_string });
+ errdefer self.gpa.free(resolved_path);
+
+ if (self.import_table.get(resolved_path)) |some| {
+ self.gpa.free(resolved_path);
return some;
}
- // TODO check for imports outside of pkg path
- if (false) return error.ImportOutsidePkgPath;
+ if (found_pkg == null) {
+ const resolved_root_path = try std.fs.path.resolve(self.gpa, &[_][]const u8{cur_pkg_dir_path});
+ defer self.gpa.free(resolved_root_path);
+
+ if (!mem.startsWith(u8, resolved_path, resolved_root_path)) {
+ return error.ImportOutsidePkgPath;
+ }
+ }
// TODO Scope.Container arena for ty and sub_file_path
const struct_payload = try self.gpa.create(Type.Payload.EmptyStruct);
errdefer self.gpa.destroy(struct_payload);
const file_scope = try self.gpa.create(Scope.File);
errdefer self.gpa.destroy(file_scope);
- const file_path = try self.gpa.dupe(u8, target_string);
- errdefer self.gpa.free(file_path);
struct_payload.* = .{ .scope = &file_scope.root_container };
file_scope.* = .{
- .sub_file_path = file_path,
+ .sub_file_path = resolved_path,
.source = .{ .unloaded = {} },
.contents = .{ .not_available = {} },
.status = .never_loaded,
diff --git a/src/zir_sema.zig b/src/zir_sema.zig
index d02c21ec47..1e6bec3ff8 100644
--- a/src/zir_sema.zig
+++ b/src/zir_sema.zig
@@ -1208,9 +1208,9 @@ fn analyzeInstImport(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerErr
const operand = try resolveConstString(mod, scope, inst.positionals.operand);
const file_scope = mod.analyzeImport(scope, inst.base.src, operand) catch |err| switch (err) {
- // error.ImportOutsidePkgPath => {
- // return mod.fail(scope, inst.base.src, "import of file outside package path: '{}'", .{operand});
- // },
+ error.ImportOutsidePkgPath => {
+ return mod.fail(scope, inst.base.src, "import of file outside package path: '{}'", .{operand});
+ },
error.FileNotFound => {
return mod.fail(scope, inst.base.src, "unable to find '{}'", .{operand});
},